lunes, 6 de noviembre de 2017

Programas en 3D

1.- Cubo 


import pygame
from pygame.locals import *

from OpenGL.GL import *
from OpenGL.GLU import *

verticies = (
    (1, -1, -1),
    (1, 1, -1),
    (-1, 0, -1),
    (0, 0, 1)
    )

edges = (
    (0,1),
    (1,2),
    (2,0),
    (3,0),
    (3,1),
    (3,2),
    )


def piramide():
    glBegin(GL_LINES)
    for edge in edges:
        for vertex in edge:
            glVertex3fv(verticies[vertex])
    glEnd()


def main():
    pygame.init()
    display = (800,600)
    pygame.display.set_mode(display, DOUBLEBUF|OPENGL)

    gluPerspective(45, (display[0]/display[1]), 0.1, 50.0)

    glTranslatef(0.0,0.0,-5)



    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        glRotatef(1, 3, 1, 1)
        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
        piramide()
        pygame.display.flip()
        pygame.time.wait(10)

main()

by http://eduardommm.blogspot.mx/



2.- Piramide


import pygame
from pygame.locals import *

from OpenGL.GL import *
from OpenGL.GLU import *

verticies = (
    (1, -1, -1),
    (1, 1, -1),
    (-1, 0, -1),
    (0, 0, 1)
    )

edges = (
    (0,1),
    (1,2),
    (2,0),
    (3,0),
    (3,1),
    (3,2),
    )


def piramide():
    glBegin(GL_LINES)
    for edge in edges:
        for vertex in edge:
            glVertex3fv(verticies[vertex])
    glEnd()


def main():
    pygame.init()
    display = (800,600)
    pygame.display.set_mode(display, DOUBLEBUF|OPENGL)

    gluPerspective(45, (display[0]/display[1]), 0.1, 50.0)

    glTranslatef(0.0,0.0,-5)



    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        glRotatef(1, 3, 1, 1)
        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
        piramide()
        pygame.display.flip()
        pygame.time.wait(10)

main()

by http://eduardommm.blogspot.mx/


3.- Cubo de Colores



import sys, math, pygame
from operator import itemgetter


class Point3D:
    def __init__(self, x=0, y=0, z=0):
        self.x, self.y, self.z = float(x), float(y), float(z)

    def rotateX(self, angle):
        """ Rotates the point around the X axis by the given angle in degrees. """  
      rad = angle * math.pi / 180        cosa = math.cos(rad)
        sina = math.sin(rad)
        y = self.y * cosa - self.z * sina
        z = self.y * sina + self.z * cosa
        return Point3D(self.x, y, z)

    def rotateY(self, angle):
        """ Rotates the point around the Y axis by the given angle in degrees. """     
   rad = angle * math.pi / 180        cosa = math.cos(rad)
        sina = math.sin(rad)
        z = self.z * cosa - self.x * sina
        x = self.z * sina + self.x * cosa
        return Point3D(x, self.y, z)

    def rotateZ(self, angle):
        """ Rotates the point around the Z axis by the given angle in degrees. """    
    rad = angle * math.pi / 180        cosa = math.cos(rad)
        sina = math.sin(rad)
        x = self.x * cosa - self.y * sina
        y = self.x * sina + self.y * cosa
        return Point3D(x, y, self.z)

    def project(self, win_width, win_height, fov, viewer_distance):
        """ Transforms this 3D point to 2D using a perspective projection. """   
     factor = fov / (viewer_distance + self.z)
        x = self.x * factor + win_width / 2        y = -self.y * factor + win_height / 2    
    return Point3D(x, y, self.z)


class Simulation:
    def __init__(self, win_width=640, win_height=480):
        pygame.init()

        self.screen = pygame.display.set_mode((win_width, win_height))
        pygame.display.set_caption("Figura de cubo 3D en python")

        self.clock = pygame.time.Clock()

        self.vertices = [
            Point3D(-1, 1, -1),
            Point3D(1, 1, -1),
            Point3D(1, -1, -1),
            Point3D(-1, -1, -1),
            Point3D(-1, 1, 1),
            Point3D(1, 1, 1),
            Point3D(1, -1, 1),
            Point3D(-1, -1, 1)
        ]

        # Define the vertices that compose each of the 6 faces. These numbers are     
   # indices to the vertices list defined above.       
 self.faces = [(0, 1, 2, 3), (1, 5, 6, 2), (5, 4, 7, 6), (4, 0, 3, 7), (0, 4, 5, 1), 
(3, 2, 6, 7)]

        # Define colors for each face        self.colors = [(255, 0, 255), (255, 0, 0), 
