Struct Vector2
A displacement in a two-dimensional Euclidean vector space.
struct Vector2(T)
if (isGeoScalar!T);
Supported scalar types are int, long, float, double, and real.
Vector2 is the zero vector (0, 0).
Vectors support addition, subtraction, negation, and scalar
multiplication. Scalar division is available when the resulting scalar
type is floating-point. Arithmetic result types follow D's scalar
arithmetic rules subject to the supported geo-d scalar types.
Equality is exact and component-wise according to the equality semantics
of T; no tolerance or epsilon is applied. Consequently, a floating-point
vector containing NaN does not compare equal to itself.
Floating-point components may be non-finite. isFinite reports whether
both components are finite. Integral vectors are always finite.
Constructors
| Name | Description |
|---|---|
this
(x, y)
|
Constructs a vector from its components. |
Properties
| Name | Type | Description |
|---|---|---|
isFinite[get]
|
bool | Returns true when both components are finite. |
x[get]
|
T | X component. |
y[get]
|
T | Y component. |
Methods
| Name | Description |
|---|---|
opBinary
(rhs)
|
Vector addition. |
opBinary
(rhs)
|
Vector subtraction. |
opBinary
(rhs)
|
Scales this vector. |
opBinary
(rhs)
|
Divides this vector by a scalar. |
opBinaryRight
(lhs)
|
Scales this vector with the scalar on the left. |
opOpAssign
(rhs)
|
Compound vector addition. |
opOpAssign
(rhs)
|
Compound vector subtraction. |
opOpAssign
(rhs)
|
Compound scalar multiplication. |
opOpAssign
(rhs)
|
Compound scalar division. |
opUnary
()
|
Vector negation. |
Example
Example using vector arithmetic through the public package API.
import geo;
alias V = Vector2!double;
auto vector =
V(2.0, -4.0);
assert(vector .x == 2.0);
assert(vector .y == -4.0);
assert(vector .isFinite);
assert(
-vector ==
V(-2.0, 4.0)
);
assert(
vector + V(1.0, 1.0) ==
V(3.0, -3.0)
);
assert(
vector * 0.5 ==
V(1.0, -2.0)
);
assert(
0.5 * vector ==
V(1.0, -2.0)
);
vector *= 2.0;
assert(
vector ==
V(4.0, -8.0)
);