Triangle Interior

Author

Eric W. Weisstein
August 21, 2008

This notebook downloaded from http://mathworld.wolfram.com/notebooks/PlaneGeometry/TriangleInterior.nb.

For more information, see Eric's MathWorld entry http://mathworld.wolfram.com/TriangleInterior.html.

©2008 Wolfram Research, Inc. except for portions noted otherwise

Algebraic

v={vx,vy};
v0={v0x,v0y};
v1={v1x,v1y};
v2={v2x,v2y};

Simplify[Solve[{vx,vy}=={v0x,v0y}+a {v1x,v1y}+b {v2x,v2y},{a,b}]]

cross[x_,y_]:=x[[1]]y[[2]]-x[[2]]y[[1]]

(cross[v,v2]-cross[v0,v2])/cross[v1,v2]

(cross[v,v1]-cross[v0,v1])/cross[v1,v2]

Using Det

InteriorQ[t_,v_]:=Module[{v0=t[[1]],v1=t[[2]]-t[[1]],v2=t[[-1]]-t[[1]]},
a=(Det[{v,v2}]-Det[{v0,v2}])/Det[{v1,v2}];
b=-(Det[{v,v1}]-Det[{v0,v1}])/Det[{v1,v2}];
a>0&&b>0&&a+b<1
]

Manipulate[Graphics[{PointSize[.05],Thickness[.01],
Blue,Line[{pt1,pt2,pt3,pt1}],
{If[InteriorQ[{pt1,pt2,pt3},pt4],Green,Red],Point[pt4]}
},
ImageSize->{450,400},PlotRange->{{-4.1,4.1},{-4.1,4.5}}],
{{pt1,{-2.98`,-2.56`}},{-4,-4},{4,4},Locator},
{{pt2,{-1.545`,3.9`}},{-4,-4},{4,4},Locator},
{{pt3,{3.955`,1.`}},{-4,-4},{4,4},Locator},
{{pt4,{0,0}},{-4,-4},{4,4},Locator}
]

Convex Hull

<<ComputationalGeometry`

InteriorQ[x_List]:=Length[ConvexHull[x]]===3

Manipulate[Graphics[{PointSize[.05],Thickness[.01],
Blue,Line[{pt1,pt2,pt3,pt1}],
{If[InteriorQ[{pt1,pt2,pt3},pt4],Green,Red],Point[pt4]}
},
ImageSize->{450,400},PlotRange->{{-4.1,4.1},{-4.1,4.5}}],
{{pt1,{-2.98`,-2.56`}},{-4,-4},{4,4},Locator},
{{pt2,{-1.545`,3.9`}},{-4,-4},{4,4},Locator},
{{pt3,{3.955`,1.`}},{-4,-4},{4,4},Locator},
{{pt4,{0,0}},{-4,-4},{4,4},Locator}
]

Determinant

InteriorQ[v_,{x_,y_}]:=And@@(Positive/@Det/@Transpose[{-Orientation[v]Subtract@@@Partition[v,2,1,1],{x,y}-#&/@v}])
Orientation[v_]:=Sign[Det[Subtract@@@Partition[v,2,1]]]

Manipulate[Graphics[{PointSize[.05],Thickness[.01],
Blue,Line[{pt1,pt2,pt3,pt1}],
{If[InteriorQ[{pt1,pt2,pt3},pt4],Green,Red],Point[pt4]}
},
ImageSize->{450,400},PlotRange->{{-4.1,4.1},{-4.1,4.5}}],
{{pt1,{-2.98`,-2.56`}},{-4,-4},{4,4},Locator},
{{pt2,{-1.545`,3.9`}},{-4,-4},{4,4},Locator},
{{pt3,{3.955`,1.`}},{-4,-4},{4,4},Locator},
{{pt4,{0,0}},{-4,-4},{4,4},Locator}
]

Trilinear

?TriangleInteriorQ

?CartesianToTrilinear

t=Triangle[{{0,0},{1,0},{.5,.7}}];

p1={.3,.7};
p2={.3,.2};
Show[GraphicsArray[Graphics[{t,RGBColor[1,0,0],PointSize[.05],Point[#]},AspectRatio->Automatic]&/@{p1,p2}]]
Null

Print[TriangleInteriorQ[p,t[[1]]]];
ExactTrilinears[CartesianToTrilinear[t[[1]],p],t[[1]]]