Class PicVector

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Cloneable

    public class PicVector
    extends PicPoint
    This class encapsulates a geometrical vector (ie it has absolutely no relation with java.util.Vector !). It is based on PicPoint, from where it conveniently inherits formatting methods, yet add some useful vector-calculus related methods, e.g. dot product, norm, etc... Being a subclass of PicPoint is a major benefit where polymorphism is concerned, since instances of this class can be fed as argument to most getters and setters in jpicedt.graphic.model.Element.
    Since:
    jPicEdt 1.3.3
    Version:
    $Id: PicVector.java,v 1.7.2.1 2007/09/02 11:56:17 reynal Exp $
    Author:
    Sylvain Reynal
    See Also:
    Serialized Form
    • Nested Class Summary

      • Nested classes/interfaces inherited from class java.awt.geom.Point2D

        java.awt.geom.Point2D.Double, java.awt.geom.Point2D.Float
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static PicVector X_AXIS
      the (1,0) unit vector ; this may be used to compute e.g.
      static PicVector Y_AXIS
      the (0,1) unit vector ; this may be used to compute e.g.
      • Fields inherited from class java.awt.geom.Point2D.Double

        x, y
    • Constructor Summary

      Constructors 
      Constructor Description
      PicVector()
      Construct a null vector.
      PicVector​(double[] f)
      Construct a vector from the first two elements of the given array.
      PicVector​(double x, double y)
      Construct a vector with coordinates (x,y)
      PicVector​(float[] f)
      Construct a vector from the first two elements of the given array.
      PicVector​(java.awt.geom.Line2D line)
      Construct a new PicVector from the two end-points of the given Line2D object.
      PicVector​(java.awt.geom.Point2D pt)
      Construct a new PicVector pointing from (0,0) to the given point
      PicVector​(java.awt.geom.Point2D pt1, java.awt.geom.Point2D pt2)
      Construct a new PicVector from the two given points
      PicVector​(java.lang.Number x, java.lang.Number y)
      Construct a vector from the given pair of Number (using their double value).
      PicVector​(java.lang.String str)
      Construct a point by parsing a String similar to that given by the toString() method, ie (x,y).
      PicVector​(PicVector picVec)
      Clone the given vector.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      PicVector add​(PicVector other)
      Add the given vector to this vector, and return this for convenience.
      PicVector add​(PicVector other, double a)
      Add the given vector scaled by "a" to this vector, and return this for convenience.
      double angle​(PicVector other)
      Return the (CCW oriented) angle between this vector and the given vector, in radians.
      double angleDegrees​(PicVector other)
      Return the (CCW oriented) angle between this vector and the given vector, in degrees.
      double det​(PicVector other)
      Return the determinant of {{x,y},{other.x,other.y}} ; this is aka vector product, where only the z-coordinate gets returned.
      double dot​(PicVector other)
      Return the dot product of this vector with the given vector
      static PicVector getDirector​(double startPtX, double startPtY, double endPtX, double endPtY, PicVector dest)
      Return a unitary vector pointing from startPt to endPt ; if dest is non-null, it's filled with the result and returned for convenience ; otherwise, a new PicPoint gets allocated.
      static PicVector getDirector​(PicPoint startPt, PicPoint endPt)
      Return a unitary vector pointing from startPt to endPt
      static PicVector getDirector​(PicPoint startPt, PicPoint endPt, PicVector dest)
      Return a unitary vector pointing from startPt to endPt ; if dest is non-null, it's filled with the result and returned for convenience ; otherwise, a new PicPoint gets allocated.
      static PicVector getOrthogonal​(PicPoint startPt, PicPoint endPt)
      Return a UNITARY vector orthogonal to the vector pointing from startPt to endPt and built by rotating this vector CCW.
      PicVector inverse()
      Change the sign of each coordinate of this vector
      boolean isColinear​(PicVector other)
      Return whether this vector is proportional to the given vector
      boolean isNull()
      Return true if this vector has a null-norm
      boolean isOrthogonal​(PicVector other)
      Return whether this vector is orthogonal to the given vector
      static void main​(java.lang.String[] args)
      validation tests
      double norm()
      Return the norm of this vector
      double norm2()
      Return the squared of the norm of this vector
      PicVector normalize()
      Normalize this vector so that this becomes a unitary vector Does nothing if this vector is a null-vector.
      static PicVector normalize​(PicVector src, PicVector dest)
      Return a normalized copy of the given vector.
      PicVector rotate​(double angle)
      Rotate this vector by the given (CCW-oriented) angle in radians.
      Current implementation arranges for a very fast code if angle is Math.PI, Math.PI/2 or -Math.PI/2.
      static PicVector rotate​(PicVector src, PicVector dest, double angle)
      Return a rotated copy of the given vector.
      PicVector scale​(double a)
      Multiply each coordinate of this vector by the given double
      PicVector scale​(double ax, double ay)
      Multiply each coordinate of this vector by the given pair of double
      static PicVector scale​(PicVector src, PicVector dest, double scale)
      Return a scaled copy of the given vector.
      PicVector setCoordinates​(java.awt.geom.Line2D line)
      Set the coordinates of this vector from the given Line2D object
      PicVector setCoordinates​(java.awt.geom.Point2D pt1, java.awt.geom.Point2D pt2)
      Set the coordinates of this vector from the two given points.
      PicVector subtract​(PicVector other)
      Subtract the given vector from this vector, and return this for convenience.
      • Methods inherited from class java.awt.geom.Point2D.Double

        getX, getY, setLocation
      • Methods inherited from class java.awt.geom.Point2D

        distance, distance, distance, distanceSq, distanceSq, distanceSq, equals, hashCode, setLocation
      • Methods inherited from class java.lang.Object

        finalize, getClass, notify, notifyAll, wait, wait, wait
    • Field Detail

      • X_AXIS

        public static PicVector X_AXIS
        the (1,0) unit vector ; this may be used to compute e.g. angles, projections, ...
      • Y_AXIS

        public static PicVector Y_AXIS
        the (0,1) unit vector ; this may be used to compute e.g. angles, projections, ...
    • Constructor Detail

      • PicVector

        public PicVector()
        Construct a null vector.
      • PicVector

        public PicVector​(PicVector picVec)
        Clone the given vector.
      • PicVector

        public PicVector​(double x,
                         double y)
        Construct a vector with coordinates (x,y)
      • PicVector

        public PicVector​(java.lang.Number x,
                         java.lang.Number y)
        Construct a vector from the given pair of Number (using their double value).
      • PicVector

        public PicVector​(float[] f)
        Construct a vector from the first two elements of the given array.
      • PicVector

        public PicVector​(double[] f)
        Construct a vector from the first two elements of the given array.
      • PicVector

        public PicVector​(java.lang.String str)
                  throws java.lang.NumberFormatException
        Construct a point by parsing a String similar to that given by the toString() method, ie (x,y).
        Throws:
        java.lang.NumberFormatException
        Since:
        jpicedt 1.3.3
      • PicVector

        public PicVector​(java.awt.geom.Point2D pt1,
                         java.awt.geom.Point2D pt2)
        Construct a new PicVector from the two given points
      • PicVector

        public PicVector​(java.awt.geom.Point2D pt)
        Construct a new PicVector pointing from (0,0) to the given point
      • PicVector

        public PicVector​(java.awt.geom.Line2D line)
        Construct a new PicVector from the two end-points of the given Line2D object.
    • Method Detail

      • setCoordinates

        public PicVector setCoordinates​(java.awt.geom.Point2D pt1,
                                        java.awt.geom.Point2D pt2)
        Set the coordinates of this vector from the two given points.
        Returns:
        this for convenience
      • setCoordinates

        public PicVector setCoordinates​(java.awt.geom.Line2D line)
        Set the coordinates of this vector from the given Line2D object
        Returns:
        this for convenience
      • add

        public PicVector add​(PicVector other)
        Add the given vector to this vector, and return this for convenience.
      • add

        public PicVector add​(PicVector other,
                             double a)
        Add the given vector scaled by "a" to this vector, and return this for convenience.
      • subtract

        public PicVector subtract​(PicVector other)
        Subtract the given vector from this vector, and return this for convenience.
      • norm

        public double norm()
        Return the norm of this vector
      • norm2

        public double norm2()
        Return the squared of the norm of this vector
      • normalize

        public PicVector normalize()
        Normalize this vector so that this becomes a unitary vector Does nothing if this vector is a null-vector.
        Returns:
        this for convenience
      • dot

        public double dot​(PicVector other)
        Return the dot product of this vector with the given vector
      • isOrthogonal

        public boolean isOrthogonal​(PicVector other)
        Return whether this vector is orthogonal to the given vector
      • isColinear

        public boolean isColinear​(PicVector other)
        Return whether this vector is proportional to the given vector
      • scale

        public PicVector scale​(double a)
        Multiply each coordinate of this vector by the given double
        Returns:
        this for convenience
      • scale

        public PicVector scale​(double ax,
                               double ay)
        Multiply each coordinate of this vector by the given pair of double
        Parameters:
        ax - scale factor for the X-coord
        ay - scale factor for the Y-coord
        Returns:
        this for convenience
      • inverse

        public PicVector inverse()
        Change the sign of each coordinate of this vector
        Returns:
        this for convenience
      • isNull

        public boolean isNull()
        Return true if this vector has a null-norm
      • det

        public double det​(PicVector other)
        Return the determinant of {{x,y},{other.x,other.y}} ; this is aka vector product, where only the z-coordinate gets returned.
      • angle

        public double angle​(PicVector other)
        Return the (CCW oriented) angle between this vector and the given vector, in radians.
        Returns:
        an angle in the range (-PI, PI]
      • angleDegrees

        public double angleDegrees​(PicVector other)
        Return the (CCW oriented) angle between this vector and the given vector, in degrees.
      • rotate

        public PicVector rotate​(double angle)
        Rotate this vector by the given (CCW-oriented) angle in radians.
        Current implementation arranges for a very fast code if angle is Math.PI, Math.PI/2 or -Math.PI/2.
        Returns:
        this for convenience
      • main

        public static void main​(java.lang.String[] args)
        validation tests
      • normalize

        public static PicVector normalize​(PicVector src,
                                          PicVector dest)
        Return a normalized copy of the given vector. If dest==null, it is allocated and returned for convenience.
      • rotate

        public static PicVector rotate​(PicVector src,
                                       PicVector dest,
                                       double angle)
        Return a rotated copy of the given vector. If dest==null, it is allocated and returned for convenience..
        See Also:
        rotate(double)
      • scale

        public static PicVector scale​(PicVector src,
                                      PicVector dest,
                                      double scale)
        Return a scaled copy of the given vector. If dest==null, it is allocated and returned for convenience..
        See Also:
        rotate(double)
      • getDirector

        public static PicVector getDirector​(PicPoint startPt,
                                            PicPoint endPt)
        Return a unitary vector pointing from startPt to endPt
      • getDirector

        public static PicVector getDirector​(PicPoint startPt,
                                            PicPoint endPt,
                                            PicVector dest)
        Return a unitary vector pointing from startPt to endPt ; if dest is non-null, it's filled with the result and returned for convenience ; otherwise, a new PicPoint gets allocated.
      • getDirector

        public static PicVector getDirector​(double startPtX,
                                            double startPtY,
                                            double endPtX,
                                            double endPtY,
                                            PicVector dest)
        Return a unitary vector pointing from startPt to endPt ; if dest is non-null, it's filled with the result and returned for convenience ; otherwise, a new PicPoint gets allocated.
      • getOrthogonal

        public static PicVector getOrthogonal​(PicPoint startPt,
                                              PicPoint endPt)
        Return a UNITARY vector orthogonal to the vector pointing from startPt to endPt and built by rotating this vector CCW.