<p>This is achieved via <strong><a href="https://en.wikipedia.org/wiki/Barycentric_coordinate_system" rel="noreferrer">Barycentric</a> Interpolation</strong>.</p>

<p>First, we find the barycentric coordinates of $P$. Barycentric coordinates represent how much weight each vertex contributes to the point, and can be used to interpolate any value which is known at the vertices across the face of a triangle.</p>

<p>Consider the 3 inner triangles $ABP$, $PBC$ and $PCA$.</p>

<p><a href="https://i.sstatic.net/Ob83U.jpg" rel="noreferrer"><img src="https://i.sstatic.net/Ob83U.jpg" alt="enter image description here"></a></p>

<p>We can say that the barycentric coordinate, or weight of the vertex $A$ on the point $P$ is proportional to the ratio of the area of inner triangle $PBC$ to the area of the whole triangle $ABC$.</p>

<p>This is intuitively evident if we consider that as $P$ approaches $A$ the triangle $PBC$ grows larger and the other two become smaller. </p>

<p>Also intuitively evident should be that the sum of the barycentric coordinates of a point <strong>inside a triangle</strong> always equals $1$. So, it is enough to find only two of the coordinates to derive the 3rd.</p>

<p>The method for computing the barycentric coordinates is:</p>

<p>$$\begin{aligned}
{Bary}_A &amp;= \frac{(B_y-C_y)(P_x-C_x) + (C_x-B_x)(P_y-C_y)}{(B_y-C_y)(A_x-C_x) + (C_x-B_x)(A_y-C_y)}\\
{Bary}_B &amp;= \frac{(C_y-A_y)(P_x-C_x) + (A_x-C_x)(P_y-C_y)}{(B_y-C_y)(A_x-C_x) + (C_x-B_x)(A_y-C_y)}\\
{Bary}_C &amp;= 1 - {Bary}_A - {Bary}_B
\end{aligned}$$</p>

<p>The derivation and reasoning is explained in the <a href="https://en.wikipedia.org/wiki/Barycentric_coordinate_system" rel="noreferrer">wikipedia article</a>.</p>

<p>Once you have coordinates, you can determine the texture coordinates of $P$ by interpolating the values at the vertices using the barycentric coordinates as weights:</p>

<h2>$$P_{uv} = {Bary}_A \cdot A_{uv} + {Bary}_B \cdot B_{uv} + {Bary}_C \cdot C_{uv}$$</h2>

<p>The reasoning is also explained very nicely in <a href="https://classes.soe.ucsc.edu/cmps160/Fall10/resources/barycentricInterpolation.pdf" rel="noreferrer">this</a> presentation.</p>

<p>Also see <a href="https://gamedev.stackexchange.com/questions/23743/whats-the-most-efficient-way-to-find-barycentric-coordinates">this question</a> for efficient methods of computation.</p>

CC BY-SA 3.0
2016-01-06T07:35:25.327Z
i sthere a error in $Bary_B$? Should the first term be $(A_y-C_y)$ or am i wrong?
joojaa
CC BY-SA 3.0
2016-01-06T07:52:02.270Z
@joojaa I don't think so. It's the same in the Wikipedia article, and it seems correct from a test calculation I did.
Rotem
CC BY-SA 3.0
2016-01-06T08:11:20.477Z
ah, so it is $-(A_y-C_y)$, might be good to point out as you would pre calculate $AC_y = (A_y-C_y)$.
joojaa
CC BY-SA 3.0
2016-01-06T08:15:03.043Z
@joojaa The entire denominator and some of the terms in the nominator can be precalculated for each triangle, only few of the terms depend on $P$. I've added a link to a question dealing with methods of calculation. In this formula I thought it would be better to keep the notation simple and uniform rather than efficient.
Rotem
CC BY-SA 3.0
2016-01-05T17:45:59.417Z
2016-01-06T13:48:06.523Z
2017-04-13T12:18:54.697Z
Rotem
<p>I want to find the texture coordinates for point P. I have the vertices of the triangle and their corresponding uv coordinates. </p>

<p>The numbers in the little squares in the texture represent color values.</p>

<p>What are the steps of computing the uv coordinates of P?</p>

<p><a href="https://i.sstatic.net/cVDiT.png" rel="noreferrer"><img src="https://i.sstatic.net/cVDiT.png" alt="enter image description here"></a></p>

