# Random point on a surface

## Recommended Posts

Decrius    100
I have a quad (2 triangles) consisting of 4 (shared) vertexes which differ all 4 in x, y and z. On that quad I want to get a random position which is exactly ON the surface. The quad does not necessarily have to be a square, it can be anything with 4 vertexes. So I'll split the quad in two triangles...but then what? How can I ever get a random position on a non-square surface? I thought of calculating the surface area, then taking a random value between zero and the surface area value...but how would I translate that into a XYZ value? Thanks!

##### Share on other sites
Mike nl    390
Use Barycentric coordinates.

To get a random point on a triangle ABC, generate the random values x (range [0,1]) and y (range [0,1-x]). Your random point is then A*x + B*y + C*(1-x-y).

Decrius    100
Ah thanks!

##### Share on other sites
Decrius    100
One question though...

AB and C are vertexes, what do you mean when you say A * x? Most pages seem to explain the other way around, where you have a point and check if it's on the triangle...I haven't done much English written mathematics yet =/

##### Share on other sites
Mike nl    390
A * x is a vector-scalar multiplication. A is a vector (multiple numbers, three in this case), x is a scalar (a single number).

The result is the component-wise multiplication of every number in A with x, i.e, B = A * x means that:
B.x = A.x * x
B.y = A.y * x
B.z = A.z * x

##### Share on other sites
Decrius    100
Thanks! It works xD

##### Share on other sites
Decrius    100
One thing though. I've been thinking about this in a 2D presentation to get a random point...

Imagine a triangle (2D) with one corner 90 degree, the other two 45 degree. Now to get a random position, you take a random Y value. Lets say the height and width of the triangle is 1 by 1. Y should be between 0 and 1...lets say it's 0.8. Now if you want to get the X value, you need to take the Y value into your calculations. Since it's not a square, the X value can now only be from 0 to 0.2! Since where 0.8Y is on the diagonal part, X is 0.2 and is then our maximal value.

This means the position of the point is never random. It has a random chance in height, but a much higher change to be closer to zero on the X axis.

Since I saw the Y value in your calculations used range(0, 1 - x), and I noticed that it's not 100% random, I thought the same might apply to your formula...

As solution I thought of this: every triangle can, fit twice in some sort of square (a rhomb, often). So if I take such a square, calculate a random point on the square, then whenever it's not on the triangle, I simply swap X and Y from direction (x = max_x - x)...would this work do you think?

Thanks!

##### Share on other sites
apatriarca    2365
In an old course at Siggraph (State of the Art in Monte Carlo Global Illumination, Siggraph 2004 Course, pag. 93) there is the following algorithm to sample a random point on a triangle with a uniform distribution:

s = sqrt(ξ1)
t = ξ2
P = (1 - s)*A + s*(1 - t)*B + s*t*C

ξ1 and ξ2 are random points in [0,1] and A, B, C are the vertices of the triangle.

EDIT:
You can also use the following formula:

u = 1 - sqrt(ξ1)
v = ξ2 * sqrt(ξ1)
P = u*A + v*B + (1 - u - v)*C

As above A, B and C are the vertices of the triangle and ξ1, ξ2 are random numbers in [0,1]. u and v are the barycentric coordinates.

[Edited by - apatriarca on August 19, 2008 1:06:47 PM]