geo_aid_script/unroll/library/
intersection.rs

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
//! The `intersection` function

use num_traits::One;

use crate::{math::Build, token::number::ProcNum};

use super::prelude::*;

/// `intesection(line, line)` - intersection of two lines.
fn intersection_function_ll(
    k: Expr<Line>,
    l: Expr<Line>,
    context: &CompileContext,
    display: Properties,
) -> Expr<Point> {
    // println!("{display:#?}");
    let mut expr = context.intersection_display(k, l, display);

    if let Some(node) = &mut expr.node {
        node.set_associated(Associated);
    }

    expr
}

/// `intesection(line, circle)` - intersection of a line and a circle.
fn intersection_function_lc(
    k: Expr<Line>,
    omega: Expr<Circle>,
    context: &mut CompileContext,
    display: Properties,
) -> Expr<Point> {
    let mut point = context.free_point_display(display);

    context.point_on_line(&point, &k, ProcNum::one());
    context.point_on_circle(&point, &omega, ProcNum::one());

    if let Some(node) = &mut point.node {
        node.set_associated(Associated);
    }

    point
}

/// `intesection(circle, circle)` - intersection of two circle.
fn intersection_function_cc(
    o1: Expr<Circle>,
    o2: Expr<Circle>,
    context: &mut CompileContext,
    display: Properties,
) -> Expr<Point> {
    let mut point = context.free_point_display(display);

    context.point_on_circle(&point, &o1, ProcNum::one());
    context.point_on_circle(&point, &o2, ProcNum::one());

    if let Some(node) = &mut point.node {
        node.set_associated(Associated);
    }

    point
}

/// The associated data. No properties.
#[derive(Debug)]
pub struct Associated;

impl BuildAssociated<PointNode> for Associated {
    fn build_associated(
        self: Box<Self>,
        _build: &mut Build,
        associated: &mut HierarchyNode<PointNode>,
    ) {
        associated.root.display_dot.set_if_unset(false);
    }
}

/// Register the function
pub fn register(library: &mut Library) {
    library.add(
        Function::new("intersection")
            .overload(intersection_function_ll)
            .overload(intersection_function_lc)
            .overload(intersection_function_cc),
    );
}