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
use crate::continence::*;
use crate::encroachment::*;
use crate::distance::*;
use crate::vertex::*;
use std::rc::Rc;

use std::hash::Hash;
use std::cmp::Eq;

use std::fmt;
use std::fmt::Debug;

#[derive(Hash, Debug)]
pub struct Edge {
    pub v1: Rc<Vertex>,
    pub v2: Rc<Vertex>,
}

impl PartialEq for Edge {
    fn eq(&self, other: &Self) -> bool {
        /* oriented edge */
        self.v1 == other.v1 && self.v2 == other.v2
    }
}

impl Eq for Edge {}

impl fmt::Display for Edge {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        return write!(f, "({} - {})", self.v1, self.v2);
    }
}

impl Edge {
    pub fn new(v1: &Rc<Vertex>, v2: &Rc<Vertex>) -> Self {
        Self {
            v1: Rc::clone(v1),
            v2: Rc::clone(v2),
        }
    }

    pub fn opposite(&self) -> Self {
        Self {
            v1: Rc::clone(&self.v2),
            v2: Rc::clone(&self.v1),
        }
    }

    pub fn length(&self) -> f64 {
        return distance(&self.v1, &self.v2);
    }

    pub fn encroach(&self, vertex: &Vertex) -> Continence {
        return encroach(&self.v1, &self.v2, vertex);
    }

    pub fn midpoint(&self) -> Vertex {
        let x1 = self.v1.x;
        let y1 = self.v1.y;
        let x2 = self.v2.x;
        let y2 = self.v2.y;

        let x_mid = (x1 + x2) / 2.0;
        let y_mid = (y1 + y2) / 2.0;
        
        return Vertex::new(x_mid, y_mid);
    }

    pub fn from_coordinates(coordinates: &Vec<f64>) -> Vec<Rc<Edge>> {
        if coordinates.len() % 2 != 0 {
            panic!("Vec must provide vertices by pair of x,y coordinates.");
        }

        let vertices_list = Vertex::from_coordinates(coordinates);
        let mut edge_list: Vec<Rc<Edge>> = Vec::new();

        for index in 0..vertices_list.len() {
            let v1 = vertices_list.get(index).unwrap();
            let v2 = match vertices_list.get(index + 1) {
                Some(vertex) => vertex,
                None => vertices_list.get(0).unwrap(),
            };
            let new_edge = Rc::new(Edge::new(v1, v2));
            edge_list.push(new_edge);
        }

        return edge_list;
    }

    pub fn from_vertices(vertices_list: &Vec<Rc<Vertex>>) -> Vec<Rc<Edge>> {
        let mut edge_list: Vec<Rc<Edge>> = Vec::new();

        for index in 0..vertices_list.len() {
            let v1 = vertices_list.get(index).unwrap();
            let v2 = match vertices_list.get(index + 1) {
                Some(vertex) => vertex,
                None => vertices_list.get(0).unwrap(),
            };
            let new_edge = Rc::new(Edge::new(v1, v2));
            edge_list.push(new_edge);
        }

        return edge_list;
    }
}

#[cfg(test)]
mod midpoint {
    use super::*;

    #[test]
    fn test_midpoint_calculation() {
        let v1 = Rc::new(Vertex::new(0.0, 0.0));
        let v2 = Rc::new(Vertex::new(1.0, 1.2));
        
        let edge = Edge::new(&v1, &v2);
        let midpoint = edge.midpoint();
        assert_eq!(midpoint.x, 0.5);
        assert_eq!(midpoint.y, 0.6);
    }
}

#[cfg(test)]
mod equality {
    use super::*;

    #[test]
    fn test_different_objects() {
        let v1 = Rc::new(Vertex::new(0.0, 0.0));
        let v2 = Rc::new(Vertex::new(1.0, 1.2));
        
        let e1 = Edge::new(&v1, &v2);
        let e2 = Edge::new(&v1, &v2);
        assert!(e1 == e2);

        let e1 = Rc::new(Edge::new(&v1, &v2));
        let e2 = Rc::new(Edge::new(&v1, &v2));
        assert!(e1 == e2);
    }
    
    #[test]
    fn test_half_edge() {
        let v1 = Rc::new(Vertex::new(0.0, 0.0));
        let v2 = Rc::new(Vertex::new(1.0, 1.2));
        
        let e1 = Edge::new(&v1, &v2);
        let e2 = Edge::new(&v2, &v1);
        assert!(e1 != e2);
        
        let e1 = Rc::new(Edge::new(&v1, &v2));
        let e2 = Rc::new(Edge::new(&v2, &v1));
        assert!(e1 != e2);
    }
}