-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphtest.rkt
253 lines (221 loc) · 6.7 KB
/
graphtest.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
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
#lang scheme/base
; call from the terminal:
; !racket %:p
(require racket/class)
(require racket/draw)
(require racket/gui)
(require racket/math)
(define no-pen (new pen% [style 'transparent]))
(define no-brush (new brush% [style 'transparent]))
; (define blue-brush (new brush% [stipple (read-bitmap "water.png")]))
(define blue-brush (new brush% [color "blue"]))
(define yellow-brush (new brush% [color "yellow"]))
(define red-pen (new pen% [color "brown"] [width 2]))
(define (draw-result dc knapsack-width knapsack-height items-lst max-weight)
; number of different colors for items of different cost
(define cost-categories 5)
(define knapsack-x 445)
(define knapsack-y 175)
(define max-cost
(apply
max
(map (lambda (x) (cdr x)) items-lst)
)
)
(define min-cost
(apply
min
(map (lambda (x) (cdr x)) items-lst)
)
)
(define total-weight
(lst-sum (map (lambda (x) (car x)) items-lst))
)
(define total-cost
(lst-sum (map (lambda (x) (cdr x)) items-lst))
)
; step between cost categories
(define seg (/ (- max-cost min-cost) cost-categories))
(define cost-categories-lst
(list
(cons (+ min-cost (* 1 seg)) (new brush% [color "gray"]))
(cons (+ min-cost (* 2 seg)) (new brush% [color "blue"]))
(cons (+ min-cost (* 3 seg)) (new brush% [color "green"]))
(cons (+ min-cost (* 4 seg)) (new brush% [color "orange"]))
(cons (+ min-cost (* 5 seg)) (new brush% [color "yellow"]))
)
)
; knapsack volume path
(define knapsack-path
(let ([p (new dc-path%)])
; (send p append left-lambda-path)
(send p move-to 0 0)
(send p line-to 0 knapsack-height)
(send p line-to knapsack-width knapsack-height)
(send p line-to knapsack-width 0)
(send p move-to 0 0)
p
)
)
; put an item (weight . cost) to the knapsack
(define (put-item i)
(let*
(
(item-height (quot-int knapsack-height (/ (car i) max-weight)))
)
(send dc set-brush (get-item-color (cdr i)))
(send dc translate 0 (- item-height))
(send dc draw-rectangle 0 0 knapsack-width item-height)
)
)
; determine color of item, depending on its cost
(define (get-item-color cost)
(cond
((< cost (car (list-ref cost-categories-lst 0))) (cdr (list-ref cost-categories-lst 0)))
((< cost (car (list-ref cost-categories-lst 1))) (cdr (list-ref cost-categories-lst 1)))
((< cost (car (list-ref cost-categories-lst 2))) (cdr (list-ref cost-categories-lst 2)))
((< cost (car (list-ref cost-categories-lst 3))) (cdr (list-ref cost-categories-lst 3)))
((<= cost (car (list-ref cost-categories-lst 4))) (cdr (list-ref cost-categories-lst 4)))
(else (cdr (list-ref cost-categories-lst 4)))
)
)
(define title
(let
(
(p (new dc-path%))
(font (make-font #:size 24 #:family 'swiss #:weight 'bold))
(str (string-append "Knapsack filled " (number->string (/ total-weight max-weight))))
)
(send p text-outline font str 0 0)
p
)
)
(define total-cost-path
(let*
(
(p (new dc-path%))
(font (make-font #:size 20 #:family 'swiss #:weight 'bold))
(str (string-append "Total cost: " (number->string total-cost)))
)
(send p text-outline font str 0 0)
p
)
)
(define (draw-mapping brush start end)
(let
(
(font (make-font #:size 12 #:family 'swiss #:weight 'bold))
(str (string-append "cost in [" (number->string start) ";" (number->string end) "]"))
)
(send dc set-brush brush)
(send dc draw-rectangle 0 0 30 30)
(send dc set-font font)
(send dc draw-text str 40 2)
(send dc translate 0 (- 50))
)
)
(define (draw-mapping-cycle lst n)
(if (= (length lst) 1)
2
(let*
(
(brush (cdadr lst))
(end (caadr lst))
(start (caar lst))
)
(draw-mapping brush start end)
(draw-mapping-cycle (cdr lst) n)
)
)
)
(let
(
(items-lst-sorted (sort items-lst (lambda (x y) (> (car x) (car y)))))
(no-transformation (send dc get-transformation))
)
(send dc set-smoothing 'smoothed)
(send dc translate knapsack-x knapsack-y)
; draw knapsack volume
(send dc set-pen "black" 2 'long-dash)
(send dc set-brush "white" 'transparent)
(send dc draw-path knapsack-path)
; put items
(send dc set-pen "black" 1 'solid)
(send dc translate 0 knapsack-height)
(foreach items-lst-sorted put-item)
(send dc set-transformation no-transformation)
; (foreach items-lst-sorted displayln)
; (displayln cost-categories-lst)
; draw title
(send dc translate 180 50)
(send dc set-brush "black" 'solid)
(send dc draw-path title)
(send dc set-transformation no-transformation)
; draw mapping
(send dc translate 50 500)
; draw initual segment
(draw-mapping (cdr (list-ref cost-categories-lst 0)) min-cost (car (list-ref cost-categories-lst 0)))
; draw other segments
(draw-mapping-cycle cost-categories-lst 1)
(send dc set-transformation no-transformation)
;draw total cost
(send dc translate 50 200)
(send dc set-brush "black" 'solid)
(send dc draw-path total-cost-path)
(send dc set-transformation no-transformation)
)
)
(define (draw-face dc)
(send dc set-smoothing 'aligned)
(send dc set-pen no-pen)
(send dc set-brush blue-brush)
(send dc draw-ellipse 25 25 100 100)
(send dc set-brush yellow-brush)
(send dc draw-rectangle 50 50 10 10)
(send dc draw-rectangle 90 50 10 10)
(send dc set-brush no-brush)
(send dc set-pen red-pen)
(send dc draw-arc 37 37 75 75 (* 5/4 pi) (* 7/4 pi))
)
(define (quot-int number q)
(inexact->exact (ceiling (* number q)))
)
(define (foreach lst f)
(if (null? lst)
2
(begin
(f (car lst))
(foreach (cdr lst) f)
)
)
)
;_______________________
(define (zip lst1 lst2)
(map cons lst1 lst2)
)
(define (lst-sum lst)
(foldl + 0 lst)
)
(let*
(
(width 695)
(heigth 637)
(width-center (/ width 2))
(heigth-center (/ heigth 2))
(target (make-bitmap width heigth))
(usls (send target load-file "knapsack.jpg"))
(dc (new bitmap-dc% [bitmap target]))
(no-transformation (send dc get-transformation))
; (knapsack-width (quot-int width 0.4))
; (knapsack-height (quot-int heigth 0.5))
(knapsack-width 120)
(knapsack-height 310)
(max-weight 15)
(items-lst (zip '(2 4 5 1 1 1) '(10 15 20 40 60 70)))
(usls (draw-result dc knapsack-width knapsack-height items-lst max-weight))
(usls (send dc set-smoothing 'smoothed))
; (usls (draw-face dc))
)
(send target save-file "box.png" 'png)
;(make-object image-snip% target)
)