#### Archived

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

# 2d rotation trouble

This topic is 6005 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 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 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;

1. 1
Rutin
21
2. 2
3. 3
JoeJ
16
4. 4
5. 5

• 26
• 20
• 13
• 13
• 17
• ### Forum Statistics

• Total Topics
631700
• Total Posts
3001781
×