All Downloads are FREE. Search and download functionalities are using the official Maven repository.

gw.util.science.Vector.gs Maven / Gradle / Ivy

There is a newer version: 1.18.2
Show newest version
package gw.util.science
uses gw.util.Rational
uses gw.lang.reflect.interval.ISequenceable
uses DimensionlessConstants#pi

abstract class Vector, 
                      U extends IUnit,
                      V extends Vector> implements IDimension {
  final var _magnitude: T as Magnitude
  final var _angle: Angle as Angle

  construct( magnitude: T, angle: Angle ) {
    _magnitude = magnitude
    _angle = angle
  } 
  
  property get X() : T {
     return Magnitude.fromBaseNumber( Magnitude.toBaseNumber() * RationalTrig.instance().cos( Angle.to( Radian ) ) )
  }
  
  property get Y() : T {
    return Magnitude.fromBaseNumber( Magnitude.toBaseNumber() * RationalTrig.instance().sin( Angle.to( Radian ) ) ) 
  }
  
  function add( v: V ) : V {
    var x = X.toBaseNumber() + v.X.toBaseNumber()
    var y = Y.toBaseNumber() + v.Y.toBaseNumber()
    var angle = RationalTrig.instance().atan( y/x )
    var mag = (x*x + y*y).sqrt()
    if( x < 0 ) {
      if( y < 0 ) {
        angle -= pi
      }
      else {
        angle += pi
      }
    }
    return new V( Magnitude.fromBaseNumber( mag ), new Angle( angle, AngleUnit.Radian, _angle.Unit ) )
  }
  
  function subtract( v: V ) : V {
    return add( -v )
  }
 
  function multiply( v: V ) : Rational  {
    var x = X.toBaseNumber() * v.X.toBaseNumber()
    var y = Y.toBaseNumber() * v.Y.toBaseNumber()
    return x + y 
  }

  function divide( v: V ) {
    throw new UnsupportedOperationException()
  }
  
  function copy( lu: U, au: AngleUnit ) : V {
    return new V( new T( Magnitude.toBaseNumber(), Magnitude.BaseUnit, lu ),
                  new Angle( Angle.toBaseNumber(), Angle.BaseUnit, au ) )
  }
  
  override function fromNumber( p0: Rational ) : V {
    return new V( new T( p0, Magnitude.Unit ), Angle )
  }

  function fromBaseNumber( p0: Rational ) : V {
    return new V( new T( p0, Magnitude.BaseUnit, Magnitude.Unit ), Angle )
  }

  override function numberType() : java.lang.Class {
    return Rational
  }

  override function toNumber() : Rational {
    return Magnitude.toNumber()
  }

  override function toBaseNumber() : Rational {
    return Magnitude.toBaseNumber()
  }

  function to( lu: U, au: AngleUnit ) : V {
    return copy( lu, au )
  }
  
  override function toString() : String {
    return Magnitude + " " + Angle
  }
  
  override function hashCode() : int {
    return 31 * Magnitude.hashCode() + Angle.hashCode() 
  }

  override function equals( o: Object ) : boolean {
    if( typeof o != typeof this ) {
      return false
    }
    var that = o as Vector
    return Magnitude == that.Magnitude && Angle == that.Angle
  }
  
  override function compareTo( o: V ) : int {
    return Magnitude.compareTo( o.Magnitude )
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy