-
Notifications
You must be signed in to change notification settings - Fork 0
/
fn-grapher.nlogo
1252 lines (1094 loc) · 22.4 KB
/
fn-grapher.nlogo
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
; global variables declared with sliders:
; number-of-labels label-length
; x-max y-max
; graphing-speed
; declared with inputs:
; y=
; eqaution#
; declared with swiches:
; show-coordinates?
; show-closest-equation?
;TODOs:
; code cleanup, organize more into MVC, perhaps?
; implement mouse-down pt report
; Change line color for new graph
;DONE:
; Align open brackets and close brackets
; Display error message in Output and BEEP
; model
globals
[
; for clear-graph
cur-num-labels
cur-label-length
cur-x-max
cur-y-max
; for internal grid system
x-factor
y-factor
; keep track of graphed equations
equations
]
; reset all parameters
to reset-all
set number-of-labels 15
set label-length 0.3
set x-max max-pxcor
set y-max max-pycor
set graphing-speed 100
set y= ""
set coordinate-precision 2
; equations reset in setup
; initialize model again
setup
end
; modify user input for graphing
to-report modify [str]
let reporter-str (word "[ x -> " str " ]")
print reporter-str
report (runresult reporter-str)
end
; set the internal grid system and update the view
to set-grid [new-x-max new-y-max]
; set internal grid system
set x-factor (new-x-max / max-pxcor)
set y-factor (new-y-max / max-pycor)
label-bounds new-x-max new-y-max
set cur-x-max new-x-max
set cur-y-max new-y-max
end
to-report equations.exist? [ y ]
report member? y equations
end
; add equation to "equations"
to equations.add [ y ]
set equations lput y equations
end
; remove equation from "equations"
to equations.remove [ y ]
ifelse is-number? y
[
set equations remove-item (y - 1) equations
]
[
set equations remove y equations
]
end
; remove all equations from "equations"
to equations.remove-all
set equations []
end
; graph all equations in "equations", WITHOUT doing anything else
to equations.graph-all
if equations = 0 or empty? equations [ stop ]
let functions (map modify equations)
foreach functions graph-function
end
;
to-report get-closest-graph [x y]
let functions map modify equations
let results map [func -> (runresult func x)] functions
let distances map [val -> (abs val) - y] results
report position (min distances) distances
end
; controllers - what buttons should access ONLY
to separate-comments end
; setup the axes, enable other options, initialize "equations" to empty list
to setup
ca
set equations []
update-axes
set-grid x-max y-max
output.setup
set equation# 0
set show-coordinates? true
set show-closest-graph? false
reset-ticks
end
; graph equation in "y="
; calls: equations.exist?, equations.add, graph-function, modify, output.print-last
to add-graph
if y= = "" [ stop ]
if equations.exist? y= [ stop ]
; catch user error or division by 0
carefully
[
let function (modify y=)
graph-function function
equations.add y=
output.setup
output.print-equations
set equation# length equations
]
; catch error
[
output.setup
output.print-equations
; user-message (word "The graph of y = " y= " is not added. The following error has occurred: " error-message " Check equation input to make sure all syntax is correct.")
output.print-message "Graphing stopped because of error:"
output.print-message word " - " error-message
output.print-message "Check the equation input to make sure all \nsyntax are correct."
beep
]
end
; clear all graphs and update internal grid
; calls: set-grid [], setup-guides [], equations.graph-all
to update-window
cd
set-grid x-max y-max
setup-guides cur-num-labels cur-label-length
equations.graph-all
end
; clear the window, setup the view of axes
; calls: clear-window, setup-guides []
to update-axes
cd
setup-guides number-of-labels label-length
equations.graph-all
end
; clear all graphs: clear all drawings and redraw the axes and labels
; calls: clear-view, setup-guides []
to clear-window
clear-view
setup-guides cur-num-labels cur-label-length
end
; reset-axes-labels
; calls: clear-view, setup-guides []
to reset-axes
set number-of-labels 15
set label-length 0.3
clear-view
setup-guides number-of-labels label-length
end
; remove the equation that "equation#" is pointing to
; calls: remove-function
to remove-equation
remove-function equation#
end
; (forever button needed) show the coordinates/closest graph of current mouse position
to show-coordinates
ifelse mouse-down? and mouse-inside?
[
ifelse count turtles with [ shape = "x" ] = 1
[
ask turtles with [ shape = "x" ]
[
setxy mouse-xcor mouse-ycor
ifelse show-coordinates? and show-closest-graph?
[
view-coordinates 2
]
[
ifelse show-coordinates?
[
view-coordinates 0
]
[
ifelse show-closest-graph?
[
view-coordinates 1
]
[
view-coordinates 3
]
]
]
]
]
[
ask turtles with [shape = "x"]
[
die
]
cro 1
[
set shape "x"
set size 0.5
set color red
]
]
]
[
ask turtles with [ shape = "x" ]
[
die
]
]
end
; graph the given equation: function (as anonymous reporter)
to graph-function [ func ]
; create the graphing agent
cro 1
[
set hidden? true
set color pink
set shape "dot"
set size 0.3
]
; initialize x y
let x (-1 * cur-x-max)
let y (runresult func x)
ask turtles with [shape = "dot"]
[
; var to keep track of whether just graphed (because of boundary issues)
; make sure turtle produce the correct graph when the last point is out of bound
let graphed false
; graph from left to right
while [x < cur-x-max]
[
; plot if y is not outside of boundary
ifelse (abs y) <= cur-y-max
[
ifelse graphed
[
pd
setxy x / x-factor y / y-factor
stamp
pu
]
[
; makes sure to not connect the last point if it was outside of boundary
setxy x / x-factor y / y-factor
stamp
set graphed true
]
]
[
set graphed false
]
; update x y
set x (x + 0.01)
set y (runresult func x)
; graphing speed
if graphing-speed != 100
[
wait 0.01 / (graphing-speed)
]
]
die
]
tick
end
; remove a function: index
to remove-function [ index ]
if index > length equations [ stop ]
if index = 0 [ stop ]
equations.remove index
output.setup
output.print-equations
set equation# length equations
update-window
equations.graph-all
end
; view
; clear all drawings and agents
; calls: output.seup, equations.remove-all
to clear-view
cd
ask turtles [die]
output.setup
equations.remove-all
end
; draw labels (marks on axes): number of labels, length of each label
; calls: draw-axes, label-axes[num-label len]
to setup-guides [num-label len]
cro 1
[
set hidden? true
set color 4
]
draw-axes
label-axes num-label len
; retain latest setting - to be used by clear-window
set cur-num-labels num-label
set cur-label-length len
ask turtles
[
die
]
end
; draw four plane axes with arrows on each end
to draw-axes ; turtles needed
ask turtles
[
set pen-size 2
setxy 0 0 pd
set ycor max-pycor pu
stamp
setxy 0 0 pd
set xcor max-pxcor pu
rt 90
stamp
setxy 0 0 pd
set ycor min-pycor pu
rt 90
stamp
setxy 0 0 pd
set xcor min-pxcor pu
rt 90
stamp
]
end
; draw marks on the axes and label "x" "y": number of labels on each axis, length of each label
to label-axes [num len] ; turtles needed
ask patch (max-pxcor - 1) 1
[
set plabel "x"
]
ask patch 1 (max-pycor)
[
set plabel "y"
]
ask turtles
[
set pen-size 1
setxy 0 0
let interval (max-pxcor / (num + 1))
let one-side [ -> repeat num [fd interval lt 90 fd len pd bk (len * 2) pu fd len rt 90]]
repeat 4
[
setxy 0 0
rt 90
run one-side
]
]
end
; label xy max and min: x, y
to label-bounds [x y]
ask patch (max-pxcor - 1) -1
[
set plabel word "max: " x
]
ask patch (min-pxcor + 4) -1
[
set plabel word "min: -" x
]
ask patch -1 (max-pycor)
[
set plabel word "max: " y
]
ask patch -1 (min-pycor + 1)
[
set plabel word "min: -" y
]
end
; setup output area
to output.setup
clear-output
output-print "Graphed Equations:"
end
; format and print all equations in "equations" to output area
to output.print-equations
if empty? equations [ stop ]
let output-list (map [equation -> (word ((position equation equations) + 1) ": y = " equation "\n") ] equations)
let output-str (reduce [ [ prev next ] -> word prev next ] output-list)
output-type output-str
end
; print the last graphed equation to output area
to output.print-last
let equation last equations
output-type (word (length equations) ": y = " equation "\n")
end
; a wrapper for "output-print"
to output.print-message [msg]
output-print msg
end
to view-coordinates [ opt ] ; turtle with shape "x" needed
; clear
if opt = 0
[
set label (word "(" precision (mouse-xcor * x-factor) coordinate-precision ", " precision (mouse-ycor * y-factor) coordinate-precision ")")
]
; graph only
if opt = 1
[
set label (
(word "closest graph: " get-closest-graph mouse-xcor mouse-ycor)
)
]
; coordinate and graph
if opt = 2
[
]
; clear
if opt = 3
[
set label ""
]
end
@#$#@#$#@
GRAPHICS-WINDOW
31
10
522
502
-1
-1
11.8
1
11
1
1
1
0
0
0
1
-20
20
-20
20
0
0
1
ticks
30.0
BUTTON
571
534
653
567
update axes
update-axes
NIL
1
T
OBSERVER
NIL
NIL
NIL
NIL
0
SLIDER
574
294
948
327
x-max
x-max
0.1
100
3.7
0.1
1
NIL
HORIZONTAL
SLIDER
574
332
950
365
y-max
y-max
0.1
100
5.3
0.1
1
NIL
HORIZONTAL
SLIDER
572
450
744
483
number-of-labels
number-of-labels
1
40
15.0
1
1
NIL
HORIZONTAL
SLIDER
572
491
744
524
label-length
label-length
0
max-pxcor
0.3
0.1
1
NIL
HORIZONTAL
BUTTON
655
41
740
74
reset all
reset-all
NIL
1
T
OBSERVER
NIL
NIL
NIL
NIL
0
INPUTBOX
574
113
953
173
y=
tan (360 * 1 / x)
1
0
String (reporter)
BUTTON
574
219
704
252
add graph
add-graph
NIL
1
T
OBSERVER
NIL
NIL
NIL
NIL
0
BUTTON
1027
424
1120
457
clear all graphs
clear-window
NIL
1
T
OBSERVER
NIL
NIL
NIL
NIL
0
BUTTON
719
221
882
254
clear window and graph
clear-window\nadd-graph
NIL
1
T
OBSERVER
NIL
NIL
NIL
NIL
0
BUTTON
574
371
676
404
update window
update-window
NIL
1
T
OBSERVER
NIL
NIL
NIL
NIL
0
TEXTBOX
577
425
727
443
Axes settings:
12
0.0
1
TEXTBOX
578
91
728
109
Graphing
12
0.0
1
TEXTBOX
575
267
725
285
Window
12
0.0
1
SLIDER
574
180
746
213
graphing-speed
graphing-speed
1
100
100.0
1
1
NIL
HORIZONTAL
TEXTBOX
575
14
725
32
Model Control
12
0.0
1
BUTTON
574
40
647
73
NIL
setup
NIL
1
T
OBSERVER
NIL
NIL
NIL
NIL
1
BUTTON
655
534
745
567
reset axes
reset-axes
NIL
1
T
OBSERVER
NIL
NIL
NIL
NIL
0
OUTPUT
995
37
1316
346
11
BUTTON
1027
378
1179
411
remove equation of #
remove-equation
NIL
1
T
OBSERVER
NIL
NIL
NIL
NIL
0
INPUTBOX
1196
352
1270
412
equation#
1.0
1
0
Number
TEXTBOX
774
377
924
405
Show coordinates of cursor\n(On mousedown)
11
0.0
1
SWITCH
772
447
946
480
show-closest-graph?
show-closest-graph?
1
1
-1000
SWITCH
772
410
946
443
show-coordinates?
show-coordinates?
0
1
-1000
BUTTON
776
535
939
568
show
show-coordinates
T
1
T
OBSERVER
NIL
NIL
NIL
NIL
0
SLIDER
772
496
969
529
coordinate-precision
coordinate-precision
0
10
2.0
1
1
dec. places
HORIZONTAL
@#$#@#$#@
## THIS IS A PART OF THE LARGER PROJECT. IT IS ***INCOMPLETE***
## THIS FIELD IS FOR HOMEWORK PURPOSES ONLY
1. reporter that accepts parameters
1.1 used for reporter `label-axes`
2. `run` command combined with *anonymous command*
2.1 used for drawing the intervals on the axes
2.2 does it suggest the possibility of a custom function input by user to graph?
## WHAT IS IT?
(a general understanding of what the model is trying to show or explain)
## HOW IT WORKS
(what rules the agents use to create the overall behavior of the model)
## HOW TO USE IT
(how to use the model, including a description of each of the items in the Interface tab)
## THINGS TO NOTICE
(suggested things for the user to notice while running the model)
## THINGS TO TRY
(suggested things for the user to try to do (move sliders, switches, etc.) with the model)
## EXTENDING THE MODEL
(suggested things to add or change in the Code tab to make the model more complicated, detailed, accurate, etc.)
## NETLOGO FEATURES
(interesting or unusual features of NetLogo that the model uses, particularly in the Code tab; or where workarounds were needed for missing features)
## RELATED MODELS
(models in the NetLogo Models Library and elsewhere which are of related interest)
## CREDITS AND REFERENCES
(a reference to the model's URL on the web if it has one, as well as any other necessary credits, citations, and links)
@#$#@#$#@
default
true
0
Polygon -7500403 true true 150 5 40 250 150 205 260 250
airplane
true
0
Polygon -7500403 true true 150 0 135 15 120 60 120 105 15 165 15 195 120 180 135 240 105 270 120 285 150 270 180 285 210 270 165 240 180 180 285 195 285 165 180 105 180 60 165 15
arrow
true
0
Polygon -7500403 true true 150 0 0 150 105 150 105 293 195 293 195 150 300 150
box
false
0
Polygon -7500403 true true 150 285 285 225 285 75 150 135
Polygon -7500403 true true 150 135 15 75 150 15 285 75
Polygon -7500403 true true 15 75 15 225 150 285 150 135
Line -16777216 false 150 285 150 135
Line -16777216 false 150 135 15 75
Line -16777216 false 150 135 285 75
bug
true
0
Circle -7500403 true true 96 182 108
Circle -7500403 true true 110 127 80
Circle -7500403 true true 110 75 80
Line -7500403 true 150 100 80 30
Line -7500403 true 150 100 220 30
butterfly
true
0
Polygon -7500403 true true 150 165 209 199 225 225 225 255 195 270 165 255 150 240
Polygon -7500403 true true 150 165 89 198 75 225 75 255 105 270 135 255 150 240
Polygon -7500403 true true 139 148 100 105 55 90 25 90 10 105 10 135 25 180 40 195 85 194 139 163
Polygon -7500403 true true 162 150 200 105 245 90 275 90 290 105 290 135 275 180 260 195 215 195 162 165
Polygon -16777216 true false 150 255 135 225 120 150 135 120 150 105 165 120 180 150 165 225
Circle -16777216 true false 135 90 30
Line -16777216 false 150 105 195 60
Line -16777216 false 150 105 105 60
car
false
0
Polygon -7500403 true true 300 180 279 164 261 144 240 135 226 132 213 106 203 84 185 63 159 50 135 50 75 60 0 150 0 165 0 225 300 225 300 180
Circle -16777216 true false 180 180 90
Circle -16777216 true false 30 180 90
Polygon -16777216 true false 162 80 132 78 134 135 209 135 194 105 189 96 180 89
Circle -7500403 true true 47 195 58
Circle -7500403 true true 195 195 58
circle
false
0
Circle -7500403 true true 0 0 300
circle 2
false
0
Circle -7500403 true true 0 0 300
Circle -16777216 true false 30 30 240
cow
false
0
Polygon -7500403 true true 200 193 197 249 179 249 177 196 166 187 140 189 93 191 78 179 72 211 49 209 48 181 37 149 25 120 25 89 45 72 103 84 179 75 198 76 252 64 272 81 293 103 285 121 255 121 242 118 224 167
Polygon -7500403 true true 73 210 86 251 62 249 48 208