Commit ceee01b4 authored by Liam M. Healy's avatar Liam M. Healy
Browse files

Fix #'right-angle

The function right-angle will find an arbitrary vector in the plane
perpendicular to a given vector. It would possibly return the zero
vector. This version is shorter and should always produce a non-zero
vector.
parent a72ecaf6
Loading
Loading
Loading
Loading
+12 −16
Original line number Diff line number Diff line
;; Functions on cartesian space
;; Liam Healy 2010-12-26 20:58:14EST cartesian.lisp
;; Time-stamp: <2012-06-03 22:34:26EDT cartesian.lisp>
;; Time-stamp: <2013-10-22 18:18:45EDT cartesian.lisp>

;; Copyright 2011, 2012 Liam M. Healy
;; Copyright 2011, 2012, 2013 Liam M. Healy
;; Distributed under the terms of the GNU General Public License
;;
;; This program is free software: you can redistribute it and/or modify
@@ -32,20 +32,16 @@

(defun right-angle (vector)
  "Find an arbitrary right angle to the vector."
  (let* ((vec (pqval vector))
	 (bigind
	   ;; Find which component is the largest
	   (iterate (for i vector-element-index vector)
	     (finding i maximizing (abs (grid:aref vec i)))))
	 (project (grid:copy-to vec))
	 (return (grid:copy-to vec)))
    ;; Find vector projected into the plane perpendicular
    (setf (grid:aref project bigind) 0.0
	  ;; The return vector is the original vector with a different
	  ;; value in what was the largest component.
	  (grid:aref return bigind)
	  (cl:- (cl:/ (grid:inner project project) (grid:aref vec bigind))))
    (grid:normalize return)))
  ;; See [[id:7154d151-388f-48d9-b1d1-ca811fc04222][Perpendicular vector]]
  ;; See http://www.gamedev.net/topic/572770-finding-vector-perpendicular-to-line-in-3d/
  ;; Take the cross product with the coordinate unit vector of the smallest component.
  (let ((vec (pqval vector)))
    (grid:normalize
     (grid:cross
      vec
      (coordinate-unit-vector
       (iterate (for i vector-element-index vec)
	 (finding i minimizing (abs (grid:aref vec i)))))))))

(defun coplanar (vect1-or-matrix &optional vect2 vect3)
  "The cosine of the angle between vect1 and the cross product of vect2 and vect3.