Skip to content

Instantly share code, notes, and snippets.

@bertt
Created July 23, 2021 10:22
Show Gist options
  • Save bertt/4b02fc314bd16f22c870f67de3eb6ad8 to your computer and use it in GitHub Desktop.
Save bertt/4b02fc314bd16f22c870f67de3eb6ad8 to your computer and use it in GitHub Desktop.
// test
var result = PointRotator.RotatePoint(new Vector2(5.0f, 0), new Vector2(0, 0), Math.PI / 2 );
Assert.IsTrue(Math.Round(result.X, 0) == 0f);
Assert.IsTrue(Math.Round(result.Y,0) == -5);
// RotatePoint method
public static class PointRotator
{
public static Vector2 RotatePoint(Vector2 point, Vector2 pivot, double radians)
{
// this is arkit so rotate the other way around...
radians = radians * -1;
var cosTheta = Math.Cos(radians);
var sinTheta = Math.Sin(radians);
var dx = point.X - pivot.X;
var dy = point.Y - pivot.Y;
var x = cosTheta * dx - sinTheta * dy + pivot.X;
var y = sinTheta * dx + cosTheta * dy + pivot.Y;
return new Vector2((float)x, (float)y);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment