How to resolve the algorithm Find the intersection of two lines step by step in the Rust programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Find the intersection of two lines step by step in the Rust programming language
Table of Contents
Problem Statement
Find the point of intersection of two lines in 2D.
The 1st line passes though (4,0) and (6,10) . The 2nd line passes though (0,3) and (10,7) .
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Find the intersection of two lines step by step in the Rust programming language
Source code in the rust programming language
#[derive(Copy, Clone, Debug)]
struct Point {
x: f64,
y: f64,
}
impl Point {
pub fn new(x: f64, y: f64) -> Self {
Point { x, y }
}
}
#[derive(Copy, Clone, Debug)]
struct Line(Point, Point);
impl Line {
pub fn intersect(self, other: Self) -> Option<Point> {
let a1 = self.1.y - self.0.y;
let b1 = self.0.x - self.1.x;
let c1 = a1 * self.0.x + b1 * self.0.y;
let a2 = other.1.y - other.0.y;
let b2 = other.0.x - other.1.x;
let c2 = a2 * other.0.x + b2 * other.0.y;
let delta = a1 * b2 - a2 * b1;
if delta == 0.0 {
return None;
}
Some(Point {
x: (b2 * c1 - b1 * c2) / delta,
y: (a1 * c2 - a2 * c1) / delta,
})
}
}
fn main() {
let l1 = Line(Point::new(4.0, 0.0), Point::new(6.0, 10.0));
let l2 = Line(Point::new(0.0, 3.0), Point::new(10.0, 7.0));
println!("{:?}", l1.intersect(l2));
let l1 = Line(Point::new(0.0, 0.0), Point::new(1.0, 1.0));
let l2 = Line(Point::new(1.0, 2.0), Point::new(4.0, 5.0));
println!("{:?}", l1.intersect(l2));
}
You may also check:How to resolve the algorithm Pernicious numbers step by step in the VBA programming language
You may also check:How to resolve the algorithm Digital root/Multiplicative digital root step by step in the CLU programming language
You may also check:How to resolve the algorithm String concatenation step by step in the AWK programming language
You may also check:How to resolve the algorithm Fork step by step in the Aikido programming language
You may also check:How to resolve the algorithm Price fraction step by step in the Raku programming language