1use crate::{CubicPoint, HexPoint, Orientation};
2use serde::{Deserialize, Serialize};
3
4#[derive(Copy, Clone, Debug, Ord, PartialOrd, Eq, PartialEq, Hash, Serialize, Deserialize)]
5pub struct Joint {
6 point: CubicPoint,
7 direction: Orientation,
8}
9
10impl Joint {
11 pub fn new<P>(point: P, direction: Orientation) -> Self
12 where
13 P: HexPoint,
14 {
15 Self { point: point.as_cubic_point(), direction }
16 }
17 pub fn from_points<S, T>(source: S, target: T) -> Self
18 where
19 S: HexPoint,
20 T: HexPoint,
21 {
22 match Orientation::from_points(source, target) {
23 Some(s) => source.as_joint(s),
24 None => panic!("{source:?} and {target:?} are not adjacent points"),
25 }
26 }
27}
28
29impl Joint {
30 pub fn source(&self) -> CubicPoint {
31 self.point
32 }
33 pub fn target(&self) -> CubicPoint {
34 self.direction.goto_points(self.point)
35 }
36 pub fn get_direction(&self) -> Orientation {
37 self.direction
38 }
39 pub fn set_direction(&mut self, direction: Orientation) {
40 self.direction = direction;
41 }
42 pub fn forward(&self) -> Self {
43 Self::new(self.point.go(self.direction), self.direction)
44 }
45 pub fn rotate(&self, clockwise: bool) -> Self {
46 Self::new(self.point, self.direction.rotate(clockwise))
47 }
48}