How many corners needed to generate a rectangle?

Started by
9 comments, last by Zakwayda 15 years, 8 months ago
Hey all, I've got a question that I can't seem to find the answer to and my tired brain is failing me. How many corners are needed to generate a rectangle so it is rotated correctly? I initially thought it would be two (assuming each corner was diagonal from the other). Now I'm thinking you might need all four (or maybe 3?). Anyways, once you get the corner points you need, how do you determine the correct rotation to apply to the rectangle so each corner of the rect is in the correct place? Thanks in advance if you have any tips or articles for me.
Advertisement
It depends on a lot of things. Is this a 2D or 3D rotation (or more dimensions)? Is the rotation around a fixed or arbitrary point/axis? Are the corners that you're given ordered? Are you trying to solve for a specific angle mod two pi or mod pi?
Quote:Original post by SiCrane
It depends on a lot of things. Is this a 2D or 3D rotation (or more dimensions)? Is the rotation around a fixed or arbitrary point/axis?


Its a rotation around the center of the rectangle in 2D.

Quote:
Are the corners that you're given ordered?


They are ordered. If I only have 2 corners, then they are diagonal to each other. If I have four corners, then I know that c1 connects to c2, which connects to c3, which connects to c4 and back to c1 again.

Quote:
Are you trying to solve for a specific angle mod two pi or mod pi?


Not sure what you are asking here, I'm trying to solve for the angle to rotate my rectangle when I create it. I create it at the center-point, with the x and y lengths calculated from the corners that I have.

Example:

With these corners:
      C     B      A      D


I can create a rectangle that looks like this:
   |--C----| B   |       | A |---D---|


I then need to rotate it so it looks like this:
     |C------B   |       |   A------D



Edit:
Make my ASCII cubes look right
The corners in your diagram form a parallelogram, not a rectangle. Or am I missing something?

Also, perhaps you could provide some more information about the problem that you're actually trying to solve (as there may be a better way to go about it).
Sorry, I'll try to be more clear. My users are going to be creating rectangles around oddly shaped objects in my gameworld by defining the 4 corners of the bounding rectangle. The objects can be oriented in any direction. The rotation of the objects is unknown (otherwise it would be trivial to rotate the rectangle to match) and can't be determined.

So I've got 4 corners of a rectangle. I can create a rectangle by specifying a center point (derived from the corners) and providing an x and y length. Now this rectangle is the correct size and in the correct location, however it is not oriented correctly (and thus some of the corners defined are not included in the rectangle because of the incorrect rotation).

I need to figure out how to rotate the rectangle around its center point, so the 4 corners of MY rectangle match the 4 corners I was given.

Hopefully that makes more sense. If not, let me know and I'll try again. :)
I don't understand where the problem is. You say you've already got four corners that you are trying to match; why don't you just use those four corners to build your rectangles?
Unfortunately, I'm constrained by systems outside of my control and I can't just generate a rectangle from the four corners. I can only create a rectangle aligned to the x and y axis, and then rotate it from there to suit my needs.

Here's pseudo-code of my problem:

void CreateRectangle(Point centerPosition, float xLength, float yLength);void RotateRectangle(float angle);{   Point centerPos;   float xLength;   float yLength;   float rotationAngle;   Point corner1, corner2, corner3, corner4;   //get corners and calculate center position   .....   //now create the rectangle   CreateRectangle(centerPos, xLength, yLength);   //calculate rotationAngle from corners   ??????     RotateRectangle(rotationAngle); }


I need to calculate the rotationAngle from the four corners. I'm sure there is an extremely trivial solution, but my sleep-deprived brain couldn't come up with it after an hour's of thought. I thought I'd ask here, cause I figured it was a very simple solution and someone would be able to point it out to me right away. Unfortunately, it seems I can't even explain things coherently at this point.

Assuming I understand you correctly:

1) Construct a vector from the origin of the unrotated rectangle to one of its corner vertices.
2) Construct a vector from the origin of the rotated rectangle to its equivalent corner vertex.
3) Use the scalar product to find the angle between both vectors.
Quote:Original post by dmatter
Assuming I understand you correctly:

1) Construct a vector from the origin of the unrotated rectangle to one of its corner vertices.
2) Construct a vector from the origin of the rotated rectangle to its equivalent corner vertex.
3) Use the scalar product to find the angle between both vectors.
I'm not sure if that's what he's looking for. By 'equivalent' corner vertex, do you mean the opposite corner? And won't that always just return an angle of 180 degrees?

@The OP: Assuming the four input points form a valid rectangle (which seems unlikely if they're plotted arbitrarily by the user, but maybe I'm not understanding that part of it), try the following:
// Assumes that 'c1' lies in the negative quadrant,// and that the corners are arranged in CCW order:vector2 x = c2 - c1;vector2 y = c4 - c1;float x_extent = x.length() / 2;float y_extent = y.length() / 2;float angle = atan2(x.y, x.x);
Quote:Original post by jyk
I'm not sure if that's what he's looking for.
Yeah it might not be, my interpretation was that he was two near-similar rectangles, one is rotated by some unknown angle and the other has yet been rotated but it needs to be in order to "overlay" it onto the rotated one. The crux of the problem being to calculate the unknown angle of the rotated rectangle - A quick inspection seems to indicate that you're code does something similar, only more simply.
Quote:By 'equivalent' corner vertex, do you mean the opposite corner?
Say he chose the 'first' vertex of the rectangle for step 1, then by equivalent I mean that in step 2 he would choose the 'first' vertex of the second rectangle too. Likewise if he had chosen the second, third or forth vertices instead.

This topic is closed to new replies.

Advertisement