This question was answered here, but not answered in R language. I am a relatively new to coding, so I haven't been able to figure out how to 'translate' the accepted answer's C++ code into R code.
As in the linked question, my line segment is defined by two endpoints: A (x1,y1) and B (x2,y2). I'm trying to find the shortest (i.e., perpendicular) distance between this line segment and a point C (x3,y3). Below is some example code illustrating that point "C" should have a shortest distance to the line segment, but point "C1" should not.
A <- c(2, 4)
B <- c(8, 16)
C <- c(3, 11)
C1<- c(11, 16)
plot(1, type = "n", xlim = c(0, 25), ylim = c(0, 25))
points(C[1], C[2], col = "red")
points(C1[1], C1[2], col = "blue")
points(A[1], A[2])
points(B[1], B[2])
segments(A[1], A[2], B[1], B[2])
Thanks in advance for any help!
I know I have to remove the semi-colons and class types to 'translate ' into R, but I think one of the main problems will be figuring out comparable class types to the vec2 types used in the C++ code.