Thread: How to Rotate a line?

1. How to Rotate a line?

Can anyone help me out with rotating a line about a fixed point?

say Like x,y = 100 and length of line is 10. (By rotation, It draws a circle of radius 10). To be clear, I have a circle of radius 10. I imagine it to be a wheel of a car. I need to rotate a line from the center to the circumference of the circle as the car is moving forward.

My code for moving a 2-d Car from left to right is below.
Code:
```#include<graphics.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"e:\\lab\\tc\\bgi");
for(i=100;i<400;i++)
{
setcolor(3);
line(i,100,i+15,85);		//RISING LINE
line(i+15,85,i+70,85);          //ST.LINE AT TOP
line(i+70,85,i+100,100);        //FALLING LINE
rectangle(i+100,150,i,100);	//BODY
rectangle(i+10,130,i+30,110);   //WINDOW1
rectangle(i+40,130,i+60,110);   //WINDOW2
rectangle(i+70,130,i+90,110);   //WINDOW3
circle(i+15,125,5);             //FACE1
circle(i+45,125,5);             //FACE2
circle(i+75,125,5);             //FACE3
circle(i+20,160,10);            //WHEEL1
circle(i+80,160,10);            //WHEEL2
delay(25);
cleardevice();
}
closegraph();
}```

2. I want to point out that void main is undefined. You should use int main instead.

3. For what you need something like this should work:
Code:
`line(x, y, x+cos(degrees)*length, y+sin(degrees)*length);`
x, and y would be the point around which to rotate. In C you also need to convert degrees to radians tho.

4. @mike_g

Could you please put that in a for loop and show?

@elysia

I can see your hatred towards void main from your siggy , but I don't have a need to define the return type for main() in my program.

5. Originally Posted by hariharan
@elysia

I can see your hatred towards void main from your siggy , but I don't have a need to define the return type for main() in my program.
You do no seem to understand. Whether or not you need to return something is irrelevant. Void main is undefined behavior and should not compile at all. Unfortunately, it does compile, but it is still undefined and very, very bad. So don't use it. Use int main.

6. I'm working with some rotating rim spokes for your car's wheels.

You should have some "bad" low-rider wheels later this evening.

It's not terribly sophisticated, (no math involved), but it might work.

Code:
```#include <graphics.h>

/*
line(x, y, x+cos(degrees)*length, y+sin(degrees)*length);
x, and y would be the point around which to rotate. In C you also need to

Can we add spokes/rims to the wheels, and show them rotating?
Add a black line, same xy's, that chases the blue one, deleting them.
*/

int main()
{

int i, msec;  //milliseconds used by delay()
int gd=DETECT, gm, errorcode;  //int gd=DETECT,gm;

/* what I use: initgraph(&gd,&gm,""); */
initgraph(&gd,&gm,"e:\\lab\\tc\\bgi");

/* check if it loaded OK */
errorcode = graphresult();
if (errorcode != grOk) {                /* an error occurred */
printf("Graphics error: &#37;s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);                                      /* return with error code */
}

for(i=100;i<400;i++)
{
setcolor(3);
line(i,100,i+15,85);		        //RISING LINE
line(i+15,85,i+70,85);                //ST.LINE AT TOP
line(i+70,85,i+100,100);            //FALLING LINE
rectangle(i+100,150,i,100);	   //BODY
rectangle(i+10,130,i+30,110);   //WINDOW1
rectangle(i+40,130,i+60,110);   //WINDOW2
rectangle(i+70,130,i+90,110);   //WINDOW3
circle(i+15,125,5);                      //FACE1
circle(i+45,125,5);                      //FACE2
circle(i+75,125,5);                      //FACE3
circle(i+20,160,10);                    //WHEEL1
circle(i+80,160,10);                    //WHEEL2

msec = 30;
line(i+20, 160, i+20, 164);      //6 o'clock line
delay(msec);                    //after a delay
setcolor(0);                    //erase it
line(i+20, 160, i+20, 164);

setcolor(3);
line(i+20, 160, i+10, 160);     //9 o'clock
delay(msec);
setcolor(0);
line(i+20, 160, i+10, 160);

setcolor(3);
line(i+20, 160, i+20, 156);     //12 o'clock
delay(msec);
setcolor(0);
line(i+20, 160, i+20, 156);

setcolor(3);
line(i+20, 160, i+30, 160);     //3 o'clock
//delay(msec);

delay(msec);  //was 25
cleardevice();                  //3 o'clock erased here
}

closegraph();
return 0;
}```
The "turning wheel spokes", could have spokes at 2, 4, 8, & 10 o'clock positions, instead, or have them both, to double up on the realism.

