From 02a0b6d9c55e33c4615baf91e29fab4b3d7a4aa5 Mon Sep 17 00:00:00 2001 From: Unknown Date: Thu, 26 Apr 2018 19:36:29 +0200 Subject: [PATCH] optimization of EllipticGradientBrush - precalculate anything that's independent of the pixel coordinate --- .../GradientBrushes/EllipticGradientBrush.cs | 28 +++++++++++++++---- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/src/ImageSharp.Drawing/Processing/Drawing/Brushes/GradientBrushes/EllipticGradientBrush.cs b/src/ImageSharp.Drawing/Processing/Drawing/Brushes/GradientBrushes/EllipticGradientBrush.cs index 21b581397f..9be54358cb 100644 --- a/src/ImageSharp.Drawing/Processing/Drawing/Brushes/GradientBrushes/EllipticGradientBrush.cs +++ b/src/ImageSharp.Drawing/Processing/Drawing/Brushes/GradientBrushes/EllipticGradientBrush.cs @@ -71,6 +71,14 @@ namespace SixLabors.ImageSharp.Processing.Drawing.Brushes.GradientBrushes private readonly float secondRadius; + private readonly float cosRotation; + + private readonly float sinRotation; + + private readonly float secondRadiusSquared; + + private readonly float referenceRadiusSquared; + /// /// Initializes a new instance of the class. /// @@ -102,6 +110,13 @@ namespace SixLabors.ImageSharp.Processing.Drawing.Brushes.GradientBrushes this.referenceAxisEnd); this.referenceRadius = this.DistanceBetween(this.center, this.referenceAxisEnd); this.secondRadius = this.referenceRadius * this.axisRatio; + + this.referenceRadiusSquared = this.referenceRadius * this.referenceRadius; + this.secondRadiusSquared = this.secondRadius * this.secondRadius; + + this.sinRotation = (float)Math.Sin(this.rotation); + this.cosRotation = (float)Math.Cos(this.rotation); + } /// @@ -112,14 +127,17 @@ namespace SixLabors.ImageSharp.Processing.Drawing.Brushes.GradientBrushes /// protected override float PositionOnGradient(int xt, int yt) { - float x0 = xt - this.center.X; // TODO: rotate this point after translation + float x0 = xt - this.center.X; float y0 = yt - this.center.Y; - float x = (float)((x0 * Math.Cos(this.rotation)) - (y0 * Math.Sin(this.rotation))); // TODO: store sin and cos of rotation as constant! - float y = (float)((x0 * Math.Sin(this.rotation)) + (y0 * Math.Cos(this.rotation))); + float x = (x0 * this.cosRotation) - (y0 * this.sinRotation); + float y = (x0 * this.sinRotation) + (y0 * this.cosRotation); + + float xSquared = x * x; + float ySquared = y * y; - var inBoundaryChecker = ((x * x) / (this.referenceRadius * this.referenceRadius)) - + ((y * y) / (this.secondRadius * this.secondRadius)); + var inBoundaryChecker = (xSquared / this.referenceRadiusSquared) + + (ySquared / this.secondRadiusSquared); return inBoundaryChecker; }