5
$\begingroup$

In an equilateral triangle that contains a point, how do you calculate 3 weights that sum to 100% and indicate how much influence each vertex has on the point.

When the point is in the center all the weights are 33%:

Example 1

And if it's on one edge they should be split between the vertices that share that edge:

Example 2

This is similar to how an HSL color wheel works:

HSL Color wheel

$\endgroup$
1

3 Answers 3

2
$\begingroup$

I ended up using another method to solve this. In the diagram below to calculate the weight for a point, find the distance from the control point to the line opposite it and then divide by the triangle's height.

For the weight of A:

weightOfA = lengthOfx / triangleHeight

enter image description here

$\endgroup$
1
$\begingroup$

Let's think of three different points of the plane, $P$, $Q$ and $R$, not all co-linear, each with some coordinate vector in $\mathbb{R}^2$. If we only deal with two at first, $P$ and $Q$, we can write a one parameter interpolation as: $$ X = P(1-t)+Qt, $$ for $t\in[0,1]$. If you like, $100t$ gives you the percentage of $Q$'s weight, and $100(1-t)$ is the percentage of $P$'s weight. Also, if you pick any point $X\in\mathbb{R}^2$ which lies in the segment between $P$ and $Q$, there is only one value of $t$ such that $X = P(1-t)+Qt$, because the equation is linear.

Now, for the third point $R$. Since $P(1-t)+Qt$ already describes all the points in the $PQ$ segment, we interpolate this expression again with the point $R$, obtaining $$ X=[P(1-t)+Qt](1-s)+Rs. $$ for $s\in[0,1]$. Expanding, we get $$ X=P(1-t)(1-s)+Qt(1-s)+Rs. $$ It looks worse than before, but it is the same trick. Any point contained in the triangle $PQR$ can be uniquely identified with two values $t\in[0,1]$ and $s\in[0,1]$. And again, $100(1-t)(1-s)$ is the percentage weight of $P$, $100t(1-s)$ that of $Q$, and $100s$ that of $R$.

I hope this helps!

$\endgroup$
2
  • 1
    $\begingroup$ Not following here unfortunately. Did you rename A,B,C in the diagrams to P,Q,R? What is X? Where do the coordinates of the point fit into the equation? $\endgroup$
    – DShook
    Mar 6, 2019 at 3:23
  • $\begingroup$ Indeed,$P$, $Q$ and $R$ are your $A$, $B$ and $C$. $X$ is the point in the triangle which you want to express as a combination of the vertex points. The coordinates of the points come into it because all the equations I wrote are vector equations. Writing $X=P(1-t)+Qt$ is shorthand for $X_1=P_1(1-t)+Q_1 t$ and $X_2=P_2(1-t)+Q_2 t$, where $X$ is the vector with coordinates $(X_1, X_2)$, and likewise for $P$, $Q$ and $R$. $\endgroup$
    – R_B
    Mar 6, 2019 at 3:29
0
$\begingroup$

The mapping between Cartesian coordinates $(x, y)$ and Barycentric coordinates $(u, v, w)$ corresponding to a triangle with vertices $(x_0, y_0)$, $(x_1, y_1)$, and $(x_2, y_2)$ is trivial.

By definition, $u + v + w = 1$, and they directly correspond to the weights of their corresponding vertices. Using the formulae shown in this answer,

  • $u$ corresponds to the weight of vertex $(x_1, y_1)$,

  • $v$ corresponds to the weight of vertex $(x_2, y_2)$, and

  • $w = 1 - u - v$ corresponds to the weight of vertex $(x_0, y_0)$.

Because the mapping is linear, this is also linear interpolation between the three vertices. It can be generalized to any simplex in $K$ dimensions, using $K+1$ barycentric coordinates. In 1D, the simplex is a line segment; in 2D, a triangle; in 3D, a tetrahedron; and so on.


From Barycentric to Cartesian coordinates we have $$\left\lbrace \begin{aligned} x &= u x_1 + v x_2 + w x_0 \\ y &= u y_1 + v y_2 + w y_0 \\ \end{aligned} \right .$$ by definition; applying $w = 1 - u - v$ we get $$\left\lbrace \begin{aligned} x &= x_0 + u (x_1 - x_0) + v (x_2 - x_0) \\ y &= y_0 + u (y_1 - y_0) + v (y_2 - y_0) \\ \end{aligned} \right .$$ In practice, we often use first vertex and the two edge vectors from it instead, i.e. $$\left\lbrace \begin{aligned} x_u &= x_1 - x_0 \\ y_u &= y_1 - y_0 \\ \end{aligned} \right . , \quad \left\lbrace \begin{aligned} x_v &= x_2 - x_0 \\ y_v &= y_2 - y_0 \\ \end{aligned} \right . \tag{1a}\label{1a}$$ so that the $u$ axis is along the line between vertices $0$ and $1$, $v$ axis along the line between vertices $0$ and $2$, and the $w$ axis between vertices $1$ and $2$. Then, $$\left\lbrace \begin{aligned} x &= x_0 + u x_u + v x_v \\ y &= y_0 + u y_u + v y_v \\ \end{aligned} \right . \tag{1b}\label{1b}$$


The inverse is $$\left\lbrace \begin{aligned} u &= \frac{(x - x_0) y_v - (y - y_0) x_v}{x_u y_v - x_v y_u} \\ v &= \frac{(y - y_0) x_u - (x - x_0) y_u}{x_u y_v - x_v y_u} \\ \end{aligned} \right.$$ where the divisor, $x_u y_v - x_v y_u$, is twice the area of the triangle, so if the triangle is degenerate (a line or a point), the divisor is zero, and there is no solution.

If you have many points to map to a given triangle, you can save significant computing effort by calculating $$\left\lbrace \begin{aligned} u_0 &= \frac{y_0 x_v - x_0 y_v}{x_u y_v - x_v y_u} \\ u_x &= \frac{y_v}{x_u y_v - x_v y_u} \\ u_y &= \frac{-x_v}{x_u y_v - x_v y_u} \\ \end{aligned} \right., \quad \left\lbrace \begin{aligned} v_0 &= \frac{x_0 y_u - y_0 x_v}{x_u y_v - x_v y_u} \\ v_x &= \frac{-y_u}{x_u y_v - x_v y_u} \\ v_y &= \frac{x_u}{x_u y_v - x_v y_u} \\ \end{aligned} \right. \tag{2a}\label{2a}$$ because then we have $$\left\lbrace \begin{aligned} u &= u_0 + x u_x + y u_y \\ v &= v_0 + x v_x + y v_y \\ \end{aligned} \right. \tag{2b}\label{2b}$$ and we have a very nice symmetry, too, between the two mapping operations.


In object-oriented programming languages like Python or C++, create a class to represent a triangle. (In C, consider using a structure with room for the twelve precalculated parameters.) Whenever the Cartesian coordinates for the triangle are defined or modified, recalculate or update $x_0$, $y_0$, $x_u$, $y_u$, $x_v$, $y_v$, $u_0$, $u_x$, $u_y$, $v_0$, $v_x$, and $v_y$, and the class can map between Cartesian and Barycentric coordinates with just four multiplications and four additions per operation: extremely lightweight and efficient!

In $K$ dimensions, each mapping operation involves $K^2$ multiplications and additions, but is still quite lightweight and efficient.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.