-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmouse_controlled_rectangle.py
More file actions
97 lines (75 loc) · 3.75 KB
/
mouse_controlled_rectangle.py
File metadata and controls
97 lines (75 loc) · 3.75 KB
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
from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *
import math
window = 0 # glut window number
# initial position of the rectangle
rect_x = 0
rect_y = 200
# constants
RECT_WIDTH = 50
RECT_HEIGHT = 50
RECT_DELAY = 20
width, height = 400, 400 # width and height of the window
dx = 0 # direction to x
dy = 0 # direction to y
mouse_y = 0 # position of mouse cursor to x
mouse_x = 0 # position of mouse cursor to y
s = 0 # path
v = 3 # speed of movement of the ball
# window size
def draw_rect(x, y, width, height):
glBegin(GL_QUADS) # start drawing a rectangle
glVertex2f(x, y) # bottom left point
glVertex2f(x + width, y) # bottom right point
glVertex2f(x + width, y + height) # top right point
glVertex2f(x, y + height) # top left point
glEnd()
def refresh2d(width, height):
glViewport(0, 0, width, height)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(0.0, width, 0.0, height, 0.0, 1.0)
glMatrixMode (GL_MODELVIEW)
glLoadIdentity()
def draw(): # ondraw is called all the time
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # clear the screen
glLoadIdentity() # reset position
# ToDo draw rectangle
refresh2d(width, height)
glColor3f(0.0, 0.0, 2.0)
draw_rect(rect_x, rect_y, RECT_WIDTH, RECT_HEIGHT)
glutSwapBuffers() # important for double buffering
def mouseControl( mx, my):
global rect_x, rect_y, dx, dy, s, mouse_x, mouse_y
my_new = height - my
mouse_x = mx
mouse_y = my_new
dif_x = mx - rect_x # differnce between position of mouse cursor and rectangle to x
dif_y = my_new - rect_y # differnce between position of mouse cursor and rectangle to y
s = math.sqrt(dif_x ** 2 + dif_y ** 2)
k = v / s
dx = k * dif_x
dy = k * dif_y
def movement(value):
global rect_x, rect_y, mouse_x, mouse_y
rect_x += dx
rect_y += dy
dif_x_new = mouse_x - rect_x # new differnce between position of mouse cursor and rectangle to x while rectangle is going
dif_y_new = mouse_y - rect_y # new differnce between position of mouse cursor and rectangle to y while rectangle is going
step_s = math.sqrt( dif_x_new ** 2 + dif_y_new ** 2) # path changed all the time when rectangle approaches to mouse cursor
# stop move when rectangle is on the cursor position
if int(step_s) <= v:
rect_x = mouse_x
rect_y = mouse_y
glutTimerFunc(RECT_DELAY, movement, 0)
glutInit() # initialize glut
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH)
glutInitWindowSize(width, height) # set window size
glutInitWindowPosition(0, 0) # set window position
window = glutCreateWindow("Approaching rectangle") # create window with title
glutDisplayFunc(draw) # set draw function callback
glutIdleFunc(draw) # draw all the time
glutPassiveMotionFunc(mouseControl)
glutTimerFunc(RECT_DELAY, movement, 0)
glutMainLoop()