# Finding the height of a point within a quad

This topic is 4972 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi. Could anyone tell me how to go about getting the height of a point in a quad? I would like to be able to supply four vertices and the x/z coordinates of the point I want to find, and get the y value back. Any advice on what to do or some code would be great. Cheers.

##### Share on other sites

 p3-----p2  |     |  |_____|   p0   p1

p = point to find where x0, z0 are known and y is what we're solving for.

quad defined by p0, p1, p2, p3 counter clockwise order.

build two triangles: t0 : (p0, p1, p2), t1 : (p0, p2, p3)

The quad doesn't have to be planar so split it up in two triangles, then create a line with origin (x0, 0, z0) and direction vector (0, 1, 0). Solve for intersection between the line and triangle t0. If intersecting, y of intersection-point will be the y you're looking for. If not, do the same with triangle t1.

That's a pretty straight-forward solution. You could use an early rejection test where projecting the quad on the xz plane and do a simple point-planar quad intersection test. Problems you may run into is if the quad is folded so that the line can cross the poly twice (both triangles intersect the line) or if the intersection actually is a line segment (line and at least one triangle is parallell and intersects).

##### Share on other sites
Realize that your problem is not well defined. What kind of surface does your quad really support ?

It can be a polygonal one. But then there are two possible tesselations. Be sure to be coherent with the rendering. Be sure that the tesselation never flicks. Better rethink the problem in terms of triangles then.

The the solution is something like that in 'triangle coordinates' :
(z in [0,1], x in [0,1] and 0<z+x<1 if the point belongs to the triangle)

C--A
| /
B

y = a*z + b*x + c*(1-z-x)

It can be a bilinear shape, but then it's not C1 continuous between quads (potential sharp edges between the quads).

Then the solution is something like that :

x and z are normalized to [0,1]x[0,1] when within the horizontal (y=0) projection of the quad.

y = a*x*z + b*(1-x)*z + c*x*(1-z) + d*(1-x)*(1-z)

It can be a bicubic shape.

Cheers guys.