Learn R Programming

pcds (version 0.1.2)

in.triangle: Check whether a point is inside a triangle

Description

Checks if the point p lies in the triangle, tri, using the barycentric coordinates, generally denoted as \((\alpha,\beta,\gamma)\).

If all (normalized or non-normalized) barycentric coordinates are positive then the point p is inside the triangle, if all are nonnegative with one or more are zero, then p falls in the boundary. If some of the barycentric coordinates are negative, then p falls outside the triangle.

boundary is a logical argument (default=FALSE) to include boundary or not, so if it is TRUE, the function checks if the point, p, lies in the closure of the triangle (i.e., interior and boundary combined) else it checks if p lies in the interior of the triangle.

Usage

in.triangle(p, tri, boundary = FALSE)

Arguments

p

A 2D point to be checked whether it is inside the triangle or not.

tri

Three 2D points, stacked row-wise, each row representing a vertex of the triangle.

boundary

A logical parameter (default=FALSE) to include boundary or not, so if it is TRUE, the function checks if the point, p, lies in the closure of the triangle (i.e., interior and boundary combined) else it checks if p lies in the interior of the triangle.

Value

A list with two elements

in.tri

A logical output, it is TRUE, if the point, p, is inside the triangle, tri, else it is FALSE.

barycentric

The barycentric coordinates \((\alpha,\beta,\gamma)\) of the point p with respect to the triangle, tri.

See Also

inTriAll and on.convex.hull from the interp package for documentation for in.convex.hull

Examples

Run this code
# NOT RUN {
A<-c(1,1); B<-c(2,0); C<-c(1.5,2); p<-c(1.4,1.2)

Tr<-rbind(A,B,C)

in.triangle(p,Tr)

p<-c(.4,-.2)
in.triangle(p,Tr)

#for the vertex A
in.triangle(A,Tr)
in.triangle(A,Tr,boundary=TRUE)

#for a point on the edge AB
D3<-(A+B)/2
in.triangle(D3,Tr)
in.triangle(D3,Tr,boundary=TRUE)

#for a NA entry point
p<-c(NA,.2)
in.triangle(p,Tr)

dat.fr<-data.frame(a=Tr)
in.triangle(p,dat.fr)

# }

Run the code above in your browser using DataLab