Skip to main content

skia_safe/core/
point.rs

1use crate::{ISize, Size, prelude::*, scalar};
2use skia_bindings::{self as sb, SkIPoint, SkPoint};
3use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
4
5pub use IPoint as IVector;
6
7#[repr(C)]
8#[derive(Copy, Clone, PartialEq, Eq, Default, Debug)]
9pub struct IPoint {
10    pub x: i32,
11    pub y: i32,
12}
13
14native_transmutable!(SkIPoint, IPoint);
15
16impl Neg for IPoint {
17    type Output = IPoint;
18    fn neg(self) -> Self::Output {
19        IPoint::new(-self.x, -self.y)
20    }
21}
22
23impl Add<IVector> for IPoint {
24    type Output = IPoint;
25    fn add(self, rhs: IVector) -> Self {
26        IPoint::new(self.x + rhs.x, self.y + rhs.y)
27    }
28}
29
30impl AddAssign<IVector> for IPoint {
31    fn add_assign(&mut self, rhs: IVector) {
32        self.x += rhs.x;
33        self.y += self.y;
34    }
35}
36
37impl Add<ISize> for IPoint {
38    type Output = IPoint;
39    fn add(self, rhs: ISize) -> Self::Output {
40        IPoint::new(self.x + rhs.width, self.y + rhs.height)
41    }
42}
43
44impl AddAssign<ISize> for IPoint {
45    fn add_assign(&mut self, rhs: ISize) {
46        self.x += rhs.width;
47        self.y += rhs.height;
48    }
49}
50
51impl Sub for IPoint {
52    type Output = Self;
53    fn sub(self, rhs: Self) -> Self {
54        IPoint::new(self.x - rhs.x, self.y - rhs.y)
55    }
56}
57
58impl SubAssign<IVector> for IPoint {
59    fn sub_assign(&mut self, rhs: Self) {
60        self.x -= rhs.x;
61        self.y -= rhs.y;
62    }
63}
64
65impl Sub<ISize> for IPoint {
66    type Output = IPoint;
67    fn sub(self, rhs: ISize) -> Self::Output {
68        IPoint::new(self.x - rhs.width, self.y - rhs.height)
69    }
70}
71
72impl SubAssign<ISize> for IPoint {
73    fn sub_assign(&mut self, rhs: ISize) {
74        self.x -= rhs.width;
75        self.y -= rhs.height;
76    }
77}
78
79impl IPoint {
80    /// Sets `x` and `y`.
81    ///
82    /// - `x` integer x-axis value
83    /// - `y` integer y-axis value
84    pub const fn new(x: i32, y: i32) -> Self {
85        IPoint { x, y }
86    }
87
88    /// Returns true if `x` and `y` are both zero.
89    pub fn is_zero(self) -> bool {
90        (self.x | self.y) == 0
91    }
92
93    /// Sets `x` and `y`.
94    ///
95    /// - `x` new value for `x`
96    /// - `y` new value for `y`
97    pub fn set(&mut self, x: i32, y: i32) {
98        *self = IPoint::new(x, y);
99    }
100
101    /// Returns true if the point is equivalent to (`x`, `y`).
102    ///
103    /// - `x` value compared with `x`
104    /// - `y` value compared with `y`
105    pub fn equals(self, x: i32, y: i32) -> bool {
106        self == IPoint::new(x, y)
107    }
108}
109
110pub type Vector = Point;
111
112#[repr(C)]
113#[derive(Copy, Clone, PartialEq, Default, Debug)]
114pub struct Point {
115    pub x: scalar,
116    pub y: scalar,
117}
118
119native_transmutable!(SkPoint, Point);
120
121impl Neg for Point {
122    type Output = Point;
123    fn neg(self) -> Self::Output {
124        Point::new(-self.x, -self.y)
125    }
126}
127
128impl Add<Vector> for Point {
129    type Output = Self;
130    fn add(self, rhs: Vector) -> Self {
131        Point::new(self.x + rhs.x, self.y + rhs.y)
132    }
133}
134
135impl AddAssign<Vector> for Point {
136    fn add_assign(&mut self, rhs: Vector) {
137        self.x += rhs.x;
138        self.y += rhs.y;
139    }
140}
141
142impl Add<Size> for Point {
143    type Output = Self;
144    fn add(self, rhs: Size) -> Self {
145        Point::new(self.x + rhs.width, self.y + rhs.height)
146    }
147}
148
149impl AddAssign<Size> for Point {
150    fn add_assign(&mut self, rhs: Size) {
151        self.x += rhs.width;
152        self.y += rhs.height;
153    }
154}
155
156impl Sub for Point {
157    type Output = Point;
158    fn sub(self, rhs: Self) -> Self {
159        Point::new(self.x - rhs.x, self.y - rhs.y)
160    }
161}
162
163impl SubAssign<Vector> for Point {
164    fn sub_assign(&mut self, rhs: Vector) {
165        self.x -= rhs.x;
166        self.y -= rhs.y;
167    }
168}
169
170impl Sub<Size> for Point {
171    type Output = Self;
172    fn sub(self, rhs: Size) -> Self {
173        Point::new(self.x - rhs.width, self.y - rhs.height)
174    }
175}
176
177impl SubAssign<Size> for Point {
178    fn sub_assign(&mut self, rhs: Size) {
179        self.x -= rhs.width;
180        self.y -= rhs.height;
181    }
182}
183
184impl Mul<scalar> for Point {
185    type Output = Self;
186    fn mul(self, rhs: scalar) -> Self {
187        Self::new(self.x * rhs, self.y * rhs)
188    }
189}
190
191impl MulAssign<scalar> for Point {
192    fn mul_assign(&mut self, rhs: scalar) {
193        self.x *= rhs;
194        self.y *= rhs;
195    }
196}
197
198// `SkPoint.h` does not define a `/` operator, but we add it to complement Mul<>.
199
200impl Div<scalar> for Point {
201    type Output = Self;
202    fn div(self, rhs: scalar) -> Self {
203        Self::new(self.x / rhs, self.y / rhs)
204    }
205}
206
207impl DivAssign<scalar> for Point {
208    fn div_assign(&mut self, rhs: scalar) {
209        self.x /= rhs;
210        self.y /= rhs;
211    }
212}
213
214impl Point {
215    /// Sets `x` and `y`. Used both to set a point and a vector.
216    ///
217    /// - `x` float x-axis value
218    /// - `y` float y-axis value
219    pub const fn new(x: scalar, y: scalar) -> Self {
220        Self { x, y }
221    }
222
223    /// Returns true if `x` and `y` are both zero.
224    pub fn is_zero(self) -> bool {
225        self.x == 0.0 && self.y == 0.0
226    }
227
228    /// Sets `x` and `y`.
229    ///
230    /// - `x` new value for `x`
231    /// - `y` new value for `y`
232    pub fn set(&mut self, x: scalar, y: scalar) {
233        *self = Self::new(x, y);
234    }
235
236    /// Sets `x` and `y`, promoting integers to float values.
237    ///
238    /// Assigning a large integer value directly to `x` or `y` may cause a compiler error,
239    /// triggered by narrowing conversion of int to float. This safely casts `p` to avoid the
240    /// error.
241    ///
242    /// - `p` integer point members promoted to float
243    pub fn iset(&mut self, p: impl Into<IPoint>) {
244        let p = p.into();
245        self.x = p.x as scalar;
246        self.y = p.y as scalar;
247    }
248
249    /// Sets `x` to the absolute value of `p.x`, and `y` to the absolute value of `p.y`.
250    ///
251    /// - `p` members providing magnitude for `x` and `y`
252    pub fn set_abs(&mut self, p: impl Into<Point>) {
253        let p = p.into();
254        self.x = p.x.abs();
255        self.y = p.y.abs();
256    }
257
258    /// Adds `offset` to each point in `points`.
259    ///
260    /// - `points` point array
261    /// - `offset` vector added to points
262    pub fn offset_points(points: &mut [Point], offset: impl Into<Vector>) {
263        let offset = offset.into();
264        points.iter_mut().for_each(|p| p.offset(offset));
265    }
266
267    /// Adds `d` to the point.
268    ///
269    /// - `d` vector to add
270    pub fn offset(&mut self, d: impl Into<Vector>) {
271        *self += d.into();
272    }
273
274    /// Returns the Euclidean distance from origin, computed as `sqrt(x * x + y * y)`.
275    pub fn length(self) -> scalar {
276        unsafe { SkPoint::Length(self.x, self.y) }
277    }
278
279    /// Returns the Euclidean distance from origin, computed as `sqrt(x * x + y * y)`.
280    pub fn distance_to_origin(self) -> scalar {
281        self.length()
282    }
283
284    /// Scales so that [`Self::length()`] returns one, while preserving the ratio of `x` to `y`,
285    /// if possible. If the prior length is nearly zero, sets the vector to (0, 0) and returns
286    /// false; otherwise returns true.
287    ///
288    /// Example (C++): <https://fiddle.skia.org/c/@Point_normalize_2>
289    pub fn normalize(&mut self) -> bool {
290        unsafe { self.native_mut().normalize() }
291    }
292
293    /// Sets the vector to (`x`, `y`) scaled so that [`Self::length()`] returns one, and so that
294    /// the vector is proportional to (`x`, `y`). If the (`x`, `y`) length is nearly zero, sets the
295    /// vector to (0, 0) and returns false; otherwise returns true.
296    ///
297    /// - `x` proportional value for `x`
298    /// - `y` proportional value for `y`
299    ///
300    /// Example (C++): <https://fiddle.skia.org/c/@Point_setNormalize>
301    pub fn set_normalize(&mut self, x: scalar, y: scalar) -> bool {
302        unsafe { self.native_mut().setNormalize(x, y) }
303    }
304
305    /// Scales the vector so that [`Self::distance_to_origin()`] returns `length`, if possible. If
306    /// the former length is nearly zero, sets the vector to (0, 0) and returns false; otherwise
307    /// returns true.
308    ///
309    /// - `length` straight-line distance to origin
310    ///
311    /// Example (C++): <https://fiddle.skia.org/c/@Point_setLength>
312    pub fn set_length(&mut self, length: scalar) -> bool {
313        unsafe { self.native_mut().setLength(length) }
314    }
315
316    /// Sets the vector to (`x`, `y`) scaled to `length`, if possible. If the former length is
317    /// nearly zero, sets the vector to (0, 0) and returns false; otherwise returns true.
318    ///
319    /// - `x` proportional value for `x`
320    /// - `y` proportional value for `y`
321    /// - `length` straight-line distance to origin
322    ///
323    /// Example (C++): <https://fiddle.skia.org/c/@Point_setLength_2>
324    pub fn set_length_xy(&mut self, x: scalar, y: scalar, length: scalar) -> bool {
325        unsafe { self.native_mut().setLength1(x, y, length) }
326    }
327
328    /// Returns the point times `scale`.
329    ///
330    /// - `scale` factor to multiply the point by
331    ///
332    /// Example (C++): <https://fiddle.skia.org/c/@Point_scale>
333    #[must_use]
334    pub fn scaled(self, scale: scalar) -> Self {
335        let mut p = Point::default();
336        unsafe { self.native().scale(scale, p.native_mut()) }
337        p
338    }
339
340    /// Scales the point in place by `scale`.
341    ///
342    /// - `scale` factor to multiply the point by
343    pub fn scale(&mut self, scale: scalar) {
344        *self = self.scaled(scale);
345    }
346
347    /// Changes the sign of `x` and `y`.
348    pub fn negate(&mut self) {
349        *self = -*self;
350    }
351
352    /// Returns true if both `x` and `y` are measurable values.
353    pub fn is_finite(self) -> bool {
354        unsafe { sb::C_SkPoint_isFinite(self.native()) }
355    }
356
357    /// Returns true if the point is equivalent to (`x`, `y`).
358    ///
359    /// - `x` value compared with `x`
360    /// - `y` value compared with `y`
361    pub fn equals(self, x: scalar, y: scalar) -> bool {
362        self == Point::new(x, y)
363    }
364
365    /// Returns the Euclidean distance from origin, computed as `sqrt(x * x + y * y)`.
366    ///
367    /// - `x` component of length
368    /// - `y` component of length
369    ///
370    /// Example (C++): <https://fiddle.skia.org/c/@Point_Length>
371    pub fn length_xy(x: scalar, y: scalar) -> scalar {
372        unsafe { SkPoint::Length(x, y) }
373    }
374
375    /// Scales `v` so that [`Self::length()`] returns one, while preserving the ratio of `v.x` to
376    /// `v.y`, if possible. If the original length is nearly zero, sets `v` to (0, 0) and returns
377    /// zero; otherwise, returns the length of `v` before it is scaled.
378    ///
379    /// The returned prior length may be `INFINITY` if it cannot be represented by a float.
380    ///
381    /// Note that [`Self::normalize()`] is faster if the prior length is not required.
382    ///
383    /// - `v` normalized to unit length
384    ///
385    /// Example (C++): <https://fiddle.skia.org/c/@Point_Normalize>
386    pub fn normalize_vector(v: &mut Vector) -> scalar {
387        unsafe { SkPoint::Normalize(v.native_mut()) }
388    }
389
390    /// Returns the Euclidean distance between `a` and `b`.
391    ///
392    /// - `a` line end point
393    /// - `b` line end point
394    pub fn distance(a: Self, b: Self) -> scalar {
395        unsafe { SkPoint::Length(a.x - b.x, a.y - b.y) }
396    }
397
398    /// Returns the dot product of `a` and `b`.
399    ///
400    /// - `a` left side of dot product
401    /// - `b` right side of dot product
402    pub fn dot_product(a: Self, b: Self) -> scalar {
403        a.x * b.x + a.y * b.y
404    }
405
406    /// Returns the cross product of `a` and `b`.
407    ///
408    /// `a` and `b` form three-dimensional vectors with z-axis value equal to zero. The cross
409    /// product is a three-dimensional vector with x-axis and y-axis values equal to zero. The
410    /// cross product z-axis component is returned.
411    ///
412    /// - `a` left side of cross product
413    /// - `b` right side of cross product
414    pub fn cross_product(a: Self, b: Self) -> scalar {
415        a.x * b.y - a.y * b.x
416    }
417
418    /// Returns the cross product of the point and `vec`.
419    ///
420    /// The point and `vec` form three-dimensional vectors with z-axis value equal to zero. The
421    /// cross product is a three-dimensional vector with x-axis and y-axis values equal to zero.
422    /// The cross product z-axis component is returned.
423    ///
424    /// - `vec` right side of cross product
425    pub fn cross(self, vec: Vector) -> scalar {
426        Self::cross_product(self, vec)
427    }
428
429    /// Returns the dot product of the point and `vec`.
430    ///
431    /// - `vec` right side of dot product
432    pub fn dot(self, vec: Vector) -> scalar {
433        Self::dot_product(self, vec)
434    }
435}
436
437impl From<(i32, i32)> for IPoint {
438    fn from(source: (i32, i32)) -> Self {
439        IPoint::new(source.0, source.1)
440    }
441}
442
443impl From<(scalar, scalar)> for Point {
444    fn from(source: (scalar, scalar)) -> Self {
445        Point::new(source.0, source.1)
446    }
447}
448
449impl From<IPoint> for Point {
450    fn from(source: IPoint) -> Self {
451        Self::new(source.x as _, source.y as _)
452    }
453}
454
455impl From<(i32, i32)> for Point {
456    fn from(source: (i32, i32)) -> Self {
457        (source.0 as scalar, source.1 as scalar).into()
458    }
459}