<div dir="ltr"><br><div>I've posted about Quadrays on edu-sig before.... going back and forth between two languages may be a good way to learn both, or to learn one using one's knowledge of the other.  In this case, I'm developing the same Quadray class in Python and Clojure.</div><div><br></div><div>I'll followup with a link to the Clojure version.  I'm just beginning with that language and I'm hoping the experts pick it apart some, as I'm sure it's not as Pythonic as it could be. :-D</div><div><br></div><div>... actually three languages if you find the links to the C++ version.</div><div><br></div><div>Kirby</div><div><br></div><div><br></div><div><div><br></div><div>"""</div><div>(cl) K. Urner, MIT License 2015</div><div>Python -> Java -> Clojure curriculum</div><div>2D + 3D Graphics:  Martian Math</div><div>Topic:  Quadrays</div><div><a href="http://www.grunch.net/synergetics/quadintro.html">http://www.grunch.net/synergetics/quadintro.html</a></div><div><a href="https://en.wikipedia.org/wiki/Quadray_coordinates">https://en.wikipedia.org/wiki/Quadray_coordinates</a></div><div><br></div><div>Asynchronous Learning Engine (Open Source project)</div><div><a href="http://controlroom.blogspot.com/2015/08/asynchronous-learning-engine-ale.html">http://controlroom.blogspot.com/2015/08/asynchronous-learning-engine-ale.html</a></div><div>"""</div><div><br></div><div>from math import sqrt</div><div><br></div><div>class Quadray:</div><div><br></div><div>    def __init__(self, OA, OB, OC, OD):</div><div>        self.OA = OA</div><div>        self.OB = OB</div><div>        self.OC = OC</div><div>        self.OD = OD</div><div>        self._norm()</div><div><br></div><div>    def _norm(self):</div><div>        """</div><div>        Quadrays have at least one element = 0 as one direction is inactive per quadrant</div><div>        """</div><div>        the_min = min(self.OA, self.OB, self.OC, self.OD)</div><div>        self.OA = self.OA - the_min</div><div>        self.OB = self.OB - the_min</div><div>        self.OC = self.OC - the_min</div><div>        self.OD = self.OD - the_min</div><div><br></div><div>    def __neg__(self):</div><div>        return Quadray(-self.OA, -self.OB, -self.OC, -self.OD)</div><div><br></div><div>    def __add__(self, other):</div><div>        return Quadray(self.OA + other.OA, self.OB + other.OB,</div><div>                        self.OC + other.OC, self.OD + other.OD)</div><div><br></div><div>    def __sub__(self, other):</div><div>        return self + (-other)</div><div><br></div><div>    def len(self):</div><div>        """</div><div>        uses Tom Ace normalization such that the sum of the elements is 0.</div><div>        Simplifies the distance formula, which is set at Edge of Unit Tetra = 2</div><div>        i.e. D(1,0,0,0) == math.sqrt(6)/2</div><div>        Tom Ace: <a href="http://www.minortriad.com/index.html">http://www.minortriad.com/index.html</a></div><div>        """</div><div>        k = sum([self.OA, self.OB, self.OC, self.OD]) / 4.0</div><div>        t0, t1, t2, t3 = self.OA - k, self.OB - k, self.OC - k, self.OD - k</div><div>        return sqrt(2) * sqrt(sum([t0**2, t1**2, t2**2, t3**2]))</div><div><br></div><div>    def __repr__(self):</div><div>        return 'Quadray(%s, %s, %s, %s)' % (self.OA, self.OB, self.OC, self.OD)</div><div><br></div><div>v0 = Quadray(1,0,0,0)</div><div>v1 = Quadray(0,1,0,0)</div><div>print(v0 - v1)</div><div>print(v0 + v1)</div><div>print(-v1)</div><div>print(v1.len())  # math.sqrt(6)/2</div><div>print(-v1.len() + v1.len())</div><div>print((v1-v1).len())</div><div><br></div><div><br></div></div></div>