I am learning and I am a beginner in C++ programming and we are currently learning about Translation, rotation and scaling. We have been given a code which shows an outer circle in a circular orbit around another inner circle. We are required to make this outer circle revolve around the inner circle. Here's the given code:
{
int gd,gm;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\tc\\bgi");
setcolor(WHITE);
rectangle(10,60,400,400);
//Central inner circle
int CC_x,CC_y;
CC_x=10+390/2;
CC_y=60+340/2;
outtextxy(CC_x, CC_y, "Central Circle");
circle(CC_x,CC_y,30);
//orbit
circle(CC_x,CC_y,120);
//Outer circle
int c_x,c_y;
double angle;
angle=0;
c_x=CC_x+120;
c_y=CC_y;
while(1){
setcolor(WHITE);
outtextxy(c_x, c_y, "Outer Circle");
circle(c_x,c_y,10);
// Blinking effect
circle(CC_x,CC_y,120);
delay(100);
setcolor(BLACK);
outtextxy(c_x, c_y, "Outer Circle");
circle(c_x,c_y,10);
delay(90);
// modified code
c_x = CC_x + (c_x - CC_x) cos(angle) – (c_y - CC_y) sin(angle)
c_y = CC_y + (c_x - CC_x) sin(angle) – (c_y - CC_y) cos(angle)
angle++
// end of modified code
}
getch();
}
I modified this by declaring double angle
and initializing it to 0. Then I used the following formulas inside the loop.
x′ = xr + (x - xr) cosθ – (y - yr) sinθ
y′ = yr + (x - xr) sinθ – (y - yr) cosθ
But the result is that the outer circle ends up spiraling down to the center of inner circle. How can I make it stay on its circular path? I hope I have explained my problem well enough. I have tried to google the solution of this problem but it only gives me the above formula which I've already tried. If you guys could help me and tell me what I am doing wrong with my code, I would be very thankful to you. Thanks.
The classical error is to use the modified
x
in the second assignment, so that you are actuallycomputing
yr + (x' - xr) sinθ – (y - yr) cosθ
(Not showing the actual code that you are using is not a good idea.)