pub struct Vec2 {
pub x: f64,
pub y: f64,
}
Expand description
Fields§
§x: f64
The x-coordinate.
y: f64
The y-coordinate.
Implementations§
Source§impl Vec2
impl Vec2
Sourcepub fn cross(self, other: Vec2) -> f64
pub fn cross(self, other: Vec2) -> f64
Cross product of two vectors.
This is signed so that (1, 0) × (0, 1) = 1
.
The following relations hold:
u.cross(v) = -v.cross(u)
v.cross(v) = 0.0
Sourcepub fn hypot(self) -> f64
pub fn hypot(self) -> f64
Magnitude of vector.
See Point::distance
for the same operation on Point
.
§Examples
use kurbo::Vec2;
let v = Vec2::new(3.0, 4.0);
assert_eq!(v.hypot(), 5.0);
Sourcepub fn length(self) -> f64
pub fn length(self) -> f64
Magnitude of vector.
This is an alias for Vec2::hypot
.
Sourcepub fn hypot2(self) -> f64
pub fn hypot2(self) -> f64
Magnitude squared of vector.
See Point::distance_squared
for the same operation on Point
.
§Examples
use kurbo::Vec2;
let v = Vec2::new(3.0, 4.0);
assert_eq!(v.hypot2(), 25.0);
Sourcepub fn length_squared(self) -> f64
pub fn length_squared(self) -> f64
Magnitude squared of vector.
This is an alias for Vec2::hypot2
.
Sourcepub fn atan2(self) -> f64
pub fn atan2(self) -> f64
Find the angle in radians between this vector and the vector Vec2 { x: 1.0, y: 0.0 }
in the positive y
direction.
If the vector is interpreted as a complex number, this is the argument. The angle is expressed in radians.
Sourcepub fn angle(self) -> f64
pub fn angle(self) -> f64
Find the angle in radians between this vector and the vector Vec2 { x: 1.0, y: 0.0 }
in the positive y
direction.
This is an alias for Vec2::atan2
.
Sourcepub fn from_angle(th: f64) -> Vec2
pub fn from_angle(th: f64) -> Vec2
A unit vector of the given angle.
With th
at zero, the result is the positive X unit vector, and
at π/2, it is the positive Y unit vector. The angle is expressed
in radians.
Thus, in a Y-down coordinate system (as is common for graphics),
it is a clockwise rotation, and in Y-up (traditional for math), it
is anti-clockwise. This convention is consistent with
Affine::rotate
.
Sourcepub fn normalize(self) -> Vec2
pub fn normalize(self) -> Vec2
Returns a vector of magnitude 1.0 with the same angle as self
; i.e.
a unit/direction vector.
This produces NaN
values when the magnitude is 0
.
Sourcepub fn ceil(self) -> Vec2
pub fn ceil(self) -> Vec2
Returns a new Vec2
,
with x
and y
rounded up to the nearest integer,
unless they are already an integer.
§Examples
use kurbo::Vec2;
let a = Vec2::new(3.3, 3.6).ceil();
let b = Vec2::new(3.0, -3.1).ceil();
assert_eq!(a.x, 4.0);
assert_eq!(a.y, 4.0);
assert_eq!(b.x, 3.0);
assert_eq!(b.y, -3.0);
Sourcepub fn floor(self) -> Vec2
pub fn floor(self) -> Vec2
Returns a new Vec2
,
with x
and y
rounded down to the nearest integer,
unless they are already an integer.
§Examples
use kurbo::Vec2;
let a = Vec2::new(3.3, 3.6).floor();
let b = Vec2::new(3.0, -3.1).floor();
assert_eq!(a.x, 3.0);
assert_eq!(a.y, 3.0);
assert_eq!(b.x, 3.0);
assert_eq!(b.y, -4.0);
Sourcepub fn expand(self) -> Vec2
pub fn expand(self) -> Vec2
Returns a new Vec2
,
with x
and y
rounded away from zero to the nearest integer,
unless they are already an integer.
§Examples
use kurbo::Vec2;
let a = Vec2::new(3.3, 3.6).expand();
let b = Vec2::new(3.0, -3.1).expand();
assert_eq!(a.x, 4.0);
assert_eq!(a.y, 4.0);
assert_eq!(b.x, 3.0);
assert_eq!(b.y, -4.0);
Sourcepub fn trunc(self) -> Vec2
pub fn trunc(self) -> Vec2
Returns a new Vec2
,
with x
and y
rounded towards zero to the nearest integer,
unless they are already an integer.
§Examples
use kurbo::Vec2;
let a = Vec2::new(3.3, 3.6).trunc();
let b = Vec2::new(3.0, -3.1).trunc();
assert_eq!(a.x, 3.0);
assert_eq!(a.y, 3.0);
assert_eq!(b.x, 3.0);
assert_eq!(b.y, -3.0);
Sourcepub fn turn_90(self) -> Vec2
pub fn turn_90(self) -> Vec2
Turn by 90 degrees.
The rotation is clockwise in a Y-down coordinate system. The following relations hold:
u.dot(v) = u.cross(v.turn_90())
u.cross(v) = u.turn_90().dot(v)
Sourcepub fn rotate_scale(self, rhs: Vec2) -> Vec2
pub fn rotate_scale(self, rhs: Vec2) -> Vec2
Combine two vectors interpreted as rotation and scaling.
Interpret both vectors as a rotation and a scale, and combine their effects. by adding the angles and multiplying the magnitudes. This operation is equivalent to multiplication when the vectors are interpreted as complex numbers. It is commutative.
Trait Implementations§
Source§impl Add<TranslateScale> for Vec2
impl Add<TranslateScale> for Vec2
Source§type Output = TranslateScale
type Output = TranslateScale
+
operator.Source§fn add(self, other: TranslateScale) -> TranslateScale
fn add(self, other: TranslateScale) -> TranslateScale
+
operation. Read moreSource§impl Add<Vec2> for CircleSegment
impl Add<Vec2> for CircleSegment
Source§impl Add<Vec2> for RoundedRect
impl Add<Vec2> for RoundedRect
Source§type Output = RoundedRect
type Output = RoundedRect
+
operator.Source§impl Add<Vec2> for TranslateScale
impl Add<Vec2> for TranslateScale
Source§type Output = TranslateScale
type Output = TranslateScale
+
operator.Source§impl AddAssign<Vec2> for Point
impl AddAssign<Vec2> for Point
Source§fn add_assign(&mut self, other: Vec2)
fn add_assign(&mut self, other: Vec2)
+=
operation. Read moreSource§impl AddAssign<Vec2> for TranslateScale
impl AddAssign<Vec2> for TranslateScale
Source§fn add_assign(&mut self, other: Vec2)
fn add_assign(&mut self, other: Vec2)
+=
operation. Read moreSource§impl AddAssign for Vec2
impl AddAssign for Vec2
Source§fn add_assign(&mut self, other: Vec2)
fn add_assign(&mut self, other: Vec2)
+=
operation. Read moreSource§impl DivAssign<f64> for Vec2
impl DivAssign<f64> for Vec2
Source§fn div_assign(&mut self, other: f64)
fn div_assign(&mut self, other: f64)
/=
operation. Read moreSource§impl MulAssign<f64> for Vec2
impl MulAssign<f64> for Vec2
Source§fn mul_assign(&mut self, other: f64)
fn mul_assign(&mut self, other: f64)
*=
operation. Read moreSource§impl Sub<Vec2> for CircleSegment
impl Sub<Vec2> for CircleSegment
Source§impl Sub<Vec2> for RoundedRect
impl Sub<Vec2> for RoundedRect
Source§type Output = RoundedRect
type Output = RoundedRect
-
operator.Source§impl Sub<Vec2> for TranslateScale
impl Sub<Vec2> for TranslateScale
Source§type Output = TranslateScale
type Output = TranslateScale
-
operator.Source§impl SubAssign<Vec2> for Point
impl SubAssign<Vec2> for Point
Source§fn sub_assign(&mut self, other: Vec2)
fn sub_assign(&mut self, other: Vec2)
-=
operation. Read moreSource§impl SubAssign<Vec2> for TranslateScale
impl SubAssign<Vec2> for TranslateScale
Source§fn sub_assign(&mut self, other: Vec2)
fn sub_assign(&mut self, other: Vec2)
-=
operation. Read moreSource§impl SubAssign for Vec2
impl SubAssign for Vec2
Source§fn sub_assign(&mut self, other: Vec2)
fn sub_assign(&mut self, other: Vec2)
-=
operation. Read more