## Plane equation

Sage
Posts: 1,403
Joined: 2005.07
Post: #1
Has anyone got a good function to draw a plane given its equation
ax+by+cz+d = 0
right now Im looping across
x and y then calculating z to draw lines, but this draws the plane all skewed and stuff.

Sir, e^iÏ€ + 1 = 0, hence God exists; reply!
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
(a, b, c) is the normal, and d is the distance from the origin along that norrmal. That should be plenty to figure out where to put a quad...
Sage
Posts: 1,403
Joined: 2005.07
Post: #3
It is I just dont know how,
Id have to find two perpendicular vectors to that normal and that means they could be at any orientation as long as they lie on the plane.

Sir, e^iÏ€ + 1 = 0, hence God exists; reply!
Luminary
Posts: 5,143
Joined: 2002.04
Post: #4
so, pick a vector v that's not (nearly) parallel or antiparallel to the norml.

Your two vectors are p = normal x v and q = normal x p.
Sage
Posts: 1,403
Joined: 2005.07
Post: #5
what does not (nearly) parallel mean?
not (nearly parallel)
or
(not nearly) parallel

Sir, e^iÏ€ + 1 = 0, hence God exists; reply!
Luminary
Posts: 5,143
Joined: 2002.04
Post: #6
not parallel, or nearly parallel, or antiparallel, or nearly antiparallel.

basically, try a fixed vector eg. (1, 0, 0), if the dot product is too close to 1 or -1, use a different fixed vector eg. (0, 1, 0) instead.
Moderator
Posts: 1,140
Joined: 2005.07
Post: #7
Take 2 points on the plane (try with x and y as 0, and y and z as 0, for example), and take the difference from them. There you have 1 vector. Take the cross product between that vector and the normal. There you have 2 vectors.
Sage
Posts: 1,199
Joined: 2004.10
Post: #8
My approach is similar to akb825. I'd take the cross of the normal with the x-axis to get the plane's y, and then the cross of those two to get the plane's x.

Code:
```vec3 x, y; y.cross( vec3( 1,0,0 ), planeNormal ); y.normalize();          x.cross( y, planeNormal ); x.normalize();```

Then, if you want to draw the plane as a grid, I'd create a matrix using those three axes, glLoadMatrix it, and then draw the plane. Easy enough.
Sage
Posts: 1,403
Joined: 2005.07
Post: #9
know its a 4x4 matrix, but where does x, y and z from the calculation go?

Sir, e^iÏ€ + 1 = 0, hence God exists; reply!
Member
Posts: 131
Joined: 2004.10
Post: #10
Finding the up and right vectors based on a normal. First start with the right vector as it will always be on the XY plane. I.E. z component is 0 (I'm talking cartesian space.)

Code:
```// Normal, up, and right vectors Point n, u, r; nx = plane.a; ny = plane.b; nz = plane.c; //abc not necessarily unit in a sloppy system. Normalize(&n);```

The normal on the XY plane is just the normal above with the nz set to zero.

Code:
```//Normal on plane: Point nop; nopx = nx; nopy = ny; nopz = 0; // make it unit length. Normalize(&nop);```

Now just find the perpendicular to the NOP to find the right vector.

Code:
```r.x = nop.y; r.y = -nop.x; r.z = 0;```

Now find the up vector on the plane.

Code:
`CrossProduct(&n, &r, &u);`

Voila you should now have two unit vectors at right angles to themselves in the plane's orientation. Given a point on the plane all you need to do is scale the up and right vectors to get points on the plane.

Note 1: You'll need to specially handle the right vector calculation when the normal is [0, 0, 1] because NOP will be [0,0,0] which has no perpendicular.

Note 2: You'll have to be careful on the right vector calculation. Above is one way but it may be the wrong way. I.E. x = -y and y = x instead.

Note 3: You'll have ot careful on the cross product. A cross B gives a different vector than B cross A. Basically it points the other direction.

Note 4: You may need to adjust the normal so that it points in a predictable direction if you find yourself getting flustered. I.E. Always pointing up if mainly upward or east if easterly or south if northerly.
Member
Posts: 131
Joined: 2004.10
Post: #11
You got to be careful here when plane normal is in the same direction as (1,0,0) but that's just nitpicking.

TomorrowPlusX Wrote:My approach is similar to akb825. I'd take the cross of the normal with the x-axis to get the plane's y, and then the cross of those two to get the plane's x.

Code:
```vec3 x, y; y.cross( vec3( 1,0,0 ), planeNormal ); y.normalize();          x.cross( y, planeNormal ); x.normalize();```

Then, if you want to draw the plane as a grid, I'd create a matrix using those three axes, glLoadMatrix it, and then draw the plane. Easy enough.
Member
Posts: 131
Joined: 2004.10
Post: #12
unknown Wrote:know its a 4x4 matrix, but where does x, y and z from the calculation go?
Code:
```(rx, ry, rz, 0 ux, uy, uz, 0 nx, ny, nx, 0 0,  0,  0,  1)```
Or the transpose. It always trips me up. r, u and n have to be unit though or else your adding scaling to the mess.
Sage
Posts: 1,403
Joined: 2005.07
Post: #13
Ok thanks a lot.
TommorowPlusX: Where do you get the vec3 stuff or did you write it?

Sir, e^iÏ€ + 1 = 0, hence God exists; reply!
Moderator
Posts: 1,140
Joined: 2005.07
Post: #14
Just for clarification, Zekaric's matrix is in column major form, which is how OpenGL processes it. If you want it in row major form (how you usually write it), just take the transpose. Also note that you will also need to translate for the d after that.
Sage
Posts: 1,199
Joined: 2004.10
Post: #15
unknown Wrote:Ok thanks a lot.
TommorowPlusX: Where do you get the vec3 stuff or did you write it?

It's adapted from sample code from this guy: http://www.humus.ca/index.php?page=3D

I added a lot, such as a plane class ( relevant here ), AABB class, and integer variants, and various other tidbits like helper functions.