-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask2.cpp
303 lines (267 loc) · 6.29 KB
/
task2.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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <math.h>
#include <cstdlib>
using namespace std;
using namespace cv;
int radius = 50;
int rows=1000,cols=1000;
// making an image on which the program works
/*
void make_center_line(int xr,int yr,int xg,int yg,int xb,int yb)
{
img2.at<Vec3b>(xr,yr)={255,0,0};
img2.at<Vec3b>(xg,yg)={0,255,0};
img2.at<Vec3b>(xb,yb)={0,0,255};
}
*/
Mat make_ball(int xr,int yr,int xg,int yg,int xb,int yb) // make the images of the ball at evrey moment of time
{
int i,j;
Mat img3(rows,cols,CV_8UC3,Scalar(0,0,0));
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
if(((i-yr)*(i-yr)+(j-xr)*(j-xr)) < radius*radius) //making the red circle(ball)
img3.at<Vec3b>(i,j)={255,0,0};
if(((i-yg)*(i-yg)+(j-xg)*(j-xg)) < radius*radius) //maknig the green circle(bal)
img3.at<Vec3b>(i,j)={0,255,0};
if(((i-yb)*(i-yb)+(j-xb)*(j-xb)) < radius*radius) //making the blue circle(bal)
img3.at<Vec3b>(i,j)={0,0,255};
}
}
return img3;
}
int rng_collision(int xr,int yr,int xg,int yg,int rm,int gm)
{
int angle = 0;
if(rm==1)//red ball is moving)
{
angle=atan((yg-yr)/(xg-xr)); //this is for red collide with green and red stops green moves
}
//similarly for green collide with red
if(gm==1)//green ball moving
{
angle=atan((yr-yg)/(xr-xg)); //this is for red collide with green and red stops green moves
}
return angle;
}
int gnb_collision(int xg,int yg,int xb,int yb,int gm,int bm)
{
int angle = 0;
if(gm==1)//green ball is moving)
{
angle=atan((yb-yg)/(xb-xg)); //this is for green collide with blue and green stops blue moves
}
//similarly for blue collide with green
if(bm==1)//blue ball is moving)
{
angle=atan((yg-yb)/(xg-xb)); //this is for blue collide with green and blue stops green moves
}
return angle;
}
int bnr_collision(int xb,int yb,int xr,int yr,int bm,int rm)
{
int angle = 0;
if(rm==1)//red ball is moving)
{
angle=atan((yb-yr)/(xb-xr)); //this is for red collide with blue and red stops blue moves
}
//similarly for blue collide with red
if(bm==1)//blue ball is moving)
{
angle=atan((yr-yb)/(xr-xb)); //this is for blue collide with red and blue stops red moves
}
return angle;
}
int vertwall_collision(int angle) // ball collidin with the wall on left and right
{
if(angle<=180)
{
angle=180-angle; //the new angle with which the ball moves
}
if(angle>180 && angle<360)
{
angle=angle-180;
}
return angle;
}
int horiwall_collision(int angle) // ball colliding with the wall on the top and the bottom
{
angle=360-angle; //the new angle with which the ball moves
return angle;
}
// 1--RED AND GREEN BALL COLLISION
// 2--GREEN AND BLUE BALL COLLISION
// 3--BLUE AND RED BALL COLLISION
int check_ball_collision(int xr,int yr,int xg,int yg,int xb,int yb)
{
if(pow((xr-xg),2)+pow((yr-yg),2) < 4*pow(radius,2))
{ cout << "RED AND GREEN BALL COLLISION";
return 1;
}
if(pow((xg-xb),2)+pow((yg-yb),2) < 4*pow(radius,2))
{
cout << "GREEN AND BLUE BALL COLLISION";
return 2;
}
if(pow((xb-xr),2)+pow((yb-yr),2) < 4*pow(radius,2))
{
cout << "BLUE AND RED BALL COLLISION";
return 3;
}
else
return 0;
}
void ball_motion(int *angle,int *x,int *y) // finding the nest closest pixtel from the line formlae tan(angle)=y/x
{ //(0 to 180)---upward motion
//(180 to 360)---downward motion
if(*angle<=90 && *angle>=270)
{
//considering the unit length to be of 3 units(can be changed) ie the circle moves by 3 units
if(*x+1+radius>=cols)
{
*angle=vertwall_collision(*angle); //right vertical wall collision
}
*x=*x+1;
if(*angle>=0 && *angle<=180) //using pointer so that all the values of x,y,angle gets changed
{
if(*y-1*abs(tan(*angle))-radius<=0)
{
*angle=horiwall_collision(*angle);
}
*y=*y-1*abs(tan(*angle));
}
else
{
if(*y+1*abs(tan(*angle))+radius>=rows)
{
*angle=horiwall_collision(*angle);
}
*y=*y+1*abs(tan(*angle));
}
}
if(*angle>=90 && *angle<=270)
{
//considering the unit length to be of 1 units(can be changed) ie the circle moves by 1 units
if(*x-1-radius<=0)
{
*angle=vertwall_collision(*angle); //left vertical wall collision
}
*x=*x-1;
if(*angle>=0 && *angle<=180)
{
if(*y-1*abs(tan(*angle))-radius<=0)
{
*angle=horiwall_collision(*angle);
}
*y=*y-1*abs(tan(*angle));
}
else
{
if(*y+1*abs(tan(*angle))+radius>=rows)
{
*angle=horiwall_collision(*angle);
}
*y=*y+1*abs(tan(*angle));
}
}
}
int main()
{
int xr,yr,xb,yb,xg,yg,collision_no=0,a,angle;
Mat img3(rows,cols,CV_8UC3,Scalar(0,0,0));
xr=600;yr=500; //intializing the positions of the balls
xg=700;yg=400;
xb=300;yb=100;
angle=90;
int rm,gm,bm;
rm=1;gm=0;bm=0;
namedWindow("billard1",WINDOW_NORMAL);
while(collision_no!=8000)
{
img3=make_ball(xr,yr,xg,yg,xb,yb);
a=check_ball_collision(xr,yr,xg,yg,xb,yb); //a contains the value of the ball which has collided
// a=1--RED AND GREEN BALL COLLISION
// a=2--GREEN AND BLUE BALL COLLISION
// a=3--BLUE AND RED BALL COLLISION
if(a==1)
{
collision_no++;
angle=rng_collision(xr,yr,xg,yg,rm,gm);
if(rm==1)
{
rm=0;gm=1;bm=0;
xg+=1;
yg+=1;
ball_motion(&angle,&xg,&yg);
}
else
{
rm=1;gm=0;bm=0;
xr+=1;
yr+=1;
ball_motion(&angle,&xr,&yr);
}
}
if(a==2)
{
collision_no++;
angle=gnb_collision(xg,yg,xb,yb,gm,bm);
if(gm==1)
{
rm=0;gm=0;bm=1;
xb+=1;
yb+=1;
ball_motion(&angle,&xb,&yb);
}
else
{
rm=0;gm=1;bm=0;
xg+=1;
yg+=1;
ball_motion(&angle,&xg,&yg);
}
}
if(a==3)
{
collision_no++;
angle=bnr_collision(xb,yb,xr,yr,bm,rm);
if(bm==1)
{
rm=1;gm=0;bm=0;
xr+=1;
yr+=1;
ball_motion(&angle,&xr,&yr);
}
else
{
rm=0;gm=0;bm=1;
xb+=1;
yb+=1;
ball_motion(&angle,&xb,&yb);
}
}
if(a==0)
{
if(rm==1)
{
cout<<angle;
ball_motion(&angle,&xr,&yr);
}
if(bm==1)
{
ball_motion(&angle,&xb,&yb);
}
if(gm==1)
{
ball_motion(&angle,&xg,&yg);
}
}
imshow("billard1",img3 );
waitKey(1);
}
}