(0, 255, 0), (0, 0, 255), (0, 255, 255), (255, 255, 0)]

        self.angle = 0
    def run(self):
        """ Main Loop """        while 1:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()

            self.clock.tick(50)
            self.screen.fill((0, 32, 0))

            # It will hold transformed vertices.            t = []

            for v in self.vertices:
                      
      r = v.rotateX(self.angle).rotateY(self.angle).rotateZ(self.angle)
                # Transform the point from 3D to 2D               
 p = r.project(self.screen.get_width(), self.screen.get_height(), 256, 4)
                # Put the point in the list of transformed vertices               
 t.append(p)

            # Calculate the average Z values of each face.            avg_z = []
            i = 0            for f in self.faces:
                z = (t[f[0]].z + t[f[1]].z + t[f[2]].z + t[f[3]].z) / 4.0              
  avg_z.append([i, z])
                i = i + 1
            # Draw the faces using the Painter's algorithm:           
           for tmp in sorted(avg_z, key=itemgetter(1), reverse=True):
                face_index = tmp[0]
                f = self.faces[face_index]
                pointlist = [(t[f[0]].x, t[f[0]].y), (t[f[1]].x, t[f[1]].y),
                             (t[f[1]].x, t[f[1]].y), (t[f[2]].x, t[f[2]].y),
                             (t[f[2]].x, t[f[2]].y), (t[f[3]].x, t[f[3]].y),
                             (t[f[3]].x, t[f[3]].y), (t[f[0]].x, t[f[0]].y)]
                pygame.draw.polygon(self.screen, self.colors[face_index], pointlist)

            self.angle += 1
            pygame.display.flip()


if __name__ == "__main__":
    Simulation().run()


by http://mendezitmg.blogspot.mx/

4.- Barras


from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax1 = fig.add_subplot(111, projection='3d')

xpos = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
ypos = [2,3,4,5,1,6,2,1,7,2,3,5,1,3,2]
num_elements = len(xpos)
zpos = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
dx = np.ones(15)
dy = np.ones(15)
dz = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]

ax1.bar3d(xpos, ypos, zpos, dx, dy, dz, color='#00ceaa')
plt.show()


by http://iscivanguz.blogspot.mx/

5.- Barras

6.- videojuego

Carpeta para descargar el proyecto

https://drive.google.com/file/d/1OSLZOAyRBuchgzU1z4SwxCLN4QmxXptk/view







martes, 31 de octubre de 2017

VPython
Programación 3D para el común de los mortales

VPython facilita crear pantallas 3D navegables y animaciones, incluso para aquellos con experiencia en programación limitada. Porque está basado en Python, también tiene mucho que ofrecer a los investigadores y programadores con experiencia.

Para una introducción rápida, vea los siguientes videos de YouTube, pero ten en cuenta que para la versión actual de VPython 7 de VPython el nombre del módulo es "vpython", no "visual", y la pantalla gráfica se muestra en una pestaña del navegador en lugar de en una ventana.

 

Objetos 3D

Asignación de Variables

Lazos del principio

Lazoz y Animación

Factores de Escala

Depuración de error de sintaxis

Listas parte 1

Listas parte 2

 

 

 

 


martes, 24 de octubre de 2017

Figura en python

from turtle import*
import tkSimpleDialog

speed(20)
shape("turtle")
setup(1000, 700, 0, 0)
screensize(1000,1000)

#Cuadros de dialogos que piden datosa=tkSimpleDialog.askstring("Hola!","Dame color del fondo")
b=tkSimpleDialog.askstring("Hola!","dame el principal color")
d=tkSimpleDialog.askstring("Hola!","dame el color 1")
e=tkSimpleDialog.askstring("Hola!","dame el color 2")
f=tkSimpleDialog.askstring("Hola!","dame el color 3")
g=tkSimpleDialog.askstring("Hola!","dame el color 4")
h=tkSimpleDialog.askstring("Hola!","dame el color 5")
vueltas= tkSimpleDialog.askinteger("Numero de figuras","Dame el num. de figuras")
pincel= tkSimpleDialog.askinteger("Numero de pixeles","Grosor de la pluma")

