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 proj_x = (point.x*self.x_axis.0 + point.y*self.x_axis.1)/(self.x_axis.0.powi(2) + self.x_axis.1.powi(2));
44 let proj_y = (point.x*self.y_axis.0 + point.y*self.y_axis.1)/(self.y_axis.0.powi(2) + self.y_axis.1.powi(2));
45 Point::new(proj_x,proj_y,Coordinates::RELATIVE(self.clone()))
47 }
48 Coordinates::RELATIVE(_) => {panic!("tried to from_absolute a point in a not absolute coordinate system :( ")}
49 }
50
51 }
52
53
54
55 pub fn plot(&self,plot: &mut Plot,color: &str){
56 let mut x_axis = Curve::new();
57 let scale = 1.0/self.x_axis.0;
58 x_axis.set_line_width(2.0);
59 x_axis.set_line_color(color);
60 x_axis.set_line_style("dashed");
61 x_axis.set_label(format!("x axis for {:?}",self.label).as_str());
62
63 x_axis.points_begin();
64 x_axis.points_add(self.center.0,self.center.1);
65 x_axis.points_add(self.center.0 + (self.x_axis.0)*scale, self.center.1 + (self.x_axis.1)*scale);
66 x_axis.points_end();
67
68
69 let mut y_axis = Curve::new();
70 y_axis.set_line_width(1.0);
71 y_axis.set_line_color(color);
72 y_axis.set_label(format!("y axis for {:?}",self.label).as_str());
73
74 y_axis.points_begin();
75 y_axis.points_add(self.center.0,self.center.1);
76 y_axis.points_add(self.center.0 + (self.y_axis.0)*scale,self.center.1 + (self.y_axis.1)*scale);
77 y_axis.points_end();
78
79 plot.add(&x_axis);
80 plot.add(&y_axis);
81 }
82
83 }
106
107
108
109
110
111