astroimsim_geometry/
coordinate_system.rs1use plotpy::{Curve, Legend, Plot};
2use serde::{Deserialize, Serialize};
3use crate::points::Point;
4
5#[derive(Clone, Debug,Serialize)]
6pub enum Coordinates {
7 ABSOLUTE,
8 RELATIVE(CoordinateSystem),
9}
10
11#[derive(Clone, Debug,Serialize,Deserialize)]
12pub struct CoordinateSystem{
13 pub x_axis: (f64, f64),
14 pub y_axis: (f64,f64),
15 pub center: (f64,f64),
16 pub label: &'static str,
17}
18
19
20impl Coordinates{
21 pub fn plot(&self, plot:&mut Plot, color:&str){
22 match &self{
23 Coordinates::ABSOLUTE => {panic!("implement me :(")}
24 Coordinates::RELATIVE(c) => {c.plot(plot,color )}
25 }
26 }
27}
28
29impl CoordinateSystem{
30 pub fn new(x_axis:(f64,f64),y_axis:(f64,f64),center: (f64,f64),label:&'static str) -> CoordinateSystem{
31
32
33 CoordinateSystem{
34 x_axis,
35 y_axis,
36 center,
37 label,
38 }
39 }
40 pub fn point_from_absolute(&self, point:Point) -> Point{
41 match point.coordinates{
42 Coordinates::ABSOLUTE => {
43 let det = self.y_axis.1*self.x_axis.0-self.y_axis.0*self.x_axis.1 ;
44 let proj_x = ((point.x -self.center.0)*self.y_axis.1-(point.y -self.center.1)*self.y_axis.0 )/det;
48 let proj_y = (-(point.x -self.center.0)*self.x_axis.1+(point.y -self.center.1)*self.x_axis.0 )/det;
49
50 Point::new(proj_x,proj_y,Coordinates::RELATIVE(self.clone()))
57 }
58 Coordinates::RELATIVE(_) => {panic!("tried to from_absolute a point in a not absolute coordinate system :( ")}
59 }
60
61 }
62
63
64
65 pub fn plot(&self,plot: &mut Plot,color: &str){
66 let mut x_axis = Curve::new();
67 let scale = 1.0/self.x_axis.0;
68 x_axis.set_line_width(2.0);
69 x_axis.set_line_color(color);
70 x_axis.set_line_style("dashed");
71 x_axis.set_label(format!("x axis for {:?}",self.label).as_str());
72
73 x_axis.points_begin();
74 x_axis.points_add(self.center.0,self.center.1);
75 x_axis.points_add(self.center.0 + (self.x_axis.0)*scale, self.center.1 + (self.x_axis.1)*scale);
76 x_axis.points_end();
77
78
79 let mut y_axis = Curve::new();
80 y_axis.set_line_width(1.0);
81 y_axis.set_line_color(color);
82 y_axis.set_label(format!("y axis for {:?}",self.label).as_str());
83
84 y_axis.points_begin();
85 y_axis.points_add(self.center.0,self.center.1);
86 y_axis.points_add(self.center.0 + (self.y_axis.0)*scale,self.center.1 + (self.y_axis.1)*scale);
87 y_axis.points_end();
88
89 plot.add(&x_axis);
90 plot.add(&y_axis);
91 }
92
93 }
116
117
118
119
120
121