Archived

This topic is now archived and is closed to further replies.

baddrizz

2d rotation trouble

Recommended Posts

heya i''m having trouble with rotating a rectangle i just want it to rotate 45 degree''s but when i go to draw it its way off were it should be here is the way i''m doing it angle = 45*(PI/180) a = cx + (x * cos(angle)) - (y * sin(angle)); b = cy + (y * cos(angle)) + (x * sin(angle)); cx and cy are the center of the rect so is it my formula or is there something i''m doing wrong?? please help

Share this post


Link to post
Share on other sites
ok, this bit might be a bit obfuscated, but this piece of code will draw a rotated rectangle on screen (using gdi). If you are using some other api you won''t find it hard to fix it.


double height=subject.height/2,width=subject.width/2,hypo=sqrt((subject.width/2*subject.width/2)+(subject.height/2*subject.height/2));

MoveToEx(main_gdc,subject.x-cos(subject.angle + atan((-height) / (width))) * hypo,
subject.y-sin(subject.angle + atan((-height) / (width))) * hypo,NULL);

LineTo(main_gdc,subject.x+cos(subject.angle + atan((height) / (width))) * hypo,
subject.y+sin(subject.angle + atan((height) / (width))) * hypo);
LineTo(main_gdc,subject.x+cos(subject.angle + atan((-height) / (width))) * hypo,
subject.y+sin(subject.angle + atan((-height) / (width))) * hypo);
LineTo(main_gdc,subject.x-cos(subject.angle + atan((height) / (width))) * hypo,
subject.y-sin(subject.angle + atan((height) / (width))) * hypo);
LineTo(main_gdc,subject.x-cos(subject.angle + atan((-height) / (width))) * hypo,
subject.y-sin(subject.angle + atan((-height) / (width))) * hypo);


/john

Share this post


Link to post
Share on other sites
const long double angle = 45*(PI/180);

const long double COS = cos(angle);
const long double SIN = sin(angle);

// Here was the mistake
// Your formulas were for rotate points
// around origin of coordinates
long double oldX = x - cx;
long double oldY = y - cy;

a = oldX * COS - oldY * SIN;
b = oldY * COS + oldX * SIN;

a += cx;
b += cy;

Share this post


Link to post
Share on other sites