Abdul Rehman
Abdul Rehman

Reputation: 2334

Rotating a 2D polygon shape algorithm

I have searched stackoverflow and find this question useful and learned about the 2D shape rotation.

I have the coordinates in such format

    int x1=-30, x2=-15, x3=20, x4=30;
    int my1=-30,y2=-15,y3=0,y4=15,y5=20,y6=30;

and have some center and pivot points like this

    int xc=320, yc=240;//Center of the figure
    int xp=0, yp=0;//Pivot point for this figure

I used this function to draw the shape

    void draw_chair()
      {    
        int loc_xc = xc+xp;
        int loc_yc = yc+yp;

        line(x2+loc_xc,my1+loc_yc,x2+loc_xc,y5+loc_yc);  
        line(x3+loc_xc,my1+loc_yc,x3+loc_xc,y5+loc_yc);   
        line(x2+loc_xc,my1+loc_yc,x3+loc_xc,my1+loc_yc);
        line(x2+loc_xc,y2+loc_yc,x3+loc_xc,y2+loc_yc);
        line(x2+loc_xc,y3+loc_yc,x3+loc_xc,y3+loc_yc);
        line(x1+loc_xc,y4+loc_yc,x4+loc_xc,y4+loc_yc);
        line(x2+loc_xc,y3+loc_yc,x1+loc_xc,y4+loc_yc);
        line(x3+loc_xc,y3+loc_yc,x4+loc_xc,y4+loc_yc);
        line(x1+loc_xc,y4+loc_yc,x1+loc_xc,y6+loc_yc);
        line(x4+loc_xc,y4+loc_yc,x4+loc_xc,y6+loc_yc);
      } 

The problem is that, Now I am confused at how to compute the rotated x and y values

I tried google and found this piece of code to rotate

    int tempx=x1;
    x1=tempx*cos(angle)-my1*sin(angle);
    my1=tempx*sin(angle)+my1*cos(angle);

    tempx=x2;
    x2=tempx*cos(angle)-y2*sin(angle);
    y2=tempx*sin(angle)+y2*cos(angle);

    tempx=x3;
    x3=tempx*cos(angle)-y3*sin(angle);
    y3=tempx*sin(angle)+y3*cos(angle);

    tempx=x4;
    x4=tempx*cos(angle)-y4*sin(angle);
    y4=tempx*sin(angle)+y4*cos(angle);

I tried this but it did not rotated shape properly but instead this code converts shape into some other strange shape. Also I have 4 x points and 6 y points, then how to compute new value for each point?

Any Idea? or hint? Thanks

Upvotes: 0

Views: 7245

Answers (1)

i Code 4 Food
i Code 4 Food

Reputation: 2154

You cannot technically rotate a coordinate, as it is just a point with no notion of direction.

The code you found is used to rotate vectors, which is indeed what you'll need, but first you would need to convert your coordinates into vectors. You can think of vectors as being the invisible line that connects the center of the figure to your points, so it consists of two points, which in your case you can assume one to be (0,0) since you later increment them with the center of the figure, and the other corresponds to your pairs such as (x2,my1), (x2,y5)... as used in your line drawing function.

Your code should actually become something like this:

PS: unless you pass in only the perfect angles, you cannot expect the figure to always work with integer coordinates. You would need them to be doubles)

int point1x, point1y;
point1x = (int) round(x2*cos(angle)-m1y*sin(angle));
point1y = (int) round(x2*sin(angle)+m1y*cos(angle));

int point2x, point2y;
point2x = (int) round(x2*cos(angle)-y5*sin(angle));
point2y = (int) round(x2*sin(angle)+y5*cos(angle));

...

line(point1x+loc_xc, point1y+loc_yc, point2x+loc_xc, point2y+loc_yc);

and so on.

Also, make sure your angle value is in radians, as both sin() and cos() functions assume that. If you are passing down degrees, convert them to radians first with the following formula:

double pi = acos(-1);
double rotation_angle = (double) angle / 180.0 * pi;

and use rotation_angle instead of angle in the code above.

Upvotes: 3

Related Questions