bgcolor(a)
pensize(pincel)
fillcolor(b)

begin_fill()

cont=0for i in range(vueltas):
    if cont ==1:
        fillcolor(d)
    elif cont==2:
        fillcolor(e)
    elif cont==3:
        fillcolor(f)
    elif cont==4:
        fillcolor(g)
    elif cont==5:
        fillcolor(h)


    begin_fill()
    penup()
    goto(250.55432, -493.03935)
    pendown()
    goto(278.9908, -463.18105)
    goto(325.911, -453.22828)
    goto(367.14389, -441.85369)
    goto(404.11132, -420.52633)
    goto(429.70415, -396.35532)
    goto(446.76604, -363.65337)
    goto(459.56246, -330.95141)
    goto(463.82793, -305.35858)
    goto(460.98428, -274.07845)
    goto(480.88982, -241.3765)
    goto(458.14063, -214.36184)
    goto(441.07874, -178.81624)
    goto(431.12598, -137.58334)
    goto(435.39145, -102.03774)
    goto(449.60969, -69.33579)
    goto(466.67158, -49.43025)
    goto(446.76604, -22.41559)
    goto(418.32956, -35.21201)
    goto(385.62761, -33.79019)
    goto(350.082, -23.83742)
    goto(325.911, -6.77553)
    goto(290.36539, -26.68106)
    goto(250.55432, -38.05566)
    goto(210.74325, -30.94654)
    goto(178.04129, -8.19735)
    goto(151.02664, -25.25924)
    goto(119.74651, -33.79019)
    goto(88.46638, -33.79019)
    goto(57.18625, -23.83742)
    goto(34.43706, -46.5866)
    goto(57.18625, -83.55403)
    goto(65.71719, -113.41233)
    goto(67.13902, -146.11429)
    goto(55.76443, -185.92536)
    goto(40.12436, -217.20549)
    goto(13.1097, -242.79832)
    goto(38.70254, -271.2348)
    goto(35.85889, -302.51493)
    goto(40.12436, -336.63871)
    goto(54.3426, -369.34066)
    goto(75.66996, -403.46444)
    goto(101.26279, -424.7918)
    goto(133.96475, -441.85369)
    goto(175.19765, -457.49375)
    goto(215.00872, -464.60287)
    goto(250.55432, -493.03935)
    end_fill()

    cont = cont+1
    print vueltas

exitonclick()

miércoles, 18 de octubre de 2017

Preguntar el color con SimpleDialog


import turtle
import tkSimpleDialog

t=turtle.Turtle()

c1=tkSimpleDialog.askstring("Color","Escribe el color del lapiz:")
c2=tkSimpleDialog.askstring("Color","Escribe el color del relleno:")

t.color(c1, c2)
t.begin_fill()

for _ in xrange(3):
 t.fd(100)
 t.left(120)

t.end_fill()
turtle.done()

Figuras Con For Anidado

Cuadrado con for anidado

import turtle

turtle.speed(10)

fondo=turtle.Screen()
fondo.bgcolor("#0000FF")
turtle.pencolor("black")
for i in range(28):
 turtle.forward(100)
 turtle.right(90)
 for j in range(1):
  turtle.forward(100)
  turtle.right(80)

turtle.exitonclick()

Triangulo con for anidado

import turtle

turtle.speed(10)

fondo=turtle.Screen()
fondo.bgcolor("#0000FF")
turtle.pencolor("black")

for i in range(28):
 turtle.forward(100)
 turtle.right(120)
 for j in range(1):
  turtle.forward(100)
  turtle.right(100)

turtle.exitonclick()

Octagono con for anidado

import turtle

turtle.speed(10)

fondo=turtle.Screen()
fondo.bgcolor("#0000FF")
turtle.pencolor("black")

for i in range(28):
 turtle.forward(100)
 turtle.right(45)
 for j in range(1):
  turtle.forward(100)
  turtle.right(40)

turtle.exitonclick()


jueves, 5 de octubre de 2017

Turtle

Programa 1

from turtle import *

setup(640, 480, 0, 0)

done()

Programa 2

from turtle import *

setup(450, 150, 0, 0)
title("Ejemplo de ventana")

done()
Programa 3

from turtle import *

title("Ejemplo de ventana")

done()
Programa 4

