;;;; SUMMARY ;; Calculating the intersection of two lines ;;;; COPYRIGHT ;; Copyright (C) 2005 James Steiner ;; Some rights reserved ;; Licensed under Creative Commons Attribution-Share Alike-Non-Commercial 2.0 license ;; visit http://www.creativecommons.org for more details globals [ greeting? ;; only used to tell GO that the greeting is showing, and should be removed. ] to startup setup go show-greeting end to setup ca cct 1 ;; intersect marker turtle [ set shape "x" set color white set size 3 ] cct 2 ;; line turtles [ set size screen-edge-x set shape "line" set color 45 + 70 * (who - 1) set heading who * 180 jump 5 ] display show-greeting end to go every 1 / 30 [ if greeting? [ clear-greeting ] no-display move-line-turtles position-marker-turtle display ] end to-report det [ a b c d ] ;; reports the determinent of a b c d: ;; | a b | ;; | c d d report (a * d - b * c) end to-report intersection [ x1 y1 x2 y2 x3 y3 x4 y4 ] ;; reports the x-coord and y-coord of the intersection of 2 lines ;; reference: eric weinstein's mathworld ;; mathworld.wolfram.com let _a det x1 y1 x2 y2 let _b det x3 y3 x4 y4 let _c x1 - x2 let _d x3 - x4 let _e y1 - y2 let _f y3 - y4 let _g (det _a _c _b _d ) let _h (det _a _e _b _f ) let _i (det _c _e _d _f ) let result [] carefully [ let _x _g / _i let _y _h / _i set result (list _x _y) ] [ set result 0 ] report result end to-report segment-intersect [ x1 y1 x2 y2 x3 y3 x4 y4 ] let intersect intersection x1 y1 x2 y2 x3 y3 x4 y4 let result 0 if intersect != 0 [ let x0 first intersect let y0 last intersect ;; put in order, so that we can see if point 0 is between if x1 > x2 [ let t x1 set x1 x2 set x2 t ] if y1 > y2 [ let t y1 set y1 y2 set y2 t ] if x3 > x4 [ let t x3 set x3 x4 set x4 t ] if y3 > y4 [ let t y3 set y3 y4 set y4 t ] ifelse x0 < x1 or x0 > x2 or y0 < y1 or y0 > y2 or x0 < x3 or x0 > x4 or y0 < y3 or y0 > y4 [ set result 1 ] [ set result intersect ] ] report result end to-report segment-intersect-turtles [ a b ] ;; reports the point of intersection, or 0 let x1 0 let x2 0 let x3 0 let x4 0 let y1 0 let y2 0 let y3 0 let y4 0 ask a [ set x1 xcor - dx * size * .5 set y1 ycor - dy * size * .5 set x2 xcor + dx * size * .5 set y2 ycor + dy * size * .5 ] ask b [ set x3 xcor - dx * size * .5 set y3 ycor - dy * size * .5 set x4 xcor + dx * size * .5 set y4 ycor + dy * size * .5 ] report ( segment-intersect x1 y1 x2 y2 x3 y3 x4 y4 ) end to-report in-line [ eqn x0 y0 ] ;; reports true if point x0 y0 is on line defined in eqn let a item 0 eqn let b item 1 eqn let c item 2 eqn report ((a * x0) + (b * y0) + c = 0 ) end to move-line-turtles ask turtles with [ who != 0 ] [ rt ((who * 2) - 1) * (1 + who) * .5 if abs (xcor + dx * size * .5 ) > screen-edge-x [ rt 180 ] ; set heading -1 * heading ] if abs (ycor + dy * size * .5 ) > screen-edge-y [ rt 180 ] ;set heading 90 + (subtract-headings 90 heading) ] jump .5 ] end to position-marker-turtle ;; marker turtle checks for intersection of line turtles ;; if no intersection, hides ;; if an intersection, moves to point of intersection ask turtle 0 [ let result segment-intersect-turtles turtle 1 turtle 2 ifelse result = 0 or result = 1 [ if not hidden? [ hide-turtle ] ] [ let xx first result let yy last result ;; if intersection is off the screen, stick to edge of screen if abs xx > screen-edge-x [ set xx screen-edge-x * (abs xx / xx ) ] if abs yy > screen-edge-y [ set yy screen-edge-y * (abs yy / yy ) ] ;; move to new location setxy xx yy ;; become visible, if needed if hidden? [ show-turtle ] ] ] end to show-greeting ask patch screen-edge-x (screen-edge-y - 2) [ set plabel "Click GO to begin the animation." ] set greeting? true end to clear-greeting set greeting? false ask patch screen-edge-x (screen-edge-y - 2) [ set plabel no-label ] end @#$#@#$#@ GRAPHICS-WINDOW 81 10 401 351 15 15 10.0 1 20 1 1 1 0 1 1 1 CC-WINDOW 5 365 410 460 Command Center 0 BUTTON 10 49 73 82 reset setup NIL 1 T OBSERVER T NIL BUTTON 11 10 74 43 NIL go T 1 T OBSERVER NIL NIL TEXTBOX 13 94 79 204 Two line segments move about. A marker appears at the point of intersection. @#$#@#$#@ WHAT IS IT? ----------- A model to demonstrate a method of calculating the intersection of two line segments. This model does not handle co-linear segments that overlap well. Additional tests for colinearity and coincidence should be added. @#$#@#$#@ default true 0 Polygon -7500403 true true 150 5 40 250 150 205 260 250 line true 5 Line -10899396 true 150 0 150 300 x false 1 Circle -2674135 false true 0 0 300 Line -2674135 true 45 45 255 255 Line -2674135 true 45 255 255 45 x2 false 1 Polygon -2674135 true true 60 0 150 150 240 0 300 60 150 150 300 240 240 300 150 150 60 300 0 240 150 150 0 60 @#$#@#$#@ NetLogo 3.0.2 @#$#@#$#@ @#$#@#$#@ @#$#@#$#@ @#$#@#$#@