Point On Line / Line Hit Point / Point Intersect Line

Started by kevin, October 04, 2021, 08:31:16 AM

Previous topic - Next topic

kevin


    Point On Line / Line Hit Point /  Point Intersect Line



PlayBASIC Code: [Select]
; PROJECT : POint On Line
; AUTHOR : PlayBASIC TUTOR
; CREATED : 4/10/2021
; EDITED : 5/10/2021
; ---------------------------------------------------------------------

type tlines
x1#,y1#,x2#,y2#
endtype

Dim Lines(100) as tLines

for lp =0 to 100

Lines(lp).x1 = rnd(800)
Lines(lp).y1 = rnd(600)
Lines(lp).x2 = rnd(800)
Lines(lp).y2 = rnd(600)


next

do
cls

mx#=mousex()
my#=mousey()

for lp =0 to 100

x1#=lines(lp).x1
y1#=lines(lp).y1
x2#=lines(lp).x2
y2#=lines(lp).y2
ink -1
if Point_On_line(mx#,my#,x1#,y1#,x2#,y2#)
ink rndrgb()
endif
line x1#,y1#,x2#,y2#
next

sync
loop spacekey()

end




function Point_On_line(pointx#,pointy#,x1#,y1#,x2#,y2#)

// compute nearest point along the line
dx31#=PointX#-x1#
dx21#=x2#-x1#
dy31#=PointY#-y1#
dy21#=y2#-y1#

l#=((dx31#*dx21#)+(dy31#*dy21#)) / ((dx21#*dx21#)+(dy21#*dy21#))

// see if our point at least lies parallel with our line
status = l#>=0 and L#<=1
if Status

// if so, we compute the nearest point on the line
x#=x1#+(dx21#*l#)
y#=y1#+(dy21#*l#)

// then get the distance from the line to our point.
Dist# = getdistance2d(x#,y#,pointx#,pointy#)

// check if it's within a tolerance of 1, might need
// to be tweaked
Status = Dist#<=1

endif

EndFunction status






   PlayBASIC Documentation:  MATH Commands