Hey everyone. What I've managed to Forrest Gump my way through today is a little set of functions that find the shortest distance between a Point in 2D space and a line segement. Off the top of my head, I can think of a few implementations. Maybe a collision system for a 2D racing game? Enjoy!
`*************************************************
`Distance From A Point To A Line
`*************************************************
`
`Written By: Kyle Barrett
`Math Help From: http://astronomy.swin.edu.au/~pbourke/geometry/
`
`*********************
` Additional Info
`---------------------
`
` This code will find the shortest distance from a point to a line
`segement. Keep in mind that this code wont work if you are trying
`to find the distance from a point to a line or ray, just a line
`segement, as that is all I needed it for.
`
` Thanks to Paul Bourke and his website for sample C source code
`and extensive maths help.
`
` Questions? Comments? Improvements? E-mail me at RallySpeedX@Hotmail.com
Sync On
`First and Last Points of Our Line
X2# = 100.0
Y2# = 100.0
X1# = 300.0
Y1# = 257.0
Do
`Clear the Screen
CLS
`Draw Our Line
Line X1#,Y1#,X2#,Y2#
`Our Point. For Demo's Sake, We'll Use The Mouse Pointer
X3# = MOUSEX()
Y3# = MOUSEY()
`Lets Find The Distance
D# = PointLineDistance(X1#,Y1#,X2#,Y2#,X3#,Y3#)
TEXT 20,20,str$(D#)
Sync
Loop
Function Dis(x1#,y1#,x2#,y2#)
VX# = X2# - X1#
VY# = Y2# - Y1#
V# = SQRT(VX#^2 + VY#^2)
Endfunction V#
Function PointLineDistance(x1#,y1#,x2#,y2#,x3#,y3#)
`First We Have To Find The Length Of The Line
LineMag# = Dis(x2#,y2#,x1#,y1#)
Text 20,35,"LineMag: " + str$(LineMag#)
`U# Is A Percentage. Its The Percent Of The Line That Can Be Added To The Line To Find The Intersection
U# = (((x3# - x1#) * (x2# - x1#)) + ((y3# - y1#) * (y2# - y1#))) / LineMag#^2
Text 20,50,"U: " + str$(U#)
`Lets Find The Intersection (Or The Closest Point On The Line To The Point)
IntersectionX# = X1# + U# * ( X2# - X1#)
IntersectionY# = Y1# + U# * ( Y2# - Y1#)
`This Section of Code Is Incase The Second Point Is Less Than The First Point.
`Normally, It Would Cause A Lot Of Problems, But We Solved Them
If X2# > X1#
If IntersectionX# > X2# then IntersectionX# = X2#
If IntersectionX# < X1# then IntersectionX# = X1#
Endif
If X2# < X1#
If IntersectionX# < X2# then IntersectionX# = X2#
If IntersectionX# > X1# then IntersectionX# = X1#
Endif
If Y2# > Y1#
If IntersectionY# > Y2# then IntersectionY# = Y2#
If IntersectionY# < Y1# then IntersectionY# = Y1#
Endif
If Y2# < Y1#
If IntersectionY# < Y2# then IntersectionY# = Y2#
If IntersectionY# > Y1# then IntersectionY# = Y1#
Endif
Text 20,65,"InterX: " + str$(IntersectionX#) + " InterY: " + str$(IntersectionY#)
`Draw Our Intersecting Line. It Should Form A Right Angle With Our Original Line
Line IntersectionX#,IntersectionY#,X3#,Y3#
`Get Our Distance From The Point To The Line
Distance# = Dis(IntersectionX#,IntersectionY#,X3#,Y3#)
Endfunction Distance#
I Am Entirely Smooth. I Admit To The Truth, I Am The Best At What I Do