Rectangle Rotation problem

This is a discussion on Rectangle Rotation problem within the C Programming forums, part of the General Programming Boards category; Hello friends i am facing a typical problem.. The following is my code to rotate a rectangle. It works fine ...

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    4

    Rectangle Rotation problem

    Hello friends i am facing a typical problem..
    The following is my code to rotate a rectangle. It works fine for 60 and 90 degrees of rotation but not for 30 and 45 degrees. I am unable to figure out where i have went wrong.. Please have a look at this..

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<graphics.h>
    #include<math.h>
    void main()
    {
     int gd=DETECT,gm,deg;
    int left,top,right,bottom;
     float rad,x1,y1,x2,y2,x3,y3,x4,y4;
     clrscr();
     initgraph(&gd,&gm,"");
       printf("Enter the co-ordinates of the rectangle :");
       scanf("%d%d%d%d",&left,&top,&right,&bottom);
     printf("\nEnter the angle to rotate :");
     scanf("%d",&deg);
     rad=deg*(3.14/180);
    
    line(left,bottom,right,bottom);
    line(left,bottom,left,top);
    line(right,bottom,right,top);
    line(left,top,right,top);
    
       x1=left*cos(rad)-bottom*sin(rad);
       y1=left*sin(rad)+bottom*cos(rad);
       x2=right*cos(rad)-bottom*sin(rad);
       y2=right*sin(rad)+bottom*cos(rad);
       x3=left*cos(rad)-top*sin(rad);
       y3=left*sin(rad)+top*cos(rad);
       x4=right*cos(rad)-top*sin(rad);
       y4=right*sin(rad)+top*cos(rad);
    
       line(fabs(x1),fabs(y1),fabs(x2),fabs(y2));
       line(fabs(x1),fabs(y1),fabs(x3),fabs(y3));
       line(fabs(x3),fabs(y3),fabs(x4),fabs(y4));
       line(fabs(x2),fabs(y2),fabs(x4),fabs(y4));
       getch();
    }
    Can you please guide me to rectify the error...

  2. #2
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Z rotation is:

    final.x = (vertex.x * cosf(angle)) + (vertex.y * sinf(angle));
    final.y = (vertex.x * -sinf(angle)) + (vertex.y * cosf(angle));

    Note that while you can rotate once using this formula, cumulative rotations will cause the vertices to get wacky. In order to transform vertices correctly you must always start in local space with a scale of 1.0f on all axes (to do correct scaling), and with the vertices centered around the local origin (to rotate around center). To rotate the object around other points in 3D space you will have to translate to that point and then rotate again.

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    4

    Thanks for the reply

    Thank you very much for your reply...
    can you please give the code using the following formula to rotate??

    xdash=x*cos(angle)-y*sin(angle);
    ydash=x*sin(angle)+y*cos(angle);

    you had given final.x,vertex.y etc...

    I cant understand how to implement your suggestion. please help me..

  4. #4
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Why when that is the wrong formula?

  5. #5
    Registered User
    Join Date
    Jul 2010
    Posts
    4
    how to make the centre of the rectangle to be at the origin always??

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    If you want to rotate a rectangle in place (using the same center point), isn't that a fairly straight forward equation?

    With a square, it's quite elementary. With a rectangle with len != width, it's a bit more difficult, but all you should need is one point of reference for the new rectangle, and everything can work out from there, without any other sin and cos calculations.

    This is a rectangle, after all.

    If it's not a true rectangle, then yeah, you need lots of math.

  7. #7
    Registered User
    Join Date
    Jul 2010
    Posts
    4
    ok thanks for the info.. I am going to modify the program by fixing the center of the rectangle now...

  8. #8
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Just use vertices and transformations. It is the simplest and works in all situations

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Image rotation - doesn't always work
    By ulillillia in forum C Programming
    Replies: 12
    Last Post: 05-03-2007, 12:46 PM
  2. Replies: 5
    Last Post: 11-07-2005, 10:34 PM
  3. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  4. Help With pointers in classes.
    By indigo0086 in forum C++ Programming
    Replies: 12
    Last Post: 10-10-2002, 02:03 PM
  5. more with my 1st structure program
    By sballew in forum C Programming
    Replies: 42
    Last Post: 10-22-2001, 08:03 PM

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