0

I am trying to solve a problem where we basically need the points inside a triangle . I found a formula ( not sure if this is barycentric coordinate one or not):

$$(x_{1},x_{2})=(1-\sqrt{r_{1}})A+\sqrt{r_{1}}r_{2}B+\sqrt{r_{1}}(1-r_{2})C$$

Here $r_{1}$ and $r_{2}$ are parameters that determine how the point $(x_{1},x_{2})$ is interpolated between the vertices $A,B, and C$. Also, I think $r_{1}$ and $r_{2}$ are standard uniformly distributed. So, My question is there any other relations between them ($r_{1},r_{2}$)?

I have used Barycentric coordinates to solve the same problem where we have $u,v,w$ which are standard uniformly distributed weights and $u+v+w=1$. ( I actually want to know if $r_{1},r_{2}$ have this kind of a relationship between them) and $$(x_{1},x_{2})=u\cdot A+v\cdot B+w\cdot C$$

Rust32627
  • 155
  • 2
    This parameterization with $\sqrt{r_k}$ doesn't ring anything for me . When you say "I found a formula"... could you explain its origin ? – Jean Marie Jan 03 '24 at 18:56
  • I was using chatgpt. The second formula I knew from from highschool. – Rust32627 Jan 04 '24 at 06:15
  • The $\sqrt{r_1}$ is an odd choice; I don't see any benefit over just using $r_1$. The only condition required is $0\leq r_1, r_2\leq 1$. The first form does have a benefit that you don't need to worry about any relation between $r_1$ and $r_2$; you can choose them independently. If you compare the two forms, you can verify that the coefficients in the first form always sum to 1, regardless of choice of $r_1$ and $r_2$. – Tony Mathew Jan 04 '24 at 08:26
  • @Tony Mathew see my answer. – Jean Marie Jan 04 '24 at 11:00

2 Answers2

2

Let me express an answer in two parts :

First part : with your first expression, letting $r_1$ and $r_2$ taking any value (with $r_1 \ge 0$), you get only a half-plane. Here is why.

Let us re-write your barycentric coordinates expression under the clearer form :

$$M=\underbrace{(1-u)}_a A+\underbrace{uv}_b B+\underbrace{u(1-v)}_cC\tag{1}$$

where $u:=\sqrt{r_1}, v=r_2$.

It is indeed barycentric coordinates because $a+b+c=1$.

(1) can be expressed as a "two-levels barycentration" :

$$M=(1-u)A+u\underbrace{(vB+(1-v)C)}_{D_v}=(1-u)A+uD_v \tag{2}$$

$D_v$ can be anywhere on line $BC$,

The second step is a barycentration of $A$ and $D_v$ which can be written

$$(1-u)A+uD_v=A+u(D_v-A)=A+u \vec{AD_v}\tag{3}$$

As $u \ge 0$, expression (3) generates only points situated on the half-line $AD_v$ in the direction of $D_v$.

Conclusion : with description (1), one gets only the points situated in the open half plane containing $BC$ delimited by the parallel to $BC$ passing through $A$.

Second part : in order to answer your initial question, take expression (2), with

$$0 \le u,v \le 1$$

(see the comment by @Tony Mathew) to get all points inside the triangle (and only them) with the geometric interest that you understand what you do with this two levels barycentric interpretation. See for example this answer.

Jean Marie
  • 88,997
  • 1
    2nd conclusion : ChatGPT isn't a good adviser... – Jean Marie Jan 04 '24 at 11:01
  • ... but has had the merit to remind us this two parameter interpretation. – Jean Marie Jan 04 '24 at 11:31
  • Take a look at my answer why ChatGPT was actually doing good work here. I assume the answer was somewhere in it's data already and not "composited" by itself. – Ingix Jan 04 '24 at 14:24
  • Indeed, I was totally wrong : the proposal by ChatGPT was very thorough (see a reference to a paper by Osada in a comment I made to the answer of @Ingix) – Jean Marie Jan 05 '24 at 09:21
2

Whichever form is more useful to you depends on what exactly you need (just points inside the triangle or equally densely distributed points). Also take a look at https://blogs.sas.com/content/iml/2020/10/19/random-points-in-triangle.html, which uses another approach that is also plausible.

Compare the following images below, where I randomly chose 10,000 points inside an equilateral triangle two ways. The top image uses "naive" barycentric coordinates (using $(x_1,x_2)=u⋅A+v⋅B+w⋅C$), the bottom image uses the weights provided by ChatGPT (using $(x_1,x_2)=(1−\sqrt{r_1})A+\sqrt{r_1}r_2B+\sqrt{r_1}(1-r_2)C$).

As you can see, both point sets "fill" the triangle, but the weighted approach (seems to) do it using a distribution that is uniform over the area, while the naive approach does not (see below image for further discussion).

naive weigths vs. square root weights

As can be seen, the naive approach is denser in the center than in the corners. That's because values near the corners can only be obtained when one of $u,v,w$ is very near to $1$, necessitating both other values to be very small. That happens rarely. Points near the triangles center however, mean all 3 values need to be roughly the same, so near $\frac13$.

Since $u,v,w$ cannot be directly obtained using a random uniform distribution for all 3 (the sum would not be $1$), they need to be scaled. Considering that, it makes sense that (before scaling) 3 uniform random variables in $[0,1)$ are more likely to be "roughtly equal" than "one of them is much larger than both others".

Coming back to why square roots are used, which one commenter found unfamiliar. That can be explained in another scenario, that I recently had to implement: Choose a point randomly from a disc (circle with interior).

If the circle's radius is $R$, with center at $(x_M, y_M)$, then the "naive" approach is to choose a factor $u$ uniform random in $[0,1]$ and an angle $\theta$ uniform random in $[0,2\pi]$. Then the point $$(x_M + uR\cos(\theta), y_M + uR\sin(\theta))$$ seems to be a good choice for a random point inside the disc.

However, it's easy to see that those points are not evenly distributed on the disc! That's because with the above algorithm, you will get values of $r=uR$ between $0$ and $\frac{R}2$ roughly half of the time. But they all lie inside the circle around the center with radius $\frac{R}2$, which covers just a quarter of the original circle! As in the triangle case, the points or more dense in the center of the circle than near the circle itself.

In this example, the "fix" is easy: You want $\frac12$ of the points to lie in a circle of half the area of the original circle, which has radius $\frac1{\sqrt2}R$, and $\frac1{\sqrt2} = \sqrt{\frac12}$. So what you need to do is not to use the $uR$ as as your radius, but $\sqrt{u}R$. That way the radius gets "streched" with respect to $uR$, making it give you points that are equally dense in every part of the circle.

This can motivate the use of square roots in the weighted formula. It's not a mathematical proof, but personally I would use the forumula aftera few more tests with other shapes (which I did, and they all were satisfactory for me).

Ingix
  • 15,665
  • [+1] Wow ! Very very interesting. Very didactic besides. I will attempt to establish rigorously the uniformity of the density, – Jean Marie Jan 04 '24 at 14:31
  • 2
    I just found a reference : look at this answer refering to a paper by Osada et al. (see in this reference formula (1) page 814). – Jean Marie Jan 04 '24 at 22:43