-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutilities.nls
More file actions
75 lines (68 loc) · 2.16 KB
/
utilities.nls
File metadata and controls
75 lines (68 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
to profile-setup ; profiles the setup
clear-all
profiler:start ; doesn't differentiate between full and small scale - that has to be manually selected before running
setup
profiler:stop
print profiler:report
profiler:stop
end
to profile
profiler:start ;; start profiling
repeat 1 [ go ] ;; run something you want to measure
profiler:stop ;; stop profiling
print profiler:report
profiler:stop
end
to-report intersects-here [ variety ]
;; each pair of segments checks for intersections
let result intersection self myself
let intersect-here not empty? result
ask variety [
if not empty? result []
]
report intersect-here
end
;; reports a two-item list of x and y coordinates, or an empty
;; list if no intersection is found
to-report intersection [t1 t2]
let m1 [tan (90 - heading)] of t1
let m2 [tan (90 - heading)] of t2
;; treat parallel/collinear lines as non-intersecting
if m1 = m2 [ report [] ]
;; is t1 vertical? if so, swap the two turtles
if abs m1 = tan 90
[
ifelse abs m2 = tan 90
[ report [] ]
[ report intersection t2 t1 ]
]
;; is t2 vertical? if so, handle specially
if abs m2 = tan 90 [
;; represent t1 line in slope-intercept form (y=mx+c)
let c1 [ycor - xcor * m1] of t1
;; t2 is vertical so we know x already
let x [xcor] of t2
;; solve for y
let y m1 * x + c1
;; check if intersection point lies on both segments
if not [x-within? x] of t1 [ report [] ]
if not [y-within? y] of t2 [ report [] ]
report list x y
]
;; now handle the normal case where neither turtle is vertical;
;; start by representing lines in slope-intercept form (y=mx+c)
let c1 [ycor - xcor * m1] of t1
let c2 [ycor - xcor * m2] of t2
;; now solve for x
let x (c2 - c1) / (m1 - m2)
;; check if intersection point lies on both segments
if not [x-within? x] of t1 [ report [] ]
if not [x-within? x] of t2 [ report [] ]
report list x (m1 * x + c1)
end
to-report x-within? [x] ;; turtle procedure
report abs (xcor - x) <= abs (size / 2 * dx)
end
to-report y-within? [y] ;; turtle procedure
report abs (ycor - y) <= abs (size / 2 * dy)
end