-
Notifications
You must be signed in to change notification settings - Fork 0
/
declarations.rkt
executable file
·217 lines (178 loc) · 6.56 KB
/
declarations.rkt
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
#lang racket
(require (prefix-in srfi: srfi/48))
(provide (struct-out particle) (struct-out gnode) (struct-out bbox) (struct-out vec)
theta iter g timeslice drawtime zipwith singleton sum concat lc bounding-box
display-forces display-particles display-bbox display-tree)
(define format-string "~4,3F") ;; This is what decides the output format of numbers
(define (print-formatted num) ;; This is what prints floating point numbers
(display (srfi:format format-string num)))
; A particle is a struct with a record of its mass, its location, and
; its velocity:
(struct particle (mass posn velocity) #:transparent)
;The struct representing the tree
(struct gnode (mass posn subtrees) #:transparent)
;Struct for the bounding box. Specifies the box by giving lower left x
;and y axis and upper right x and y axis values.
(struct bbox (llx lly rux ruy) #:transparent)
; struct representing the vector of a particle in 2D space.
(struct vec (x y) #:transparent)
;Global declarations
(define theta 2) ;decides which particles are deemed close to one another
(define iter 1000) ;decides number of iterations to run
(define g 18) ;gravitational constant in UniverseX
(define timeslice .1) ;the simulation is updated after unit time
(define drawtime 10) ;Draw every drawtime iterations
(define (zipwith f l1 l2)
(cond [(or (null? l1) (null? l2)) '()] ;
[else (cons (f (car l1) (car l2)) (zipwith f (cdr l1) (cdr l2)))]))
(define (singleton l) ( = (length l) 1))
(define (sum l) (foldl + 0 l))
(define (concat l) (foldr append `() l))
(define (bounding-box particles)
(define (fn-upper nextp acc) (max (vec-x nextp) (vec-y nextp) acc))
(define (fn-lower nextp acc) (min (vec-x nextp) (vec-y nextp) acc))
(let ([ur (+ 1 (foldr fn-upper -50000000 (map particle-posn particles)))]
[ll (- (foldr fn-lower 50000000 (map particle-posn particles)) 1)])
(bbox ll ll ur ur )))
(define-syntax lc
(syntax-rules (: <- @)
[(lc expr : var <- drawn-from) (map (lambda (var) expr) drawn-from)]
[(lc expr : @ guard) (if guard (list expr) `())]
[(lc expr : @ guard qualifier ...)
(append* (lc (lc expr : qualifier ...) : @ guard))]
[(lc expr : var <- drawn-from qualifier ...)
(append* (lc (lc expr : qualifier ... ) : var <- drawn-from))]))
(define (display-vec t)
(display "(")
(print-formatted (vec-x t))
(display ", ")
(print-formatted (vec-y t))
(display ")"))
(define (display-particle p)
(begin
(display "(particle: ")
(print-formatted (particle-mass p))
(display ", ")
(display-vec (particle-posn p))
(display ", ")
(display-vec (particle-velocity p))
(display ")")))
(define (display-particles ps)
(cond [(not (null? ps))
(begin
(display "[")
(display-particles-helper ps 1)
(display "]"))]))
(define (display-particles-helper ps indent)
(cond [(not (null? ps)) (begin
(printblanks indent)
(display-particle (car ps))
(newline)
(display-particles-helper (cdr ps) 2)
)]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (display-force p)
(begin
(display "(force: ")
(display-vec p)
(display ")")))
(define (display-forces ps)
(cond [(not (null? ps))
(begin
(display "[")
(display-forces-helper ps 1)
(display "]"))]))
(define (display-forces-helper ps indent)
(cond [(not (null? ps)) (begin
(printblanks indent)
(display-force (car ps))
(newline)
(display-forces-helper (cdr ps) 2)
)]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Each entity is placed at the begining of a line and given a certain
; indent and at the end of printing leaves the state at the begining
; of a line
(define (display-tree t)
(display-tree-helper t 0))
(define (display-tree-helper t indent)
(cond [(null? (gnode-subtrees t))
(begin
(display "(leaf ")
(print-formatted (gnode-mass t))
(display " ")
(display-vec (gnode-posn t))
(display ")"))]
[else (display "(gnode ")
(print-formatted (gnode-mass t))
(display " ")
(display-vec (gnode-posn t))
(newline)
(printblanks (+ indent 2))
(display "[")
(display-list (gnode-subtrees t) (+ indent 3))
(display "])")
]))
;
(define (display-list lt indent)
(begin
(cond [(not (null? lt))
(begin
(display-tree-helper (car lt) indent)
(cond [(not (null? (cdr lt)))
(begin
(newline)
(printblanks indent)
(display-list (cdr lt) indent))]))])))
(define (display-bbox bb)
(begin
(display "(bbox: ")
(print-formatted (bbox-llx bb))
(display " ")
(print-formatted (bbox-lly bb))
(display " ")
(print-formatted (bbox-rux bb))
(display " ")
(print-formatted (bbox-ruy bb))
(display ")")
(newline)))
(define (printblanks indent)
(cond[(not (= indent 0))
(begin
(display " ")
(printblanks (- indent 1)))]))
;(define indent 0)
;(define (display-tree t)
; (display-tree-helper t))
;
;(define (display-tree-helper t)
; (cond [(null? (gnode-subtrees t))
; (begin
; (display "(leaf ")
; (print-formatted (gnode-mass t))
; (display " ")
; (display-vec (gnode-posn t))
; (display ")"))]
; [else (display "(gnode ")
; (print-formatted (gnode-mass t))
; (display " ")
; (display-vec (gnode-posn t))
; (newline)
; (printblanks (+ indent 2))
; (display "[")
; (set! indent (+ indent 3))
; (display-list (gnode-subtrees t))
; (set! indent (- indent 3))
; (display "])")
;
; ]))
;(define (display-list lt)
; (begin
; (cond [(not (null? lt))
; (begin ;(printblanks indent)
; (display-tree-helper (car lt))
; (cond [(not (null? (cdr lt)))
; (begin
; (newline)
; (printblanks indent)
; (display-list (cdr lt)))]))])))