Decrius 100 Report post Posted August 14, 2008 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! 0 Share this post Link to post Share on other sites
Mike nl 390 Report post Posted August 14, 2008 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). 0 Share this post Link to post Share on other sites
Decrius 100 Report post Posted August 14, 2008 Ah thanks! 0 Share this post Link to post Share on other sites
Decrius 100 Report post Posted August 15, 2008 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 =/ 0 Share this post Link to post Share on other sites
Mike nl 390 Report post Posted August 15, 2008 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 * xB.y = A.y * xB.z = A.z * x 0 Share this post Link to post Share on other sites
Decrius 100 Report post Posted August 16, 2008 Thanks! It works xD 0 Share this post Link to post Share on other sites
Decrius 100 Report post Posted August 16, 2008 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! 0 Share this post Link to post Share on other sites
apatriarca 2365 Report post Posted August 19, 2008 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)*CAs 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] 0 Share this post Link to post Share on other sites