Example #1
0
 /**
  * @param GridInterface   $grid
  * @param UnitComposition $unitComposition
  * @param int|float       $tickLength           This is how much time passes per tick, in seconds.
  *
  * @throws EngineException
  * @throws \Exception
  */
 public function __construct(GridInterface $grid, UnitComposition $unitComposition, $tickLength = 1)
 {
     $this->grid = $grid;
     $this->unitComposition = $unitComposition;
     if ($tickLength instanceof Time) {
         $this->tickLength = $tickLength;
     } elseif (is_numeric($tickLength)) {
         $this->tickLength = $this->unitComposition->getUnitClass(UnitComposition::TIME, $tickLength);
     } else {
         throw new EngineException('Invalid tick length.');
     }
 }
Example #2
0
 public function testMomentum()
 {
     $unit = new UnitComposition();
     $momentum = $unit->getUnitClass(UnitComposition::MOMENTUM);
     $mass = $unit->getUnitClass(UnitComposition::MASS);
     $velocity = $unit->getUnitClass(UnitComposition::VELOCITY);
     $momentum->preConvertedAdd(1);
     $mass->preConvertedAdd(1);
     $velocity->preConvertedAdd(1);
     $this->assertInstanceOf('Samsara\\Newton\\Units\\Mass', PhysicsProvider::momentumCalcs($momentum, $velocity));
     $this->assertInstanceOf('Samsara\\Newton\\Units\\Momentum', PhysicsProvider::momentumCalcs($mass, $velocity));
     $this->assertInstanceOf('Samsara\\Newton\\Units\\Velocity', PhysicsProvider::momentumCalcs($momentum, $mass));
 }
Example #3
0
 /**
  * Attempts to take the square root of this unit. In practice it is very rare for a unit that has a common purpose
  * or name on its own to be rooted. Instead it is more common for the square root to occur after a series of
  * multiplication and division, so you may provide numerators and denominators to this method as well which will
  * occur BEFORE the square root is taken.
  *
  * @param   Quantity[] $numerators
  * @param   Quantity[] $denominators
  * @param   int $precision
  * @return  Quantity
  * @throws  \Exception
  */
 public function squareRoot(array $numerators = [], array $denominators = [], $precision = 2)
 {
     $numerators[] = $this;
     return $this->unitCompClass->naiveSquareRoot($numerators, $denominators, $precision);
 }
Example #4
0
 /**
  * Universal Gravitation Equation:
  *
  * A = (G m1 m2)/ r^2
  *
  * @param   Mass[]|Acceleration[]|Length[]  ...$quantities
  * @return  Mass|Acceleration|Length
  * @throws  \Exception
  */
 public static function universalGravitation(...$quantities)
 {
     if (count($quantities) != 3) {
         throw new \Exception('The Universal Gravitation Equation requires at least three given units to calculate something.');
     }
     $vals = [];
     foreach ($quantities as $unit) {
         if ($unit instanceof Mass) {
             /** @var Mass */
             $vals['mass'][] = $unit;
         } elseif ($unit instanceof Length) {
             /** @var Length */
             $vals['length'] = $unit;
         } elseif ($unit instanceof Acceleration) {
             /** @var Acceleration */
             $vals['acceleration'] = $unit;
         } else {
             throw new \Exception('Only Mass, Acceleration and Length valid units for the Universal Gravitation Equation.');
         }
     }
     $gravitation = new Gravitation();
     $unitComposition = new UnitComposition();
     if (array_key_exists('mass', $vals) && count($vals['mass']) == 2) {
         if (array_key_exists('length', $vals)) {
             /** @return Acceleration */
             return $unitComposition->naiveMultiOpt([$gravitation, $vals['mass'][0], $vals['mass'][1]], [$vals['length'], $vals['length']]);
         } else {
             /** @return Length */
             return $gravitation->squareRoot([$vals['mass'][0], $vals['mass'][1]], [$vals['acceleration']]);
         }
     } else {
         /** @return Mass */
         return $unitComposition->naiveMultiOpt([$vals['acceleration'], $vals['length'], $vals['length']], [$gravitation, $vals['mass'][0]]);
     }
 }