Class GVector

java.lang.Object
com.dkt.graphics.elements.GraphicE
com.dkt.graphics.elements.GVector
All Implemented Interfaces:
Cloneable

public class GVector extends GraphicE
This class represents a Vector, basically a line with a triangular tip.
If the modulus of the vector is 0 then not even the triangle is drawn.
Author:
Federico Vera <[email protected]>
  • Field Summary

    Fields inherited from class com.dkt.graphics.elements.GraphicE

    STROKE
  • Constructor Summary

    Constructors
    Constructor
    Description
    GVector(int x, int y, double l, double a)
    Basic Vector constructor
    GVector(int x1, int y1, int x2, int y2)
    Basic Vector constructor
    GVector(GLine line)
    Creates a new vector from a given GLine, using the start point as the application point.
    Copy constructor
  • Method Summary

    Modifier and Type
    Method
    Description
    add(GVector... vectors)
    Adds a finite number of vectors with no overflow check
    double
    Retrieves the angle between this vector and the one passed as an argument
    double
    Retrieves the argument of the vector (in degrees)
     
    double
    Calculates the module of the orthogonal vector resulting of the cross product between this vector and the one passed as an argument, considering the z coordinate of both vectors equal to zero
    double
     
    double
    Calculates the dot product between this vector and the one passed as an argument
    void
    Draws the component on the given graphics
    boolean
     
    int
    Retrieves the X projection of this vector
    int
    Retrieves the Y projection of this vector
    int
     
    double
    Retrieves the modulus of the vector
    void
    move(int x, int y)
    Moves the application point of this vector to the given coordinates
    Retrieves a vector with the same direction and application point but with a modulus equal to 1
    double
    Retrieves the angle between this vector and the one passed as an argument
    double
    Retrieves the argument of the vector (in radians)
    void
    radRotate(double a)
     
    void
    rotate(double a)
     
    void
    scalarMultiplication(double scalar)
    Multiplies this vector by a scalar
    void
    setArgument(double arg)
    Changes the argument of this vector (in degrees)
    void
    setArrowTipAngle(double arrowAngle)
    Sets the new angle for the tip of the arrow (in degrees).
    void
    setArrowWeight(int arrowWeight)
    Changes the arrow weight.
    void
    setModulus(double modulus)
    Changes the modulus of this vector
    void
    setRadArgument(double arg)
    Changes the argument of this vector (in radians)
    subtract(GVector... vectors)
     
    void
    traslate(int x, int y)
    Traslates this element on X and Y
    int
    x()
    Retrieves the X coordinate of the point of application of the vector
    int
    xf()
    Retrieves the X coordinate of the tip of the vector
    int
    y()
    Retrieves the Y coordinate of the point of application of the vector
    int
    yf()
    Retrieves the Y coordinate of the tip of the vector

    Methods inherited from class com.dkt.graphics.elements.GraphicE

    getPaint, getStroke, setPaint, setStroke

    Methods inherited from class java.lang.Object

    finalize, getClass, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • GVector

      public GVector(GVector e)
      Copy constructor
      Parameters:
      e - GVector to copy
      Throws:
      IllegalArgumentException - if e is null
    • GVector

      public GVector(int x, int y, double l, double a)
      Basic Vector constructor
      Parameters:
      x - x coordinate of the application point of the vector
      y - y coordinate of the application point of the vector
      l - vector modulus
      a - vector angle (in degrees)
    • GVector

      public GVector(GLine line) throws IllegalArgumentException
      Creates a new vector from a given GLine, using the start point as the application point.
      Parameters:
      line - the line to use as base for this GVector
      Throws:
      IllegalArgumentException - if line is null
    • GVector

      public GVector(int x1, int y1, int x2, int y2)
      Basic Vector constructor
      Parameters:
      x1 - x coordinate of the application point of the vector
      y1 - y coordinate of the application point of the vector
      x2 - x coordinate of the end of the vector
      y2 - y coordinate of the end of the vector
  • Method Details

    • scalarMultiplication

      public void scalarMultiplication(double scalar)
      Multiplies this vector by a scalar
      Parameters:
      scalar - scale
    • dot

      public double dot(GVector v)
      Calculates the dot product between this vector and the one passed as an argument
      Parameters:
      v - other vector
      Returns:
      dot product
      Throws:
      IllegalArgumentException - if v is null
    • cross

      public double cross(GVector v)
      Calculates the module of the orthogonal vector resulting of the cross product between this vector and the one passed as an argument, considering the z coordinate of both vectors equal to zero
      Parameters:
      v - other vector
      Returns:
      module of the cross product vector
      Throws:
      IllegalArgumentException - if v is null
    • angleBetween

      public double angleBetween(GVector v)
      Retrieves the angle between this vector and the one passed as an argument
      Parameters:
      v - other vector
      Returns:
      Angle between vectors (in degrees)
      Throws:
      IllegalArgumentException - if v is null
    • radAngleBetween

      public double radAngleBetween(GVector v)
      Retrieves the angle between this vector and the one passed as an argument
      Parameters:
      v - other vector
      Returns:
      Angle between vectors (in radians)
      Throws:
      IllegalArgumentException - if v is null
    • normalized

      public GVector normalized()
      Retrieves a vector with the same direction and application point but with a modulus equal to 1
      Returns:
      normalized vector
    • setModulus

      public void setModulus(double modulus)
      Changes the modulus of this vector
      Parameters:
      modulus - new modulus of the vector
    • setRadArgument

      public void setRadArgument(double arg)
      Changes the argument of this vector (in radians)
      Parameters:
      arg - new argument of the vector
    • setArgument

      public void setArgument(double arg)
      Changes the argument of this vector (in degrees)
      Parameters:
      arg - new argument of the vector
    • setArrowWeight

      public void setArrowWeight(int arrowWeight)
      Changes the arrow weight.
      The arrow weight is defined as the length of the arrow, by default the arrow will be appended to the line, but if arrowWeight is negative then the arrow will end at the end of the line.
      Parameters:
      arrowWeight - new arrow weight
    • setArrowTipAngle

      public void setArrowTipAngle(double arrowAngle)
      Sets the new angle for the tip of the arrow (in degrees).
      Parameters:
      arrowAngle - angle in degrees.
    • x

      public int x()
      Retrieves the X coordinate of the point of application of the vector
      Returns:
      x coordinate
    • y

      public int y()
      Retrieves the Y coordinate of the point of application of the vector
      Returns:
      y coordinate
    • xf

      public int xf()
      Retrieves the X coordinate of the tip of the vector
      Returns:
      X coordinate
    • yf

      public int yf()
      Retrieves the Y coordinate of the tip of the vector
      Returns:
      Y coordinate
    • getXComponent

      public int getXComponent()
      Retrieves the X projection of this vector
      Returns:
      x component
    • getYComponent

      public int getYComponent()
      Retrieves the Y projection of this vector
      Returns:
      y component
    • modulus

      public double modulus()
      Retrieves the modulus of the vector
      Returns:
      modulus
    • argument

      public double argument()
      Retrieves the argument of the vector (in degrees)
      Returns:
      argument
    • radArgument

      public double radArgument()
      Retrieves the argument of the vector (in radians)
      Returns:
      argument
    • rotate

      public void rotate(double a)
    • radRotate

      public void radRotate(double a)
    • add

      public GVector add(GVector... vectors)
      Adds a finite number of vectors with no overflow check
      Parameters:
      vectors - the vectors to add
      Returns:
      A new GVector equal to the resultant
      Throws:
      IllegalArgumentException - if no vector is passed
    • subtract

      public GVector subtract(GVector... vectors)
    • distance

      public double distance(GVector v)
    • draw

      public void draw(Graphics2D g)
      Description copied from class: GraphicE
      Draws the component on the given graphics
      Specified by:
      draw in class GraphicE
      Parameters:
      g - where to draw
    • traslate

      public void traslate(int x, int y)
      Description copied from class: GraphicE
      Traslates this element on X and Y
      Specified by:
      traslate in class GraphicE
      Parameters:
      x - size in px of the horizontal translation
      y - size in px of the vertical translation
    • move

      public void move(int x, int y)
      Moves the application point of this vector to the given coordinates
      Parameters:
      x - new x coordinate
      y - new y coordinate
    • clone

      public GVector clone()
      Specified by:
      clone in class GraphicE
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class GraphicE
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class GraphicE