forked from Munksgaard/rbbg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
svg-utils.lisp
69 lines (57 loc) · 2.27 KB
/
svg-utils.lisp
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
(defstruct hexagon
x ; få lavet alias ting
y
(color 'white)
(border-color 'black))
(defun tip-height (side-length)
(sqrt (- (expt side-length 2) (expt (/ side-length 2) 2))))
(defun tip-base (side-length)
(sqrt (- (expt side-length 2) (expt (tip-height side-length) 2))))
(defun vertices-pixel-coords (a-offset side-length)
(let ((h (tip-height side-length))
(j (tip-base side-length)))
(list a-offset
(cons (+ (car a-offset) j) (- (cdr a-offset) h))
(cons (+ (car a-offset) j side-length) (- (cdr a-offset) h))
(cons (+ (car a-offset) (* 2 j) side-length) (cdr a-offset))
(cons (+ (car a-offset) j side-length) (+ (cdr a-offset) h))
(cons (+ (car a-offset) j) (+ (cdr a-offset) h)))))
(defun hexagon-pixel-offset (hexagon side-length)
(let ((h (tip-height side-length))
(j (tip-base side-length)))
(cons (* (+ side-length (- side-length j)) (hexagon-x hexagon))
(+ (* 2 h (hexagon-y hexagon))
(* (1+ (hexagon-x hexagon)) h)))))
(defun vertices->pixelpairs (vertices)
(mapcar #'pair->string vertices))
(defun pair->string (ls)
(format nil "~d,~d" (car ls) (cdr ls)))
(defun list->string (ls)
(reduce #'(lambda (x y) (concatenate 'string x " " y)) ls))
(defun svg-header ()
(princ "<?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\">\
\
<svg width=\"100%\" height=\"100%\" version=\"1.1\"\
xmlns=\"http://www.w3.org/2000/svg\">")
(terpri))
(defun svg-hexagon (hexagon side-length)
(princ "<polygon points=\"")
(princ (list->string (vertices->pixelpairs
(vertices-pixel-coords
(hexagon-pixel-offset hexagon side-length)
side-length))))
(format t "\" style=\"fill:~A; stroke:~A; stroke-width:2\" />"
(hexagon-color hexagon)
(hexagon-border-color hexagon)))
(defun svg-hexagons (hexagons side-length)
(mapc #'(lambda (x) (progn
(svg-hexagon x side-length)
(terpri))) hexagons))
(defun svg-footer ()
(princ "</svg>"))
(defun hexagons->svg (hexs side-length)
(svg-header)
(svg-hexagons hexs side-length)
(svg-footer))