-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrot2.cpp
67 lines (59 loc) · 1.3 KB
/
rot2.cpp
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
#include<gl\glut.h>
#include<cmath>
typedef struct {
float x;
float y;
} Point2D;
Point2D p1, p2;
void display() {
p1.x = 0;
p2.x = 0;
glClearColor(1.0f,1.0f,1.0f,1.0f);
glClear(GL_COLOR_BUFFER_BIT);
}
void drawLine(Point2D p) {
glColor3f(0.0,0.0,0.0);
glBegin(GL_LINES);
glVertex2i(p1.x, p1.y);
glVertex2i(p.x, p.y);
glEnd();
glPointSize(6.0);
glBegin(GL_POINTS);
glVertex2i(p.x, p.y);
glEnd();
glFlush();
}
Point2D rotate(Point2D p, float ang) {
ang = (ang * 3.14) / 180;
Point2D ptemp;
ptemp.x = p.x * cos(ang) - p.y * sin(ang);
ptemp.y = p.x * sin(ang) + p.y * cos(ang);
return ptemp;
}
void mouse(int btn, int state, int x, int y) {
if(state == GLUT_DOWN) {
if(btn == GLUT_LEFT_BUTTON) {
p2.x = x; p2.y = 600 - y;
drawLine(p2);
}
else if( btn == GLUT_RIGHT_BUTTON) {
p2 = rotate(p2, 30);
drawLine(p2);
}
}
glClear(GL_COLOR_BUFFER_BIT);
}
int main( int argc, char ** argv ) {
glutInit( &argc, argv );
glutInitWindowPosition( 0, 0 );
glutInitWindowSize( 800, 600 );
glutCreateWindow( "My Drawing Screen" );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluOrtho2D( 0, 800, 0, 600 );
glViewport(0, 0, 800, 600);
glutMouseFunc( mouse );
glutDisplayFunc( display );
glutMainLoop();
return 0 ;
}