problem with distance from plane algorithm

Hi in my program the following code calculates the distance from a point to a plane:

 	m_a = m_coord[0].y*(m_coord[1].z-m_coord[2].z) + m_coord[1].y*(m_coord[2].z-m_coord[0].z) + m_coord[2].y*(m_coord[0].z-m_coord[1].z);
	m_b = m_coord[0].z*(m_coord[1].x-m_coord[2].x) + m_coord[1].z*(m_coord[2].x-m_coord[0].x) + m_coord[2].z*(m_coord[0].x-m_coord[1].x);
	m_c = m_coord[0].x*(m_coord[1].y-m_coord[2].y) + m_coord[1].x*(m_coord[2].y-m_coord[0].y) + m_coord[2].x*(m_coord[1].y-m_coord[1].y);
	m_d = (m_coord[0].x * (m_coord[1].y * m_coord[2].z - m_coord[2].y * m_coord[1].z))
		+ (m_coord[1].x * (m_coord[2].y * m_coord[0].z - m_coord[0].y * m_coord[2].z))
		+ (m_coord[2].x * (m_coord[0].y * m_coord[1].z - m_coord[1].y * m_coord[0].z));

	m_d *= -1.0f;
	float distance=  ((m_a * point.x + m_b * point.y + m_c * point.z + m_d) / (float)(sqrtf(m_a * m_a + m_b * m_b + m_c * m_c)));
	if(distance< 0.0f)
		distance*= -1.0f;

It works fine when the plane lies on the z plane, ie when all the z coordinates are the same but when the plane is orientated any other way the value of distance is wrong.

Any help?

Pffft. If I could read that code I might be able to help…

Are you trying to get the distance from an infinite plane or from a finite surface?

The infinite plane distance is simple:
construct a transformation matrix to map a point to the plane’s coordinate system, transform the point, inspect the Y coordinate - or whichever coordinate you have defined as “up”. That’s the distance.
Whether you see a negative coordinate as a positive distance or as 0 depends on what you want to do with it.
A somewhat more optimised method would be to subtract a base point (which is on the plane) from the point, then apply the inverse rotation matrix. That’s an optimisation because you don’t actually have to apply the whole matrix - only the Y unit vector (or whatever coordinate is “up”).

The finite plane would require you to determine if it’s on the outside or on the inside of the surface. On the inside, it would be the same as the infinite plane. On the outside, you’d have to find the nearest edge, then the nearest point on that edge, etc.,etc.

Whoops. Slight correction:
it’s not one of the unit vectors, it’s only the Y or Z coordinates of all three unit vectors.

If you have your planes in point-normal form (or you can put them in that form), then what you can do is the following

Calculate a vector from the point on the plane to your free point.

Take the dot product this vector and the normal of the plane.

If your plane’s normal has unit length (it’s length is 1), then this dot product is your answer, otherwise it’s in terms of the length of the plane’s normal…