Class Vec2
Defined in: lina.js.
Constructor Attributes | Constructor Name and Description |
---|---|
Vec2(dx, dy)
2D vector that has an internal cartesian representation (dx, dy)
and support for transformations.
|
Method Attributes | Method Name and Description |
---|---|
accelerate(force, maxLength, minLength)
Apply a 'force' vector or offset (in-place) within given limits.
|
|
<static> |
Vec2.add(v1, vecOrDx, dy)
Return a new vector that combines two vectors.
|
add(vecOrDx, dy)
Add another vector to this vector and return the current instance.
|
|
angle()
Return the angle between positive x axis and this vector.
|
|
angleTo(v2)
Return the angle between this vector and v2.
|
|
copy()
Return a new copy of this vector.
|
|
<static> |
Vec2.cross(v1, v2)
Calculate the cross product of this vector and v2.
|
cross(v2)
Calculate the cross product of this vector and v2.
|
|
<static> |
Vec2.dot(v1, v2)
Calculate the dot product (inner product) of this vector and v2.
|
dot(v2)
Calculate the dot product (inner product) of this vector and v2.
|
|
getPolar()
Return polar coordinates for this vector.
|
|
isColinear(v2)
Check if v2 is parallel to this vector.
|
|
isNull()
Check, if vector is (0, 0)
|
|
isPerp(v2)
Check if v2 is perpendicular to this vector.
|
|
length()
Return vector length.
|
|
<static> |
Vec2.limit(length)
Return copy of vector with limited length.
|
limit(length)
Clamp vector length (in-place).
|
|
<static> |
Vec2.normalize()
Return a normalized copy of v1.
|
Normalize to a unit vector (in-place) and return this instance.
|
|
perp()
Set vector orientation to perpendicular of itself (in-place) and return this
instance.
|
|
<static> |
Vec2.perp(v1)
Return a perpendicluar copy of a vector.
|
<static> |
Vec2.revert(v1)
Return a flipped copy of vector.
|
revert()
Flip this vector (in-place) and return this instance.
|
|
rotate(a)
Rotate this vector (in-place) and return this instance.
|
|
<static> |
Vec2.rotate(v1, a)
Return a rotated copy of a vector.
|
<static> |
Vec2.scale(v1, f)
Return a copy of v1 with length multiplied by a factor.
|
scale(f)
Multiply vector length by a factor (in-place) and return this instance.
|
|
set(dx, dy)
Set coordinates.
|
|
setAngle(a)
Set the angle (in-place, relative to positive x axis).
|
|
setLength(length)
Set vector length (in-place) and return this instance.
|
|
<static> |
Vec2.setLength(v1, length)
Return a copy of vector with a defined length.
|
setNull()
Set vector to (0, 0)
|
|
Return vector length ^2.
|
|
sub(vecOrDx, dy)
Subtract another vector from this vector and return the current instance.
|
|
<static> |
Vec2.sub(v1, vecOrDx, dy)
Return a new vector that combines v1 minus v2.
|
toString(prec)
Return string representation '(dx, dy)'.
|
|
transform(m)
Apply transformation matrix (in-place) and return this instance.
|
|
<static> |
Vec2.transform(v1, m)
Return a transformed copy of a vector.
|
Class Detail
Vec2(dx, dy)
2D vector that has an internal cartesian representation (dx, dy)
and support for transformations.
When applying transformations, a vector is handled as [dx, dy, 0].
var v = new Vec2(3, 4); v.rotate(Math.PI).translate(1, 2);
- Parameters:
- {float|Vec2|JS-object} dx
- X-coordinate or a Vec2 instance or {dx:_, dy:_}
- {float} dy
- Y-coordinate or undefined, if x is a Vec2 instance or {dx:_, dy:_}
Method Detail
{Vec2}
accelerate(force, maxLength, minLength)
Apply a 'force' vector or offset (in-place) within given limits.
- Parameters:
- {float | Vec2} force
- vector. If a float is passed, then it is assumed to be the length of a vector along current direction.
- {float} maxLength
- (optional) maximum length of resulting vector.
- {float} minLength
- (optional) minimum length. If omitted, a Null-Vector is returned when EPS is reached.
- Returns:
- {Vec2}
<static>
{Vec2}
Vec2.add(v1, vecOrDx, dy)
Return a new vector that combines two vectors.
- Returns:
- {Vec2}
{Vec2}
add(vecOrDx, dy)
Add another vector to this vector and return the current instance.
- Parameters:
- {Vec2|JS-Object} vecOrDx
- Second vector.
- dy
- Returns:
- {Vec2}
{float}
angle()
Return the angle between positive x axis and this vector.
- Returns:
- {float} [-pi .. pi], ccw
{float}
angleTo(v2)
Return the angle between this vector and v2.
- Parameters:
- v2
- Returns:
- {float} [-pi .. pi], ccw
{Vec2}
copy()
Return a new copy of this vector.
- Returns:
- {Vec2}
<static>
{float}
Vec2.cross(v1, v2)
Calculate the cross product of this vector and v2.
- Returns:
- {float}
{float}
cross(v2)
Calculate the cross product of this vector and v2.
- Parameters:
- {Vec2|JS-Object} v2
- Other vector.
- Returns:
- {float}
<static>
{float}
Vec2.dot(v1, v2)
Calculate the dot product (inner product) of this vector and v2.
- Returns:
- {float}
{float}
dot(v2)
Calculate the dot product (inner product) of this vector and v2.
- Parameters:
- {Vec2|JS-Object} v2
- Other vector.
- Returns:
- {float}
{JS-Object}
getPolar()
Return polar coordinates for this vector.
- Returns:
- {JS-Object} {a:angle in rad, r: radius}.
{boolaen}
isColinear(v2)
Check if v2 is parallel to this vector.
- Parameters:
- {Vec2|JS-Object} v2
- Other vector.
- Returns:
- {boolaen}
{boolean}
isNull()
Check, if vector is (0, 0)
- Returns:
- {boolean}
{boolaen}
isPerp(v2)
Check if v2 is perpendicular to this vector.
- Parameters:
- {Vec2|JS-Object} v2
- Other vector.
- Returns:
- {boolaen}
{float}
length()
Return vector length.
- Returns:
- {float}
<static>
{Vec2}
Vec2.limit(length)
Return copy of vector with limited length.
- Parameters:
- {Vec2|JS-Object} v1.
- {float} length
- Maximum length.
- Returns:
- {Vec2}
{Vec2}
limit(length)
Clamp vector length (in-place).
- Parameters:
- {float} length
- Maximum length.
- Returns:
- {Vec2}
<static>
{Vec2}
Vec2.normalize()
Return a normalized copy of v1.
- Parameters:
- {Vec2|JS-Object} v1.
- Returns:
- {Vec2}
{Vec2}
normalize()
Normalize to a unit vector (in-place) and return this instance.
- Returns:
- {Vec2}
{Vec2}
perp()
Set vector orientation to perpendicular of itself (in-place) and return this
instance.
This is equivalent to a rotation by 90�, only faster.
- Returns:
- {Vec2}
<static>
{Vec2}
Vec2.perp(v1)
Return a perpendicluar copy of a vector.
This is equivalent to a rotation by 90�, only faster.
- Parameters:
- {Vec2|JS-Object} v1
- First vector.
- Returns:
- {Vec2}
<static>
{Vec2}
Vec2.revert(v1)
Return a flipped copy of vector.
- Parameters:
- {Vec2|JS-Object} v1
- First vector.
- Returns:
- {Vec2}
{Vec2}
revert()
Flip this vector (in-place) and return this instance.
- Returns:
- {Vec2}
{Vec2}
rotate(a)
Rotate this vector (in-place) and return this instance.
- Parameters:
- {float} a
- Angle in radians.
- Returns:
- {Vec2}
<static>
{Vec2}
Vec2.rotate(v1, a)
Return a rotated copy of a vector.
- Parameters:
- {Vec2|JS-Object} v1
- First vector.
- {float} a
- Angle in radians.
- Returns:
- {Vec2}
<static>
{Vec2}
Vec2.scale(v1, f)
Return a copy of v1 with length multiplied by a factor.
- Parameters:
- {Vec2|JS-Object} v1
- First vector.
- {float} f
- Scaling factor.
- Returns:
- {Vec2}
{Vec2}
scale(f)
Multiply vector length by a factor (in-place) and return this instance.
- Parameters:
- {float} f
- Scaling factor.
- Returns:
- {Vec2}
{Vec2}
set(dx, dy)
Set coordinates.
- Parameters:
- {float|Vec2|JS-object} dx
- X-coordinate or a Vec2 instance or {dx:_, dy:_}
- {float} dy
- Y-coordinate or undefined, if y is a Vec2 instance or {dx:_, dy:_}
- Returns:
- {Vec2}
{Vec2}
setAngle(a)
Set the angle (in-place, relative to positive x axis).
- Parameters:
- a
- Returns:
- {Vec2}
{Vec2}
setLength(length)
Set vector length (in-place) and return this instance.
- Parameters:
- {float} length
- New length.
- Returns:
- {Vec2}
<static>
{Vec2}
Vec2.setLength(v1, length)
Return a copy of vector with a defined length.
- Parameters:
- {Vec2|JS-Object} v1
- First vector.
- {float} length
- New length.
- Returns:
- {Vec2}
{Vec2}
setNull()
Set vector to (0, 0)
- Returns:
- {Vec2}
{float}
sqrLength()
Return vector length ^2.
This is faster than calling length().
- Returns:
- {float}
{Vec2}
sub(vecOrDx, dy)
Subtract another vector from this vector and return the current instance.
- Parameters:
- {Point2|JS-Object} vecOrDx
- Second point.
- dy
- Returns:
- {Vec2}
<static>
{Vec2}
Vec2.sub(v1, vecOrDx, dy)
Return a new vector that combines v1 minus v2.
- Returns:
- {Vec2}
toString(prec)
Return string representation '(dx, dy)'.
- Parameters:
- prec
{Vec2}
transform(m)
Apply transformation matrix (in-place) and return this instance.
- Parameters:
- m
- Returns:
- {Vec2}
<static>
{Vec2}
Vec2.transform(v1, m)
Return a transformed copy of a vector.
- Parameters:
- {Vec2|JS-Object} v1
- First vector.
- m
- Returns:
- {Vec2}