How to resolve the algorithm Find the intersection of two lines step by step in the Swift 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 Swift 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 Swift programming language
Source code in the swift programming language
struct Point {
var x: Double
var y: Double
}
struct Line {
var p1: Point
var p2: Point
var slope: Double {
guard p1.x - p2.x != 0.0 else { return .nan }
return (p1.y-p2.y) / (p1.x-p2.x)
}
func intersection(of other: Line) -> Point? {
let ourSlope = slope
let theirSlope = other.slope
guard ourSlope != theirSlope else { return nil }
if ourSlope.isNaN && !theirSlope.isNaN {
return Point(x: p1.x, y: (p1.x - other.p1.x) * theirSlope + other.p1.y)
} else if theirSlope.isNaN && !ourSlope.isNaN {
return Point(x: other.p1.x, y: (other.p1.x - p1.x) * ourSlope + p1.y)
} else {
let x = (ourSlope*p1.x - theirSlope*other.p1.x + other.p1.y - p1.y) / (ourSlope - theirSlope)
return Point(x: x, y: theirSlope*(x - other.p1.x) + other.p1.y)
}
}
}
let l1 = Line(p1: Point(x: 4.0, y: 0.0), p2: Point(x: 6.0, y: 10.0))
let l2 = Line(p1: Point(x: 0.0, y: 3.0), p2: Point(x: 10.0, y: 7.0))
print("Intersection at : \(l1.intersection(of: l2)!)")
You may also check:How to resolve the algorithm Real constants and functions step by step in the Jsish programming language
You may also check:How to resolve the algorithm Benford's law step by step in the Oberon-2 programming language
You may also check:How to resolve the algorithm Loops/Nested step by step in the E programming language
You may also check:How to resolve the algorithm Run-length encoding step by step in the sed programming language
You may also check:How to resolve the algorithm Hello world/Newline omission step by step in the Standard ML programming language