Skip to content

Instantly share code, notes, and snippets.

@malcommac
Created October 15, 2015 10:14
Show Gist options
  • Save malcommac/83ddd061854868a34670 to your computer and use it in GitHub Desktop.
Save malcommac/83ddd061854868a34670 to your computer and use it in GitHub Desktop.
Scale & Translate Points
public static func scale(points: [StrokePoint], toSize size: Double) -> [StrokePoint] {
let boundingBox = StrokePoint.boundingBox(points)
var newPoints: [StrokePoint] = []
for point in points {
let qx = point.x * (size / boundingBox.width)
let qy = point.y * (size / boundingBox.height)
newPoints.append(StrokePoint(x: qx, y: qy))
}
return newPoints
}
public static func translate(points: [StrokePoint], to pt: StrokePoint) -> [StrokePoint] {
let centroidPoint = StrokePoint.centroid(points)
var newPoints: [StrokePoint] = []
for point in points {
let qx = point.x + pt.x - centroidPoint.x
let qy = point.y + pt.y - centroidPoint.y
newPoints.append(StrokePoint(x: qx, y: qy))
}
return newPoints
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment