cazan_common/geometry/
point.rs1use serde::{Deserialize, Serialize};
2#[cfg(feature = "wasm")]
3use wasm_bindgen::prelude::wasm_bindgen;
4
5#[cfg_attr(feature = "wasm", wasm_bindgen)]
6#[derive(Clone, Copy, Eq, PartialEq, PartialOrd, Ord, Debug, Serialize, Deserialize)]
7pub struct Point {
8 pub x: u16,
9 pub y: u16,
10}
11
12#[cfg_attr(feature = "wasm", wasm_bindgen)]
13impl Point {
14 #[cfg_attr(feature = "wasm", wasm_bindgen(constructor))]
15 pub fn new(x: u16, y: u16) -> Self {
16 Self { x, y }
17 }
18
19 pub fn right(&self) -> Option<Point> {
20 self.x.checked_add(1).map(|x| Self { x, y: self.y })
21 }
22
23 pub fn left(&self) -> Option<Point> {
24 self.x.checked_sub(1).map(|x| Self { x, y: self.y })
25 }
26
27 pub fn up(&self) -> Option<Point> {
28 self.y.checked_sub(1).map(|y| Self { x: self.x, y })
29 }
30
31 pub fn down(&self) -> Option<Point> {
32 self.y.checked_add(1).map(|y| Self { x: self.x, y })
33 }
34
35 pub fn right_up(&self) -> Option<Point> {
36 self.right().and_then(|point| point.up())
37 }
38
39 pub fn right_down(&self) -> Option<Point> {
40 self.right().and_then(|point| point.down())
41 }
42
43 pub fn left_up(&self) -> Option<Point> {
44 self.left().and_then(|point| point.up())
45 }
46
47 pub fn left_down(&self) -> Option<Point> {
48 self.left().and_then(|point| point.down())
49 }
50}