Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rotate rectangle points individually distort the rectangle

enter image description hereI'm trying to rotate a rectangle by rotating its points , using this code

  var
 dx,dy:real;
 rotp:Tpoint;
begin
  dx := (CenterPoint.Y * Sin(angle)) - (CenterPoint.X * Cos(angle)) + CenterPoint.X;
  dy := -(CenterPoint.X * Sin(angle)) - (CenterPoint.Y * Cos(angle)) + CenterPoint.Y;
  rotP.X := round((point.X * Cos(angle)) - (point.Y * Sin(angle)) + dx);
  rotP.Y := round((point.X * Sin(angle)) + (point.Y * Cos(angle)) + dy);
  result:= rotP;
end;

but the round function makes the rectangle distorted , has anyone any idea how to overcome this?

I attached the image, the white points are the points i rotate about the center point, i'm sure that the image is rotated well thus, the white points should be identical to the corners of the image.

like image 946
Sara S. Avatar asked Nov 20 '11 13:11

Sara S.


1 Answers

The only way I can see that this approach would fail is if you are transforming every point on the perimeter. If you are doing that, don't. Transform the corners and draw lines between each corner using graphics primitives.

Update: Your comment gives the game away. You are rotating repeatedly and accumulating errors every time you digitise by converting to integer. Deal with that by storing your coordinates as double precision values and just convert to integer on demand when you need to draw.

In fact, if I were you I would treat your master data to be a position and an angle, both stored to double precision. I would not store the coordinates of the corners at all. I would store a position (center or one of the corners) and an orientation angle (relative to a fixed global axis system). That way you will always draw a true rectangle. At each integration step increment position and orientation as necessary and then calculate the position of the corners from the master data. Do it like this and you will never suffer from distortion of your shape.

like image 66
David Heffernan Avatar answered Oct 05 '22 14:10

David Heffernan