-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrotated_pattern.py
120 lines (82 loc) · 2.86 KB
/
rotated_pattern.py
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
from __future__ import division
from __future__ import print_function
import glfw
from OpenGL.GL import *
from OpenGL.GLU import *
import numpy as np
from math import floor, sin, cos
def key_callback(window, key, scancode, action, mode):
if key == glfw.KEY_ESCAPE and action == glfw.PRESS:
print("closed window for pressing Escape key")
glfw.set_window_should_close(window, True)
def reshape_callback(window, width, height):
glViewport(0,0,width, height)
def setpixel(x,y,color):
glColor3fv(color)
glBegin(GL_POINTS)
glVertex2f(x,y)
glEnd()
def drawAxes():
glColor3f(1.0,1.0,1.0)
glBegin(GL_LINES)
glVertex2f(-200,0)
glVertex2f(200,0)
glVertex2f(0,-200)
glVertex2f(0,200)
glEnd()
def rotationOrigin2D(point, angle):
angle = np.deg2rad(angle)
#print(angle)
x_new = point[0]* cos(angle) - point[1] * sin(angle)
y_new = point[0]* sin(angle) + point[1] * cos(angle)
return (x_new, y_new)
def main():
if not glfw.init():
raise Exception("glfw cannot be initialised.......")
window=glfw.create_window(700,700, "rotation 2D",None,None)
if not window:
glfw.terminate()
raise Exception("glfw window not created")
w,h = glfw.get_framebuffer_size(window)
print("width: {}, height:{}".format(w,h))
glfw.set_window_pos(window, 400,40)
glfw.make_context_current(window)
glfw.set_key_callback(window, key_callback)
glfw.set_window_size_callback(window, reshape_callback)
gluOrtho2D(-200.0, 200.0,-200.0,200.0)
setpixel(0,0,[1,0,1])
while not glfw.window_should_close(window):
glClear(GL_COLOR_BUFFER_BIT)
#glClearColor(0.0,0.76,0.56,1.0)
glClearColor(0,0,0,1.0)
#drawAxes()
a =(40,40) #yellow
b = (40, 100) #green
c=(100,100) #red
d=(100,40) #blue
i=8
while i!=0:
glBegin(GL_QUADS)
glColor3f(1,1,0) #yellow vertex
glVertex2fv(a)
glColor3f(0,1,0)
glVertex2fv(b)
glColor3f(1,0,0)
glVertex2fv(c)
glColor3f(0,0,1)#blue vertex
glVertex2fv(d)
glEnd()
#print("rotate square vertices by 45 degrees anti clockwise x-dir")
#rotate square vertices by 45 degrees anti clockwise x-dir
angle = 45
a = rotationOrigin2D(a, angle)
b = rotationOrigin2D(b, angle)
c = rotationOrigin2D(c,angle)
d = rotationOrigin2D(d,angle)
i=i-1
#print(A_new,"||", B_new,"||", C_new,"||", D_new)
glfw.swap_buffers(window)
glfw.poll_events()
glfw.terminate()
if __name__ == "__main__":
main()