Created
February 4, 2019 11:14
-
-
Save paulw11/5aa9ede38e2be61db3e8d717b09dfb3c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import UIKit | |
class RegionTest { | |
var region: CGPath! | |
var points = [CGPoint]() | |
init?() { | |
guard let regionCoordinates = self.loadJson(filename:"pointlist"), | |
let pathCoordinates = self.loadJson(filename: "coordinates") else { | |
return nil | |
} | |
self.points = pathCoordinates | |
let path = CGMutablePath() | |
for point in regionCoordinates { | |
path.move(to: point) | |
} | |
path.closeSubpath() | |
self.region = path | |
} | |
func loadJson(filename fileName: String) -> [CGPoint]? { | |
if let url = Bundle.main.url(forResource: fileName, withExtension: "json") { | |
do { | |
let data = try Data(contentsOf: url) | |
let decoder = JSONDecoder() | |
let jsonData = try decoder.decode([String].self, from: data) | |
var points = [CGPoint]() | |
for str in jsonData { | |
let split1 = str.split(separator: ",") | |
let latStr = split1[0] | |
let lonStr = split1[1] | |
let latSplit = latStr.split(separator: ":") | |
let lat = Float(latSplit[1])! | |
let lonSplit = lonStr.split(separator: ":") | |
let lon = Float(lonSplit[1])! | |
points.append(CGPoint(x:CGFloat(lat),y:CGFloat(lon))) | |
} | |
return points | |
} catch { | |
print("error:\(error)") | |
} | |
} | |
return nil | |
} | |
func contains() -> Bool { | |
var ret = true | |
for point in points { | |
if !region.contains(point) { | |
ret = false | |
} | |
} | |
return ret | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment