1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
use std::rc::Rc;
use std::string::String;
use std::sync::Arc;
use std::{fs::File, io::Write, path::Path};

use crate::generator::expression::expr::AnglePoint;
use crate::generator::expression::{Expression, PointExpr, ScalarExpr};
use crate::generator::Complex;
use crate::projector::{
    Output, Rendered, RenderedAngle, RenderedCircle, RenderedLine, RenderedPoint, RenderedRay,
    RenderedSegment,
};
use crate::script::HashableArc;

/// Function getting the point's name (if it exists, if not then it returns the point's coordinates)
fn get_point_name(
    expr: &Arc<Expression<PointExpr>>,
    output: &Output,
    point: Complex,
    scale: f64,
) -> String {
    match output.map.get(&HashableArc::new(Arc::clone(expr))) {
        Some(p) => {
            format!("q{}", p.uuid)
        }
        None => {
            format!("({}, {})", (point.real * scale), point.imaginary * scale)
        }
    }
}

fn points(point: &Rc<RenderedPoint>, scale: f64) -> String {
    let position = point.position * scale;
    format!(
        "\\coordinate [label=left:${}$] ({}) at ({}, {}); \\fill[black] ({}) circle (1pt);",
        point.label, point.uuid, position.real, position.imaginary, point.uuid
    )
}

fn lines(line: &RenderedLine, scale: f64) -> String {
    let pos1 = line.points.0 * scale;
    let pos2 = line.points.1 * scale;
    format!(
        "\\draw ({},{}) -- ({},{});",
        pos1.real, pos1.imaginary, pos2.real, pos2.imaginary
    )
}

fn angles(angle: &RenderedAngle, scale: f64, output: &Output) -> String {
    let no_arcs = String::from("l"); // Requires a change later! It has to be based on info from the script
    match &angle.expr.kind {
        ScalarExpr::AnglePoint(AnglePoint { arm1, origin, arm2 }) => {
            format!(
                r#"
                \begin{{scope}}
                \coordinate (A) at {};
                \coordinate (B) at {};
                \coordinate (C) at {};
                \tkzMarkAngle[size = 0.5,mark = none,arc={no_arcs},mkcolor = black](A,B,C)
                \end{{scope}}
                "#,
                get_point_name(arm1, output, angle.points.0, scale),
                get_point_name(origin, output, angle.points.1, scale),
                get_point_name(arm2, output, angle.points.2, scale),
            )
        }
        // There are hard coded values in \coordinate, it is intentional, every point has it label marked by Rendered::Point sequence above
        ScalarExpr::AngleLine(_) => {
            format!(
                r#"
                \begin{{scope}}
                \coordinate (A) at ({}, {});
                \coordinate (B) at ({}, {});
                \coordinate (C) at ({}, {});
                \tkzMarkAngle[size = 2,mark = none,arc={no_arcs},mkcolor = black](A,B,C)
                \end{{scope}}
                "#,
                angle.points.0.real,
                angle.points.0.imaginary,
                angle.points.1.real,
                angle.points.1.imaginary,
                angle.points.2.real,
                angle.points.2.imaginary
            )
        }
        _ => unreachable!(),
    }
}

fn segments(segment: &RenderedSegment, scale: f64) -> String {
    let pos1 = segment.points.0 * scale;
    let pos2 = segment.points.1 * scale;
    format!(
        r#"
        \begin{{scope}}
        \coordinate (A) at ({}, {});
        \coordinate (B) at ({}, {});
        \tkzDrawSegment[thin](A,B)
        \end{{scope}}
        "#,
        pos1.real, pos1.imaginary, pos2.real, pos2.imaginary,
    )
}

fn rays(ray: &RenderedRay, scale: f64) -> String {
    let pos1 = ray.points.0 * scale;
    let pos2 = ray.points.1 * scale;
    format!(
        r#"
        \begin{{scope}}
        \coordinate (A) at ({}, {});
        \coordinate (B) at ({}, {});
        \tkzDrawSegment[thin](A,B)
        \end{{scope}}
        "#,
        pos1.real, pos1.imaginary, pos2.real, pos2.imaginary
    )
}

fn circles(circle: &RenderedCircle, scale: f64) -> String {
    let pos1 = circle.center * scale;
    let pos2 = circle.draw_point * scale;
    format!(
        r#"
        \begin{{scope}}
        \coordinate (A) at ({}, {});
        \coordinate (B) at ({}, {});
        \tkzDrawCircle(A,B)
        \end{{scope}}
        "#,
        pos1.real, pos1.imaginary, pos2.real, pos2.imaginary
    )
}
/// Draws the given figure to a .tex file using tikz library.
///
/// # Panics
/// Panics whenever there is a filesystem related problem.
pub fn draw(target: &Path, canvas_size: (usize, usize), output: &Output) {
    // We must allow losing precision here.
    #[allow(clippy::cast_precision_loss)]
    let scale = f64::min(20.0 / canvas_size.0 as f64, 20.0 / canvas_size.1 as f64);
    let mut content = String::from(
        r#"
    \documentclass{article}
    \usepackage{tikz}
    \usepackage{tkz-euclide}
    \usetikzlibrary {angles,calc,quotes}
    \begin{document}
    \begin{tikzpicture}
    "#,
    );
    for item in &output.vec_rendered {
        match item {
            Rendered::Point(point) => {
                content += &points(point, scale);
            }
            Rendered::Line(line) => {
                content += &lines(line, scale);
            }
            Rendered::Angle(angle) => {
                content += &angles(angle, scale, output);
            }
            Rendered::Segment(segment) => {
                content += &segments(segment, scale);
            }
            Rendered::Ray(ray) => {
                content += &rays(ray, scale);
            }
            Rendered::Circle(circle) => {
                content += &circles(circle, scale);
            }
        }
    }
    content += "\\end{tikzpicture} \\end{document}";

    let mut file = File::create(target).unwrap();
    file.write_all(content.as_bytes()).unwrap();
}