Package org.evoludo.geom
Class Vector2D
- All Implemented Interfaces:
Point
Utility class for 2D vector manipulations.
- Author:
- Christoph Hauert
-
Field Summary
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionadd(double dx, double dy) Shift the coordinates of this 2D vectordxto the right anddyupwards.Add the 2D vectorsaandband store the result in this vector.Add the 2D vectoraddto this vector.doubleCalculate the dot product of 2D vectordand this vector.doublelength()Calculate the length of the 2D vector.doublelength2()Calculate the length squared of the 2D vector,|v|2.doubleSame aslength2().negate()Negates the coordinates of 2D vectorvto obtain-v.Normalizes the 2D vectorvsuch that its length is|v|=1while preserving the direction.normalize(double l) Normalizes the 2D vectorvsuch that its length isl, i.e.Scales the 2D vector by the scalar factorsand adds the 2D vectorato obtains*v+a.Set the 2D vector to pointing fromfromtoto.sub(double dx, double dy) Subtractdxanddyfrom coordinates, i.e.Subtract the 2D vectorsubfrom this vector.Subtract 2D vectorsbfromaand store result in this 2D vector.toString()
-
Constructor Details
-
Vector2D
public Vector2D()Create a new 2D vector(0,0). -
Vector2D
Create a new 2D vector from point/vectorp.- Parameters:
p- the point/vector to copy
-
Vector2D
public Vector2D(double x, double y) Create a new 2D vector with coordinates(x,y).- Parameters:
x- thex-coordinatey- they-coordinate
-
-
Method Details
-
set
Set the 2D vector to pointing fromfromtoto.- Parameters:
from- the starting pointto- the end point- Returns:
- the new vector
-
add
Shift the coordinates of this 2D vectordxto the right anddyupwards.- Parameters:
dx- the shift in thex-coordinatedy- the shift in they-coordinate- Returns:
- the shifted vector
(x+dx,y+dy) - See Also:
-
add
Add the 2D vectoraddto this vector.- Parameters:
add- the vector to add- Returns:
- the new vector
-
add
Add the 2D vectorsaandband store the result in this vector.- Parameters:
a- the first vectorb- the second vector- Returns:
- the new vector
a+b - See Also:
-
sub
Subtractdxanddyfrom coordinates, i.e. shiftx-coordinate to the left bydxand they-coordinate down bydy. This yields the same result asadd(-dx, -dy).- Parameters:
dx- the shift in thex-coordinatedy- the shift in they-coordinate- Returns:
- the new vector
(x-dx,y-dy)
-
sub
Subtract the 2D vectorsubfrom this vector.- Parameters:
sub- the vector to subtract- Returns:
- the new vector
-
sub
Subtract 2D vectorsbfromaand store result in this 2D vector.- Parameters:
a- the first vector for subtractionb- the vector to subtract froma- Returns:
- the vector
a-b - See Also:
-
dot
Calculate the dot product of 2D vectordand this vector.- Parameters:
d- the vector to calculate the dot product with- Returns:
- the dot product
v.d
-
negate
Negates the coordinates of 2D vectorvto obtain-v.- Returns:
- the vector
v=-v
-
normalize
Normalizes the 2D vectorvsuch that its length is|v|=1while preserving the direction.- Returns:
- the normalized vector
- See Also:
-
normalize
Normalizes the 2D vectorvsuch that its length isl, i.e.|v|=l, while preserving the direction.- Parameters:
l- the new length of the vector- Returns:
- the scaled vector
- See Also:
-
scaleAdd
Scales the 2D vector by the scalar factorsand adds the 2D vectorato obtains*v+a. This is a shortcut forv.scale(s).add(a).- Parameters:
s- the scalar factora- the vector to add- Returns:
- the new vector
s*v+a - See Also:
-
length
public double length()Calculate the length of the 2D vector.For computational efficiency the fairly expensive square-roots calculations should be avoided whenever possible.
- Returns:
- the length of vector
|v| - See Also:
-
length2
public double length2()Calculate the length squared of the 2D vector,|v|2.- Returns:
- the length squared
- See Also:
-
lengthSquared
public double lengthSquared()Same aslength2(). For (historical) compatibility withVector2ffrom java3d.- Returns:
- the length squared
- See Also:
-
toString
-