intersect.line.circle: The points of intersection of a line and a circle

View source: R/ArcSliceFunctions.R

intersect.line.circleR Documentation

The points of intersection of a line and a circle

Description

Returns the intersection point(s) of a line and a circle. The line is determined by the two points p1 and p2 and the circle is centered at point cent and has radius rad. If the circle does not intersect the line, the function yields NULL; if the circle intersects at only one point, it yields only that point; otherwise it yields both intersection points as output. When there are two intersection points, they are listed in the order of the x-coordinates of p1 and p2; and if the x-coordinates of p1 and p2 are equal, intersection points are listed in the order of y-coordinates of p1 and p2.

Usage

intersect.line.circle(p1, p2, cent, rad)

Arguments

p1, p2

2D points that determine the straight line (i.e., through which the straight line passes).

cent

A 2D point representing the center of the circle.

rad

A positive real number representing the radius of the circle.

Value

point(s) of intersection between the circle and the line (if they do not intersect, the function yields NULL as the output)

Author(s)

Elvan Ceyhan

See Also

intersect2lines

Examples

## Not run: 
P1<-c(.3,.2)*100
P2<-c(.6,.3)*100
cent<-c(1.1,1.1)*100
rad<-2*100

intersect.line.circle(P1,P2,cent,rad)
intersect.line.circle(P2,P1,cent,rad)
intersect.line.circle(P1,P1+c(0,1),cent,rad)
intersect.line.circle(P1+c(0,1),P1,cent,rad)

dist.point2line(cent,P1,P2)
rad2<-dist.point2line(cent,P1,P2)$d
intersect.line.circle(P1,P2,cent,rad2)
intersect.line.circle(P1,P2,cent,rad=.8)
intersect.line.circle(P1,P2,cent,rad=.78)

#plot of the line and the circle
A<-c(.3,.2); B<-c(.6,.3); cent<-c(1,1); rad<-2 #check dist.point2line(cent,A,B)$dis, .3

IPs<-intersect.line.circle(A,B,cent,rad)

xr<-range(A[1],B[1],cent[1])
xf<-(xr[2]-xr[1])*.1 #how far to go at the lower and upper ends in the x-coordinate
x<-seq(xr[1]-rad-xf,xr[2]+rad+xf,l=20)  #try also l=100
lnAB<-Line(A,B,x)
y<-lnAB$y

Xlim<-range(x,cent[1])
Ylim<-range(y,A[2],B[2],cent[2]-rad,cent[2]+rad)
xd<-Xlim[2]-Xlim[1]
yd<-Ylim[2]-Ylim[1]

plot(rbind(A,B,cent),pch=1,asp=1,xlab="x",ylab="y",
xlim=Xlim+xd*c(-.05,.05),ylim=Ylim+yd*c(-.05,.05))
lines(x,y,lty=1)
interp::circles(cent[1],cent[2],rad)
IP.txt<-c()
if (!is.null(IPs))
{
  for (i in 1:(length(IPs)/2))
    IP.txt<-c(IP.txt,paste("I",i, sep = ""))
}
txt<-rbind(A,B,cent,IPs)
text(txt+cbind(rep(xd*.03,nrow(txt)),rep(-yd*.03,nrow(txt))),c("A","B","M",IP.txt))

## End(Not run)


elvanceyhan/pcds documentation built on June 29, 2023, 8:12 a.m.