-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnonContinousLines.cpp
59 lines (58 loc) · 1.35 KB
/
nonContinousLines.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
#include <iostream>
#include <GL/glut.h>
int i=0;
//structure that holds the coordinates of a points
struct pika {
int xp;
int yp;
};
//array of points
struct pika pikat[100];
//initializer for the viewport
void init(){
glClearColor(0.0,0.0,0.0,0.0);
glViewport(0,0,300,300);
gluOrtho2D(0.0,300.0,0.0,300.0);
}
//clear the buffer
void display(){
glClear(GL_COLOR_BUFFER_BIT);
}
//draws a line that connects two points
void drawLine(pika pt1, pika pt2){
glBegin(GL_LINE_STRIP);
glVertex2i(pt1.xp,pt1.yp);
glVertex2i(pt2.xp,pt2.yp);
glEnd();
glFlush();
}
//function that handles mouse events
//when the number of clicks is even, it saves the coordinates of the last click.
//when the number of clicks is odd, it draws a line that connects the last two clicks.
void mouse(int button, int state, int x, int y){
if (button==GLUT_LEFT_BUTTON && state==GLUT_DOWN){
if(i%2==0){
pikat[i].xp=x;
pikat[i].yp=300-y;
i=i+1;
}
else{
pikat[i].xp = x;
pikat[i].yp = 300-y;
drawLine(pikat[i-1],pikat[i]);
i=i+1;
}
}
glutPostRedisplay();
}
//main function
int main(int argc, char*argv[]){
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(300,300);
glutCreateWindow("Dritare");
init();
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMainLoop();
}