-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
239 lines (207 loc) · 3.8 KB
/
main.c
File metadata and controls
239 lines (207 loc) · 3.8 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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#include<GL/glut.h>
#include<math.h>
#include<stdlib.h>
int px,py;
int scene=0;
struct Cordinates
{
int x,y;
struct Cordinates *next;
};
struct Cordinates *points=NULL,*rpoints=NULL;
void init(void)
{
glClearColor(1.0,1.0,1.0,1.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0,800.0,0.0,600.0);
glClear(GL_COLOR_BUFFER_BIT);
}
void circle(int x,int y,int r)
{
float i;
glBegin(GL_LINES);
for(i=0;i<360;i+=0.5)
{
glVertex2i(x,y);
glVertex2f(x+r*cosf(i),y+r*sinf(i));
}
glEnd();
}
void use_reverse_point(int x,int y)
{
struct Cordinates *temp;
if(rpoints==NULL)
{
struct Cordinates *new=(struct Cordinates *)malloc(sizeof(struct Cordinates));
new->x=x;
new->y=y;
new->next=NULL;
rpoints=new;
temp=rpoints;
}
else
{
struct Cordinates *temp;
struct Cordinates *new=(struct Cordinates *)malloc(sizeof(struct Cordinates));
new->x=x;
new->y=y;
new->next=rpoints;
rpoints=new;
}
glColor3f(1,0,0);
temp=rpoints;
if(temp->next!=NULL)
{
glBegin(GL_LINES);
glVertex2i(rpoints->x,rpoints->y);
glVertex2i(rpoints->next->x,rpoints->next->y);
glEnd();
}
glFlush();
}
void simulate_movement()
{
//printf("in simulate movement");
struct Cordinates *temp;
temp=points;
if(temp->next!=NULL)
{
glColor3f(1,1,1);
/*glBegin(GL_LINES);
glVertex2i(temp->x,temp->y);
glVertex2i(temp->next->x,temp->next->y);
glEnd();*/
circle(px,py,10);
glColor3f(0,0,0);
circle(temp->next->x,temp->next->y,10);
px=temp->next->x;
py=temp->next->y;
struct Cordinates *t=temp;
points=temp->next;
use_reverse_point(t->x,t->y);
free(t);
usleep(50000);
}
else
{
glColor3f(1,1,1);
circle(px,py,10);
free(temp);
points=NULL;
scene=2;
}
glFlush();
}
void simulate_reverse_movement()
{
//printf("in simulate movement");
struct Cordinates *temp;
temp=rpoints;
if(temp->next!=NULL)
{
glColor3f(1,1,1);
glBegin(GL_LINES);
glVertex2i(temp->x,temp->y);
glVertex2i(temp->next->x,temp->next->y);
glEnd();
circle(px,py,10);
glColor3f(0,0,0);
circle(temp->next->x,temp->next->y,10);
px=temp->next->x;
py=temp->next->y;
struct Cordinates *t=temp;
rpoints=temp->next;
//use_reverse_point(t->x,t->y);
free(t);
usleep(50000);
}
else
{
glColor3f(1,1,1);
circle(px,py,10);
free(temp);
rpoints=NULL;
scene=0;
}
glFlush();
}
void draw()
{
switch(scene)
{
case 1: simulate_movement();
break;
case 2: simulate_reverse_movement();
}
glFlush();
}
void use_point(int x,int y)
{
struct Cordinates *temp;
if(points==NULL)
{
struct Cordinates *new=(struct Cordinates *)malloc(sizeof(struct Cordinates));
new->x=x;
new->y=y;
new->next=NULL;
points=new;
temp=points;
}
else
{
struct Cordinates *temp;
temp=points;
while(temp->next!=NULL)
temp=temp->next;
struct Cordinates *new=(struct Cordinates *)malloc(sizeof(struct Cordinates));
new->x=x;
new->y=y;
new->next=NULL;
temp->next=new;
}
glColor3f(1,0,0);
temp=points;
if(temp->next!=NULL)
{
while(temp->next->next!=NULL)
temp=temp->next;
glBegin(GL_LINES);
glVertex2i(temp->x,temp->y);
glVertex2i(x,y);
glEnd();
}
glFlush();
}
void mouse(GLint button,GLint state,GLint x,GLint y)
{
//printf("mouse event");
if(scene==0)
{
if(button==GLUT_LEFT_BUTTON&&state==GLUT_UP)
{
//printf("mouse up");
scene=1;
}
}
//printf("out of function mouse_clicked=%d\n",mouse_clicked);
//printf("\n");
}
void mouse_move(int x,int y)
{
if(x>=0&&x<=800&&y>=0&&y<=600)
use_point(x,600-y);
}
int main(int argc,char *argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowPosition(50,100);
glutInitWindowSize(800,600);
glutCreateWindow("Ball movement simulation");
init();
glutDisplayFunc(draw);
glutIdleFunc(draw);
glutMouseFunc(mouse);
glutMotionFunc(mouse_move);
glutMainLoop();
}