Point to line distance

Point to line distance  preview image

1 collaborator

Eugene_martin Eugene Martin (Author)

Tags

geometry 

"line intersection and distance"

Tagged by Eugene Martin about 3 years ago

gis 

"Linear equations line intersection"

Tagged by Eugene Martin about 3 years ago

Visible to everyone | Changeable by the author
Model was written in NetLogo 5.3.1 • Viewed 176 times • Downloaded 18 times • Run 0 times
Download the 'Point to line distance ' modelDownload this modelEmbed this model

Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)


Comments and Questions

Please start the discussion about this model! (You'll first need to log in.)

Click to Run Model

turtles-own [name]
globals [p2l_dist nearestelement online1 Intersect step alt Area Above DPx DPy A1 A2 B1 B2 A1x A1y A2x A2y B1x B1y B2x B2y Ix Iy Am Ab Bm Bb D-Pointword A1word A2word B1word B2word Ixword Iyword Iword Intersection Disp ExtList]

to doit
  if step = 0 [line_1]
  if step = 1 [dpoint]
  if step = 2 [ipoint]
  if step = 3 [line_2]
  if step = 4 [altdist]
  set step step + 1
  if step > 5 [set step 0 ca clear-output]
end 

to line_1
    ask patches with [pxcor = 0] [set pcolor 1]
    ask patches with [pycor = 0] [set pcolor 1]
    create-turtles 1 [set name "A1" setxy random-xcor random-ycor set shape "circle" set color red set size 2 set label name set label-color white]
    set A1 one-of turtles with [name = "A1"]
    let A1who [who] of A1
    create-turtles 1 [set name "A2" setxy random-xcor random-ycor set shape "circle" set color red set size 2 set label name set label-color white]
    set A2 one-of turtles with [name = "A2"]
    let A2who [who] of A2
    ask A1 [create-link-with A2]
    ask link A1who A2who [set thickness 0.8 set color red]
    ;line A:  y=(m*x) + b  or x = (y - b) / m
    set A1x [xcor] of A1
    set A1y [ycor] of A1
    set A2x [xcor] of A2
    set A2y [ycor] of A2
    set Am (A1y - A2y) / (A1x - A2x)
    set Ab A1y - (Am * A1x)
    set A1word (word "(" substring (word A1x) 0 6 ", " substring (word A1y) 0 6 ")")
    set A2word (word "(" substring (word A2x) 0 6 ", " substring (word A2y) 0 6 ")")
    ;extend the line extension
    set ExtList []
    let scale 0.99
    if abs ((Am * ((world-width * scale) / -2.0)) + Ab) < ((world-height * scale) / 2) [set ExtList lput (list ((world-width * scale) / -2.0) ((Am * ((world-width * scale) / -2.0)) + Ab))  ExtList]
    if abs ((Am * ((world-width * scale) / 2.0)) + Ab) < ((world-height * scale) / 2) [set ExtList lput (list ((world-width * scale) / 2.0) ((Am * ((world-width * scale) / 2.0)) + Ab))  ExtList]
    if abs ((((world-height * scale) / 2.0) - Ab) / Am) < ((world-width * scale) / 2) [set ExtList lput (list ((((world-height * scale) / 2.0) - Ab) / Am) ((world-height * scale) / 2.0))  ExtList]
    if abs ((((world-height * scale) / -2.0) - Ab) / Am) < ((world-width * scale) / 2) [set ExtList lput (list ((((world-height * scale) / -2.0) - Ab) / Am) ((world-height * scale) / -2.0))  ExtList]
    show ExtList
    create-turtles 1 [set name "AE1" set xcor first first ExtList set ycor last first ExtList set color black]
    create-turtles 1 [set name "AE2" set xcor first last ExtList set ycor last last ExtList set color black]
    ask one-of turtles with [name = "AE1"] [create-link-with one-of turtles with [name = "AE2"]]
    ask link ([who] of one-of turtles with [name = "AE1"]) ([who] of one-of turtles with [name = "AE2"]) [set thickness 0.2 set color red]
end 

to dpoint
    create-turtles 1 [set name "D-Point" setxy random-xcor random-ycor set shape "circle" set color yellow set size 2 set label name set label-color white]
    set B1x [xcor] of one-of turtles with [name = "D-Point"]
    set B1y [ycor] of one-of turtles with [name = "D-Point"]
    set D-Pointword (word "(" substring (word B1x) 0 6 ", " substring (word B1y) 0 6 ")")
end 

to ipoint
        set Bm -1 / Am
        set Bb B1y - (Bm * B1x) ;get the y intercept
        set B2x 0
        set B2y Bb
        ;solve for point intersection at right angle
        set Ix ((Bb - Ab) / (Bm - Am)) * -1
        set Iy Bm * Ix + Bb
        set Intersect (word "(" substring (word Ix) 0 6 ", " substring (word Iy) 0 6 ")")
        ;set B2word (word "(" substring (word B2x) 0 6 ", " substring (word B2y) 0 6 ")")
        ;report the distance
        set alt (((B1x - Ix) ^ 2) + ((B1y - Iy) ^ 2)) ^ 0.5
        set Area ((((A1x - A2x) ^ 2 + (A1y - A2y) ^ 2) ^ 0.5)  + alt ) / 2
        ifelse B1y > Iy [set Above true][set Above false]
        ifelse (abs Ix > (world-width / 2.0)) or (abs Iy > (world-height / 2.0)) [set Disp "Outside display"][set Disp "YES" create-turtles 1 [set name "intersect" set label name setxy Ix Iy set shape "star" set color WHITE set size 3]]
end 

to line_2
  set ExtList []
  let scale 0.99
  if abs ((Bm * ((world-width * scale) / -2.0)) + Bb) < ((world-height * scale) / 2) [set ExtList lput (list ((world-width * scale) / -2.0) ((Bm * ((world-width * scale) / -2.0)) + Bb))  ExtList]
  if abs ((Bm * ((world-width * scale) / 2.0)) + Bb) < ((world-height * scale) / 2) [set ExtList lput (list ((world-width * scale) / 2.0) ((Bm * ((world-width * scale) / 2.0)) + Bb))  ExtList]
  if abs ((((world-height * scale) / 2.0) - Bb) / Bm) < ((world-width * scale) / 2) [set ExtList lput (list ((((world-height * scale) / 2.0) - Bb) / Bm) ((world-height * scale) / 2.0))  ExtList]
  if abs ((((world-height * scale) / -2.0) - Bb) / Bm) < ((world-width * scale) / 2) [set ExtList lput (list ((((world-height * scale) / -2.0) - Bb) / Bm) ((world-height * scale) / -2.0))  ExtList]
  show ExtList
  create-turtles 1 [set name "BE1" set xcor first first ExtList set ycor last first ExtList set color black]
  create-turtles 1 [set name "BE2" set xcor first last ExtList set ycor last last ExtList set color black]
  ask one-of turtles with [name = "BE1"] [create-link-with one-of turtles with [name = "BE2"]]
  ask link ([who] of one-of turtles with [name = "BE1"]) ([who] of one-of turtles with [name = "BE2"]) [set thickness 0.2 set color green]
  if Disp = "YES" [
    ask one-of turtles with [name = "intersect"] [create-link-with one-of turtles with [name = "D-Point"]]
    ask link ([who] of one-of turtles with [name = "intersect"]) ([who] of one-of turtles with [name = "D-Point"]) [set thickness 0.8 set color green]
  ]
end 

to altdist
  if Disp = "YES" [
    ;is the intersect point on ineAline 1?
    ifelse ((Ix < A1x and Ix > A2x) or (Ix < A2x and Ix > A1x)) and ((Iy < A1y and Iy > A2y) or (Iy < A2y and Iy > A1y)) [set online1 "YES" set nearestelement "along line"][set online1 "NO" set nearestelement "endpoint"]
    ifelse online1 = "YES"
      [ask link ([who] of one-of turtles with [name = "intersect"]) ([who] of one-of turtles with [name = "D-Point"]) [set thickness 0.8 set color yellow]
       ask one-of turtles with [name = "D-Point"] [set p2l_dist distancexy Ix Iy]]
      [ask link ([who] of one-of turtles with [name = "intersect"]) ([who] of one-of turtles with [name = "D-Point"]) [die]
       ask one-of turtles with [name = "D-Point"] [create-link-with one-of turtles with [name = [name] of min-one-of turtles with [name = "A1" or name = "A2"] [distance one-of turtles with [name = "D-Point"]]]]
       ask link ([who] of one-of turtles with [name = "D-Point"]) ([who] of one-of turtles with [name = [name] of min-one-of turtles with [name = "A1" or name = "A2"] [distance one-of turtles with [name = "D-Point"]]]) [set thickness 0.8 set color yellow ]]
    ]
end 

There are 2 versions of this model.

Uploaded by When Description Download
Eugene Martin about 3 years ago clear all button Download this version
Eugene Martin about 3 years ago Initial upload Download this version

Attached files

File Type Description Last updated
Point to line distance .png preview Preview for 'Point to line distance ' about 3 years ago, by Eugene Martin Download

This model does not have any ancestors.

This model does not have any descendants.