Skip to main content

astroimsim_geometry/
points.rs

1use std::ops::{Add, Mul};
2use plotpy::{Curve, Plot};
3use serde::Serialize;
4use crate::coordinate_system::{Coordinates};
5
6#[derive(Clone, Debug,Serialize)]
7pub struct Point{
8    pub x: f64,
9    pub y: f64,
10    pub coordinates: Coordinates
11}
12
13impl Point{
14    pub fn new(x:f64,y:f64,coordinates: Coordinates) -> Point{
15        Point{x,y,coordinates}
16    }
17    pub fn to_absolute(&self) -> Point{
18        match &self.coordinates{
19            Coordinates::ABSOLUTE => {
20                //println!("already in absolute");
21                self.clone()}
22            Coordinates::RELATIVE(coordinate_system) => {
23                let absolute_x =  self.x * coordinate_system.x_axis.0 + self.y *coordinate_system.y_axis.0 + coordinate_system.center.0;
24                let absolute_y = self.x * coordinate_system.x_axis.1 + self.y * coordinate_system.y_axis.1 + coordinate_system.center.1;
25                Point::new(absolute_x,absolute_y,Coordinates::ABSOLUTE)
26            }
27        }
28    }
29    pub fn convert(&self, coordinate_system: &Coordinates) -> Point{
30        let absolute = self.to_absolute();
31        match coordinate_system{
32            Coordinates::ABSOLUTE => { absolute }
33            Coordinates::RELATIVE(coordinate_system  ) => {
34                coordinate_system.point_from_absolute(absolute)
35            }
36        }
37    }
38    pub fn values(&self)-> (f64,f64){
39        (self.x,self.y)
40    }
41
42    pub fn plot(&self, plot:&mut Plot,color:&str){
43
44        let mut point = Curve::new();
45        point.set_line_style("none")
46            .set_marker_color(color)
47            .set_marker_every(1)
48            .set_marker_size(7.0)
49            .set_marker_style("*");
50        point.points_begin();
51        let (x,y) = self.to_absolute().values();
52        point.points_add(x,y);
53        point.points_end();
54        plot.add(&point);
55
56
57
58
59
60
61    }
62}
63
64
65impl Add for Point{
66    type Output = Point;
67    fn add(self, rhs: Point) -> Self::Output {
68
69        Point{
70            x: self.to_absolute().x + rhs.to_absolute().x,
71            y: self.to_absolute().y + rhs.to_absolute().y,
72            coordinates:Coordinates::ABSOLUTE
73
74        }
75    }
76}