git.haldean.org plotter / b6aeb9b bezier.lisp
b6aeb9b

Tree @b6aeb9b (Download .tar.gz)

bezier.lisp @b6aeb9braw · history · blame

(load "package.lisp")

(in-package :so3-cnc)

; a more useful constant
(setq tau (* 2 pi))

(defun vec+ (v1 v2) (cons (+ (car v1) (car v2)) (+ (cdr v1) (cdr v2))))
(defun vec- (v1 v2) (cons (- (car v1) (car v2)) (- (cdr v1) (cdr v2))))
(defun vec* (s v) (cons (* s (car v)) (* s (cdr v))))
(defun dot (v1 v2) (+ (* (car v1) (car v2)) (* (cdr v1) (cdr v2))))
(defun norm (v) (sqrt (dot v v)))
(defun normalize (v) (vec* (/ 1 (norm v)) v))

(defun eval-bezier (theta pts)
  (if (= 1 (length pts))
      (first pts)
      (eval-bezier theta (loop for x in pts
                               for y in (rest pts)
                               collect (vec+ (vec* (- 1 theta) x) (vec* theta y))))))

(defun eval-bezier-cubic (theta p0 p1 p2 p3)
  (eval-bezier theta (list p0 p1 p2 p3)))
(defun eval-bezier-quadratic (theta p0 p1 p2)
  (eval-bezier theta (list p0 p1 p2)))

(defun angle-between (v0 v1)
  (* (if (< 0 (- (* (car v0) (cdr v1)) (* (cdr v0) (car v1)))) 1.0d0 -1.0d0)
     (acos (/ (dot v0 v1) (* (norm v0) (norm v1))))
     ))