├── .gitignore ├── .swift-version ├── LICENSE ├── README.md ├── Triangulation.podspec ├── Triangulation.xcodeproj ├── project.pbxproj ├── project.xcworkspace │ ├── contents.xcworkspacedata │ └── xcshareddata │ │ └── IDEWorkspaceChecks.plist └── xcshareddata │ └── xcschemes │ └── Triangulation.xcscheme ├── Triangulation ├── Circumcircle.swift ├── Delaunay.swift ├── Edge.swift ├── Extension.swift ├── Hash.swift ├── Info.plist ├── Triangle.swift ├── Triangulation.h ├── TriangulationView.swift └── Vertex.swift ├── TriangulationDemo ├── AppDelegate.swift ├── Assets.xcassets │ ├── AppIcon.appiconset │ │ └── Contents.json │ └── Contents.json ├── Base.lproj │ ├── LaunchScreen.storyboard │ └── Main.storyboard ├── IMG_1043.JPG ├── Info.plist └── ViewController.swift └── image ├── after.png └── before.png /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 4 | 5 | ## Build generated 6 | build/ 7 | DerivedData/ 8 | 9 | ## Various settings 10 | *.pbxuser 11 | !default.pbxuser 12 | *.mode1v3 13 | !default.mode1v3 14 | *.mode2v3 15 | !default.mode2v3 16 | *.perspectivev3 17 | !default.perspectivev3 18 | xcuserdata/ 19 | 20 | ## Other 21 | *.moved-aside 22 | *.xccheckout 23 | *.xcscmblueprint 24 | 25 | ## Obj-C/Swift specific 26 | *.hmap 27 | *.ipa 28 | *.dSYM.zip 29 | *.dSYM 30 | 31 | ## Playgrounds 32 | timeline.xctimeline 33 | playground.xcworkspace 34 | 35 | # Swift Package Manager 36 | # 37 | # Add this line if you want to avoid checking in source code from Swift Package Manager dependencies. 38 | # Packages/ 39 | # Package.pins 40 | # Package.resolved 41 | .build/ 42 | 43 | # CocoaPods 44 | # 45 | # We recommend against adding the Pods directory to your .gitignore. However 46 | # you should judge for yourself, the pros and cons are mentioned at: 47 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 48 | # 49 | # Pods/ 50 | 51 | # Carthage 52 | # 53 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 54 | # Carthage/Checkouts 55 | 56 | Carthage/Build 57 | 58 | # fastlane 59 | # 60 | # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the 61 | # screenshots whenever they are needed. 62 | # For more information about the recommended setup visit: 63 | # https://docs.fastlane.tools/best-practices/source-control/#source-control 64 | 65 | fastlane/report.xml 66 | fastlane/Preview.html 67 | fastlane/screenshots/**/*.png 68 | fastlane/test_output 69 | -------------------------------------------------------------------------------- /.swift-version: -------------------------------------------------------------------------------- 1 | 4.2 -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Seungyoun Yi 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Triangulation 2 | [![Version](https://img.shields.io/cocoapods/v/Triangulation.svg?style=flat)](http://cocoapods.org/pods/Triangulation) 3 | [![Carthage Compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage) 4 | [![License: MIT](https://img.shields.io/badge/license-MIT-blue.svg?style=flat)](https://github.com/younatics/Triangulation/blob/master/LICENSE) 5 | [![Platform](https://img.shields.io/cocoapods/p/Triangulation.svg?style=flat)](http://cocoapods.org/pods/Triangulation) 6 | [![Swift 4.0](https://img.shields.io/badge/Swift-4.2-orange.svg?style=flat)](https://developer.apple.com/swift/) 7 | 8 | ## Introduction 9 | 📐 Triangulate your image! `Triangulation` will magically change your image like this! 10 | 11 | | Before | After | 12 | | :----------: | :-----------------------: | 13 | | ![Before](https://github.com/younatics/Triangulation/blob/master/image/before.png) | ![After](https://github.com/younatics/Triangulation/blob/master/image/after.png) | 14 | 15 | 16 | ## Requirements 17 | 18 | `Triangulation` is written in Swift 4.2. Compatible with iOS 9.0+ 19 | 20 | ## Installation 21 | 22 | ### Cocoapods 23 | 24 | Triangulation is available through [CocoaPods](http://cocoapods.org). To install 25 | it, simply add the following line to your Podfile: 26 | 27 | ```ruby 28 | pod 'Triangulation' 29 | ``` 30 | ### Carthage 31 | ``` 32 | github "younatics/Triangulation" 33 | ``` 34 | 35 | ## Usage 36 | 37 | Add `TriangulationView` with custom cell size. That's it! 38 | ```swift 39 | let triangleView = TriangulationView(frame: view.bounds, image: image, cellSize: 40) 40 | view.addSubview(triangleView) 41 | ``` 42 | 43 | ## References 44 | #### Please tell me or make pull request if you use this library in your application :) 45 | 46 | ## Author 47 | [younatics](https://twitter.com/younatics) 48 | Twitter 49 | 50 | ## License 51 | Triangulation is available under the MIT license. See the LICENSE file for more info. 52 | -------------------------------------------------------------------------------- /Triangulation.podspec: -------------------------------------------------------------------------------- 1 | # 2 | # Be sure to run `pod lib lint YNDropDownMenu.podspec' to ensure this is a 3 | # valid spec before submitting. 4 | # 5 | # Any lines starting with a # are optional, but their use is encouraged 6 | # To learn more about a Podspec see http://guides.cocoapods.org/syntax/podspec.html 7 | # 8 | 9 | Pod::Spec.new do |s| 10 | s.name = 'Triangulation' 11 | s.version = '1.0.0' 12 | s.summary = 'Triangulate your image!' 13 | 14 | s.description = <<-DESC 15 | Magic will be happened when you use Triangulation! 16 | DESC 17 | 18 | s.homepage = 'https://github.com/younatics/Triangulation' 19 | s.license = { :type => 'MIT', :file => 'LICENSE' } 20 | s.author = { "Seungyoun Yi" => "younatics@gmail.com" } 21 | 22 | s.source = { :git => 'https://github.com/younatics/Triangulation.git', :tag => s.version.to_s } 23 | s.source_files = 'Triangulation/*.swift' 24 | 25 | s.ios.deployment_target = '9.0' 26 | s.frameworks = 'CoreGraphics', 'UIKit', 'GameplayKit' 27 | s.requires_arc = true 28 | end 29 | -------------------------------------------------------------------------------- /Triangulation.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 50; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 19BAC8D221622E6500705240 /* Triangulation.h in Headers */ = {isa = PBXBuildFile; fileRef = 19BAC8C421622E6500705240 /* Triangulation.h */; settings = {ATTRIBUTES = (Public, ); }; }; 11 | 19BAC8E121622F2100705240 /* Hash.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19BAC8DB21622F2100705240 /* Hash.swift */; }; 12 | 19BAC8E221622F2100705240 /* Delaunay.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19BAC8DC21622F2100705240 /* Delaunay.swift */; }; 13 | 19BAC8E321622F2100705240 /* Vertex.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19BAC8DD21622F2100705240 /* Vertex.swift */; }; 14 | 19BAC8E421622F2100705240 /* Circumcircle.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19BAC8DE21622F2100705240 /* Circumcircle.swift */; }; 15 | 19BAC8E521622F2100705240 /* Triangle.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19BAC8DF21622F2100705240 /* Triangle.swift */; }; 16 | 19BAC8E621622F2100705240 /* Edge.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19BAC8E021622F2100705240 /* Edge.swift */; }; 17 | 19BAC8E821622F6600705240 /* Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19BAC8E721622F6600705240 /* Extension.swift */; }; 18 | 19BAC8F22162320C00705240 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19BAC8F12162320C00705240 /* AppDelegate.swift */; }; 19 | 19BAC8F42162320C00705240 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19BAC8F32162320C00705240 /* ViewController.swift */; }; 20 | 19BAC8F72162320C00705240 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 19BAC8F52162320C00705240 /* Main.storyboard */; }; 21 | 19BAC8F92162320E00705240 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 19BAC8F82162320E00705240 /* Assets.xcassets */; }; 22 | 19BAC8FC2162320E00705240 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 19BAC8FA2162320E00705240 /* LaunchScreen.storyboard */; }; 23 | 19BAC91E216233AE00705240 /* Triangulation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 19BAC8C121622E6500705240 /* Triangulation.framework */; }; 24 | 19BAC91F216233AE00705240 /* Triangulation.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 19BAC8C121622E6500705240 /* Triangulation.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; 25 | 19BAC924216234B500705240 /* IMG_1043.JPG in Resources */ = {isa = PBXBuildFile; fileRef = 19BAC923216234B500705240 /* IMG_1043.JPG */; }; 26 | 19BAC9262162363000705240 /* TriangulationView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 19BAC8E92162304900705240 /* TriangulationView.swift */; }; 27 | /* End PBXBuildFile section */ 28 | 29 | /* Begin PBXContainerItemProxy section */ 30 | 19BAC920216233AE00705240 /* PBXContainerItemProxy */ = { 31 | isa = PBXContainerItemProxy; 32 | containerPortal = 19BAC8B821622E6500705240 /* Project object */; 33 | proxyType = 1; 34 | remoteGlobalIDString = 19BAC8C021622E6500705240; 35 | remoteInfo = Triangulation; 36 | }; 37 | /* End PBXContainerItemProxy section */ 38 | 39 | /* Begin PBXCopyFilesBuildPhase section */ 40 | 19BAC922216233AE00705240 /* Embed Frameworks */ = { 41 | isa = PBXCopyFilesBuildPhase; 42 | buildActionMask = 2147483647; 43 | dstPath = ""; 44 | dstSubfolderSpec = 10; 45 | files = ( 46 | 19BAC91F216233AE00705240 /* Triangulation.framework in Embed Frameworks */, 47 | ); 48 | name = "Embed Frameworks"; 49 | runOnlyForDeploymentPostprocessing = 0; 50 | }; 51 | /* End PBXCopyFilesBuildPhase section */ 52 | 53 | /* Begin PBXFileReference section */ 54 | 19BAC8C121622E6500705240 /* Triangulation.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Triangulation.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 55 | 19BAC8C421622E6500705240 /* Triangulation.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Triangulation.h; sourceTree = ""; }; 56 | 19BAC8C521622E6500705240 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 57 | 19BAC8DB21622F2100705240 /* Hash.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Hash.swift; sourceTree = ""; }; 58 | 19BAC8DC21622F2100705240 /* Delaunay.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Delaunay.swift; sourceTree = ""; }; 59 | 19BAC8DD21622F2100705240 /* Vertex.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Vertex.swift; sourceTree = ""; }; 60 | 19BAC8DE21622F2100705240 /* Circumcircle.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Circumcircle.swift; sourceTree = ""; }; 61 | 19BAC8DF21622F2100705240 /* Triangle.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Triangle.swift; sourceTree = ""; }; 62 | 19BAC8E021622F2100705240 /* Edge.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Edge.swift; sourceTree = ""; }; 63 | 19BAC8E721622F6600705240 /* Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Extension.swift; sourceTree = ""; }; 64 | 19BAC8E92162304900705240 /* TriangulationView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TriangulationView.swift; sourceTree = ""; }; 65 | 19BAC8EF2162320C00705240 /* TriangulationDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = TriangulationDemo.app; sourceTree = BUILT_PRODUCTS_DIR; }; 66 | 19BAC8F12162320C00705240 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 67 | 19BAC8F32162320C00705240 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; 68 | 19BAC8F62162320C00705240 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 69 | 19BAC8F82162320E00705240 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 70 | 19BAC8FB2162320E00705240 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 71 | 19BAC8FD2162320E00705240 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 72 | 19BAC923216234B500705240 /* IMG_1043.JPG */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = IMG_1043.JPG; sourceTree = ""; }; 73 | /* End PBXFileReference section */ 74 | 75 | /* Begin PBXFrameworksBuildPhase section */ 76 | 19BAC8BE21622E6500705240 /* Frameworks */ = { 77 | isa = PBXFrameworksBuildPhase; 78 | buildActionMask = 2147483647; 79 | files = ( 80 | ); 81 | runOnlyForDeploymentPostprocessing = 0; 82 | }; 83 | 19BAC8EC2162320C00705240 /* Frameworks */ = { 84 | isa = PBXFrameworksBuildPhase; 85 | buildActionMask = 2147483647; 86 | files = ( 87 | 19BAC91E216233AE00705240 /* Triangulation.framework in Frameworks */, 88 | ); 89 | runOnlyForDeploymentPostprocessing = 0; 90 | }; 91 | /* End PBXFrameworksBuildPhase section */ 92 | 93 | /* Begin PBXGroup section */ 94 | 19BAC8B721622E6500705240 = { 95 | isa = PBXGroup; 96 | children = ( 97 | 19BAC8C321622E6500705240 /* Triangulation */, 98 | 19BAC8F02162320C00705240 /* TriangulationDemo */, 99 | 19BAC8C221622E6500705240 /* Products */, 100 | ); 101 | sourceTree = ""; 102 | }; 103 | 19BAC8C221622E6500705240 /* Products */ = { 104 | isa = PBXGroup; 105 | children = ( 106 | 19BAC8C121622E6500705240 /* Triangulation.framework */, 107 | 19BAC8EF2162320C00705240 /* TriangulationDemo.app */, 108 | ); 109 | name = Products; 110 | sourceTree = ""; 111 | }; 112 | 19BAC8C321622E6500705240 /* Triangulation */ = { 113 | isa = PBXGroup; 114 | children = ( 115 | 19BAC8E92162304900705240 /* TriangulationView.swift */, 116 | 19BAC8DE21622F2100705240 /* Circumcircle.swift */, 117 | 19BAC8DC21622F2100705240 /* Delaunay.swift */, 118 | 19BAC8E021622F2100705240 /* Edge.swift */, 119 | 19BAC8DB21622F2100705240 /* Hash.swift */, 120 | 19BAC8DF21622F2100705240 /* Triangle.swift */, 121 | 19BAC8DD21622F2100705240 /* Vertex.swift */, 122 | 19BAC8E721622F6600705240 /* Extension.swift */, 123 | 19BAC8C421622E6500705240 /* Triangulation.h */, 124 | 19BAC8C521622E6500705240 /* Info.plist */, 125 | ); 126 | path = Triangulation; 127 | sourceTree = ""; 128 | }; 129 | 19BAC8F02162320C00705240 /* TriangulationDemo */ = { 130 | isa = PBXGroup; 131 | children = ( 132 | 19BAC8F12162320C00705240 /* AppDelegate.swift */, 133 | 19BAC8F32162320C00705240 /* ViewController.swift */, 134 | 19BAC8F52162320C00705240 /* Main.storyboard */, 135 | 19BAC8F82162320E00705240 /* Assets.xcassets */, 136 | 19BAC923216234B500705240 /* IMG_1043.JPG */, 137 | 19BAC8FA2162320E00705240 /* LaunchScreen.storyboard */, 138 | 19BAC8FD2162320E00705240 /* Info.plist */, 139 | ); 140 | path = TriangulationDemo; 141 | sourceTree = ""; 142 | }; 143 | /* End PBXGroup section */ 144 | 145 | /* Begin PBXHeadersBuildPhase section */ 146 | 19BAC8BC21622E6500705240 /* Headers */ = { 147 | isa = PBXHeadersBuildPhase; 148 | buildActionMask = 2147483647; 149 | files = ( 150 | 19BAC8D221622E6500705240 /* Triangulation.h in Headers */, 151 | ); 152 | runOnlyForDeploymentPostprocessing = 0; 153 | }; 154 | /* End PBXHeadersBuildPhase section */ 155 | 156 | /* Begin PBXNativeTarget section */ 157 | 19BAC8C021622E6500705240 /* Triangulation */ = { 158 | isa = PBXNativeTarget; 159 | buildConfigurationList = 19BAC8D521622E6500705240 /* Build configuration list for PBXNativeTarget "Triangulation" */; 160 | buildPhases = ( 161 | 19BAC8BC21622E6500705240 /* Headers */, 162 | 19BAC8BD21622E6500705240 /* Sources */, 163 | 19BAC8BE21622E6500705240 /* Frameworks */, 164 | 19BAC8BF21622E6500705240 /* Resources */, 165 | ); 166 | buildRules = ( 167 | ); 168 | dependencies = ( 169 | ); 170 | name = Triangulation; 171 | productName = Triangulation; 172 | productReference = 19BAC8C121622E6500705240 /* Triangulation.framework */; 173 | productType = "com.apple.product-type.framework"; 174 | }; 175 | 19BAC8EE2162320C00705240 /* TriangulationDemo */ = { 176 | isa = PBXNativeTarget; 177 | buildConfigurationList = 19BAC9142162320E00705240 /* Build configuration list for PBXNativeTarget "TriangulationDemo" */; 178 | buildPhases = ( 179 | 19BAC8EB2162320C00705240 /* Sources */, 180 | 19BAC8EC2162320C00705240 /* Frameworks */, 181 | 19BAC8ED2162320C00705240 /* Resources */, 182 | 19BAC922216233AE00705240 /* Embed Frameworks */, 183 | ); 184 | buildRules = ( 185 | ); 186 | dependencies = ( 187 | 19BAC921216233AE00705240 /* PBXTargetDependency */, 188 | ); 189 | name = TriangulationDemo; 190 | productName = TriangulationDemo; 191 | productReference = 19BAC8EF2162320C00705240 /* TriangulationDemo.app */; 192 | productType = "com.apple.product-type.application"; 193 | }; 194 | /* End PBXNativeTarget section */ 195 | 196 | /* Begin PBXProject section */ 197 | 19BAC8B821622E6500705240 /* Project object */ = { 198 | isa = PBXProject; 199 | attributes = { 200 | LastSwiftUpdateCheck = 1000; 201 | LastUpgradeCheck = 1000; 202 | ORGANIZATIONNAME = "Seungyoun Yi"; 203 | TargetAttributes = { 204 | 19BAC8C021622E6500705240 = { 205 | CreatedOnToolsVersion = 10.0; 206 | LastSwiftMigration = 1000; 207 | }; 208 | 19BAC8EE2162320C00705240 = { 209 | CreatedOnToolsVersion = 10.0; 210 | }; 211 | }; 212 | }; 213 | buildConfigurationList = 19BAC8BB21622E6500705240 /* Build configuration list for PBXProject "Triangulation" */; 214 | compatibilityVersion = "Xcode 9.3"; 215 | developmentRegion = en; 216 | hasScannedForEncodings = 0; 217 | knownRegions = ( 218 | en, 219 | Base, 220 | ); 221 | mainGroup = 19BAC8B721622E6500705240; 222 | productRefGroup = 19BAC8C221622E6500705240 /* Products */; 223 | projectDirPath = ""; 224 | projectRoot = ""; 225 | targets = ( 226 | 19BAC8C021622E6500705240 /* Triangulation */, 227 | 19BAC8EE2162320C00705240 /* TriangulationDemo */, 228 | ); 229 | }; 230 | /* End PBXProject section */ 231 | 232 | /* Begin PBXResourcesBuildPhase section */ 233 | 19BAC8BF21622E6500705240 /* Resources */ = { 234 | isa = PBXResourcesBuildPhase; 235 | buildActionMask = 2147483647; 236 | files = ( 237 | ); 238 | runOnlyForDeploymentPostprocessing = 0; 239 | }; 240 | 19BAC8ED2162320C00705240 /* Resources */ = { 241 | isa = PBXResourcesBuildPhase; 242 | buildActionMask = 2147483647; 243 | files = ( 244 | 19BAC8FC2162320E00705240 /* LaunchScreen.storyboard in Resources */, 245 | 19BAC8F92162320E00705240 /* Assets.xcassets in Resources */, 246 | 19BAC8F72162320C00705240 /* Main.storyboard in Resources */, 247 | 19BAC924216234B500705240 /* IMG_1043.JPG in Resources */, 248 | ); 249 | runOnlyForDeploymentPostprocessing = 0; 250 | }; 251 | /* End PBXResourcesBuildPhase section */ 252 | 253 | /* Begin PBXSourcesBuildPhase section */ 254 | 19BAC8BD21622E6500705240 /* Sources */ = { 255 | isa = PBXSourcesBuildPhase; 256 | buildActionMask = 2147483647; 257 | files = ( 258 | 19BAC8E221622F2100705240 /* Delaunay.swift in Sources */, 259 | 19BAC8E121622F2100705240 /* Hash.swift in Sources */, 260 | 19BAC8E821622F6600705240 /* Extension.swift in Sources */, 261 | 19BAC8E621622F2100705240 /* Edge.swift in Sources */, 262 | 19BAC8E521622F2100705240 /* Triangle.swift in Sources */, 263 | 19BAC8E421622F2100705240 /* Circumcircle.swift in Sources */, 264 | 19BAC9262162363000705240 /* TriangulationView.swift in Sources */, 265 | 19BAC8E321622F2100705240 /* Vertex.swift in Sources */, 266 | ); 267 | runOnlyForDeploymentPostprocessing = 0; 268 | }; 269 | 19BAC8EB2162320C00705240 /* Sources */ = { 270 | isa = PBXSourcesBuildPhase; 271 | buildActionMask = 2147483647; 272 | files = ( 273 | 19BAC8F42162320C00705240 /* ViewController.swift in Sources */, 274 | 19BAC8F22162320C00705240 /* AppDelegate.swift in Sources */, 275 | ); 276 | runOnlyForDeploymentPostprocessing = 0; 277 | }; 278 | /* End PBXSourcesBuildPhase section */ 279 | 280 | /* Begin PBXTargetDependency section */ 281 | 19BAC921216233AE00705240 /* PBXTargetDependency */ = { 282 | isa = PBXTargetDependency; 283 | target = 19BAC8C021622E6500705240 /* Triangulation */; 284 | targetProxy = 19BAC920216233AE00705240 /* PBXContainerItemProxy */; 285 | }; 286 | /* End PBXTargetDependency section */ 287 | 288 | /* Begin PBXVariantGroup section */ 289 | 19BAC8F52162320C00705240 /* Main.storyboard */ = { 290 | isa = PBXVariantGroup; 291 | children = ( 292 | 19BAC8F62162320C00705240 /* Base */, 293 | ); 294 | name = Main.storyboard; 295 | sourceTree = ""; 296 | }; 297 | 19BAC8FA2162320E00705240 /* LaunchScreen.storyboard */ = { 298 | isa = PBXVariantGroup; 299 | children = ( 300 | 19BAC8FB2162320E00705240 /* Base */, 301 | ); 302 | name = LaunchScreen.storyboard; 303 | sourceTree = ""; 304 | }; 305 | /* End PBXVariantGroup section */ 306 | 307 | /* Begin XCBuildConfiguration section */ 308 | 19BAC8D321622E6500705240 /* Debug */ = { 309 | isa = XCBuildConfiguration; 310 | buildSettings = { 311 | ALWAYS_SEARCH_USER_PATHS = NO; 312 | CLANG_ANALYZER_NONNULL = YES; 313 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 314 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 315 | CLANG_CXX_LIBRARY = "libc++"; 316 | CLANG_ENABLE_MODULES = YES; 317 | CLANG_ENABLE_OBJC_ARC = YES; 318 | CLANG_ENABLE_OBJC_WEAK = YES; 319 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 320 | CLANG_WARN_BOOL_CONVERSION = YES; 321 | CLANG_WARN_COMMA = YES; 322 | CLANG_WARN_CONSTANT_CONVERSION = YES; 323 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 324 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 325 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 326 | CLANG_WARN_EMPTY_BODY = YES; 327 | CLANG_WARN_ENUM_CONVERSION = YES; 328 | CLANG_WARN_INFINITE_RECURSION = YES; 329 | CLANG_WARN_INT_CONVERSION = YES; 330 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 331 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 332 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 333 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 334 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 335 | CLANG_WARN_STRICT_PROTOTYPES = YES; 336 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 337 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 338 | CLANG_WARN_UNREACHABLE_CODE = YES; 339 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 340 | CODE_SIGN_IDENTITY = "iPhone Developer"; 341 | COPY_PHASE_STRIP = NO; 342 | CURRENT_PROJECT_VERSION = 1; 343 | DEBUG_INFORMATION_FORMAT = dwarf; 344 | ENABLE_STRICT_OBJC_MSGSEND = YES; 345 | ENABLE_TESTABILITY = YES; 346 | GCC_C_LANGUAGE_STANDARD = gnu11; 347 | GCC_DYNAMIC_NO_PIC = NO; 348 | GCC_NO_COMMON_BLOCKS = YES; 349 | GCC_OPTIMIZATION_LEVEL = 0; 350 | GCC_PREPROCESSOR_DEFINITIONS = ( 351 | "DEBUG=1", 352 | "$(inherited)", 353 | ); 354 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 355 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 356 | GCC_WARN_UNDECLARED_SELECTOR = YES; 357 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 358 | GCC_WARN_UNUSED_FUNCTION = YES; 359 | GCC_WARN_UNUSED_VARIABLE = YES; 360 | IPHONEOS_DEPLOYMENT_TARGET = 9.0; 361 | MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; 362 | MTL_FAST_MATH = YES; 363 | ONLY_ACTIVE_ARCH = YES; 364 | SDKROOT = iphoneos; 365 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 366 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 367 | VERSIONING_SYSTEM = "apple-generic"; 368 | VERSION_INFO_PREFIX = ""; 369 | }; 370 | name = Debug; 371 | }; 372 | 19BAC8D421622E6500705240 /* Release */ = { 373 | isa = XCBuildConfiguration; 374 | buildSettings = { 375 | ALWAYS_SEARCH_USER_PATHS = NO; 376 | CLANG_ANALYZER_NONNULL = YES; 377 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 378 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 379 | CLANG_CXX_LIBRARY = "libc++"; 380 | CLANG_ENABLE_MODULES = YES; 381 | CLANG_ENABLE_OBJC_ARC = YES; 382 | CLANG_ENABLE_OBJC_WEAK = YES; 383 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 384 | CLANG_WARN_BOOL_CONVERSION = YES; 385 | CLANG_WARN_COMMA = YES; 386 | CLANG_WARN_CONSTANT_CONVERSION = YES; 387 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 388 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 389 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 390 | CLANG_WARN_EMPTY_BODY = YES; 391 | CLANG_WARN_ENUM_CONVERSION = YES; 392 | CLANG_WARN_INFINITE_RECURSION = YES; 393 | CLANG_WARN_INT_CONVERSION = YES; 394 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 395 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 396 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 397 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 398 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 399 | CLANG_WARN_STRICT_PROTOTYPES = YES; 400 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 401 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 402 | CLANG_WARN_UNREACHABLE_CODE = YES; 403 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 404 | CODE_SIGN_IDENTITY = "iPhone Developer"; 405 | COPY_PHASE_STRIP = NO; 406 | CURRENT_PROJECT_VERSION = 1; 407 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 408 | ENABLE_NS_ASSERTIONS = NO; 409 | ENABLE_STRICT_OBJC_MSGSEND = YES; 410 | GCC_C_LANGUAGE_STANDARD = gnu11; 411 | GCC_NO_COMMON_BLOCKS = YES; 412 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 413 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 414 | GCC_WARN_UNDECLARED_SELECTOR = YES; 415 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 416 | GCC_WARN_UNUSED_FUNCTION = YES; 417 | GCC_WARN_UNUSED_VARIABLE = YES; 418 | IPHONEOS_DEPLOYMENT_TARGET = 9.0; 419 | MTL_ENABLE_DEBUG_INFO = NO; 420 | MTL_FAST_MATH = YES; 421 | SDKROOT = iphoneos; 422 | SWIFT_COMPILATION_MODE = wholemodule; 423 | SWIFT_OPTIMIZATION_LEVEL = "-O"; 424 | VALIDATE_PRODUCT = YES; 425 | VERSIONING_SYSTEM = "apple-generic"; 426 | VERSION_INFO_PREFIX = ""; 427 | }; 428 | name = Release; 429 | }; 430 | 19BAC8D621622E6500705240 /* Debug */ = { 431 | isa = XCBuildConfiguration; 432 | buildSettings = { 433 | CLANG_ENABLE_MODULES = YES; 434 | CODE_SIGN_IDENTITY = ""; 435 | CODE_SIGN_STYLE = Automatic; 436 | DEFINES_MODULE = YES; 437 | DEVELOPMENT_TEAM = 4G7K3CN2JU; 438 | DYLIB_COMPATIBILITY_VERSION = 1; 439 | DYLIB_CURRENT_VERSION = 1; 440 | DYLIB_INSTALL_NAME_BASE = "@rpath"; 441 | INFOPLIST_FILE = Triangulation/Info.plist; 442 | INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; 443 | LD_RUNPATH_SEARCH_PATHS = ( 444 | "$(inherited)", 445 | "@executable_path/Frameworks", 446 | "@loader_path/Frameworks", 447 | ); 448 | PRODUCT_BUNDLE_IDENTIFIER = com.seungyounyi.Triangulation; 449 | PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)"; 450 | SKIP_INSTALL = YES; 451 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 452 | SWIFT_VERSION = 4.2; 453 | TARGETED_DEVICE_FAMILY = "1,2"; 454 | }; 455 | name = Debug; 456 | }; 457 | 19BAC8D721622E6500705240 /* Release */ = { 458 | isa = XCBuildConfiguration; 459 | buildSettings = { 460 | CLANG_ENABLE_MODULES = YES; 461 | CODE_SIGN_IDENTITY = ""; 462 | CODE_SIGN_STYLE = Automatic; 463 | DEFINES_MODULE = YES; 464 | DEVELOPMENT_TEAM = 4G7K3CN2JU; 465 | DYLIB_COMPATIBILITY_VERSION = 1; 466 | DYLIB_CURRENT_VERSION = 1; 467 | DYLIB_INSTALL_NAME_BASE = "@rpath"; 468 | INFOPLIST_FILE = Triangulation/Info.plist; 469 | INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; 470 | LD_RUNPATH_SEARCH_PATHS = ( 471 | "$(inherited)", 472 | "@executable_path/Frameworks", 473 | "@loader_path/Frameworks", 474 | ); 475 | PRODUCT_BUNDLE_IDENTIFIER = com.seungyounyi.Triangulation; 476 | PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)"; 477 | SKIP_INSTALL = YES; 478 | SWIFT_VERSION = 4.2; 479 | TARGETED_DEVICE_FAMILY = "1,2"; 480 | }; 481 | name = Release; 482 | }; 483 | 19BAC9152162320E00705240 /* Debug */ = { 484 | isa = XCBuildConfiguration; 485 | buildSettings = { 486 | ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; 487 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 488 | CODE_SIGN_STYLE = Automatic; 489 | DEVELOPMENT_TEAM = 4G7K3CN2JU; 490 | INFOPLIST_FILE = TriangulationDemo/Info.plist; 491 | LD_RUNPATH_SEARCH_PATHS = ( 492 | "$(inherited)", 493 | "@executable_path/Frameworks", 494 | ); 495 | PRODUCT_BUNDLE_IDENTIFIER = com.seungyounyi.TriangulationDemo; 496 | PRODUCT_NAME = "$(TARGET_NAME)"; 497 | SWIFT_VERSION = 4.2; 498 | TARGETED_DEVICE_FAMILY = "1,2"; 499 | }; 500 | name = Debug; 501 | }; 502 | 19BAC9162162320E00705240 /* Release */ = { 503 | isa = XCBuildConfiguration; 504 | buildSettings = { 505 | ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; 506 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 507 | CODE_SIGN_STYLE = Automatic; 508 | DEVELOPMENT_TEAM = 4G7K3CN2JU; 509 | INFOPLIST_FILE = TriangulationDemo/Info.plist; 510 | LD_RUNPATH_SEARCH_PATHS = ( 511 | "$(inherited)", 512 | "@executable_path/Frameworks", 513 | ); 514 | PRODUCT_BUNDLE_IDENTIFIER = com.seungyounyi.TriangulationDemo; 515 | PRODUCT_NAME = "$(TARGET_NAME)"; 516 | SWIFT_VERSION = 4.2; 517 | TARGETED_DEVICE_FAMILY = "1,2"; 518 | }; 519 | name = Release; 520 | }; 521 | /* End XCBuildConfiguration section */ 522 | 523 | /* Begin XCConfigurationList section */ 524 | 19BAC8BB21622E6500705240 /* Build configuration list for PBXProject "Triangulation" */ = { 525 | isa = XCConfigurationList; 526 | buildConfigurations = ( 527 | 19BAC8D321622E6500705240 /* Debug */, 528 | 19BAC8D421622E6500705240 /* Release */, 529 | ); 530 | defaultConfigurationIsVisible = 0; 531 | defaultConfigurationName = Release; 532 | }; 533 | 19BAC8D521622E6500705240 /* Build configuration list for PBXNativeTarget "Triangulation" */ = { 534 | isa = XCConfigurationList; 535 | buildConfigurations = ( 536 | 19BAC8D621622E6500705240 /* Debug */, 537 | 19BAC8D721622E6500705240 /* Release */, 538 | ); 539 | defaultConfigurationIsVisible = 0; 540 | defaultConfigurationName = Release; 541 | }; 542 | 19BAC9142162320E00705240 /* Build configuration list for PBXNativeTarget "TriangulationDemo" */ = { 543 | isa = XCConfigurationList; 544 | buildConfigurations = ( 545 | 19BAC9152162320E00705240 /* Debug */, 546 | 19BAC9162162320E00705240 /* Release */, 547 | ); 548 | defaultConfigurationIsVisible = 0; 549 | defaultConfigurationName = Release; 550 | }; 551 | /* End XCConfigurationList section */ 552 | }; 553 | rootObject = 19BAC8B821622E6500705240 /* Project object */; 554 | } 555 | -------------------------------------------------------------------------------- /Triangulation.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Triangulation.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Triangulation.xcodeproj/xcshareddata/xcschemes/Triangulation.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 33 | 39 | 40 | 41 | 42 | 43 | 49 | 50 | 51 | 52 | 53 | 54 | 64 | 65 | 71 | 72 | 73 | 74 | 75 | 76 | 82 | 83 | 89 | 90 | 91 | 92 | 94 | 95 | 98 | 99 | 100 | -------------------------------------------------------------------------------- /Triangulation/Circumcircle.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Circumcircle.swift 3 | // DelaunayTriangulationSwift 4 | // 5 | // Created by Alex Littlejohn on 2016/01/08. 6 | // Copyright © 2016 zero. All rights reserved. 7 | // 8 | 9 | /// Represents a bounding circle for a set of 3 vertices 10 | struct Circumcircle { 11 | let vertex1: Vertex 12 | let vertex2: Vertex 13 | let vertex3: Vertex 14 | let x: Double 15 | let y: Double 16 | let rsqr: Double 17 | } 18 | -------------------------------------------------------------------------------- /Triangulation/Delaunay.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Delaunay.swift 3 | // DelaunayTriangulationSwift 4 | // 5 | // Created by Alex Littlejohn on 2016/01/08. 6 | // Copyright © 2016 zero. All rights reserved. 7 | // 8 | 9 | import Darwin 10 | 11 | @objc class Delaunay : NSObject { 12 | override init() { } 13 | 14 | func supertriangle(_ vertices: [Vertex]) -> [Vertex] { 15 | var xmin = Double(Int32.max) 16 | var ymin = Double(Int32.max) 17 | var xmax = -Double(Int32.max) 18 | var ymax = -Double(Int32.max) 19 | 20 | for i in 0.. xmax { xmax = vertices[i].x } 23 | if vertices[i].y < ymin { ymin = vertices[i].y } 24 | if vertices[i].y > ymax { ymax = vertices[i].y } 25 | } 26 | 27 | let dx = xmax - xmin 28 | let dy = ymax - ymin 29 | let dmax = max(dx, dy) 30 | let xmid = xmin + dx * 0.5 31 | let ymid = ymin + dy * 0.5 32 | 33 | return [ 34 | Vertex(x: xmid - 20 * dmax, y: ymid - dmax), 35 | Vertex(x: xmid, y: ymid + 20 * dmax), 36 | Vertex(x: xmid + 20 * dmax, y: ymid - dmax) 37 | ] 38 | } 39 | 40 | /* Calculate a circumcircle for a set of 3 vertices */ 41 | private func circumcircle(_ i: Vertex, j: Vertex, k: Vertex) -> Circumcircle { 42 | let x1 = i.x 43 | let y1 = i.y 44 | let x2 = j.x 45 | let y2 = j.y 46 | let x3 = k.x 47 | let y3 = k.y 48 | let xc: Double 49 | let yc: Double 50 | 51 | let fabsy1y2 = abs(y1 - y2) 52 | let fabsy2y3 = abs(y2 - y3) 53 | 54 | if fabsy1y2 < Double.ulpOfOne { 55 | let m2 = -((x3 - x2) / (y3 - y2)) 56 | let mx2 = (x2 + x3) / 2 57 | let my2 = (y2 + y3) / 2 58 | xc = (x2 + x1) / 2 59 | yc = m2 * (xc - mx2) + my2 60 | } else if fabsy2y3 < Double.ulpOfOne { 61 | let m1 = -((x2 - x1) / (y2 - y1)) 62 | let mx1 = (x1 + x2) / 2 63 | let my1 = (y1 + y2) / 2 64 | xc = (x3 + x2) / 2 65 | yc = m1 * (xc - mx1) + my1 66 | } else { 67 | let m1 = -((x2 - x1) / (y2 - y1)) 68 | let m2 = -((x3 - x2) / (y3 - y2)) 69 | let mx1 = (x1 + x2) / 2 70 | let mx2 = (x2 + x3) / 2 71 | let my1 = (y1 + y2) / 2 72 | let my2 = (y2 + y3) / 2 73 | xc = (m1 * mx1 - m2 * mx2 + my2 - my1) / (m1 - m2) 74 | 75 | if fabsy1y2 > fabsy2y3 { 76 | yc = m1 * (xc - mx1) + my1 77 | } else { 78 | yc = m2 * (xc - mx2) + my2 79 | } 80 | } 81 | 82 | let dx = x2 - xc 83 | let dy = y2 - yc 84 | let rsqr = dx * dx + dy * dy 85 | 86 | return Circumcircle(vertex1: i, vertex2: j, vertex3: k, x: xc, y: yc, rsqr: rsqr) 87 | } 88 | 89 | func dedup(_ edges: [Vertex]) -> [Vertex] { 90 | 91 | var e = edges 92 | var a: Vertex?, b: Vertex?, m: Vertex?, n: Vertex? 93 | 94 | var j = e.count 95 | while j > 0 { 96 | j -= 1 97 | b = j < e.count ? e[j] : nil 98 | j -= 1 99 | a = j < e.count ? e[j] : nil 100 | 101 | var i = j 102 | while i > 0 { 103 | i -= 1 104 | n = e[i] 105 | i -= 1 106 | m = e[i] 107 | 108 | if (a == m && b == n) || (a == n && b == m) { 109 | e.removeSubrange(j...j + 1) 110 | e.removeSubrange(i...i + 1) 111 | break 112 | } 113 | } 114 | } 115 | 116 | return e 117 | } 118 | 119 | func triangulate(_ vertices: [Vertex]) -> [Triangle] { 120 | var _vertices = Array(Set(vertices)) 121 | 122 | guard _vertices.count >= 3 else { 123 | return [Triangle]() 124 | } 125 | 126 | let n = _vertices.count 127 | var open = [Circumcircle]() 128 | var completed = [Circumcircle]() 129 | var edges = [Vertex]() 130 | 131 | var indices = [Int](0.. 0 && dx * dx > open[j].rsqr { 146 | completed.append(open.remove(at: j)) 147 | continue 148 | } 149 | 150 | let dy = _vertices[c].y - open[j].y 151 | 152 | if dx * dx + dy * dy - open[j].rsqr > Double.ulpOfOne { 153 | continue 154 | } 155 | 156 | edges += [ 157 | open[j].vertex1, open[j].vertex2, 158 | open[j].vertex2, open[j].vertex3, 159 | open[j].vertex3, open[j].vertex1 160 | ] 161 | 162 | // edges += [ 163 | // Edge(vertex1: open[j].vertex1, vertex2: open[j].vertex2), 164 | // Edge(vertex1: open[j].vertex2, vertex2: open[j].vertex3), 165 | // Edge(vertex1: open[j].vertex3, vertex2: open[j].vertex1) 166 | // ] 167 | 168 | open.remove(at: j) 169 | } 170 | edges = dedup(edges) 171 | 172 | var j = edges.count 173 | while j > 0 { 174 | 175 | j -= 1 176 | let b = edges[j] 177 | j -= 1 178 | let a = edges[j] 179 | open.append(circumcircle(a, j: b, k: _vertices[c])) 180 | } 181 | } 182 | completed += open 183 | 184 | let ignored: Set = [_vertices[n], _vertices[n + 1], _vertices[n + 2]] 185 | 186 | let results = completed.compactMap { (circumCircle) -> Triangle? in 187 | let current: Set = [circumCircle.vertex1, circumCircle.vertex2, circumCircle.vertex3] 188 | let intersection = ignored.intersection(current) 189 | if intersection.count > 0 { 190 | return nil 191 | } 192 | return Triangle(vertex1: circumCircle.vertex1, vertex2: circumCircle.vertex2, vertex3: circumCircle.vertex3) 193 | } 194 | return results 195 | } 196 | } 197 | -------------------------------------------------------------------------------- /Triangulation/Edge.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Edge.swift 3 | // DelaunaySwift 4 | // 5 | // Created by Alex Littlejohn on 2016/04/07. 6 | // Copyright © 2016 zero. All rights reserved. 7 | // 8 | 9 | struct Edge { 10 | let vertex1: Vertex 11 | let vertex2: Vertex 12 | } 13 | 14 | extension Edge: Equatable { 15 | static func ==(lhs: Edge, rhs: Edge) -> Bool { 16 | return lhs.vertex1 == rhs.vertex1 && lhs.vertex2 == rhs.vertex2 || lhs.vertex1 == rhs.vertex2 && lhs.vertex2 == rhs.vertex1 17 | } 18 | } 19 | 20 | extension Edge: Hashable { 21 | var hashValue: Int { 22 | var seed = UInt(0) 23 | hash_combine(seed: &seed, value: UInt(bitPattern: vertex1.hashValue)) 24 | hash_combine(seed: &seed, value: UInt(bitPattern: vertex2.hashValue)) 25 | return Int(bitPattern: seed) 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Triangulation/Extension.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Extension.swift 3 | // Triangulation 4 | // 5 | // Created by Seungyoun Yi on 01/10/2018. 6 | // Copyright © 2018 Seungyoun Yi. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | extension Triangle { 12 | func toPath() -> CGPath { 13 | 14 | let path = CGMutablePath() 15 | let point1 = vertex1.pointValue() 16 | let point2 = vertex2.pointValue() 17 | let point3 = vertex3.pointValue() 18 | 19 | path.move(to: point1) 20 | path.addLine(to: point2) 21 | path.addLine(to: point3) 22 | path.addLine(to: point1) 23 | 24 | path.closeSubpath() 25 | 26 | return path 27 | } 28 | } 29 | 30 | extension Double { 31 | static func random() -> Double { 32 | return Double(arc4random()) / 0xFFFFffff 33 | } 34 | 35 | static func random(_ min: Double, _ max: Double) -> Double { 36 | return Double.random() * (max - min) + min 37 | } 38 | } 39 | 40 | extension CGFloat { 41 | static func random(_ min: CGFloat, _ max: CGFloat) -> CGFloat { 42 | return CGFloat(Double.random(Double(min), Double(max))) 43 | } 44 | } 45 | 46 | extension UIColor { 47 | func randomColor() -> UIColor { 48 | let hue = CGFloat( Double.random() ) // 0.0 to 1.0 49 | let saturation: CGFloat = 0.5 // 0.5 to 1.0, away from white 50 | let brightness: CGFloat = 1.0 // 0.5 to 1.0, away from black 51 | let color = UIColor(hue: hue, saturation: saturation, brightness: brightness, alpha: 1) 52 | return color 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /Triangulation/Hash.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Hash.swift 3 | // DelaunaySwift 4 | // 5 | // Created by Volodymyr Boichentsov on 21/08/2017. 6 | // Copyright © 2017 zero. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | func hash_combine(seed: inout UInt, value: UInt) { 12 | let tmp = value &+ 0x9e3779b9 &+ (seed << 6) &+ (seed >> 2) 13 | seed ^= tmp 14 | } 15 | -------------------------------------------------------------------------------- /Triangulation/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | FMWK 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | $(CURRENT_PROJECT_VERSION) 21 | 22 | 23 | -------------------------------------------------------------------------------- /Triangulation/Triangle.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Triangle.swift 3 | // DelaunayTriangulationSwift 4 | // 5 | // Created by Alex Littlejohn on 2016/01/08. 6 | // Copyright © 2016 zero. All rights reserved. 7 | // 8 | 9 | import CoreGraphics 10 | 11 | /// A simple struct representing 3 vertices 12 | struct Triangle { 13 | 14 | init(vertex1: Vertex, vertex2: Vertex, vertex3: Vertex) { 15 | self.vertex1 = vertex1 16 | self.vertex2 = vertex2 17 | self.vertex3 = vertex3 18 | } 19 | 20 | let vertex1: Vertex 21 | let vertex2: Vertex 22 | let vertex3: Vertex 23 | 24 | func v1() -> CGPoint { 25 | return vertex1.pointValue() 26 | } 27 | 28 | func v2() -> CGPoint { 29 | return vertex2.pointValue() 30 | } 31 | 32 | func v3() -> CGPoint { 33 | return vertex3.pointValue() 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Triangulation/Triangulation.h: -------------------------------------------------------------------------------- 1 | // 2 | // Triangulation.h 3 | // Triangulation 4 | // 5 | // Created by Seungyoun Yi on 01/10/2018. 6 | // Copyright © 2018 Seungyoun Yi. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | //! Project version number for Triangulation. 12 | FOUNDATION_EXPORT double TriangulationVersionNumber; 13 | 14 | //! Project version string for Triangulation. 15 | FOUNDATION_EXPORT const unsigned char TriangulationVersionString[]; 16 | 17 | // In this header, you should import all the public headers of your framework using statements like #import 18 | 19 | 20 | -------------------------------------------------------------------------------- /Triangulation/TriangulationView.swift: -------------------------------------------------------------------------------- 1 | // 2 | // TriangleView.swift 3 | // DelaunayTriangulationSwift 4 | // 5 | // Created by Alex Littlejohn on 2016/01/08. 6 | // Copyright © 2016 zero. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | import GameplayKit 11 | 12 | public class TriangulationView: UIView { 13 | public override init(frame: CGRect) { 14 | super.init(frame: frame) 15 | } 16 | 17 | convenience public init(frame: CGRect, image: UIImage, cellSize: CGFloat = 30) { 18 | self.init(frame: frame) 19 | 20 | let vertices = generateVertices(bounds.size, cellSize: cellSize) 21 | let triangles = Delaunay().triangulate(vertices) 22 | 23 | for triangle in triangles { 24 | let triangleLayer = CAShapeLayer() 25 | let path = triangle.toPath() 26 | triangleLayer.path = path 27 | let color = getPixelColor(cropImage(image, toRect: path.boundingBox)) 28 | triangleLayer.fillColor = color.cgColor 29 | triangleLayer.backgroundColor = UIColor.clear.cgColor 30 | layer.addSublayer(triangleLayer) 31 | } 32 | } 33 | 34 | func cropImage(_ imageToCrop:UIImage, toRect rect:CGRect) -> UIImage? { 35 | if let imageRef = imageToCrop.cgImage?.cropping(to: rect) { 36 | return UIImage(cgImage:imageRef) 37 | } else { 38 | return nil 39 | } 40 | } 41 | 42 | func getPixelColor(_ image:UIImage?, _ point: CGPoint = CGPoint.zero) -> UIColor { 43 | guard let image = image, 44 | let cgImageData = image.cgImage?.dataProvider?.data, 45 | let pixelData = CGDataProvider(data: cgImageData)?.data else { return UIColor.clear } 46 | 47 | let data = CFDataGetBytePtr(pixelData)! 48 | let x = Int(point.x) 49 | let y = Int(point.y) 50 | let index = Int(image.size.width) * y + x 51 | let expectedLengthA = Int(image.size.width * image.size.height) 52 | let (expectedLengthRGB, expectedLengthRGBA) = (3 * expectedLengthA, 4 * expectedLengthA) 53 | let numBytes = CFDataGetLength(pixelData) 54 | 55 | switch numBytes { 56 | case expectedLengthA: 57 | return UIColor(red: 0, green: 0, blue: 0, alpha: CGFloat(data[index])/255.0) 58 | case expectedLengthRGB: 59 | return UIColor(red: CGFloat(data[3*index])/255.0, green: CGFloat(data[3*index+1])/255.0, blue: CGFloat(data[3*index+2])/255.0, alpha: 1.0) 60 | case expectedLengthRGBA: 61 | return UIColor(red: CGFloat(data[4*index])/255.0, green: CGFloat(data[4*index+1])/255.0, blue: CGFloat(data[4*index+2])/255.0, alpha: CGFloat(data[4*index+3])/255.0) 62 | default: 63 | return UIColor.clear 64 | } 65 | } 66 | 67 | required init?(coder aDecoder: NSCoder) { 68 | super.init(coder: aDecoder) 69 | } 70 | 71 | /// Generate set of vertices for our triangulation to use 72 | func generateVertices(_ size: CGSize, cellSize: CGFloat, variance: CGFloat = 0.75, seed: UInt64 = numericCast(arc4random())) -> [Vertex] { 73 | let cellsX = (size.width + 4 * cellSize) / cellSize 74 | let cellsY = (size.height + 4 * cellSize) / cellSize 75 | 76 | let bleedX = ((cellsX * cellSize) - size.width)/2 77 | let bleedY = ((cellsY * cellSize) - size.height)/2 78 | 79 | let _variance = cellSize * variance / 4 80 | 81 | var points = [Vertex]() 82 | let minX = -bleedX 83 | let maxX = size.width + bleedX 84 | let minY = -bleedY 85 | let maxY = size.height + bleedY 86 | 87 | let generator = GKLinearCongruentialRandomSource(seed: seed) 88 | for i in stride(from: minX, to: maxX, by: cellSize) { 89 | for j in stride(from: minY, to: maxY, by: cellSize) { 90 | let x = i + CGFloat.random(cellSize / 2, cellSize * 2)/2 + CGFloat(generator.nextUniform()) + CGFloat.random(-_variance, _variance) 91 | let y = j + CGFloat.random(cellSize / 2, cellSize * 2)/2 + CGFloat(generator.nextUniform()) + CGFloat.random(-_variance, _variance) 92 | 93 | points.append(Vertex(x: Double(x), y: Double(y))) 94 | } 95 | } 96 | return points 97 | } 98 | } 99 | 100 | -------------------------------------------------------------------------------- /Triangulation/Vertex.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Vertex.swift 3 | // DelaunayTriangulationSwift 4 | // 5 | // Created by Alex Littlejohn on 2016/01/08. 6 | // Copyright © 2016 zero. All rights reserved. 7 | // 8 | 9 | import CoreGraphics 10 | 11 | struct Vertex: Hashable { 12 | init(x: Double, y: Double) { 13 | self.x = x 14 | self.y = y 15 | } 16 | 17 | func pointValue() -> CGPoint { 18 | return CGPoint(x: x, y: y) 19 | } 20 | 21 | let x: Double 22 | let y: Double 23 | } 24 | 25 | extension Vertex: Equatable { 26 | static public func ==(lhs: Vertex, rhs: Vertex) -> Bool { 27 | return lhs.x == rhs.x && lhs.y == rhs.y 28 | } 29 | } 30 | 31 | extension Array where Element: Equatable { 32 | func removeDuplicates() -> [Element] { 33 | var result = [Element]() 34 | 35 | for value in self { 36 | if result.contains(value) == false { 37 | result.append(value) 38 | } 39 | } 40 | 41 | return result 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /TriangulationDemo/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.swift 3 | // TriangulationDemo 4 | // 5 | // Created by Seungyoun Yi on 01/10/2018. 6 | // Copyright © 2018 Seungyoun Yi. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | @UIApplicationMain 12 | class AppDelegate: UIResponder, UIApplicationDelegate { 13 | 14 | var window: UIWindow? 15 | 16 | 17 | func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { 18 | // Override point for customization after application launch. 19 | return true 20 | } 21 | 22 | func applicationWillResignActive(_ application: UIApplication) { 23 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 24 | // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. 25 | } 26 | 27 | func applicationDidEnterBackground(_ application: UIApplication) { 28 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 29 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 30 | } 31 | 32 | func applicationWillEnterForeground(_ application: UIApplication) { 33 | // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. 34 | } 35 | 36 | func applicationDidBecomeActive(_ application: UIApplication) { 37 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 38 | } 39 | 40 | func applicationWillTerminate(_ application: UIApplication) { 41 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 42 | } 43 | 44 | 45 | } 46 | 47 | -------------------------------------------------------------------------------- /TriangulationDemo/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "20x20", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "20x20", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "29x29", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "29x29", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "40x40", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "40x40", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "iphone", 35 | "size" : "60x60", 36 | "scale" : "2x" 37 | }, 38 | { 39 | "idiom" : "iphone", 40 | "size" : "60x60", 41 | "scale" : "3x" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "size" : "20x20", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "size" : "20x20", 51 | "scale" : "2x" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "size" : "29x29", 56 | "scale" : "1x" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "size" : "29x29", 61 | "scale" : "2x" 62 | }, 63 | { 64 | "idiom" : "ipad", 65 | "size" : "40x40", 66 | "scale" : "1x" 67 | }, 68 | { 69 | "idiom" : "ipad", 70 | "size" : "40x40", 71 | "scale" : "2x" 72 | }, 73 | { 74 | "idiom" : "ipad", 75 | "size" : "76x76", 76 | "scale" : "1x" 77 | }, 78 | { 79 | "idiom" : "ipad", 80 | "size" : "76x76", 81 | "scale" : "2x" 82 | }, 83 | { 84 | "idiom" : "ipad", 85 | "size" : "83.5x83.5", 86 | "scale" : "2x" 87 | }, 88 | { 89 | "idiom" : "ios-marketing", 90 | "size" : "1024x1024", 91 | "scale" : "1x" 92 | } 93 | ], 94 | "info" : { 95 | "version" : 1, 96 | "author" : "xcode" 97 | } 98 | } -------------------------------------------------------------------------------- /TriangulationDemo/Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "version" : 1, 4 | "author" : "xcode" 5 | } 6 | } -------------------------------------------------------------------------------- /TriangulationDemo/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /TriangulationDemo/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /TriangulationDemo/IMG_1043.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/younatics/Triangulation/39c0c8d4f35cb2d1acdae4e92d23e62ff22e7ade/TriangulationDemo/IMG_1043.JPG -------------------------------------------------------------------------------- /TriangulationDemo/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | LSRequiresIPhoneOS 22 | 23 | UILaunchStoryboardName 24 | LaunchScreen 25 | UIMainStoryboardFile 26 | Main 27 | UIRequiredDeviceCapabilities 28 | 29 | armv7 30 | 31 | UISupportedInterfaceOrientations 32 | 33 | UIInterfaceOrientationPortrait 34 | UIInterfaceOrientationLandscapeLeft 35 | UIInterfaceOrientationLandscapeRight 36 | 37 | UISupportedInterfaceOrientations~ipad 38 | 39 | UIInterfaceOrientationPortrait 40 | UIInterfaceOrientationPortraitUpsideDown 41 | UIInterfaceOrientationLandscapeLeft 42 | UIInterfaceOrientationLandscapeRight 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /TriangulationDemo/ViewController.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.swift 3 | // TriangulationDemo 4 | // 5 | // Created by Seungyoun Yi on 01/10/2018. 6 | // Copyright © 2018 Seungyoun Yi. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | import Triangulation 11 | 12 | class ViewController: UIViewController { 13 | @IBOutlet weak var imageView: UIImageView! 14 | override func viewDidLoad() { 15 | super.viewDidLoad() 16 | 17 | let image = imageView.image! 18 | let triangleView = TriangulationView(frame: imageView.frame, image: image, cellSize: 20) 19 | view.addSubview(triangleView) 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /image/after.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/younatics/Triangulation/39c0c8d4f35cb2d1acdae4e92d23e62ff22e7ade/image/after.png -------------------------------------------------------------------------------- /image/before.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/younatics/Triangulation/39c0c8d4f35cb2d1acdae4e92d23e62ff22e7ade/image/before.png --------------------------------------------------------------------------------