# How can I make a quad based on a plane?

## Recommended Posts

Nameles    122
If I have a plane (a, b, c, d) how can I make a unit quad from it? I know, it’s probably very simple but some help would be wonderful :) From my limited math knowledge I think I’ll have to do two cross products to get the two axes that are in the plane. Then the vertices would be something like:
( axis_1 + axis_2, axis_1 – axis_2,  -axis_1 + axis_2, -axis_1 – axis_2 )

Then there is the distance. Maybe:
dist_vec = plane.normal * plane.d
quad( axis_1 + axis_2 + dist_vec, axis_1 – axis_2 + dist_vec,  -axis_1 + axis_2 + dist_vec, -axis_1 – axis_2 + dist_vec)

Is this even close?

#### Share this post

##### Share on other sites
Cocalus    554
Looks like your on the right track. I'm going to assume that your planes normal is normalized. Also I'm doing this off the top of my head But it should be correct

Pick some unit vector that's not parallel to your planes normal (Which vector you choose will change the quads rotation). Then we find the axes with 2 cross products. So axis1 = crossp(plane.normal,someunitvector) and axis2=crossp(plane.normal,axis1). Now to find the vertices of the quad we'll start with a point on the plane, then add our axes to it to get the other vertices so

vertex1 = plane.normal * plane.d;
vertex2 = vertex1 + axis1;
vertex3 = vertex1 + axis2;
vertex4 = vertex2 + axis2; //you may need to renumber these

You can shift the quad by adding the axes. for instance to center the quad over the first point add (-0.5*axis1)+(-0.5*axis2) to vertex1 before calculating vertex2.

#### Share this post

##### Share on other sites
Nameles    122
Sweet, that worked great.

I chose to use either 0,1,0 or 0,0,1 as the non parallel vector.
I just check to see if 0,0,1 is equal to the plane normal and if it is I use 0,1,0 instead.

Is there a better way to pick the starting vector?

I'm thinking I could generalize this to "quad from point/normal" both of which I can get from a plane. Then I can use that shift to place the quad anywhere.

#### Share this post

##### Share on other sites
jyk    2094
Quote:
 Is there a better way to pick the starting vector?
A method that works well is to use the world axis corresponding to the component of the reference vector with the smallest absolute value. So if the vector were (-5, 4, 1), you'd cross it with (0,0,1); if it were (-9, -4, 10), you'd cross it with (0,1,0); and so on. Obviously your reference vector is normalized so the values won't be integers like that, but you get the idea.

#### Share this post

##### Share on other sites
Cocalus    554
Quote:
 Original post by NamelesIs there a better way to pick the starting vector?

Depends on what your doing, if you can be certain a certain vector wont be used then you could just use that one. You could also use what jyk suggested.But note with your current method, you need to be careful of a plane normal being nearly equal to 0,0,1 (rounding errors and all). So you'll either need to use an epsilon based check. Or use the dot product like.

if ( dot(plane.normal, vector3(0.0,0.0,1.0)) > 0.99) //I randomly picked .99 as a number close to but smaller than 1    someunitvector = vector3(0.0,1.0,0.0);else    someunitvector = vector3(0.0,0.0,1.0);

#### Share this post

##### Share on other sites
Nameles    122
With the method jyk suguest, what do you do when you have something like (0,0,1).
Just arbitrarily pick between (1,0,0) and (0,1,0)?

#### Share this post

##### Share on other sites
Cocalus    554
Pretty much. If the quad is centered then both will produce the same vertices (different order though).

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account

## Sign in

Already have an account? Sign in here.

Sign In Now