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 vectordx
to the right anddy
upwards.Add the 2D vectorsa
andb
and store the result in this vector.Add the 2D vectoradd
to this vector.double
Calculate the dot product of 2D vectord
and this vector.double
length()
Calculate the length of the 2D vector.double
length2()
Calculate the length squared of the 2D vector,|v|2
.double
Same aslength2()
.negate()
Negates the coordinates of 2D vectorv
to obtain-v
.Normalizes the 2D vectorv
such that its length is|v|=1
while preserving the direction.normalize
(double l) Normalizes the 2D vectorv
such that its length isl
, i.e.Scales the 2D vector by the scalar factors
and adds the 2D vectora
to obtains*v+a
.Set the 2D vector to pointing fromfrom
toto
.sub
(double dx, double dy) Subtractdx
anddy
from coordinates, i.e.Subtract the 2D vectorsub
from this vector.Subtract 2D vectorsb
froma
and 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 fromfrom
toto
.- Parameters:
from
- the starting pointto
- the end point- Returns:
- the new vector
-
add
Shift the coordinates of this 2D vectordx
to the right anddy
upwards.- 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 vectoradd
to this vector.- Parameters:
add
- the vector to add- Returns:
- the new vector
-
add
Add the 2D vectorsa
andb
and store the result in this vector.- Parameters:
a
- the first vectorb
- the second vector- Returns:
- the new vector
a+b
- See Also:
-
sub
Subtractdx
anddy
from coordinates, i.e. shiftx
-coordinate to the left bydx
and 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 vectorsub
from this vector.- Parameters:
sub
- the vector to subtract- Returns:
- the new vector
-
sub
Subtract 2D vectorsb
froma
and 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 vectord
and this vector.- Parameters:
d
- the vector to calculate the dot product with- Returns:
- the dot product
v.d
-
negate
Negates the coordinates of 2D vectorv
to obtain-v
.- Returns:
- the vector
v=-v
-
normalize
Normalizes the 2D vectorv
such that its length is|v|=1
while preserving the direction.- Returns:
- the normalized vector
- See Also:
-
normalize
Normalizes the 2D vectorv
such 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 factors
and adds the 2D vectora
to 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 withVector2f
from java3d.- Returns:
- the length squared
- See Also:
-
toString
-