|
1 | 1 | // Copyright © 2019 Saleem Abdulrasool < [email protected]>
|
2 | 2 | // SPDX-License-Identifier: BSD-3-Clause
|
3 | 3 |
|
| 4 | +/// A structure that contains a point in a two-dimensional coordinate system. |
4 | 5 | public struct Point {
|
5 |
| - public static let zero: Point = Point(x: 0, y: 0) |
6 |
| - |
7 |
| - public var x: Double |
8 |
| - public var y: Double |
9 |
| - |
10 |
| - public init(x: Float, y: Float) { |
11 |
| - self.init(x: Double(x), y: Double(y)) |
12 |
| - } |
| 6 | + // MARK - Creating Point Values |
13 | 7 |
|
| 8 | + /// Creates a point with coordinates specified as floating-point values. |
14 | 9 | public init(x: Double, y: Double) {
|
15 | 10 | self.x = x
|
16 | 11 | self.y = y
|
17 | 12 | }
|
18 | 13 |
|
| 14 | + /// Creates a point with coordinates specified as integer values. |
19 | 15 | public init(x: Int, y: Int) {
|
20 | 16 | self.init(x: Double(x), y: Double(y))
|
21 | 17 | }
|
22 | 18 |
|
| 19 | + // MARK - Special Values |
| 20 | + |
| 21 | + /// The point with location (0,0). |
| 22 | + public static var zero: Point { |
| 23 | + Point(x: 0, y: 0) |
| 24 | + } |
| 25 | + |
| 26 | + /// Creates a point with location (0,0). |
| 27 | + public init() { |
| 28 | + self.x = 0.0 |
| 29 | + self.y = 0.0 |
| 30 | + } |
| 31 | + |
| 32 | + // MARK - Geometric Properties |
| 33 | + |
| 34 | + /// The x-coordinate of the point. |
| 35 | + public var x: Double |
| 36 | + |
| 37 | + /// The y-coordinate of the point. |
| 38 | + public var y: Double |
| 39 | + |
| 40 | + // MARK - Transforming Points |
| 41 | + |
| 42 | + /// Returns the point resulting from an affine transformation of an existing |
| 43 | + /// point. |
23 | 44 | public func applying(_ transform: AffineTransform) -> Point {
|
24 | 45 | return Point(x: transform.a * self.x + transform.c * self.y + transform.tx,
|
25 | 46 | y: transform.b * self.x + transform.d * self.y + transform.ty)
|
|
0 commit comments