-
Notifications
You must be signed in to change notification settings - Fork 0
/
draw-svg.scm
184 lines (151 loc) · 4.52 KB
/
draw-svg.scm
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
(define xml-write-attribute
(lambda (name-value)
(let ((name (car name-value))
(value (cadr name-value)))
(display " ")
(display name)
(display "=\"")
(display value)
(display "\""))))
(define xml-write-open-tag
(lambda (name attr)
(display "<")
(display name)
(for-each xml-write-attribute attr)
(display ">")
(newline)))
(define xml-write-close-tag
(lambda (name)
(display "</")
(display name)
(display ">")
(newline)))
;; Retorna os atributos como uma lista de associação
;; e a sublista onde começa o texto dentro da tag.
(define get-attr-list/start-data
(lambda (lst)
(let next ((lst lst)
(attr '()))
(if (or (null? lst)
(not (symbol? (car lst))))
(list (reverse attr) lst)
(next (cddr lst)
(cons (list (car lst)
(cadr lst))
attr))))))
;; Traduz a parte interna do XML e a escreve
(define xml-write-data
(lambda (lst)
(if (not (null? lst))
(let ((element (car lst)))
(if (string? element)
(display element)
(xml-write-tag element))
(xml-write-data (cdr lst))))))
;; Recebe uma tag XML, traduz a tag e a escreve
(define xml-write-tag
(lambda (tag-object)
(let ((tag-name (car tag-object))
(attr/start-data (get-attr-list/start-data
(cdr tag-object))))
(let ((attr (car attr/start-data))
(start-data (cadr attr/start-data)))
(xml-write-open-tag tag-name attr)
(xml-write-data start-data))
(xml-write-close-tag tag-name))))
(define number->string/maybe
(lambda (x)
(if (number? x)
(number->string x)
x)))
(define make-attr-list
(lambda args
(map number->string/maybe args)))
(define make-svg-line
(lambda (x1 y1 x2 y2 stroke fill stroke-width)
(cons 'line
(make-attr-list 'x1 x1
'y1 y1
'x2 x2
'y2 y2
'fill fill
'stroke stroke
'stroke-width stroke-width))))
(define list-of-points
(lambda args
(let next-point ((points (map number->string args))
(result ""))
(if (null? points)
result
(next-point (cddr points)
(string-append result
(car points) ","
(cadr points) " "))))))
(define make-svg-triangle
(lambda (x1 y1
x2 y2
x3 y3
stroke fill stroke-width)
(cons 'polygon
(make-attr-list 'fill fill
'stroke stroke
'stroke-width stroke-width
'points (list-of-points x1 y1 x2 y2 x3 y3)))))
(define svg-preamble
"<?xml version=\"1.0\" standalone=\"no\"?>
<!DOCTYPE svg PUBLIC
\"-//W3C//DTD SVG 1.1//EN\"
\"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">")
(define make-svg-image
(lambda (objects)
(append (list 'svg
'version 1.1
'xmlns "http://www.w3.org/2000/svg")
objects)))
(define images->xml
(lambda (img file)
(with-output-to-file file
(lambda ()
(display svg-preamble)
(newline)
(xml-write-tag img)))))
;;;;;;
(define triangle?
(lambda (obj)
(eq? (car obj) 'triangle)))
(define make-triangle/sides
(lambda (a b c)
(list 'triangle a b c)))
(define triangle-side
(lambda (tri idx)
(list-ref tri (+ idx 1))))
(define square
(lambda (x)
(* x x)))
(define triangle-compute-angle
(lambda (a b c)
(let ((numerator (+ (square b)
(square c)
(- (square a))))
(denominator (* 2 b c)))
(acos (/ numerator denominator)))))
(define triangle->svg
(lambda (tri x y stroke fill stroke-width)
(let ((a (triangle-side tri 0))
(b (triangle-side tri 1))
(c (triangle-side tri 2)))
(let ((angle (triangle-compute-angle b a c)))
(let ((x2 (+ x a))
(y2 y)
(x3 (* (cos angle) b))
(y3 (* (sin angle) b)))
(make-svg-triangle x y
x2 y2
x3 y3
stroke fill stroke-width))))))
(define make-triangle/angles
(lambda (alpha beta gamma)
...))
(define triangle-angle
(lambda (tri idx)
...))