Added SpaceNode
Added SpaceNode

file:b/SpaceNode.php (new)
--- /dev/null
+++ b/SpaceNode.php
@@ -1,1 +1,143 @@
-
+<?php

+	class SpaceNode {

+		public function __construct($name, $radius, $coords, $ultra = null) {

+			$this->name = $name;

+   		    $this->mean_radius = $radius;

+		    $this->relative_coords = $coords;

+		    if (is_object($ultra)) {

+		        //add the ultra node as a parent

+				$this->ultra = $ultra;

+		    } else {

+		        // If we don't have an ultra node, we should belong to the universe.

+		        if (is_object($GLOBALS["universe"])) {

+		            $this->ultra = $GLOBALS["universe"];

+		        // If there is no universe, WE ARE THE UNIVERSE.

+		        } else {

+		            $GLOBALS["universe"] = $this;

+		        }

+		    }

+		    

+		    $this->id = uniqid();

+		    

+			return $this;

+		}

+		

+		public change_ultra($ultra) {

+		    // remove from the current parent

+		    if ($this->ultra->remove_infra($this)) {

+		        // add to the new parent

+		        if ($ultra->add_infra($this)) {

+		            // change the parent

+		            $this->ultra = $ultra;

+		        }

+		    }

+		}

+		

+		public add_infra($infra) {

+		    if (empty($this->child_list[$infra->id()])) {

+		        $this->child_list[$infra->id()] = $infra;

+		    } else {

+		        throw new Exception("Tried to add duplicate child object '" . $infra->name() . "'to '" . $this->name . "'");

+		    }

+		}

+		

+		public remove_infra($infra) {

+		    if (!empty($this->child_list[$infra->id()])) {

+		        unset($this->child_list[$infra->id()]);

+		    } else {

+		        throw new Exception("Tried to remove non-child object '" . $infra->name(). "' from '" . $this->name . "'");

+		    }

+		}

+		

+		protected $name = "";

+		protected $id = "";

+		protected $child_list = array();

+		protected $super = null;

+		protected $mean_radius = 0;

+		protected $relative_coords = null;

+	}

+	

+	abstract class Coords {

+	    // This is a stub that may contain functions or parameters that apply universally to all coordinate systems,

+	    // but may never be instantiated itself.

+	    

+	}

+	

+	class SphericalCoords extends Coords {

+		public function __construct($r, $t, $p) {

+			$this->radius = $r;

+			$this->theta = $t;

+			$this->phi = $p;

+			

+			return $this;

+		}

+		

+		public function triplet() {

+			return array(

+				$this->radius,

+				$this->theta,

+				$this->phi

+				);

+		}

+		

+		public function to_Cartesian() {

+		    return new CartesianCoords(

+		    	$this->radius * sin($this->theta) * cos($this->phi),

+		    	$this->radius * sin($this->theta) * sin($this->phi),

+		    	$this->radius * cos($this->theta)

+		    );

+		}

+		

+		protected $radius = 0;

+		protected $theta  = 0;

+		protected $phi = 0;

+	}

+	

+	class CartesianCoords extends Coords {

+	    public function __construct($x, $y, $z) {

+	        $this->x = $x;

+	        $this->y = $y;

+	        $this->z = $z;

+	        

+	        return $this;

+	    }

+	    

+	    public function triplet() {

+	        return array(

+	        	$this->x,

+	        	$this->y,

+	        	$this->z

+	        	);

+	    }

+	    

+	    public function to_Spherical() {

+	        $r = pow($this->x,2) * pow($this->y,2) * pow($this->z,2);

+	        $t = acos($this->z / $r);

+	        $p = acos($this->y / $this->x);

+	        return new SphericalCoords($r, $t, $p);

+	    }

+	    

+	    protected $x = 0;

+	    protected $y = 0;

+	    protected $z = 0;

+	}

+	

+	class LinearCoords extends CartesianCoords {

+	    public function __construct($d) {

+	        // make a car;tesian object, with just one axis

+	        $this->y = 0;

+	        $this->z = 0;

+	        $this->x = $d;

+	        

+	        return $this;

+	    }

+	    

+	    public funct_d($d) {

+

+		protected $distance = 0;

+	}

+	

+	    //Do we need a 

+	$universe = new SpaceNode("universe node", 50, new LinearCoords(0), null);

+	

+?>