A small circle in the center of each wheel could serve as a visual wheel hub, when the spokes would
stop at the circumference of the hub, instead of going on to the center of the wheel (which looks a bit cartoony, imo). Edit: tried this, and it
didn't look good. Went with more spokes, and a sliding scale of "visibility", below. That worked very well.

I played around with the timing a lot, the above looks good on a P3, but it may need to be adjusted for other cpu's.

7. This is the best rear wheel I could come up with, consistent with your design. I left the front wheel unchanged for comparison.

You're cruisin' now!

Code:
```#include <graphics.h>
/*
line(x, y, x+cos(degrees)*length, y+sin(degrees)*length);
x, and y would be the point around which to rotate. In C you also need to

I didn't use the above info.
*/
int main()
{
int i, msec;  //milliseconds used by delay()
int gd=DETECT, gm, errorcode;  //int gd=DETECT,gm;
//initgraph(&gd,&gm,"");      //for my system
initgraph(&gd,&gm,"e:\\lab\\tc\\bgi");

/* check if it loaded OK */
errorcode = graphresult();
if (errorcode != grOk) {  /* an error occurred */
printf("Graphics error: &#37;s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);             /* return with error code */
}

for(i=100;i<400;i++)
{

setcolor(3);
line(i,100,i+15,85);			//RISING LINE
line(i+15,85,i+70,85);			//ST.LINE AT TOP
line(i+70,85,i+100,100);		//FALLING LINE
rectangle(i+100,150,i,100);		//BODY
rectangle(i+10,130,i+30,110);		//WINDOW1
rectangle(i+40,130,i+60,110);		//WINDOW2
rectangle(i+70,130,i+90,110);		//WINDOW3
circle(i+15,125,5);			//FACE1
circle(i+45,125,5);			//FACE2
circle(i+75,125,5);			//FACE3
circle(i+20,160,10);			//WHEEL1
circle(i+20,160,12);			//Wheel depth
circle(i+80,160,10);			//WHEEL2

msec = 10;
line(i+20, 160, i+20, 164);     	//6 o'clock line
//delay(msec);				   //after a delay
if(i % 3 == 0) {
setcolor(0);				//erase it
line(i+20, 160, i+20, 164);
}

setcolor(3);
line(i+20, 160, i+16, 163);		//8 o'clock line
//delay(msec);				//after a delay
if(i % 4 == 0) {
setcolor(0);			//erase it
line(i+20, 160, i+16, 163);
}
setcolor(3);
line(i+20, 160, i+10, 160);		//9 o'clock
//delay(msec);
if(i % 5 == 0) {
setcolor(0);
line(i+20, 160, i+10, 160);
}
setcolor(3);
line(i+20, 160, i+15, 158);		//10 o'clock
//delay(msec);
if(i % 6 == 0) {
setcolor(0);
line(i+20, 160, i+15, 158);
}
setcolor(3);
line(i+20, 160, i+20, 156);		//12 o'clock
delay(msec);
if(i % 7 == 0) {
setcolor(0);
line(i+20, 160, i+20, 156);
}
setcolor(3);
line(i+20, 160, i+25, 158);		//2 o'clock
delay(msec);
if(i % 8 == 0) {
setcolor(0);
line(i+20, 160, i+25, 158);
}
setcolor(3);
line(i+20, 160, i+30, 160);		//3 o'clock
delay(msec);
if(i % 9 == 0) {
setcolor(0);
line(i+20, 160, i+30, 160);
}
setcolor(3);
line(i+20, 160, i+25, 162);		//4 o'clock
delay(msec);
if(i % 10 == 0) {
setcolor(0);
line(i+20, 160, i+25, 162);
}
delay(msec);				//was 25
cleardevice();				//last o'clock line erased here
}

closegraph();
return 0;
}```
The delay before erasing gave way to a better looking scheme involving the a sliding scale use of mod on i, to determine if a line should be erased.

8. Woah bro! you are damn good at this! thanks

9. Originally Posted by hariharan
Woah bro! you are damn good at this! thanks
As a matter of fact, I'm a total noob at any kind of animation. I did spend a *lot* of time trying different things, to make that wheel look as good as it does.

10. Well heres my solution. Not sure if it will actually work as I never used graphics.h, and haven't tested it. But anyway:
Code:
```#include <graphics.h>
#include <math.h>
#include <time.h>

int main()
{

int i;
int gd=DETECT, gm, errorcode;

/* what I use: initgraph(&gd,&gm,""); */
initgraph(&gd,&gm,"e:\\lab\\tc\\bgi");

/* check if it loaded OK */
errorcode = graphresult();
if (errorcode != grOk) {                /* an error occurred */
printf("Graphics error: &#37;s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);                                      /* return with error code */
}

// Some new variables
int spokes, degrees = 0, speed = 3, angle;
unsigned start_time;

for(i=100;i<400;i++)
{
start_time = clock();

setcolor(3);
line(i,100,i+15,85);		        //RISING LINE
line(i+15,85,i+70,85);                //ST.LINE AT TOP
line(i+70,85,i+100,100);            //FALLING LINE
rectangle(i+100,150,i,100);	   //BODY
rectangle(i+10,130,i+30,110);   //WINDOW1
rectangle(i+40,130,i+60,110);   //WINDOW2
rectangle(i+70,130,i+90,110);   //WINDOW3
circle(i+15,125,5);                      //FACE1
circle(i+45,125,5);                      //FACE2
circle(i+75,125,5);                      //FACE3
circle(i+20,160,10);                    //WHEEL1
circle(i+80,160,10);                    //WHEEL2

// Draw each spoke
for(spokes = 0; spokes < 8; spokes ++)
{
line(i+20, 160, i+20+cos(angle)*10, 160+sin(angle)*10;
}
// Rotate
if((degrees+=speed) > 359) degrees -= 360;

// Run at ~60fps
if(clock()-start_time < 17) delay(17-(clock()-start_time));
cleardevice();
}

closegraph();
return 0;
}```

11. Code:
`      delay(17-(clock()-start_time));`
Lets hope:
1. clock() returns a time compatible with delay.
2. That clock()-starttime isn't greater than 17.

--
Mats

12. 1. clock() returns a time compatible with delay.
It should be because its running in windows
2. That clock()-starttime isn't greater than 17.
Good point, I'll do a quick edit. Theres probably bound to be other stuff wrong with it too

13. Actually, the above code runs in DOS - albeit emulated inside Windows.

--
Mats

14. Oh, I though graphics.h was a windows thing. Guess not. Well I supposed if you ran it in DOSbox on Linux it it would be a bit screwed up then.

15. Originally Posted by mike_g
Oh, I though graphics.h was a windows thing. Guess not. Well I supposed if you ran it in DOSbox on Linux it it would be a bit screwed up then.
Probably not, it's not dependant on the underlaying OS architecture, it's the implementation of "clock" that I'm concerned with - I'm just pointing out that "clock()" may not return a number of milliseconds. Unfortunately it gets a bit difficult to fix if you want to make it fully portable.

--
Mats