CC BY-SA 3.0
2016-01-05T16:59:28.540Z
There is a affine transform that will map each corner to its texture coordinate, you can use that to map P to its uv.
ratchet freak
CC BY-SA 3.0
2016-01-05T17:06:38.397Z
@ratchetfreak could you provide me a link plz ?
john john
CC BY-SA 3.0
2016-01-06T07:26:36.763Z
There is a good write up on how to do the intersection point calculation as well as barycentric cord calculation in one go [in this paper](http://www.cs.virginia.edu/~gfx/courses/2003/ImageSynthesis/papers/Acceleration/Fast%20MinimumStorage%20RayTriangle%20Intersection.pdf). This essentially amounts to transforming the triangle.
joojaa
<uv-mapping>
How to map square texture to triangle?
john john
# How to map square texture to triangle?

(Asked by: username_0 on 2016-01-05)

I want to find the texture coordinates for point P. I have the vertices of the triangle and their corresponding uv coordinates.

The numbers in the little squares in the texture represent color values.

What are the steps of computing the uv coordinates of P?

[![enter image description here](https://i.sstatic.net/cVDiT.png)](https://i.sstatic.net/cVDiT.png)

**Tags:** uv-mapping

> **Comments:**
> **username_1**: There is a affine transform that will map each corner to its texture coordinate, you can use that to map P to its uv.  
> **username_0**: @username_1 could you provide me a link plz ?  
> **username_2**: There is a good write up on how to do the intersection point calculation as well as barycentric cord calculation in one go [in this paper](http://www.cs.virginia.edu/~gfx/courses/2003/ImageSynthesis/papers/Acceleration/Fast%20MinimumStorage%20RayTriangle%20Intersection.pdf). This essentially amounts to transforming the triangle.  

---

## 1 answers

### Answer by username_3 on 2016-01-05. Score: 15 | Accepted answer

This is achieved via **[Barycentric](https://en.wikipedia.org/wiki/Barycentric_coordinate_system) Interpolation**.

First, we find the barycentric coordinates of $P$. Barycentric coordinates represent how much weight each vertex contributes to the point, and can be used to interpolate any value which is known at the vertices across the face of a triangle.

Consider the 3 inner triangles $ABP$, $PBC$ and $PCA$.

[![enter image description here](https://i.sstatic.net/Ob83U.jpg)](https://i.sstatic.net/Ob83U.jpg)

We can say that the barycentric coordinate, or weight of the vertex $A$ on the point $P$ is proportional to the ratio of the area of inner triangle $PBC$ to the area of the whole triangle $ABC$.

This is intuitively evident if we consider that as $P$ approaches $A$ the triangle $PBC$ grows larger and the other two become smaller.

Also intuitively evident should be that the sum of the barycentric coordinates of a point **inside a triangle** always equals $1$. So, it is enough to find only two of the coordinates to derive the 3rd.

The method for computing the barycentric coordinates is:

$$\begin{aligned}
{Bary}\_A &= \frac{(B\_y-C\_y)(P\_x-C\_x) + (C\_x-B\_x)(P\_y-C\_y)}{(B\_y-C\_y)(A\_x-C\_x) + (C\_x-B\_x)(A\_y-C\_y)}\\
{Bary}\_B &= \frac{(C\_y-A\_y)(P\_x-C\_x) + (A\_x-C\_x)(P\_y-C\_y)}{(B\_y-C\_y)(A\_x-C\_x) + (C\_x-B\_x)(A\_y-C\_y)}\\
{Bary}\_C &= 1 - {Bary}\_A - {Bary}\_B
\end{aligned}$$

The derivation and reasoning is explained in the [wikipedia article](https://en.wikipedia.org/wiki/Barycentric_coordinate_system).

Once you have coordinates, you can determine the texture coordinates of $P$ by interpolating the values at the vertices using the barycentric coordinates as weights:

## $$P\_{uv} = {Bary}\_A \cdot A\_{uv} + {Bary}\_B \cdot B\_{uv} + {Bary}\_C \cdot C\_{uv}$$

The reasoning is also explained very nicely in [this](https://classes.soe.ucsc.edu/cmps160/Fall10/resources/barycentricInterpolation.pdf) presentation.

Also see [this question](https://gamedev.stackexchange.com/questions/23743/whats-the-most-efficient-way-to-find-barycentric-coordinates) for efficient methods of computation.

> **Comments:**
> **username_2**: i sthere a error in $Bary_B$? Should the first term be $(A_y-C_y)$ or am i wrong?  
> **username_3**: @username_2 I don't think so. It's the same in the Wikipedia article, and it seems correct from a test calculation I did.  
> **username_2**: ah, so it is $-(A_y-C_y)$, might be good to point out as you would pre calculate $AC_y = (A_y-C_y)$.  
> **username_3**: @username_2 The entire denominator and some of the terms in the nominator can be precalculated for each triangle, only few of the terms depend on $P$. I've added a link to a question dealing with methods of calculation. In this formula I thought it would be better to keep the notation simple and uniform rather than efficient.  

---

