-
Notifications
You must be signed in to change notification settings - Fork 1
/
3D
83 lines (70 loc) · 2.62 KB
/
3D
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import numpy as np
import pygame as pg, sys
from pygame.locals import *
pg.init()
DISPLAYSURF = pg.display.set_mode((600, 500))
pg.display.set_caption('raspidarasilo-like pseudo3D')
#Set up colours
black = (0, 0, 0)
red = (255, 0, 0)
silver = (192,192,192)
number=10 #number of objects in cube's side
dis=25 #distance between neighboring elements in cube
h=200 #distance from camera to "screen"
dcen=400 #distance from camera to center of cube
radius=15
# fill cube with initial coordinates of elements
cube=[[]]
for ix in range(number):
for iy in range(number):
for iz in range(number):
cube+=[[(ix-number/2)*dis,(iy-number/2)*dis,(iz-number/2)*dis]]
cube.pop(0)
gamma=0 #angle to give a "sinusoidal pattern" to the rotation angles
clock = pg.time.Clock()
while True: # main game loop
for event in pg.event.get():
if event.type == QUIT:
pg.quit()
sys.exit()
#algorithm for rotation
alpha1=0.1*np.sin(gamma) #angle of rotation around z
alpha2=0.1*np.sin(gamma+1) #angle of rotation around x
alpha3=0.1*np.sin(gamma+2) #angle of rotation around y
mat1=[[np.cos(alpha1),-np.sin(alpha1)],[np.sin(alpha1),np.cos(alpha1)]] #matrix of rotation around z
mat2=[[np.cos(alpha2),-np.sin(alpha2)],[np.sin(alpha2),np.cos(alpha2)]] #matrix of rotation around x
mat3=[[np.cos(alpha3),-np.sin(alpha3)],[np.sin(alpha3),np.cos(alpha3)]] #matrix of rotation around y
#apply rotation matrices to each element in cube
for i in range(len(cube)):
[[cube[i][0]],[cube[i][1]]]=np.dot(mat1,[[cube[i][0]],[cube[i][1]]])
[[cube[i][1]],[cube[i][2]]]=np.dot(mat2,[[cube[i][1]],[cube[i][2]]])
[[cube[i][2]],[cube[i][0]]]=np.dot(mat3,[[cube[i][2]],[cube[i][0]]])
gamma+=0.1
if gamma>np.pi*2:
gamma-=np.pi*2
#sort elements of cube by z to draw them in the right order
dict={}
list=[]
sortedcube=[[]]
for point in cube:
key=point[2]
while key in dict:
key+=0.01 #crutch to avoid repetition of keys
dict[key]=point
list+=[key]
list.sort(reverse=True)
for z in list:
sortedcube+=[dict[z]]
sortedcube.pop(0)
DISPLAYSURF.fill(black)
#draw
for point in sortedcube:
coef=h/(point[2]+dcen) #coefficient of perspective diminution
xpos=int(round(point[0]*coef))+300
ypos=int(round(point[1]*coef))+250
pos=(xpos,ypos) #position of the center of circle in the screen
r=int(round(radius*coef))
pg.draw.circle(DISPLAYSURF,red,pos,r,0)
pg.draw.circle(DISPLAYSURF,silver,pos,r,1)
pg.display.update()
clock.tick(30)