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 pub const fn new(x: i32, y: i32) -> Self {
85 IPoint { x, y }
86 }
87
88 pub fn is_zero(self) -> bool {
90 (self.x | self.y) == 0
91 }
92
93 pub fn set(&mut self, x: i32, y: i32) {
98 *self = IPoint::new(x, y);
99 }
100
101 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
198impl 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 pub const fn new(x: scalar, y: scalar) -> Self {
220 Self { x, y }
221 }
222
223 pub fn is_zero(self) -> bool {
225 self.x == 0.0 && self.y == 0.0
226 }
227
228 pub fn set(&mut self, x: scalar, y: scalar) {
233 *self = Self::new(x, y);
234 }
235
236 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 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 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 pub fn offset(&mut self, d: impl Into<Vector>) {
271 *self += d.into();
272 }
273
274 pub fn length(self) -> scalar {
276 unsafe { SkPoint::Length(self.x, self.y) }
277 }
278
279 pub fn distance_to_origin(self) -> scalar {
281 self.length()
282 }
283
284 pub fn normalize(&mut self) -> bool {
290 unsafe { self.native_mut().normalize() }
291 }
292
293 pub fn set_normalize(&mut self, x: scalar, y: scalar) -> bool {
302 unsafe { self.native_mut().setNormalize(x, y) }
303 }
304
305 pub fn set_length(&mut self, length: scalar) -> bool {
313 unsafe { self.native_mut().setLength(length) }
314 }
315
316 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 #[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 pub fn scale(&mut self, scale: scalar) {
344 *self = self.scaled(scale);
345 }
346
347 pub fn negate(&mut self) {
349 *self = -*self;
350 }
351
352 pub fn is_finite(self) -> bool {
354 unsafe { sb::C_SkPoint_isFinite(self.native()) }
355 }
356
357 pub fn equals(self, x: scalar, y: scalar) -> bool {
362 self == Point::new(x, y)
363 }
364
365 pub fn length_xy(x: scalar, y: scalar) -> scalar {
372 unsafe { SkPoint::Length(x, y) }
373 }
374
375 pub fn normalize_vector(v: &mut Vector) -> scalar {
387 unsafe { SkPoint::Normalize(v.native_mut()) }
388 }
389
390 pub fn distance(a: Self, b: Self) -> scalar {
395 unsafe { SkPoint::Length(a.x - b.x, a.y - b.y) }
396 }
397
398 pub fn dot_product(a: Self, b: Self) -> scalar {
403 a.x * b.x + a.y * b.y
404 }
405
406 pub fn cross_product(a: Self, b: Self) -> scalar {
415 a.x * b.y - a.y * b.x
416 }
417
418 pub fn cross(self, vec: Vector) -> scalar {
426 Self::cross_product(self, vec)
427 }
428
429 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}