Page Index Toggle Pages: 1 Send TopicPrint
Normal Topic Coordinates of a rotated box (Read 1837 times)
MagnusSpeychal
Junior Member
**
Offline



Posts: 53
Joined: Jun 18th, 2007
Coordinates of a rotated box
Aug 31st, 2007 at 6:35am
Print Post  
Hi

I have noticed that the coordinates of a box obtained by the GetLeft()-, GetRight-, GetTop()- and GetBottom()-methods as well as the BoundingRect()-method do not get updated when the box is rotated. It seems that the coordinates only get updated when the box is moved. Currently I have had to make some calculations in order to get the correct coordinates when the box i rotated. Is it possible to update the box coordinates manually when rotating a box or is there some other way to get hold of the correct coordinates?


Best regards,
Magnus
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Coordinates of a rotated box
Reply #1 - Aug 31st, 2007 at 8:41am
Print Post  
Hi,

You can find the bounds of the rotated box using the rotateRect method shown below.

Code
Select All
POINT rotatePointAt(POINT point, POINT pivot, float angle)
{
	if (angle > -0.00001f && angle < 0.00001f)
		return point;

	point.x -= pivot.x;
	point.y -= pivot.y;

	float rad = angle * M_PI / 180;
	POINT res = {
		point.x * cos(rad) - point.y * sin(rad),
		point.x * sin(rad) + point.y * cos(rad)
	};

	res.x += pivot.x;
	res.y += pivot.y;
	return res;
}


void rotatePointsAt(POINT* points, int count, POINT pivot, float angle)
{
	if (angle > -0.00001f && angle < 0.00001f)
		return;

	for (int i = 0; i < count; ++i)
		points[i] = rotatePointAt(points[i], pivot, angle);
}


RECT rotateRectAt(const RECT& rc, POINT pivot, float angle)
{
	if (angle > -0.00001f && angle < 0.00001f)
		return rc;

	POINT p[] = {
		{ rc.left, rc.top },
		{ rc.right, rc.top },
		{ rc.right, rc.bottom },
		{ rc.left, rc.bottom }
	};
	rotatePointsAt(p, 4, pivot, angle);

	float minX = min(p[0].x, min(p[1].x, min(p[2].x, p[3].x)));
	float minY = min(p[0].y, min(p[1].y, min(p[2].y, p[3].y)));
	float maxX = max(p[0].x, max(p[1].x, max(p[2].x, p[3].x)));
	float maxY = max(p[0].y, max(p[1].y, max(p[2].y, p[3].y)));

	RECT res = { minX, minY, maxX, maxY };
	return res;
}
 



Stoyan
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint