Skip to content

Instantly share code, notes, and snippets.

@jsbain
Created September 24, 2018 06:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsbain/6881aaaf165e248b3f8c0549df03503c to your computer and use it in GitHub Desktop.
Save jsbain/6881aaaf165e248b3f8c0549df03503c to your computer and use it in GitHub Desktop.
3dview.py
from scene import *
#import sound
import random
import math
import numpy as np
A = Action
class Shape3D (object):
'''numpy version of original Vector3d code'''
PROJ=np.mat(np.eye(3)[0:2])
def __init__(self, values=[[]],conn=[[]], r=10):
self.r=r
self.vector = np.matrix(values)
self.conn=conn #connection matrix. [0,1],[1,2], etc
self.initvector=self.vector
def reset(self):
self.vector=self.initvector
def as_path(self):
'''return a ui.Path'''
r=self.r
conn=self.conn
p=ui.Path()
pts=self.to2D().T
for c in self.conn:
p.move_to(*pts[c[0],:].tolist()[0])
p.line_to(*pts[c[1],:].tolist()[0])
for pt in pts:
p.append_path( p.oval(pt[0,0]-r, pt[0,1]-r,r*2,r*2) )
#debug: show border
#p.append_path(p.rect(*p.bounds.as_tuple()))
return p
def to2D(self):
return Shape3D.PROJ*self.vector
def rotateX(self, fi):
c=math.cos(fi)
s=math.sin(fi)
rotMat = np.matrix([[c, -s, 0],
[s, c, 0],
[0, 0, 1]])
self.rotate(rotMat)
def rotateY(self, fi):
c=math.cos(fi)
s=math.sin(fi)
rotMat = np.matrix([[c, 0, -s],
[0, 1, 0],
[s, 0, c]])
self.rotate(rotMat)
def rotateZ(self, fi):
c=math.cos(fi)
s=math.sin(fi)
rotMat = np.matrix([[1, 0, 0],
[0, c, -s],
[0, s, c]])
self.rotate(rotMat)
def rotate(self, rotMat):
self.vector = rotMat*self.vector
Vector3D=lambda x:np.matrix(x).T
class MyScene (Scene):
def setup(self):
self.angle = 0.005
self.angleX = 0
self.angleY = 0
self.init_angle = self.angle
self.centro = np.mat([500,400,0]).T
#----CUBE-----
self.p = np.mat([[-100,-100,-100],
[100,-100,-100],
[100,100,-100],
[-100,100,-100],
[-100,-100,100],
[100,-100,100],
[100,100,100],
[-100,100,100]]).T
#pdist=np.power(sum([np.power((s.p[i0]-s.p[i0].T),2) for i0 in range(3)]),.5)
conn=[[0,1],
[0,3],
[0,4],
[1,2],
[1,5],
[2,3],
[2,6],
[3,7],
[4,5],
[4,7],
[5,6],
[6,7] ]
#-----PIRAMID-----
'''
self.p = np.mat([[-100,-100,-100],
[100,-100,-100],
[100,100,-100],
[-100,100,-100],
[0,0,100]]).T
conn=[[0,1],
[0,3],
[0,4],
[1,2],
[1,4],
[2,3],
[2,4],
[3,4]]
'''
self.shape=Shape3D(self.p,conn)
self.puntos = []
self.shapenode=ShapeNode()
self.shapenode.position=[500,400]
self.shapenode.stroke_color = '#ffff00'
self.shapenode.color='#ff00ff'
#self.shapenode.fill_color='#0000ff'
self.add_child(self.shapenode)
self.update_pts()
def did_change_size(self):
self.update_pts()
pass
def update_pts(self):
self.shape.rotateY(self.angleX)
self.shape.rotateZ(self.angleY)
self.shapenode.path=self.shape.as_path()
def touch_began(self, touch):
#self.angle = 0
self.move = 0
x, y = touch.location
self.inicio = {'x':x, 'y':y}
def touch_moved(self, touch):
self.move = 1
x, y = touch.location
x = abs(self.inicio['x']) - abs(x)
y = abs(self.inicio['y']) - abs(y)
self.angleX = math.radians(x)/50
self.angleY = math.radians(y)/50
self.update_pts()
def touch_ended(self, touch):
#self.angle = self.init_angle
if self.move == 1:
self.angleX = 0
self.angleY = 0
else:
self.angleX = self.angle
self.angleY = self.angle
self.update_pts()
if __name__ == '__main__':
s=MyScene()
run(s, show_fps=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment