Created
July 14, 2020 22:42
-
-
Save atomoil/26b4405bbcc4fd0ac8b596c137a9cbfe to your computer and use it in GitHub Desktop.
This file contains 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
extension CGPoint { | |
func midBetween(_ other: CGPoint) -> CGPoint { | |
return CGPoint(x: (self.x + other.x) / 2.0, | |
y: (self.y + other.y) / 2.0) | |
} | |
func perpedicularTo(_ other: CGPoint, distance: CGFloat) -> (CGPoint, CGPoint) { | |
// Vector from p to p1; | |
var diff = CGPoint(x: other.x - self.x, y: other.y - self.y); | |
// Distance from p to p1: | |
let length = CGFloat(hypotf(Float(diff.x), Float(diff.y))) | |
// Normalize difference vector to length 1: | |
diff.x /= length; | |
diff.y /= length; | |
// Compute perpendicular vector: | |
let perp = CGPoint(x: -diff.y, y: diff.x) | |
//let markLength = 3.0; // Whatever you need ... | |
let a = CGPoint(x: self.x + perp.x * distance/2, y: self.y + perp.y * distance/2) | |
let b = CGPoint(x: self.x - perp.x * distance/2, y: self.y - perp.y * distance/2) | |
return (a, b) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment