-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrotation2D.py
143 lines (105 loc) · 3.51 KB
/
rotation2D.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
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
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_new = rotationOrigin2D(a, angle)
B_new = rotationOrigin2D(b, angle)
C_new = rotationOrigin2D(c,angle)
D_new = rotationOrigin2D(d,angle)
#print(A_new,"||", B_new,"||", C_new,"||", D_new)
glBegin(GL_QUADS)
glColor3f(1,1,0)
glVertex2fv(A_new)
glColor3f(0,1,0)
glVertex2fv(B_new)
glColor3f(1,0,0)
glVertex2fv(C_new)
glColor3f(0,0,1)
glVertex2fv(D_new)
glEnd()
#rotate square vertices by 90 degrees anti clockwise x-dir
angle = 90
A_new = rotationOrigin2D(a, angle)
B_new = rotationOrigin2D(b, angle)
C_new = rotationOrigin2D(c,angle)
D_new = rotationOrigin2D(d,angle)
glBegin(GL_QUADS)
glColor3f(1,1,0)
glVertex2fv(A_new)
glColor3f(0,1,0)
glVertex2fv(B_new)
glColor3f(1,0,0)
glVertex2fv(C_new)
glColor3f(0,0,1)
glVertex2fv(D_new)
glEnd()
glfw.swap_buffers(window)
glfw.poll_events()
glfw.terminate()
if __name__ == "__main__":
main()