user1240679
user1240679

Reputation: 6979

Getting all vertices of a rectangle

In my program, I had a requirement to plot a rectangle that that is prependicular to a line coming from the centre.

To orient the rectangle in this way in 3D space, I used the gluLookAt giving it the lookAt point and plotted the rectangular figure. This worked correctly for me.

To draw the rectangle (in my framework, that uses openGL at the back), I now use a rectangle class and have extended it with a 3D Node (where node is something that has a lookAt point). Given width, height and the top vertex, the rectangle is drawn (node is at the top left vertex and orients the rectangle using lookAt).

Node also has a getPosition() function that gives me its 3D position (top left in rectangle - say 300,400,20). I am trying to get the position of the other three vertices in 3D space to use for my operation. Since the rectangle is oriented in 3D space, other three vertices can't just be fetched by addition of width and height. With the rectangle oriented in 3D, how do I get the position of the other three vertices?

Upvotes: 1

Views: 2160

Answers (2)

phaazon
phaazon

Reputation: 2002

You can retreive the position of the 3 other points using the normal of the rectangle. In order to orient a rectangle in space, you need 2 information:

  • its position, commonly represented as a 3 or 4 components vector, or a 4x4 matrix ;
  • its orientation, commonly represented as a quaternion.

If you have the orientation represented with a normal, and only have one point, you just can’t deduce the other points (because you need another information to solve the rotation equation around the normal). I think the best idea is to use quaternion to orient things in space (you can still retreive the normal from it), but you can also use a normal + one vector from the rectangle. You said you only have one point, and a tuple (width,height), so the common method based on the × operation won’t make it through.

I suggest you to:

  • make your Node class a class that correctly handles orientation; lookAt isn’t designed for that job ;
  • combine the translation matrix (position) with a cast matrix from the quaternion (orientation) to correctly handle both position and orientation ;
  • use that matrix to extract a rotated vector you’ll used like rotated × normal to get the 3 points.

Upvotes: 0

Aki Suihkonen
Aki Suihkonen

Reputation: 20017

The minimum amount of coordinates is just slightly less than 9: that's three vertices of a generic rectangle in 3d-space (Ax,Ay,Az, Bx,By,Bz, Cx,Cy,Cz).

The last one is e.g. D=A+(B-A)+(C-A)=B+C-A.

Why it's slightly less, is that any triplet of A,B,C coordinates do not necessarily form a 90 degree angle -- but it really doesn't make much sense to pursuit for the simplistic possible arrangement and be prepared to calculate cross-products or normalize vectors.

 A----B
 |    |
 C---(D)

EDIT: Vector arithmetic primary:

To Add / Subtract vectors, one sums the elements. A=B+C means (ax = bx+cx; ay=by+cy; az=bz+cz).

Dot product in (any dimension) is the sum of product of terms: dot(A,B) = ax*bx + ay*by + az*bz; // for 2,3,4, any number of elements/dimensions.

Cross product is a special operator that is well defined at least in 2 and 3 dimensions. One of the geometric interpretations of cross product is that it produces a vector that is perpendicular to both vectors of it's parameters.

If A is a vector (ax,ay,az), it also means a direction vector from origin O=(0,0,0) i.e. A = A-O = (ax-0,ay-0,az-0); Likewise (B-A) is a [direction] vector from A to B (sometimes written as AB (with an arrow --> on top))

One can 'add' these directed vectors e.g. as:

    o----->
           \
            \
      <------o
     /
    /
   x

And so, if one adds the vector A+(B-A) + (C-A), one ends to the point D.

Upvotes: 1

Related Questions