From 4b73295140df7cb645431aeb01b867f944725ef8 Mon Sep 17 00:00:00 2001 From: Jaap Wijnen Date: Thu, 26 Oct 2017 02:21:17 +0200 Subject: [PATCH 1/4] initial points lines planes commit --- .../project.pbxproj | 296 ++++++++++++++++++ .../contents.xcworkspacedata | 7 + .../Points Lines Planes/2D/Line2D.swift | 161 ++++++++++ .../Points Lines Planes/2D/Point2D.swift | 40 +++ .../Points Lines Planes/main.swift | 24 ++ Points Lines Planes/README.md | 34 ++ 6 files changed, 562 insertions(+) create mode 100644 Points Lines Planes/Points Lines Planes.xcodeproj/project.pbxproj create mode 100644 Points Lines Planes/Points Lines Planes.xcodeproj/project.xcworkspace/contents.xcworkspacedata create mode 100644 Points Lines Planes/Points Lines Planes/2D/Line2D.swift create mode 100644 Points Lines Planes/Points Lines Planes/2D/Point2D.swift create mode 100644 Points Lines Planes/Points Lines Planes/main.swift create mode 100644 Points Lines Planes/README.md diff --git a/Points Lines Planes/Points Lines Planes.xcodeproj/project.pbxproj b/Points Lines Planes/Points Lines Planes.xcodeproj/project.pbxproj new file mode 100644 index 000000000..aeba4777b --- /dev/null +++ b/Points Lines Planes/Points Lines Planes.xcodeproj/project.pbxproj @@ -0,0 +1,296 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 48; + objects = { + +/* Begin PBXBuildFile section */ + 8EA8A3BB1F9F7B6300FD8BC0 /* Point2D.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8EA8A3BA1F9F7B6300FD8BC0 /* Point2D.swift */; }; + 8EA8A3BD1F9F7B7100FD8BC0 /* Line2D.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8EA8A3BC1F9F7B7100FD8BC0 /* Line2D.swift */; }; + 8EF612481F9E00E000267358 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8EF612471F9E00E000267358 /* main.swift */; }; +/* End PBXBuildFile section */ + +/* Begin PBXCopyFilesBuildPhase section */ + 8EF612421F9E00E000267358 /* CopyFiles */ = { + isa = PBXCopyFilesBuildPhase; + buildActionMask = 2147483647; + dstPath = /usr/share/man/man1/; + dstSubfolderSpec = 0; + files = ( + ); + runOnlyForDeploymentPostprocessing = 1; + }; +/* End PBXCopyFilesBuildPhase section */ + +/* Begin PBXFileReference section */ + 8EA8A3BA1F9F7B6300FD8BC0 /* Point2D.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Point2D.swift; sourceTree = ""; }; + 8EA8A3BC1F9F7B7100FD8BC0 /* Line2D.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Line2D.swift; sourceTree = ""; }; + 8EF612441F9E00E000267358 /* Points Lines Planes */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = "Points Lines Planes"; sourceTree = BUILT_PRODUCTS_DIR; }; + 8EF612471F9E00E000267358 /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = ""; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + 8EF612411F9E00E000267358 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + 8EA8A3C21F9F7C9000FD8BC0 /* 2D */ = { + isa = PBXGroup; + children = ( + 8EA8A3BA1F9F7B6300FD8BC0 /* Point2D.swift */, + 8EA8A3BC1F9F7B7100FD8BC0 /* Line2D.swift */, + ); + path = 2D; + sourceTree = ""; + }; + 8EF6123B1F9E00DF00267358 = { + isa = PBXGroup; + children = ( + 8EF612461F9E00E000267358 /* Points Lines Planes */, + 8EF612451F9E00E000267358 /* Products */, + ); + sourceTree = ""; + }; + 8EF612451F9E00E000267358 /* Products */ = { + isa = PBXGroup; + children = ( + 8EF612441F9E00E000267358 /* Points Lines Planes */, + ); + name = Products; + sourceTree = ""; + }; + 8EF612461F9E00E000267358 /* Points Lines Planes */ = { + isa = PBXGroup; + children = ( + 8EF612471F9E00E000267358 /* main.swift */, + 8EA8A3C21F9F7C9000FD8BC0 /* 2D */, + ); + path = "Points Lines Planes"; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXNativeTarget section */ + 8EF612431F9E00E000267358 /* Points Lines Planes */ = { + isa = PBXNativeTarget; + buildConfigurationList = 8EF6124B1F9E00E000267358 /* Build configuration list for PBXNativeTarget "Points Lines Planes" */; + buildPhases = ( + 8EF612401F9E00E000267358 /* Sources */, + 8EF612411F9E00E000267358 /* Frameworks */, + 8EF612421F9E00E000267358 /* CopyFiles */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = "Points Lines Planes"; + productName = "Points Lines Planes"; + productReference = 8EF612441F9E00E000267358 /* Points Lines Planes */; + productType = "com.apple.product-type.tool"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + 8EF6123C1F9E00DF00267358 /* Project object */ = { + isa = PBXProject; + attributes = { + LastSwiftUpdateCheck = 0900; + LastUpgradeCheck = 0900; + ORGANIZATIONNAME = Workmoose; + TargetAttributes = { + 8EF612431F9E00E000267358 = { + CreatedOnToolsVersion = 9.0.1; + ProvisioningStyle = Automatic; + }; + }; + }; + buildConfigurationList = 8EF6123F1F9E00DF00267358 /* Build configuration list for PBXProject "Points Lines Planes" */; + compatibilityVersion = "Xcode 8.0"; + developmentRegion = en; + hasScannedForEncodings = 0; + knownRegions = ( + en, + ); + mainGroup = 8EF6123B1F9E00DF00267358; + productRefGroup = 8EF612451F9E00E000267358 /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + 8EF612431F9E00E000267358 /* Points Lines Planes */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXSourcesBuildPhase section */ + 8EF612401F9E00E000267358 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 8EA8A3BB1F9F7B6300FD8BC0 /* Point2D.swift in Sources */, + 8EF612481F9E00E000267358 /* main.swift in Sources */, + 8EA8A3BD1F9F7B7100FD8BC0 /* Line2D.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin XCBuildConfiguration section */ + 8EF612491F9E00E000267358 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + CODE_SIGN_IDENTITY = "Mac Developer"; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = dwarf; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_TESTABILITY = YES; + GCC_C_LANGUAGE_STANDARD = gnu11; + GCC_DYNAMIC_NO_PIC = NO; + GCC_NO_COMMON_BLOCKS = YES; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + ); + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + MACOSX_DEPLOYMENT_TARGET = 10.13; + MTL_ENABLE_DEBUG_INFO = YES; + ONLY_ACTIVE_ARCH = YES; + SDKROOT = macosx; + SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + }; + name = Debug; + }; + 8EF6124A1F9E00E000267358 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + CODE_SIGN_IDENTITY = "Mac Developer"; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + ENABLE_NS_ASSERTIONS = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; + GCC_C_LANGUAGE_STANDARD = gnu11; + GCC_NO_COMMON_BLOCKS = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + MACOSX_DEPLOYMENT_TARGET = 10.13; + MTL_ENABLE_DEBUG_INFO = NO; + SDKROOT = macosx; + SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; + }; + name = Release; + }; + 8EF6124C1F9E00E000267358 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = 7C4LVS3ZVC; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 4.0; + }; + name = Debug; + }; + 8EF6124D1F9E00E000267358 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = 7C4LVS3ZVC; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 4.0; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + 8EF6123F1F9E00DF00267358 /* Build configuration list for PBXProject "Points Lines Planes" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 8EF612491F9E00E000267358 /* Debug */, + 8EF6124A1F9E00E000267358 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + 8EF6124B1F9E00E000267358 /* Build configuration list for PBXNativeTarget "Points Lines Planes" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 8EF6124C1F9E00E000267358 /* Debug */, + 8EF6124D1F9E00E000267358 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; +/* End XCConfigurationList section */ + }; + rootObject = 8EF6123C1F9E00DF00267358 /* Project object */; +} diff --git a/Points Lines Planes/Points Lines Planes.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/Points Lines Planes/Points Lines Planes.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 000000000..82bfe2b7d --- /dev/null +++ b/Points Lines Planes/Points Lines Planes.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/Points Lines Planes/Points Lines Planes/2D/Line2D.swift b/Points Lines Planes/Points Lines Planes/2D/Line2D.swift new file mode 100644 index 000000000..c92cbdbed --- /dev/null +++ b/Points Lines Planes/Points Lines Planes/2D/Line2D.swift @@ -0,0 +1,161 @@ +// +// Line2D.swift +// Points Lines Planes +// +// Created by Jaap Wijnen on 24-10-17. +// + +struct Line2D: Equatable { + + var slope: Slope + var offset: Double + var direction: Direction + + enum Slope: Equatable { + case finite(slope: Double) + case infinite(offset: Double) + + static func ==(lhs: Slope, rhs: Slope) -> Bool { + switch (lhs, rhs) { + case (.finite(let slope1), .finite(let slope2)): return slope1 == slope2 + case (.infinite(let offset1), .infinite(let offset2)): return offset1 == offset2 + default: return false + } + } + } + + enum Direction: Equatable { + case increasing + case decreasing + + static func ==(lhs: Direction, rhs: Direction) -> Bool { + switch (lhs, rhs) { + case (.increasing, .increasing): return true + case (.decreasing, .decreasing): return true + default: return false + } + } + } + + init(from p1: Point2D, to p2: Point2D) { + if p1 == p2 { fatalError("Points can not be equal when creating a line between them") } + if p1.x == p2.x { + self.slope = .infinite(offset: p1.x) + self.offset = 0 + self.direction = p1.y < p2.y ? .increasing : .decreasing + + return + } + + let slope = (p1.y - p2.y)/(p1.x - p2.x) + self.slope = .finite(slope: slope) + offset = (p1.y + p2.y - slope * (p1.x + p2.x))/2 + if slope >= 0 { + // so a horizontal line going left to right is called increasing + self.direction = p1.x < p2.x ? .increasing : .decreasing + } else { + self.direction = p1.x < p2.x ? .decreasing : .increasing + } + } + + fileprivate init(slope: Slope, offset: Double, direction: Direction) { + self.slope = slope + self.offset = offset + self.direction = direction + } + + // returns y coordinate on line for given x + func y(at x: Double) -> Double { + switch self.slope { + case .finite(let slope): + return slope * x + self.offset + case .infinite: + fatalError("y can be anywhere on vertical line") + } + } + + // returns x coordinate on line for given y + func x(at y: Double) -> Double { + switch self.slope { + case .finite(let slope): + if slope == 0 { + fatalError("x can be anywhere on horizontal line") + } + return (y - self.offset)/slope + case .infinite(let offset): + return offset + } + } + + // finds intersection point between two lines. returns nil when lines don't intersect or lie on top of each other. + func intersect(with line: Line2D) -> Point2D? { + if self == line { return nil } + switch (self.slope, line.slope) { + case (.infinite, .infinite): + // lines are either parallel or on top of each other. + return nil + case (.finite(let slope1), .finite(let slope2)): + if slope1 == slope2 { return nil } // lines are parallel + // lines are not parallel calculate intersection point + let x = (line.offset - self.offset)/(slope1 - slope2) + let y = (slope1 + slope2) * x + self.offset + line.offset + return Point2D(x: x, y: y) + case (.infinite(let offset), .finite): + // one line is vertical so we only check what y value the other line has at that point + let x = offset + let y = line.y(at: x) + return Point2D(x: x, y: y) + case (.finite, .infinite(let offset)): + // one line is vertical so we only check what y value the other line has at that point + // lines are switched with respect to case above this one + let x = offset + let y = self.y(at: x) + return Point2D(x: x, y: y) + } + } + + // returns a line perpendicular to self at the given y coordinate + // direction of perpendicular lines always changes clockwise + func perpendicularLineAt(y: Double) -> Line2D { + return perpendicularLineAt(p: Point2D(x: self.x(at: y), y: y)) + } + + // returns a line perpendicular to self at the given x coordinate + // direction of perpendicular lines always changes clockwise + func perpendicularLineAt(x: Double) -> Line2D { + return perpendicularLineAt(p: Point2D(x: x, y: self.y(at: x))) + } + + private func perpendicularLineAt(p: Point2D) -> Line2D { + switch self.slope { + case .finite(let slope): + if slope == 0 { + // line is horizontal so new line will be vertical + let dir: Direction = self.direction == .increasing ? .decreasing : .increasing + return Line2D(slope: .infinite(offset: p.x), offset: 0, direction: dir) + } + + // line is neither horizontal nor vertical + // we make a new line through the point p with new slope -1/slope + let offset = (slope + 1/slope)*p.x + self.offset + + // determine direction of new line based on direction of the old line and its slope + let dir: Direction + switch self.direction { + case .increasing: + dir = slope > 0 ? .decreasing : .increasing + case .decreasing: + dir = slope > 0 ? .increasing : .decreasing + } + return Line2D(slope: .finite(slope: -1/slope), offset: offset, direction: dir) + case .infinite: + // line is vertical so new line will be horizontal + let dir: Direction = self.direction == .increasing ? .increasing : .decreasing + return Line2D(slope: .finite(slope: 0), offset: p.y, direction: dir) + } + } + + static func ==(lhs: Line2D, rhs: Line2D) -> Bool { + return lhs.slope == rhs.slope && lhs.offset == rhs.offset && lhs.direction == rhs.direction + } +} diff --git a/Points Lines Planes/Points Lines Planes/2D/Point2D.swift b/Points Lines Planes/Points Lines Planes/2D/Point2D.swift new file mode 100644 index 000000000..f51ffd64d --- /dev/null +++ b/Points Lines Planes/Points Lines Planes/2D/Point2D.swift @@ -0,0 +1,40 @@ +// +// Point2D.swift +// Points Lines Planes +// +// Created by Jaap Wijnen on 24-10-17. +// + +struct Point2D: Equatable { + var x: Double + var y: Double + + static func ==(lhs: Point2D, rhs: Point2D) -> Bool { + return lhs.x == rhs.x && lhs.y == rhs.y + } + + // returns true if point is on or right of line + func isRight(of line: Line2D) -> Bool { + switch line.slope { + case .finite: + let y = line.y(at: self.x) + switch line.direction { + case .increasing: + return y >= self.y + case .decreasing: + return y <= self.y + } + case .infinite(let offset): + switch line.direction { + case .increasing: + return self.x >= offset + case .decreasing: + return self.x <= offset + } + } + } + + func isLeft(of line: Line2D) -> Bool { + return !self.isRight(of: line) + } +} diff --git a/Points Lines Planes/Points Lines Planes/main.swift b/Points Lines Planes/Points Lines Planes/main.swift new file mode 100644 index 000000000..02137fc9d --- /dev/null +++ b/Points Lines Planes/Points Lines Planes/main.swift @@ -0,0 +1,24 @@ +// +// main.swift +// Points Lines Planes +// +// Created by Jaap Wijnen on 23-10-17. +// + +let p0 = Point2D(x: 0, y: 2) +let p1 = Point2D(x: 1, y: 1) +let p2 = Point2D(x: 1, y: 3) +let p3 = Point2D(x: 3, y: 1) +let p4 = Point2D(x: 3, y: 3) + +let horLine = Line2D(from: p1, to: p3) +let verLine = Line2D(from: p1, to: p2) +let line45 = Line2D(from: p1, to: p4) + +print(horLine.intersect(with: verLine)) +print(p0.isRight(of: line45)) +print(p0.isRight(of: horLine)) +print(p0.isRight(of: verLine)) +let lineperp = horLine.perpendicularLineAt(x: 2) +print(lineperp) + diff --git a/Points Lines Planes/README.md b/Points Lines Planes/README.md new file mode 100644 index 000000000..23c965978 --- /dev/null +++ b/Points Lines Planes/README.md @@ -0,0 +1,34 @@ +# Points Lines (Planes) +This implements data structures for points lines and planes(not yet) in (for now) 2D space and a few functions to play around with them. This was originally written to improve on the Convex Hull algorithm but I thought it might be a nice addition in itself. Im planning to add 3D implementations as well. + +# implementation +Two `struct`s are implemented the `Point2D` and `Line2D`. + +``` +struct Point2D: { + var x: Double + var y: double +} +``` + +``` +struct Line2D { + var slope: Slope + var offset: Double + var direction: Direction +} +``` +Here `Slope` is an enum to account for vertical lines. +slope is infinite for vertical lines, offset is the x coordinate where the line crosses the line `y=0`. +slope is finite for any other line and contains a double with the actual value of the slope. +``` +enum Slope { + case finite(slope: Double) + case infinite(offset: Double) +} +``` +`Line2D` also contains a `Direction` enum. This is introduced in order to make lines directional in order to be able to determine what is left and right of a certain line. It is `.increasing` if the line points in positive y direction and `.decreasing` if it points in the negative y direction. + +`Line2D`'s offset is the the y-coordinate where the line crosses the vertical x=0 line + +*Written for the Swift Algorithm Club by Jaap Wijnen.* From 7f6f5a5a56baa1a1bb6854dd2a29699f1be2c558 Mon Sep 17 00:00:00 2001 From: Jaap Wijnen Date: Thu, 26 Oct 2017 02:40:29 +0200 Subject: [PATCH 2/4] typo --- Points Lines Planes/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Points Lines Planes/README.md b/Points Lines Planes/README.md index 23c965978..2d36667ec 100644 --- a/Points Lines Planes/README.md +++ b/Points Lines Planes/README.md @@ -29,6 +29,6 @@ enum Slope { ``` `Line2D` also contains a `Direction` enum. This is introduced in order to make lines directional in order to be able to determine what is left and right of a certain line. It is `.increasing` if the line points in positive y direction and `.decreasing` if it points in the negative y direction. -`Line2D`'s offset is the the y-coordinate where the line crosses the vertical x=0 line +`Line2D`'s offset is the the y-coordinate where the line crosses the vertical `x=0` line. *Written for the Swift Algorithm Club by Jaap Wijnen.* From 04b2d3010b3db16b1bfd472e9686364bbf785a94 Mon Sep 17 00:00:00 2001 From: Kelvin Lau Date: Fri, 8 Mar 2019 23:13:13 -0500 Subject: [PATCH 3/4] Updates for Swift 5.0 Removes unnecessary equatable implementations, since default implementations of `Equatable` has identical result. --- .../Points Lines Planes/2D/Line2D.swift | 20 ------------------- 1 file changed, 20 deletions(-) diff --git a/Points Lines Planes/Points Lines Planes/2D/Line2D.swift b/Points Lines Planes/Points Lines Planes/2D/Line2D.swift index c92cbdbed..c23040b14 100644 --- a/Points Lines Planes/Points Lines Planes/2D/Line2D.swift +++ b/Points Lines Planes/Points Lines Planes/2D/Line2D.swift @@ -14,27 +14,11 @@ struct Line2D: Equatable { enum Slope: Equatable { case finite(slope: Double) case infinite(offset: Double) - - static func ==(lhs: Slope, rhs: Slope) -> Bool { - switch (lhs, rhs) { - case (.finite(let slope1), .finite(let slope2)): return slope1 == slope2 - case (.infinite(let offset1), .infinite(let offset2)): return offset1 == offset2 - default: return false - } - } } enum Direction: Equatable { case increasing case decreasing - - static func ==(lhs: Direction, rhs: Direction) -> Bool { - switch (lhs, rhs) { - case (.increasing, .increasing): return true - case (.decreasing, .decreasing): return true - default: return false - } - } } init(from p1: Point2D, to p2: Point2D) { @@ -154,8 +138,4 @@ struct Line2D: Equatable { return Line2D(slope: .finite(slope: 0), offset: p.y, direction: dir) } } - - static func ==(lhs: Line2D, rhs: Line2D) -> Bool { - return lhs.slope == rhs.slope && lhs.offset == rhs.offset && lhs.direction == rhs.direction - } } From 339cc1489b3b70e7464db4af7e8f398d45ce50db Mon Sep 17 00:00:00 2001 From: Kelvin Lau Date: Fri, 8 Mar 2019 23:13:42 -0500 Subject: [PATCH 4/4] Updates to Swift 5.0 Removes `Equatable` impl. --- Points Lines Planes/Points Lines Planes/2D/Point2D.swift | 4 ---- 1 file changed, 4 deletions(-) diff --git a/Points Lines Planes/Points Lines Planes/2D/Point2D.swift b/Points Lines Planes/Points Lines Planes/2D/Point2D.swift index f51ffd64d..bbd8305f8 100644 --- a/Points Lines Planes/Points Lines Planes/2D/Point2D.swift +++ b/Points Lines Planes/Points Lines Planes/2D/Point2D.swift @@ -9,10 +9,6 @@ struct Point2D: Equatable { var x: Double var y: Double - static func ==(lhs: Point2D, rhs: Point2D) -> Bool { - return lhs.x == rhs.x && lhs.y == rhs.y - } - // returns true if point is on or right of line func isRight(of line: Line2D) -> Bool { switch line.slope {