-
Notifications
You must be signed in to change notification settings - Fork 53
/
Line.swift
82 lines (76 loc) · 3.36 KB
/
Line.swift
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
//
// Line.swift
// Euclid
//
// Created by Nick Lockwood on 20/11/2019.
// Copyright © 2018 Nick Lockwood. All rights reserved.
//
// Distributed under the permissive MIT license
// Get the latest version from here:
//
// https://github.com/nicklockwood/Euclid
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
public struct Line: Hashable {
public init(point: Vector, direction: Vector) {
self.point = point
self.direction = direction
}
public init(from: LineSegment) {
point = from.point1
direction = from.direction
}
public var point: Vector {
didSet { point = point.quantized() }
}
public var direction: Vector {
didSet { direction = direction.normalized() }
}
public func distance(to: Vector) -> Double {
// See "Vector formulation" at https://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line
let aMinusP = point - to
let v = aMinusP - (direction * aMinusP.dot(direction))
return v.length
}
public func intersection(with: Line) -> Vector? {
if direction.z == 0, with.direction.z == 0, point.z == with.point.z {
return lineIntersection(point, point + direction, with.point, with.point + with.direction)
} else if direction.y == 0, with.direction.y == 0, point.y == with.point.y {
// Switch dimensions and then solve
let p0 = Vector(point.x, point.z, point.y)
let p1 = p0 + Vector(direction.x, direction.z, 0)
let p2 = Vector(with.point.x, with.point.z, with.point.y)
let p3 = p2 + Vector(with.direction.x, with.direction.z, 0)
let solution = lineIntersection(p0, p1, p2, p3)
return solution.map { Vector($0.x, $0.z, $0.y) }
} else if direction.x == 0, with.direction.x == 0, point.x == with.point.x {
// Switch dimensions and then solve
let p0 = Vector(point.y, point.z, point.x)
let p1 = p0 + Vector(direction.y, direction.z, 0)
let p2 = Vector(with.point.y, with.point.z, with.point.x)
let p3 = p2 + Vector(with.direction.y, with.direction.z, 0)
let solution = lineIntersection(p0, p1, p2, p3)
return solution.map { Vector($0.z, $0.x, $0.y) }
} else {
// TODO: Generalize to 3D
return nil
}
}
}