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 (0, 1) × (1, 0) = 1
.
sourcepub fn hypot(self) -> f64
pub fn hypot(self) -> f64
Magnitude of vector.
This is similar to self.hypot2().sqrt()
but defers to the platform
f64::hypot
method, which in general will handle the case where
self.hypot2() > f64::MAX
.
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);
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 moreimpl Copy for Vec2
impl StructuralPartialEq for Vec2
Auto Trait Implementations§
impl Freeze for Vec2
impl RefUnwindSafe for Vec2
impl Send for Vec2
impl Sync for Vec2
impl Unpin for Vec2
impl UnwindSafe for Vec2
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)