Skip to content

Instantly share code, notes, and snippets.

@garmstro
Created October 7, 2015 18:04
Show Gist options
  • Save garmstro/98d60c36d342b055c8f3 to your computer and use it in GitHub Desktop.
Save garmstro/98d60c36d342b055c8f3 to your computer and use it in GitHub Desktop.
/**
Calculate the x-pixel offset for an angle using perspective projection
http://stackoverflow.com/questions/16619104/perspective-projection-in-android-in-an-augmented-reality-application
- parameter angle: The viewing angle for the object
- returns: xOffset CGFloat for the x location of the object on the screen
*/
private func getXPerspectiveOffset(angle: Double) -> CGFloat {
var xOffset = CGFloat(0.0)
if currentHeading != nil && focalLen != nil {
let viewAngle = degreesToRadians(angle - currentHeading!)
xOffset = CGFloat(tan(viewAngle)) * CGFloat(focalLen!)
}
//Larger value = more to the right
return xOffset + view.frame.width/2.0
}
/**
Calculate the y-pixel offset for an angle using perspective projection
http://stackoverflow.com/questions/16619104/perspective-projection-in-android-in-an-augmented-reality-application
- parameter angle: The viewing angle for the object
- returns: yOffset CGFloat for the y location of the object on the screen
*/
private func getYPerspectiveOffset(angle: Double) -> CGFloat {
var yOffset = CGFloat(0.0)
if currentPitch != nil && focalLen != nil {
let viewAngle = degreesToRadians(angle - currentPitch!)
yOffset = CGFloat(tan(viewAngle)) * CGFloat(focalLen!)
}
//Larger value = lower
return view.frame.height/2.0 - yOffset
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment