Created
December 16, 2015 12:42
-
-
Save odrobnik/f32ded1fce2d168b6b66 to your computer and use it in GitHub Desktop.
Enumerating points on a line no further than a certain distance apart
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
func enumeratePointsOnLine(startPoint: CGPoint, endPoint: CGPoint, maxDistance: CGFloat, block: (point: CGPoint)->()) | |
{ | |
// for single point we don't iterate anything | |
guard !CGPointEqualToPoint(startPoint, endPoint) else | |
{ | |
block(point: startPoint) | |
return | |
} | |
let deltaX = endPoint.x - startPoint.x | |
let deltaY = endPoint.y - startPoint.y | |
let length = sqrt(deltaX * deltaX + deltaY * deltaY) | |
let parts = ceil(length / maxDistance) | |
let partDeltaX = deltaX / parts | |
let partDeltaY = deltaY / parts | |
for i in 0 ... Int(parts) | |
{ | |
let thisPoint = CGPoint(x: startPoint.x + CGFloat(i) * partDeltaX, y: startPoint.y + CGFloat(i) * partDeltaY) | |
block(point: thisPoint) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment