How to Rotate a line?

This is a discussion on How to Rotate a line? within the C Programming forums, part of the General Programming Boards category; Can anyone help me out with rotating a line about a fixed point? say Like x,y = 100 and length ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    6

    Smile 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();
    }
    Please help me out

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    I want to point out that void main is undefined. You should use int main instead.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    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. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    6
    @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. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    Quote Originally Posted by hariharan View Post
    @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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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 
    convert degrees to radians tho.
    
    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.
    Last edited by Adak; 01-28-2008 at 05:57 AM.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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 
    convert degrees to radians tho.
    
    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.
    Last edited by Adak; 01-28-2008 at 01:02 AM.

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    6
    Woah bro! you are damn good at this! thanks

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by hariharan View Post
    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.

    I'm glad you like it.

  10. #10
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    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>
    #define DEG_TO_RAD 0.0174532925
    
    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 ++)  
          {
              angle = (degrees+spokes*45)*DEG_TO_RAD;
              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;
    }
    Last edited by mike_g; 01-28-2008 at 06:23 AM.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    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. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Actually, the above code runs in DOS - albeit emulated inside Windows.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    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. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by mike_g View Post
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing Length of Input and the Limited Input
    By dnguyen1022 in forum C Programming
    Replies: 33
    Last Post: 11-29-2008, 03:13 PM
  2. adding line numbers and concatenating a filename
    By durrty in forum C Programming
    Replies: 25
    Last Post: 06-28-2008, 03:36 AM
  3. Finding carriage returns (\c) in a line
    By JizJizJiz in forum C++ Programming
    Replies: 37
    Last Post: 07-19-2006, 05:44 PM
  4. Adding Line numbers in Word
    By Mister C in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 06-24-2004, 08:45 PM
  5. how to rotate a line of text ??
    By darrenteo in forum C Programming
    Replies: 5
    Last Post: 04-06-2002, 09:09 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21