from turtle import *

setup(250, 100, 0, 0)
title("Ejemplo de ventana")
hideturtle()
dot(10, 0, 0, 0)
setup(450, 150, 0, 0)

done()
Programa 5

from turtle import *

setup(450, 150, 0, 0)
screensize(10, 10)

done()
Programa 6

from turtle import *

setup(150, 150, 0, 0)
screensize(300, 300)

done()
Programa 7

from turtle import *

setup(450, 150, 0, 0)
screensize(400, 300)

done()
Programa 8

from turtle import *

setup(250, 100, 0, 0)
screensize(100, 100)
hideturtle()
dot(10, 0, 0, 0)
screensize(200, 100)

done()
Programa 9

from turtle import *

setup(450, 150, 0, 0)
screensize(300, 150)

showturtle()

done()
Programa 10

from turtle import *

setup(450, 150, 0, 0)
screensize(300, 150)

goto(100, 0)

done()
Programa 11

from turtle import *

setup(450, 150, 0, 0)
screensize(300, 150)

goto(100, 0)
hideturtle()

done()
Programa 12

from turtle import *

setup(450, 150, 0, 0)
screensize(300, 150)

hideturtle()
goto(100, 0)

done()
Programa 13

from turtle import *

setup(450, 200, 0, 0)
screensize(300, 150)

goto(0, 0)

done()
Programa 14

from turtle import *

setup(450, 200, 0, 0)
screensize(300, 150)

goto(100, 50)

done()
Programa 15

from turtle import *

setup(450, 200, 0, 0)
screensize(300, 150)

goto(100, 50)
goto(100, -50)
goto(50, -50)

done()
Programa 16
from turtle import *

setup(450, 200, 0, 0)
screensize(300, 150)
title("www.mclibre.org")
hideturtle()

pensize(5)
fillcolor("red")
begin_fill()
goto(100, 0)
goto(100, 50)
goto(0, 50)
goto(0, 0)
end_fill()

done()
Programa 17

from turtle import *

setup(450, 200, 0, 0)
screensize(300, 150)
title("www.mclibre.org")
hideturtle()

pencolor("red")
pensize(5)
begin_fill()
goto(100, 0)
goto(100, 50)
goto(0, 50)
goto(0, 0)
end_fill()

done()
Programa 18

from turtle import *

setup(450, 200, 0, 0)
screensize(300, 150)
title("www.mclibre.org")
hideturtle()

pensize(5)
fillcolor("red")
begin_fill()
goto(100, 0)
goto(100, 50)
goto(0, 50)
end_fill()

done()
Programa 19

from turtle import *

setup(450, 200, 0, 0)
screensize(300, 150)
title("www.mclibre.org")
hideturtle()

pensize(5)
fillcolor("red")
begin_fill()
goto(50, 50)
goto(100, -50)
goto(150, 0)
goto(0, 0)
end_fill()

done()
Programa 20

from turtle import *

setup(450, 200, 0, 0)
screensize(300, 150)
title("www.mclibre.org")
hideturtle()

pensize(5)
fillcolor("red")
begin_fill()
goto(0, 75)
goto(100, 0)
goto(100, 75)
end_fill()

done()
Programa 21

from turtle import *

setup(450, 200, 0, 0)
screensize(300, 150)
title("www.mclibre.org")
hideturtle()

pensize(5)
fillcolor("red")
begin_fill()
goto(75, 0)
goto(75, 75)
penup()
goto(-100, 75)
pendown()
goto(-100,0)
goto(-25, 0)
end_fill()

done()
Programa 22

from turtle import *

setup(450, 200, 0, 0)
screensize(300, 150)
title("www.mclibre.org")
hideturtle()

pensize(5)
fillcolor("red")
begin_fill()
goto(75, 0)
goto(75, 75)
end_fill()
penup()
goto(-100, 75)
pendown()
begin_fill()
goto(-100,0)
goto(-25, 0)
end_fill()

done()
Programa 23

from turtle import *

setup(450, 200, 0, 0)
screensize(300, 150)
title("www.mclibre.org")
hideturtle()

pensize(5)
fillcolor("red")
begin_fill()
goto(75, 0)
goto(75, 75)
goto(0,0)
penup()
goto(-100, 75)
pendown()
goto(-100,0)
goto(-25, 0)
goto(-100, 75)
end_fill()

done()