-
Notifications
You must be signed in to change notification settings - Fork 6
/
ex-192.rkt
112 lines (87 loc) · 1.86 KB
/
ex-192.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
#lang htdp/bsl+
(require 2htdp/image)
(require 2htdp/universe)
(require test-engine/racket-tests)
; CONSTANTS
(define BACKGROUND-WIDTH 300)
(define BACKGROUND-HEIGHT 200)
(define BACKGROUND (empty-scene BACKGROUND-WIDTH BACKGROUND-HEIGHT))
; DATA DEFINITIONS
; Polygon is either:
; - (list Posn Posn Posn)
; - (cons Posn Polygon)
(define triangle-p
(list
(make-posn 20 10)
(make-posn 20 20)
(make-posn 30 20)
))
(define square-p
(list
(make-posn 10 10)
(make-posn 20 10)
(make-posn 20 20)
(make-posn 10 20)
))
; FUNCTIONS
; Polygon -> Image
(define (render-world ws)
(render-poly BACKGROUND ws)
)
; Image Polygon -> Image
(check-expect
(render-poly BACKGROUND triangle-p)
(scene+line
(scene+line
(scene+line BACKGROUND 20 10 20 20 "red")
20 20 30 20 "red")
30 20 20 10 "red")
)
(define (render-poly img poly)
(render-line
(connect-dots img poly)
(first poly)
(last poly)
))
; Image NELP -> Image
; Connects a list of points on top of an image
(define (connect-dots img nelp)
(cond
[(empty? (rest nelp)) img]
[else
(render-line
(connect-dots img (rest nelp))
(first nelp)
(second nelp)
)]))
; Image Posn Posn -> Image
; renders a line from p to q into img
(define (render-line img p q)
(scene+line
img
(posn-x p) (posn-y p) (posn-x q) (posn-y q)
"red"
))
; Non-empty-list-of-anything -> Anything
; Returns the last element of a non empty list
(check-expect (last (list 1 2 3)) 3)
(check-expect (last (list 1)) 1)
(define (last loa)
(cond
[(empty? (rest loa)) (first loa)]
[else (last (rest loa))]
))
(define (main ws)
(big-bang
ws
[to-draw render-world]
))
; TEST & MAIN CALL
(test)
(main
(list
(make-posn 50 50)
(make-posn 50 100)
(make-posn 100 100)
(make-posn 100 50)
))