Jump to content
• Advertisement

# Random point on a surface

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

If you intended to correct an error in the post then please contact us.

## Recommended Posts

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 this post

##### Share on other sites
Advertisement
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).

Ah thanks!

#### Share this post

##### Share on other sites
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 this post

##### Share on other sites
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 this post

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

#### Share this post

##### Share on other sites
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 this post

##### Share on other sites
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]

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
2. 2
Rutin
21
3. 3
JoeJ
18
4. 4
5. 5
• Advertisement

• 14
• 40
• 23
• 13
• 13
• ### Forum Statistics

• Total Topics
631722
• Total Posts
3001892
×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!