diff --git a/src/ImageProcessorCore/Numerics/Point.cs b/src/ImageProcessorCore/Numerics/Point.cs index 370592406..472a1c773 100644 --- a/src/ImageProcessorCore/Numerics/Point.cs +++ b/src/ImageProcessorCore/Numerics/Point.cs @@ -158,6 +158,29 @@ namespace ImageProcessorCore return new Vector2(this.X, this.Y); } + /// + /// Rotates a point around a given a rotation matrix. + /// + /// The point to rotate + /// Rotation matrix used + /// + public static Point Rotate( Point point, Matrix3x2 rotation ) + { + return new Point( Vector2.Transform( point.backingVector, rotation ) ); + } + + /// + /// Creates a rotation matrix for + /// + /// The origin point to rotate around + /// Rotation in degrees + /// + public static Matrix3x2 CreateRotatation( Point origin, float degrees ) + { + float radians = (float)ImageMaths.DegreesToRadians( degrees ); + return Matrix3x2.CreateRotation( radians, origin.backingVector ); + } + /// /// Rotates a point around a given origin by the specified angle in degrees. /// diff --git a/src/ImageProcessorCore/Samplers/Rotate.cs b/src/ImageProcessorCore/Samplers/Rotate.cs index b85667730..bc10e2fdb 100644 --- a/src/ImageProcessorCore/Samplers/Rotate.cs +++ b/src/ImageProcessorCore/Samplers/Rotate.cs @@ -3,6 +3,8 @@ // Licensed under the Apache License, Version 2.0. // +using System.Numerics; + namespace ImageProcessorCore.Samplers { using System.Threading.Tasks; @@ -62,6 +64,8 @@ namespace ImageProcessorCore.Samplers float widthFactor = source.Width / (float)target.Width; float heightFactor = source.Height / (float)target.Height; + Matrix3x2 rotation = Point.CreateRotatation( centre, negativeAngle ); + Parallel.For( startY, endY, @@ -78,7 +82,7 @@ namespace ImageProcessorCore.Samplers int originX = (int)((x - startX) * widthFactor); // Rotate at the centre point - Point rotated = Point.Rotate(new Point(originX, originY), centre, negativeAngle); + Point rotated = Point.Rotate(new Point(originX, originY), rotation); if (sourceRectangle.Contains(rotated.X, rotated.Y)) { target[x, y] = source[rotated.X, rotated.Y];