<commit_before>use super::number::Number;
use super::point::Point;


#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum LineIntersection<T> {
   Point(Point<T>),
   Overlapping,
   None,
}

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub struct Line<T> {
   a: T,
   b: T,
   c: T
}

impl<T> Line<T> where T: Number {
   pub fn new(x1: T, y1: T, x2: T, y2: T) -> Self {
      Line {
         a: y2 - y1,
         b: x1 - x2,
         c: x2 * y1 - x1 * y2
      }
   }

   pub fn intersect(&self, line: &Self) -> LineIntersection<T> {
      let denominator = self.a * line.b - line.a * self.b;

      if denominator == T::zero() {
         if self == line {
            return LineIntersection::Overlapping;
         } else {
            return LineIntersection::None;
         }
      }

      let x = (line.c * self.b - self.c * line.b).rounding_div(denominator);
      let y = (line.a * self.c - self.a * line.c).rounding_div(denominator);

      LineIntersection::Point(Point::new(x, y))
   }
}

#[cfg(test)]
mod tests {
   use super::super::point::Point;

   use super::*;

   #[test]
   fn test_line_new_i64() {
      let ln = Line::new(
         10_000_i64, 20_000_i64,
         -100_000_i64, 0_i64
      );

      assert_eq!(ln.a, -20_000);
      assert_eq!(ln.b, 110_000);
      assert_eq!(ln.c, -2_000_000_000);
   }

   #[test]
   fn test_line_intersect_i64() {
      let l1 = Line::new(
         1_i64, 1_i64,
         7_i64, 5_i64
      );

      let l2 = Line::new(
         4_i64, 5_i64,
         7_i64, 0_i64
      );

      let intersection = l1.intersect(&l2);

      assert_eq!(intersection, LineIntersection::Point(Point::new(5_i64, 4_i64)));
   }

   #[test]
   fn test_line_intersect_mirror_i64() {
      let l1 = Line::new(
         1_i64, -1_i64,
         7_i64, -5_i64
      );

      let l2 = Line::new(
         4_i64, -5_i64,
         7_i64, 0_i64
      );

      let intersection = l1.intersect(&l2);

      assert_eq!(intersection, LineIntersection::Point(Point::new(5_i64, -4_i64)));
   }
}
<commit_msg>Setting Line arguments to f64 to avoid overflowing issues<commit_after>use super::point::Point;


#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum LineIntersection {
   Point(Point),
   Overlapping,
   None,
}

#[derive(Debug, PartialEq, Clone, Copy)]
pub struct Line {
   pub a: f64,
   pub b: f64,
   pub c: f64
}

impl Line {
   pub fn new(x1: i64, y1: i64, x2: i64, y2: i64) -> Self {
      let (x1, y1, x2, y2) = (x1 as f64, y1 as f64, x2 as f64, y2 as f64);
      Line {
         a: y2 - y1,
         b: x1 - x2,
         c: x2 * y1 - x1 * y2
      }
   }

   pub fn intersect(&self, line: &Self) -> LineIntersection {
      let denominator = self.a * line.b - line.a * self.b;

      if denominator == 0. {
         if self == line {
            return LineIntersection::Overlapping;
         } else {
            return LineIntersection::None;
         }
      }

      let x = (line.c * self.b - self.c * line.b) / denominator;
      let y = (line.a * self.c - self.a * line.c) / denominator;

      LineIntersection::Point(Point::new(x.round() as i64, y.round() as i64))
   }
}

#[cfg(test)]
mod tests {
   use super::super::point::Point;

   use super::*;

   #[test]
   fn test_line_new() {
      let ln = Line::new(
         10_000, 20_000,
         -100_000, 0
      );

      assert_eq!(ln.a, -20_000.);
      assert_eq!(ln.b, 110_000.);
      assert_eq!(ln.c, -2_000_000_000.);
   }

   #[test]
   fn test_line_intersect() {
      let l1 = Line::new(
         1, 1,
         7, 5
      );

      let l2 = Line::new(
         4, 5,
         7, 0
      );

      let intersection = l1.intersect(&l2);

      assert_eq!(intersection, LineIntersection::Point(Point::new(5, 4)));
   }

   #[test]
   fn test_line_intersect_mirror() {
      let l1 = Line::new(
         1, -1,
         7, -5
      );

      let l2 = Line::new(
         4, -5,
         7, 0
      );

      let intersection = l1.intersect(&l2);

      assert_eq!(intersection, LineIntersection::Point(Point::new(5, -4)));
   }
}

{'fraction_non_alphanumeric': 0.15192687747035574, 'fraction_numerical': 0.061017786561264824, 'mean_word_length': 1.9815905743740796, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2438150', 'n_tokens_mistral': 1699, 'n_tokens_neox': 1563, 'n_words': 354}