Browse Source

optimization of EllipticGradientBrush

- precalculate anything that's independent of the pixel coordinate
pull/542/head
Unknown 8 years ago
parent
commit
a6e04024f8
  1. 28
      src/ImageSharp.Drawing/Processing/Drawing/Brushes/GradientBrushes/EllipticGradientBrush.cs

28
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 secondRadius;
private readonly float cosRotation;
private readonly float sinRotation;
private readonly float secondRadiusSquared;
private readonly float referenceRadiusSquared;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="RadialGradientBrushApplicator" /> class. /// Initializes a new instance of the <see cref="RadialGradientBrushApplicator" /> class.
/// </summary> /// </summary>
@ -102,6 +110,13 @@ namespace SixLabors.ImageSharp.Processing.Drawing.Brushes.GradientBrushes
this.referenceAxisEnd); this.referenceAxisEnd);
this.referenceRadius = this.DistanceBetween(this.center, this.referenceAxisEnd); this.referenceRadius = this.DistanceBetween(this.center, this.referenceAxisEnd);
this.secondRadius = this.referenceRadius * this.axisRatio; 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);
} }
/// <inheritdoc /> /// <inheritdoc />
@ -112,14 +127,17 @@ namespace SixLabors.ImageSharp.Processing.Drawing.Brushes.GradientBrushes
/// <inheritdoc /> /// <inheritdoc />
protected override float PositionOnGradient(int xt, int yt) 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 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 x = (x0 * this.cosRotation) - (y0 * this.sinRotation);
float y = (float)((x0 * Math.Sin(this.rotation)) + (y0 * Math.Cos(this.rotation))); float y = (x0 * this.sinRotation) + (y0 * this.cosRotation);
float xSquared = x * x;
float ySquared = y * y;
var inBoundaryChecker = ((x * x) / (this.referenceRadius * this.referenceRadius)) var inBoundaryChecker = (xSquared / this.referenceRadiusSquared)
+ ((y * y) / (this.secondRadius * this.secondRadius)); + (ySquared / this.secondRadiusSquared);
return inBoundaryChecker; return inBoundaryChecker;
} }

Loading…
Cancel
Save