summaryrefslogtreecommitdiff
path: root/turtle.lisp
blob: 3d597875f26483ccd5e70c9a11f4a31dcbc869d2 (plain)
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
;;;; turtle.lisp
;;;; Copyright (C) 2017 Bruno Cichon <ebrasca.ebrasca@openmailbox.org>
;;;; This code is licensed under the GPLv3 license.

(in-package #:turtle-graphics)

;;; "turtle-graphics" goes here. Hacks and glory await!

(defparameter *i* -1)
(defparameter *vertices* nil)
(defparameter *index* nil)

(defclass turtle ()
  ((points :accessor points
           :initform nil)
   (translation :accessor tra
                :initform (v! 0.0 0.0 0.0 0.0))
   (rotation :accessor rot
             :initform (m4:identity))
   (radius :accessor r
           :initform 0.0)
   (pile :accessor pile
         :initform '())))

;;; Extrusion
(defun translation (turtle n)
  (setf (tra turtle)
        (v4:+ (v4:*S (m4:get-column (rot turtle) 1) n)
              (tra turtle))))

(defun extrusion (turtle n)
  (mapcar #'(lambda (vertex)
              (add-point
               (let* ((x (v4:- (aref *vertices* vertex) (tra turtle))))
                 (v4:+ (tra turtle)
                       (v4:*S (m4:get-column (rot turtle) 1) n)
                       (m4:*v (rot turtle)
                              (v4:- x
                                    (v4:*S x (* (r turtle)
                                                (sqrt (v:dot x x))))))))))
          (points turtle)))

(defun forward (turtle n)
  "Move turtle n units forward and draw"
  (let ((new-points (extrusion turtle n)))
    (dolist (item (triangulate (points turtle) new-points))
      (push item
            *index*))
    (setf (points turtle)
          new-points
          (r turtle)
          0.0)
    (translation turtle n)))

(defun jump (turtle n)
  "Move turtle n units forward"
  (let ((new-points (extrusion turtle n)))
    (dolist (item new-points)
      (push item
            *index*))
    (setf (points turtle)
          new-points
          (r turtle)
          0.0)
    (translation turtle n)))

;;; Rotation
(defun roll (turtle u)
  "Rotate in axis j by u angle"
  (let ((tmp (m4:get-column (m4:identity) 1)))
    (setf (rot turtle)
          (m4:* (m4:rotation-from-axis-angle (v! (aref tmp 0)
                                                 (aref tmp 1)
                                                 (aref tmp 2))
                                             u)
                (rot turtle)))))

(defun pitch (turtle v)
  "Rotate in axis k by v angle"
  (let ((tmp (m4:get-column (m4:identity) 2)))
    (setf (rot turtle)
          (m4:* (m4:rotation-from-axis-angle (v! (aref tmp 0)
                                                 (aref tmp 1)
                                                 (aref tmp 2))
                                             v)
                (rot turtle)))))

(defun yaw (turtle w)
  "Rotate in axis i by w angle"
  (let ((tmp (m4:get-column (m4:identity) 0)))
    (setf (rot turtle)
          (m4:* (m4:rotation-from-axis-angle (v! (aref tmp 0)
                                                 (aref tmp 1)
                                                 (aref tmp 2))
                                             w)
                (rot turtle)))))

;;; Other
(defun push-turtle (turtle)
  "Remember the current state of turtle"
  (push (list (copy-list (points turtle))
              (copy-seq (rot turtle))
              (copy-seq (tra turtle))
              (r turtle))
        (pile turtle)))

(defun pop-turtle (turtle)
  "Restore the last remembered state of turtle and remove it from the list of remembered states"
  (let ((pile (pop (pile turtle))))
    (setf (points turtle) (first pile)
          (rot turtle) (second pile)
          (tra turtle) (third pile)
          (r turtle) (fourth pile))))

(defun set-radius (turtle r)
  "Set how radius need to change."
  (setf (r turtle)
        (- 1 r)))

;;; Geometry
(defun add-point (point)
  (incf *i*)
  
  (when (= *i* (car (array-dimensions *vertices*)))
    (adjust-array *vertices* (list (* 2 (car (array-dimensions *vertices*))))))
  
  (setf (aref *vertices* *i*) point)
  *i*)

(defun circle (turtle n r)
  (iter (with angle := (/ (* 2 3.1415927) n))
        (for i :from 0 :to n)
        (for res := (cons (add-point (m4:*v (m4:rotation-y (* i angle))
                                            (v! r 0.0 0.0)))
                          res))
        (finally (setf (points turtle)
                       (cons 0 res)))))

(defun triangulate (index0 index1)
  "2__3
   | /|
   |/ |
   0__1"
  (iter (with p0 = (first index0))
        (with p2 = (first index1))
        (for p1 in (cdr index0))
        (for p3 in (cdr index1))
        (nconcing (list p0 p1 p3 p3 p2 p0))
        (setf p0 p1
              p2 p3)))

(defun make-geometry (l-system)
  (let ((*i* -1)
        (*vertices* (make-array '(10) :adjustable t))
        (*index* '())
        (turtle (make-instance 'turtle)))
    (iter (for (symbol . parameter) :in l-system)
          (if parameter
              (apply symbol turtle parameter)
              (funcall symbol turtle)))
    (values *vertices*
            *index*)))