Triangle: Is a Point inside or outside?

Consider a triangle ABC specified by three non-collinear points A, B abd C. Any point P in the plane of the points can then be unqiquely expressed as:

P= uA+vB+wC

for some constants u, w, and w, where u+v+w = 1/

The triplet (u,v,w) correspodins to the barycentric coordinates of tthe points.

For triangle ABC, the baycentric coordinates of the cetrcies A, B and C are (1, 0, 0), (0,1,0) and (0,0,1) respectively.

In general, a point with barycentric coordinates (u,v,w) is inside (or on) the triangle if and only if \(0\le v, w,w \le 1\), or alternatively, if and only if \(0\le v \le 1\) and \(v+w \le 1\)

The abrycentric coordinates actually parametrrize the plane follows from P=uA+vB+wC realky just being a reformulation of 
P=A+v(B-A)+w(C-A), with v and w arbitraty as
P= A+v(B-A) +w(C-A)==(1-v-w)A+vB+wC
In the latter formulation, the two indepent diection vectors AB and AC form a coordinate system with origin A, allowing any point p in the plane to be paramterised in terms of v and w alone. Clearly barycenrtic coordinaets ia r edundant represntation in that the third component an be expressed in ters of the first two. 

To solve for the barycentric coordianets the expression reformulate the expression \(P= A+v(B-A)+w(C-A)\) as \(v(B-A)+w(C-A)=P-A\) and, for ease of analysis, let \(\mathbf v_0=B-A\), \(\mathbf v_1=C-A\) and \(\mathbf v_2=P-A\), so that the expression becomes:
\[v\mathbf v_0 + w\mathbf v_1=\mathbf v_2\]
Now dot product both sides with both \(\mathbf v_0\) and \(\mathbf v_1\)
\[(v\mathbf v_0 + w\mathbf v_1)\cdot \mathbf v_0=\mathbf v_2\cdot \mathbf v_0 \]
\[(v\mathbf v_0 + w\mathbf v_1)\cdot \mathbf v_1=\mathbf v_2\cdot \mathbf v_1 \]
Since the dot product is distributive over addition, the above expressions are equivalent to:
\[v(\mathbf v_0\cdot \mathbf v_0)+w(\mathbf v_1\cdot \mathbf v_0)=\mathbf v_2\cdot \mathbf v_0\]
\[v(\mathbf v_0\cdot \mathbf v_1)+w(\mathbf v_1\cdot \mathbf v_1)=\mathbf v_2\cdot \mathbf v_1\]
The above are set of linear equations and hence can be solved with the Cramer's rule.


Cramer's rule for 2x2 system


For the linear system:
\[\left\{{\begin{matrix}a_{1}x+b_{1}y&={\color {red}c_{1}}\\a_{2}x+b_{2}y&={\color {red}c_{2}}\end{matrix}}\right.\]
which in matrix format is
\[\begin{bmatrix}a_1&b_1\\a_2&b_2 \end{bmatrix} \begin{bmatrix}x\\y\end{bmatrix}= \begin{bmatrix}{\color {red}c_{1}}\\{\color {red}c_{2}}\end{bmatrix}\]
Assume \(a_1b_2 − b_1a_2\) nonzero. Then, with help of determinants, x and y can be found with Cramer's rule as
\[\begin{aligned}x&={\frac {\begin{vmatrix}{\color {red}{c_{1}}}&b_{1}\\{\color {red}{c_{2}}}&b_{2}\end{vmatrix}}{\begin{vmatrix}a_{1}&b_{1}\\a_{2}&b_{2}\end{vmatrix}}}={{\color {red}c_{1}}b_{2}-b_{1}{\color {red}c_{2}} \over a_{1}b_{2}-b_{1}a_{2}},\quad y={\frac {\begin{vmatrix}a_{1}&{\color {red}{c_{1}}}\\a_{2}&{\color {red}{c_{2}}}\end{vmatrix}}{\begin{vmatrix}a_{1}&b_{1}\\a_{2}&b_{2}\end{vmatrix}}}={a_{1}{\color {red}c_{2}}-{\color {red}c_{1}}a_{2} \over a_{1}b_{2}-b_{1}a_{2}}\end{aligned}\]


Javascript Implementation

let v0 = b - a, v1 = c - a; v2 = p - a;
let d00 = v0.dot(v0);
let d10 = v1.dot(v0);
let d01 = v0.dot(v1);
let d11 = v1.dot(v1);
let d20 = v2.dot(v0);
let d21 = v2.dot(v1);
let D = d00*d11-d10*d01;
let v = (d11*d20 - d01*d21) / D;
let w = (d00}d21 - d01*d20) / D;
let u = 1 - v - w;

As dot product is commutative, d10 and d01 are equal. I have calculated them separately for ease of comprehension.




0 件のコメント:

コメントを投稿

Numerical Integration: 2nd and 4th order Runge-Kutta schemes

Second-order Runge-Kutta scheme (RK2) There are several variations of the second order Runge-Kutta schemes of numerical integration. They ar...