ImageResizer  3.4.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Events
Public Member Functions | Static Public Member Functions | List of all members
ImageResizer.Util.PolygonMath Class Reference

Defines a collection of utility functions for manipulating polygons. These functions may be (re)moved in future releases; be prepared. More...

Public Member Functions

delegate object ForEachFunction (object o)
 

Static Public Member Functions

static PointF[] RoundPoints (PointF[] a)
 Rounds the elements of the specified array [not used] More...
 
static PointF[,] RoundPoints (PointF[,] a)
 Rounds the elements of the specified array [not used] More...
 
static void ForEach (Array a, ForEachFunction func)
 Modifies the specified array by applying the specified function to each element. More...
 
static PointF[] RotatePoly (PointF[] poly, double degrees)
 Rotates the specified polygon (or set of points) around the origin. More...
 
static PointF[] RotatePoly (PointF[] poly, double degrees, PointF origin)
 Rotates the specified polygon (or set of points) around the origin. More...
 
static PointF[] ScalePoints (PointF[] poly, double xfactor, double yfactor, PointF origin)
 
static PointF ScalePoint (PointF point, double xfactor, double yfactor, PointF origin)
 
static PointF[] ToPoly (RectangleF rect)
 Returns a clockwise array of points on the rectangle. Point 0 is top-left. More...
 
static PointF[] NormalizePoly (PointF[] poly)
 Moves the polygon so that the upper-left corner of its bounding box is located at 0,0. More...
 
static PointF RotateVector (PointF v, double radians)
 Rotates the specified point around the origin. More...
 
static PointF RotateVector (PointF v, double radians, PointF origin)
 
static PointF ChangeMagnitude (PointF v, float length)
 Returns a modified version of the specified vector with the desired length. More...
 
static RectangleF GetBoundingBox (PointF[] points)
 Returns a bounding box for the specified set of points. More...
 
static RectangleF GetBoundingBox (double[] flattenedPoints)
 Returns a bounding box for the specified set of points. Odd points are Y values, even points are X values More...
 
static PointF[] MovePoly (PointF[] points, PointF offset)
 Returns a modified version of the array, with each element being offset by the specified amount. More...
 
static Boolean ArraysEqual (PointF[] a1, PointF[] a2)
 Returns true if the member elements of the specified arrays match, and the arrays are of the same length. More...
 
static PointF[] getParallelogram (PointF[] quad)
 Moves element 4 to spot 3 and truncates to 3 elements. For compatiblity with Graphics.DrawImage More...
 
static SizeF getParallelogramSize (PointF[] p)
 Determines the width and height of the paralellogram. More...
 
static PointF[] GetSubArray (PointF[,] array, int index)
 Grabs a single-dimension array from a 2 dimensional array, using the specified primary index. More...
 
static Brush GenerateRadialBrush (Color inner, Color outer, PointF pt, float width)
 Approximates a radial brush using a high-rez PathGradientBrush. More...
 
static SizeF ScaleInside (SizeF inner, SizeF bounding)
 Scales 'inner' to fit inside 'bounding' while maintaining aspect ratio. Upscales and downscales. More...
 
static SizeF ScaleOutside (SizeF innerBounds, SizeF outer)
 Scales 'outer' to be equal or larger than 'innerBounds' while maintaining aspect ratio. Upscales and downscales. More...
 
static SizeF DownScaleInside (SizeF inner, SizeF bounding)
 Scales 'inner' to fit inside 'bounding' while maintaining aspect ratio. Only downscales. More...
 
static bool FitsInside (SizeF inner, SizeF outer)
 Returns true if 'inner' fits inside or equals 'outer' More...
 
static PointF[,] GetCorners (PointF[] poly, float width)
 Returns an array of parallelograms. These parallelgrams are the 'corners' outside each vertex in 'poly'. The adjacent edges are perpendicular to 'poly'. Point 1 of each parallelogram will match the respective point in 'poly' Points are clockwise. More...
 
static PointF[,] GetCorners (PointF[] poly, float[] widths)
 Returns an array of parallelograms. These parallelgrams are the 'corners' outside each vertex in 'poly'. The adjacent edges are perpendicular to 'poly'. Point 1 of each parallelogram will match the respective point in 'poly' Points are clockwise. More...
 
static PointF[,] GetSides (PointF[] poly, float width)
 Returns an array of parallelograms. These parallelgrams are the 'sides' bounding the polygon. Points are clockwise. Point 1 is the top-left outer point, point 2 the top-right, point 3 the bottom-right, and point 4 the bottom-left. More...
 
static PointF[] InflatePoly (PointF[] poly, float offset)
 Expands all sides on the specified polygon by the specified offset. Assumes the polygon is concave. Returns a new polygon More...
 
static PointF[] InflatePoly (PointF[] poly, float[] offsets)
 Expands all sides on the specified polygon by the specified offsets. Assumes the polygon is concave. Returns a new polygon. More...
 
static PointF[] CenterInside (PointF[] inner, PointF[] outer)
 Moves 'inner' so that the center of its bounding box equals the center of the bounding box of 'outer' More...
 
static RectangleF CenterInside (SizeF size, RectangleF bounds)
 Creates a rectangle of size 'size' with a center matching that of bounds. More...
 
static Rectangle ToRectangle (RectangleF r)
 Rounds a floating-point rectangle to an integer rectangle using System.Round More...
 
static Size RoundPoints (SizeF sizeF)
 
static RectangleF AlignWith (RectangleF obj, RectangleF container, ContentAlignment align)
 Aligns the specified rectangle object with its reference ('container') rectangle using the specified alignment. The container can be smaller than 'obj'. More...
 
static PointF[] AlignWith (PointF[] obj, PointF[] container, ContentAlignment align)
 Aligns the specified polygon with its container (reference) polygon using the specified alignment. The container can be smaller than 'obj'. More...
 
static PointF Average (PointF a, PointF b)
 Returns a point equidistant beweteen A and B More...
 
static bool IsUnrotated (PointF[] a)
 Verifies that the specified 4 points are not rotated - that each point shares either the x or y coordinate with the previous point More...
 
static double GetShortestPair (PointF[] poly)
 Returns the length of the shortest line segment in the given polygon. More...
 
static double Dist (PointF a, PointF b)
 Returns the distance between two points More...
 
static double NormalizeTo90Intervals (double d)
 Normalizes the given angle to a positive multiple of 90 degrees between 0 and 270. More...
 
static RotateFlipType CombineFlipAndRotate (RotateFlipType flip, double angle)
 
static RotateFlipType CombineFlipAndRotate (FlipMode flip, double angle)
 Combines the given flipping info and rotation angle into a RotateFlipType value. Rotation angle will snap to nearest 90-degree multiple More...
 
static RectangleF GetCroppingRectangle (double[] cropValues, double xunits, double yunits, SizeF imageSize)
 Used for converting custom crop rectangle coordinates into a valid cropping rectangle. Positive values are relative to 0,0, negative values relative to width, height. X2 and Y2 values of 0 become width and height respectively. More...
 
static RectangleF ClipRectangle (RectangleF box, RectangleF bounds)
 Returns 'box' clipped to be within 'bounds' More...
 
static RectangleF ExpandTo (RectangleF box, SizeF copySize)
 Expands the given rectangle to be the given size while keeping it centered. More...
 
static RectangleF ScaleRect (RectangleF rect, double ExpandX, double ExpandY)
 Expands the given rectangle using the specified scale multipliers, leaving it centered More...
 

Detailed Description

Defines a collection of utility functions for manipulating polygons. These functions may be (re)moved in future releases; be prepared.

Definition at line 13 of file PolygonMath.cs.

Member Function Documentation

static RectangleF ImageResizer.Util.PolygonMath.AlignWith ( RectangleF  obj,
RectangleF  container,
ContentAlignment  align 
)
inlinestatic

Aligns the specified rectangle object with its reference ('container') rectangle using the specified alignment. The container can be smaller than 'obj'.

Parameters
obj
container
align
Returns

Definition at line 598 of file PolygonMath.cs.

598  {
599  if (align == ContentAlignment.BottomLeft || align == ContentAlignment.MiddleLeft || align == ContentAlignment.TopLeft)
600  obj.X = container.X;
601  if (align == ContentAlignment.BottomCenter || align == ContentAlignment.MiddleCenter || align == ContentAlignment.TopCenter)
602  obj.X = container.X + (container.Width - obj.Width) / 2;
603  if (align == ContentAlignment.BottomRight || align == ContentAlignment.MiddleRight || align == ContentAlignment.TopRight)
604  obj.X = container.X + (container.Width - obj.Width);
605  if (align == ContentAlignment.TopLeft || align == ContentAlignment.TopCenter || align == ContentAlignment.TopRight)
606  obj.Y = container.Y;
607  if (align == ContentAlignment.MiddleLeft || align == ContentAlignment.MiddleCenter || align == ContentAlignment.MiddleRight)
608  obj.Y = container.Y + (container.Height - obj.Height) / 2;
609  if (align == ContentAlignment.BottomLeft || align == ContentAlignment.BottomCenter || align == ContentAlignment.BottomRight)
610  obj.Y = container.Y + (container.Height - obj.Height);
611 
612  return obj;
613  }
static PointF [] ImageResizer.Util.PolygonMath.AlignWith ( PointF[]  obj,
PointF[]  container,
ContentAlignment  align 
)
inlinestatic

Aligns the specified polygon with its container (reference) polygon using the specified alignment. The container can be smaller than 'obj'.

Parameters
obj
container
align
Returns

Definition at line 621 of file PolygonMath.cs.

621  {
622  RectangleF origBounds = PolygonMath.GetBoundingBox(obj);
623  RectangleF newBounds = AlignWith(PolygonMath.GetBoundingBox(obj), PolygonMath.GetBoundingBox(container), align);
624  return PolygonMath.MovePoly(obj, new PointF(newBounds.X - origBounds.X, newBounds.Y - origBounds.Y));
625  }
static RectangleF AlignWith(RectangleF obj, RectangleF container, ContentAlignment align)
Aligns the specified rectangle object with its reference ('container') rectangle using the specified ...
Definition: PolygonMath.cs:598
static Boolean ImageResizer.Util.PolygonMath.ArraysEqual ( PointF[]  a1,
PointF[]  a2 
)
inlinestatic

Returns true if the member elements of the specified arrays match, and the arrays are of the same length.

Parameters
a1
a2
Returns

Definition at line 275 of file PolygonMath.cs.

Referenced by ImageResizer.ImageBuilder.RenderBackground().

276  {
277 
278  if (a1.Length != a2.Length) return false;
279  for (int i = 0; i < a1.Length; i++)
280  {
281  if (a1[i] != a2[i]) return false;
282  }
283  return true;
284  }
static PointF ImageResizer.Util.PolygonMath.Average ( PointF  a,
PointF  b 
)
inlinestatic

Returns a point equidistant beweteen A and B

Parameters
a
b
Returns

Definition at line 633 of file PolygonMath.cs.

633  {
634  return new PointF((a.X + b.X) / 2, (a.Y + b.Y) / 2);
635  }
static PointF [] ImageResizer.Util.PolygonMath.CenterInside ( PointF[]  inner,
PointF[]  outer 
)
inlinestatic

Moves 'inner' so that the center of its bounding box equals the center of the bounding box of 'outer'

Parameters
inner
outer
Returns

Definition at line 557 of file PolygonMath.cs.

558  {
559  RectangleF inBox = GetBoundingBox(inner);
560  RectangleF outBox = GetBoundingBox(outer);
561 
562  return MovePoly(NormalizePoly(inner), new PointF((outBox.Width - inBox.Width) / 2 + outBox.X,
563  (outBox.Height - inBox.Height) / 2 + outBox.Y));
564  }
static PointF[] NormalizePoly(PointF[] poly)
Moves the polygon so that the upper-left corner of its bounding box is located at 0...
Definition: PolygonMath.cs:151
static PointF[] MovePoly(PointF[] points, PointF offset)
Returns a modified version of the array, with each element being offset by the specified amount...
Definition: PolygonMath.cs:258
static RectangleF GetBoundingBox(PointF[] points)
Returns a bounding box for the specified set of points.
Definition: PolygonMath.cs:218
static RectangleF ImageResizer.Util.PolygonMath.CenterInside ( SizeF  size,
RectangleF  bounds 
)
inlinestatic

Creates a rectangle of size 'size' with a center matching that of bounds.

Returns

Definition at line 571 of file PolygonMath.cs.

571  {
572  return new RectangleF(bounds.Width / 2 + bounds.X - (size.Width / 2), bounds.Height / 2 + bounds.Y - (size.Height / 2), size.Width, size.Height);
573  }
static PointF ImageResizer.Util.PolygonMath.ChangeMagnitude ( PointF  v,
float  length 
)
inlinestatic

Returns a modified version of the specified vector with the desired length.

Parameters
v
length
Returns

Definition at line 205 of file PolygonMath.cs.

206  {
207  double curLength = Math.Sqrt((v.X * v.X) + (v.Y * v.Y));
208  float factor = (float)(length / curLength);
209  return new PointF(v.X * factor, v.Y * factor);
210  }
static RectangleF ImageResizer.Util.PolygonMath.ClipRectangle ( RectangleF  box,
RectangleF  bounds 
)
inlinestatic

Returns 'box' clipped to be within 'bounds'

Parameters
box
bounds
Returns

Definition at line 795 of file PolygonMath.cs.

795  {
796  var topleft = new PointF(Math.Min(bounds.Right, Math.Max(box.X,bounds.X)),
797  Math.Min(bounds.Bottom, Math.Max(box.Y,bounds.Y)));
798  var bottomright = new PointF(Math.Min(bounds.Right, Math.Max(box.Right,bounds.X)),
799  Math.Min(bounds.Bottom, Math.Max(box.Bottom,bounds.Y)));
800 
801  return new RectangleF(topleft,new SizeF(bottomright.X - topleft.X, bottomright.Y - topleft.Y));
802  }
static RotateFlipType ImageResizer.Util.PolygonMath.CombineFlipAndRotate ( FlipMode  flip,
double  angle 
)
inlinestatic

Combines the given flipping info and rotation angle into a RotateFlipType value. Rotation angle will snap to nearest 90-degree multiple

Parameters
flip
angle
Returns

Definition at line 706 of file PolygonMath.cs.

706  {
707  angle = NormalizeTo90Intervals(angle);
708  if (flip == FlipMode.None) {
709  return (RotateFlipType)(int)(angle / 90);
710  } else if (flip == FlipMode.X) {
711  return (RotateFlipType)(int)(4 + (angle / 90));
712  } else if (flip == FlipMode.Y) {
713  if (angle == 0) return (RotateFlipType)6;
714  if (angle == 90) return (RotateFlipType)7;
715  if (angle == 180) return (RotateFlipType)4;
716  if (angle == 270) return (RotateFlipType)5;
717  } else if (flip == FlipMode.XY) {
718  if (angle == 0) return (RotateFlipType)2;
719  if (angle == 90) return (RotateFlipType)3;
720  if (angle == 180) return (RotateFlipType)0;
721  if (angle == 270) return (RotateFlipType)1;
722  } else {
723  throw new ArgumentException("Invalid FlipMode value " + flip.ToString());
724  }
725  throw new ArgumentException("Invalid angle value " + angle.ToString());
726  }
FlipMode
Horizontal and vertical flipping. Convertible to System.Drawing.RotateFlipType by casting...
static double NormalizeTo90Intervals(double d)
Normalizes the given angle to a positive multiple of 90 degrees between 0 and 270.
Definition: PolygonMath.cs:678
static double ImageResizer.Util.PolygonMath.Dist ( PointF  a,
PointF  b 
)
inlinestatic

Returns the distance between two points

Parameters
pointF
pointF_2
Returns

Definition at line 669 of file PolygonMath.cs.

669  {
670  return Math.Sqrt((b.X - a.X) * (b.X - a.X) + (b.Y - a.Y) * (b.Y - a.Y));
671  }
static SizeF ImageResizer.Util.PolygonMath.DownScaleInside ( SizeF  inner,
SizeF  bounding 
)
inlinestatic

Scales 'inner' to fit inside 'bounding' while maintaining aspect ratio. Only downscales.

Parameters
inner
bounding
Returns

Definition at line 408 of file PolygonMath.cs.

409  {
410  SizeF result = ScaleInside(inner, bounding);
411  if (result.Width > inner.Width || result.Height > inner.Height) return inner;
412  else return result;
413  }
static SizeF ScaleInside(SizeF inner, SizeF bounding)
Scales &#39;inner&#39; to fit inside &#39;bounding&#39; while maintaining aspect ratio. Upscales and downscales...
Definition: PolygonMath.cs:364
static RectangleF ImageResizer.Util.PolygonMath.ExpandTo ( RectangleF  box,
SizeF  copySize 
)
inlinestatic

Expands the given rectangle to be the given size while keeping it centered.

Parameters
box
copySize
Returns

Definition at line 810 of file PolygonMath.cs.

810  {
811  var dx = copySize.Width - box.Width;
812  var dy = copySize.Height - box.Height;
813  return new RectangleF(box.X - (dx / 2), box.Y - (dy / 2), copySize.Width, copySize.Height);
814  }
static bool ImageResizer.Util.PolygonMath.FitsInside ( SizeF  inner,
SizeF  outer 
)
inlinestatic

Returns true if 'inner' fits inside or equals 'outer'

Parameters
inner
outer
Returns

Definition at line 420 of file PolygonMath.cs.

Referenced by ImageResizer.Plugins.CropAround.CropAroundPlugin.LayoutImage(), and ImageResizer.ImageBuilder.LayoutImage().

421  {
422  if (inner.Width > outer.Width) return false;
423  if (inner.Height > outer.Height) return false;
424  return true;
425  }
static void ImageResizer.Util.PolygonMath.ForEach ( Array  a,
ForEachFunction  func 
)
inlinestatic

Modifies the specified array by applying the specified function to each element.

Parameters
a
funcobject delegate(object o){}
Returns

Definition at line 57 of file PolygonMath.cs.

58  {
59  long[] ix = new long[a.Rank];
60  //Init index
61  for (int i = 0; i < ix.Length;i++) ix[i] = a.GetLowerBound(i);
62 
63  //Loop through all items
64  for (long i = 0; i < a.LongLength; i++){
65  a.SetValue(func(a.GetValue(ix)),ix);
66 
67  //Increment ix, the index
68  for (int j = 0; j < ix.Length; j++)
69  {
70  if (ix[j] < a.GetUpperBound(j))
71  {
72  ix[j]++;
73  break; //We're done incrementing.
74  }
75  else
76  {
77  //Ok, reset this one and increment the next.
78  ix[j] = a.GetLowerBound(j);
79  //If this is the last dimension, assert
80  //that we are at the last element
81  if (j == ix.Length - 1)
82  {
83  if (i < a.LongLength - 1) throw new Exception();
84  }
85  continue;
86  }
87  }
88  }
89  return;
90  }
static Brush ImageResizer.Util.PolygonMath.GenerateRadialBrush ( Color  inner,
Color  outer,
PointF  pt,
float  width 
)
inlinestatic

Approximates a radial brush using a high-rez PathGradientBrush.

Parameters
inner
outer
pt
width
Returns

Definition at line 334 of file PolygonMath.cs.

335  {
336  //This should approximate one outer point per pixel.
337  PointF[] path = new PointF[(int)Math.Round(width * 2 * Math.PI) + 1];
338  for (int i = 0; i < path.Length - 1; i++)
339  {
340  double radians = ((double)i - width) / width; //calculate the radians at this index.
341  //Calculate a point based off the radians.
342  path[i] = new PointF((float)(Math.Sin(radians) * width + pt.X), (float)(Math.Cos(radians) * width + pt.Y));
343  }
344  path[path.Length - 1] = path[0]; //Loop back to complete the circle.
345 
346  PathGradientBrush b = new PathGradientBrush(path);
347  b.CenterColor = inner;
348  b.CenterPoint = pt;
349  b.WrapMode = WrapMode.Clamp;
350  //All outer colors are the same.
351  Color[] colors = new Color[path.Length];
352  for (int i = 0; i < colors.Length; i++) colors[i] = outer;
353  b.SurroundColors = colors;
354  b.SetSigmaBellShape(1);
355  return b;
356  }
static RectangleF ImageResizer.Util.PolygonMath.GetBoundingBox ( PointF[]  points)
inlinestatic

Returns a bounding box for the specified set of points.

Parameters
points
Returns

Definition at line 218 of file PolygonMath.cs.

Referenced by ImageResizer.Util.PolygonMath.AlignWith(), ImageResizer.Plugins.Watermark.Layer.CalculateLayerCoordinates(), and ImageResizer.Plugins.Basic.Trial.PreFlushChanges().

219  {
220  float left = float.MaxValue;
221  float top = float.MaxValue;
222  float right = float.MinValue;
223  float bottom = float.MinValue;
224  foreach (PointF f in points)
225  {
226  if (f.X < left) left = f.X;
227  if (f.X > right) right = f.X;
228  if (f.Y < top) top = f.Y;
229  if (f.Y > bottom) bottom = f.Y;
230  }
231  return new RectangleF(left, top, right - left, bottom - top);
232  }
static RectangleF ImageResizer.Util.PolygonMath.GetBoundingBox ( double[]  flattenedPoints)
inlinestatic

Returns a bounding box for the specified set of points. Odd points are Y values, even points are X values

Parameters
points
Returns

Definition at line 238 of file PolygonMath.cs.

238  {
239  double? minx = null, maxx = null, miny = null, maxy = null;
240  for (var i = 0; i < flattenedPoints.Length; i++) {
241  var v = flattenedPoints[i];
242  if (i % 2 == 0) {
243  if (minx == null || v < minx.Value) minx = v;
244  if (maxx == null || v > maxx.Value) maxx = v;
245  } else {
246  if (miny == null || v < miny.Value) miny = v;
247  if (maxy == null || v > maxy.Value) maxy = v;
248  }
249  }
250  return new RectangleF((float)minx, (float)miny, (float)(maxx - minx), (float)(maxy - miny));
251  }
static PointF [,] ImageResizer.Util.PolygonMath.GetCorners ( PointF[]  poly,
float  width 
)
inlinestatic

Returns an array of parallelograms. These parallelgrams are the 'corners' outside each vertex in 'poly'. The adjacent edges are perpendicular to 'poly'. Point 1 of each parallelogram will match the respective point in 'poly' Points are clockwise.

TODO - some rounding issues going on, not exact numbers here

Parameters
poly
width
Returns

Definition at line 436 of file PolygonMath.cs.

437  {
438  //Build the widths array.
439  float[] widths = new float[poly.Length];
440  for(int i = 0; i < widths.Length;i++) widths[i] = width;
441  //Call
442  return GetCorners(poly,widths);
443  }
static PointF[,] GetCorners(PointF[] poly, float width)
Returns an array of parallelograms. These parallelgrams are the &#39;corners&#39; outside each vertex in &#39;pol...
Definition: PolygonMath.cs:436
static PointF [,] ImageResizer.Util.PolygonMath.GetCorners ( PointF[]  poly,
float[]  widths 
)
inlinestatic

Returns an array of parallelograms. These parallelgrams are the 'corners' outside each vertex in 'poly'. The adjacent edges are perpendicular to 'poly'. Point 1 of each parallelogram will match the respective point in 'poly' Points are clockwise.

Each float in widths[] corresponds to the point in poly[]. This is the distance to go perpendicularly from the line beween poly[i] and poly[i +1].

Parameters
poly
widths
Returns

Definition at line 456 of file PolygonMath.cs.

457  {
458  if (poly.Length != widths.Length) throw new ArgumentException("Arrays 'poly' and 'widths' must have the same number of elements");
459 
460  PointF[,] corners = new PointF[poly.Length, 4];
461  int end = (poly.Length - 1); //the last index in the array
462  for (int i = 0; i < poly.Length; i++)
463  {
464 
465  //Get next an prev points. Wrap around. Clockwise.
466  PointF next = (i < end) ? poly[i + 1] : poly[i - end];
467  PointF prev = (i > 0) ? poly[i - 1] : poly[i + end];
468  PointF current = poly[i];
469 
470  float prevWidth = (i > 0) ? widths[i-1] : widths[i + end];
471  float width = widths[i];
472 
473  //Radians = pi/(180*degrees)
474  //Degrees = radians*180/pi
475  //Get vectors perpendicular to next and prev, with lengths of 'offset'.
476  PointF pP = ChangeMagnitude(
477  RotateVector(new PointF(prev.X - current.X, prev.Y - current.Y), Math.PI / 2) //rotate 90 clockwise.
478  , prevWidth); //scale to offset length.
479 
480  PointF pN = ChangeMagnitude(
481  RotateVector(new PointF(next.X - current.X, next.Y - current.Y), Math.PI / -2) //rotate 90 counter-clockwise.
482  , width); //scale to offset length.
483 
484  //Add to get points 2 and 4 of the parallelogram.
485  //Add both to get point 3
486  corners[i, 0] = current;
487  corners[i, 1] = new PointF(current.X + pP.X, current.Y + pP.Y);
488  corners[i, 2] = new PointF(current.X + pP.X + pN.X, current.Y + pP.Y + pN.Y);
489  corners[i, 3] = new PointF(current.X + pN.X, current.Y + pN.Y);
490  }
491  return corners;
492  }
static PointF ChangeMagnitude(PointF v, float length)
Returns a modified version of the specified vector with the desired length.
Definition: PolygonMath.cs:205
static PointF RotateVector(PointF v, double radians)
Rotates the specified point around the origin.
Definition: PolygonMath.cs:162
static RectangleF ImageResizer.Util.PolygonMath.GetCroppingRectangle ( double[]  cropValues,
double  xunits,
double  yunits,
SizeF  imageSize 
)
inlinestatic

Used for converting custom crop rectangle coordinates into a valid cropping rectangle. Positive values are relative to 0,0, negative values relative to width, height. X2 and Y2 values of 0 become width and height respectively.

Parameters
cropValuesAn array of 4 elements defining x1, y1, x2, and y2 of the cropping rectangle
xunitsThe width x1 and x2 are relative to
yunitsThe height y1 and y2 are relative to
imageSizeThe size of the uncropped image
Returns

Definition at line 738 of file PolygonMath.cs.

738  {
739  RectangleF defValue = new RectangleF(new PointF(0, 0), imageSize);
740  double[] c = cropValues;
741 
742  //Step 2, Apply units to values, resolving against imageSize
743  for (int i = 0; i < c.Length; i++) {
744  bool xvalue = i % 2 == 0;
745  if (xvalue && xunits != 0) c[i] *= (imageSize.Width / xunits);
746  if (!xvalue && xunits != 0) c[i] *= (imageSize.Height / yunits);
747 
748  //Prohibit values larger than imageSize
749  if (xvalue && c[i] > imageSize.Width) c[i] = imageSize.Width;
750  if (!xvalue && c[i] > imageSize.Height) c[i] = imageSize.Height;
751  }
752 
753  //Step 3, expand width/height crop to 4-value crop (not currently used)
754  if (c.Length == 2) {
755  if (c[0] < 1 || c[1] < 1) return defValue; //We can't do anything with negative values here
756  //Center horizontally and vertically.
757  double x = (imageSize.Width - c[0]) / 2;
758  double y = (imageSize.Height - c[1]) / 2;
759 
760  c = new double[] { x, y, x + c[0], y + c[1] };
761  }
762 
763  double x1 = c[0], y1 = c[1], x2 = c[2], y2 = c[3];
764 
765  //allow negative offsets
766  if (x1 < 0) x1 += imageSize.Width;
767  if (y1 < 0) y1 += imageSize.Height;
768  if (x2 <= 0) x2 += imageSize.Width;
769  if (y2 <= 0) y2 += imageSize.Height;
770 
771 
772  //Require box stay in bounds.
773  if (x1 < 0) x1 = 0; if (x2 < 0) x2 = 0;
774  if (y1 < 0) y1 = 0; if (y2 < 0) y2 = 0;
775  if (x1 > imageSize.Width) x1 = imageSize.Width;
776  if (x2 > imageSize.Width) x2 = imageSize.Width;
777  if (y1 > imageSize.Height) y1 = imageSize.Height;
778  if (y2 > imageSize.Height) y2 = imageSize.Height;
779 
780  //Require positive width and height.
781  if (x2 <= x1 || y2 <= y1) {
782  //Use original dimensions - can't recover from negative width or height in cropping rectangle
783  return new RectangleF(new PointF(0, 0), imageSize);
784  }
785 
786  return new RectangleF((float)x1, (float)y1, (float)(x2 - x1), (float)(y2 - y1));
787  }
static PointF [] ImageResizer.Util.PolygonMath.getParallelogram ( PointF[]  quad)
inlinestatic

Moves element 4 to spot 3 and truncates to 3 elements. For compatiblity with Graphics.DrawImage

Parameters
quad
Returns

Definition at line 292 of file PolygonMath.cs.

293  {
294  PointF[] p = new PointF[3];
295  p[0] = quad[0];
296  p[1] = quad[1];
297  p[2] = quad[3];
298  return p;
299  }
static SizeF ImageResizer.Util.PolygonMath.getParallelogramSize ( PointF[]  p)
inlinestatic

Determines the width and height of the paralellogram.

Parameters
p
Returns

Definition at line 306 of file PolygonMath.cs.

306  {
307  double width = Math.Sqrt(Math.Pow(p[0].X - p[1].X,2) + Math.Pow(p[0].Y - p[1].Y,2));
308  double height = Math.Sqrt(Math.Pow(p[1].X - p[2].X,2) + Math.Pow(p[1].Y - p[2].Y,2));
309  return new SizeF((float)width, (float)height);
310  }
static double ImageResizer.Util.PolygonMath.GetShortestPair ( PointF[]  poly)
inlinestatic

Returns the length of the shortest line segment in the given polygon.

Returns

Definition at line 652 of file PolygonMath.cs.

652  {
653  PointF last = poly[poly.Length - 1];
654  double max = 0;
655  foreach (PointF p in poly) {
656  double dist = Math.Sqrt((p.X - last.X) * (p.X - last.X) + (p.Y - last.Y) * (p.Y - last.Y));
657  if (dist > max) max = dist;
658  last = p;
659  }
660  return max;
661  }
static PointF [,] ImageResizer.Util.PolygonMath.GetSides ( PointF[]  poly,
float  width 
)
inlinestatic

Returns an array of parallelograms. These parallelgrams are the 'sides' bounding the polygon. Points are clockwise. Point 1 is the top-left outer point, point 2 the top-right, point 3 the bottom-right, and point 4 the bottom-left.

Parameters
poly
width
Returns

Definition at line 500 of file PolygonMath.cs.

501  {
502  //Just grab the sides between the corners.
503  PointF[,] corners = GetCorners(poly, width);
504  PointF[,] sides = new PointF[corners.GetUpperBound(0) + 1, 4];
505  for (int i = 0; i <= corners.GetUpperBound(0); i++)
506  {
507  int next = (i < corners.GetUpperBound(0)) ? i + 1 : i - corners.GetUpperBound(0);
508  sides[i, 0] = corners[i, 3];
509  sides[i, 3] = corners[i, 0];
510  sides[i, 1] = corners[next, 1];
511  sides[i, 2] = corners[next, 0];
512  }
513  return sides;
514  }
static PointF[,] GetCorners(PointF[] poly, float width)
Returns an array of parallelograms. These parallelgrams are the &#39;corners&#39; outside each vertex in &#39;pol...
Definition: PolygonMath.cs:436
static PointF [] ImageResizer.Util.PolygonMath.GetSubArray ( PointF  array[,],
int  index 
)
inlinestatic

Grabs a single-dimension array from a 2 dimensional array, using the specified primary index.

Parameters
array
index
Returns

Definition at line 319 of file PolygonMath.cs.

320  {
321  PointF[] sub = new PointF[array.GetUpperBound(1) + 1];
322  for (int i = 0; i < array.GetUpperBound(1) + 1; i++)
323  sub[i] = array[index, i];
324  return sub;
325  }
static PointF [] ImageResizer.Util.PolygonMath.InflatePoly ( PointF[]  poly,
float  offset 
)
inlinestatic

Expands all sides on the specified polygon by the specified offset. Assumes the polygon is concave. Returns a new polygon

Parameters
poly
offset
Returns

Definition at line 522 of file PolygonMath.cs.

Referenced by ImageResizer.Resizing.LayoutBuilder.AddRing().

523  {
524  PointF[,] corners = GetCorners(poly, offset);
525  PointF[] newPoly = new PointF[poly.Length];
526  for (int i = 0; i <= corners.GetUpperBound(0); i++)
527  newPoly[i] = corners[i, 2]; //Just grab the outer corner
528 
529  return newPoly;
530 
531  }
static PointF[,] GetCorners(PointF[] poly, float width)
Returns an array of parallelograms. These parallelgrams are the &#39;corners&#39; outside each vertex in &#39;pol...
Definition: PolygonMath.cs:436
static PointF [] ImageResizer.Util.PolygonMath.InflatePoly ( PointF[]  poly,
float[]  offsets 
)
inlinestatic

Expands all sides on the specified polygon by the specified offsets. Assumes the polygon is concave. Returns a new polygon.

Parameters
poly
offsetsAn array the same size as poly[], with the distances to expand the edges. Edges are between i and i+1
Returns

Definition at line 540 of file PolygonMath.cs.

541  {
542  PointF[,] corners = GetCorners(poly, offsets);
543  PointF[] newPoly = new PointF[poly.Length];
544  for (int i = 0; i <= corners.GetUpperBound(0); i++)
545  newPoly[i] = corners[i, 2]; //Just grab the outer corner
546 
547  return newPoly;
548 
549  }
static PointF[,] GetCorners(PointF[] poly, float width)
Returns an array of parallelograms. These parallelgrams are the &#39;corners&#39; outside each vertex in &#39;pol...
Definition: PolygonMath.cs:436
static bool ImageResizer.Util.PolygonMath.IsUnrotated ( PointF[]  a)
inlinestatic

Verifies that the specified 4 points are not rotated - that each point shares either the x or y coordinate with the previous point

Returns

Definition at line 640 of file PolygonMath.cs.

640  {
641  PointF lastPoint = a[a.GetUpperBound(0)];
642  foreach (PointF p in a) {
643  if (p.X != lastPoint.X && p.Y != lastPoint.Y) return false;
644  lastPoint = p;
645  }
646  return true;
647  }
static PointF [] ImageResizer.Util.PolygonMath.MovePoly ( PointF[]  points,
PointF  offset 
)
inlinestatic

Returns a modified version of the array, with each element being offset by the specified amount.

Parameters
points
offset
Returns

Definition at line 258 of file PolygonMath.cs.

259  {
260  PointF[] pts = new PointF[points.Length];
261  for (int i = 0; i < points.Length; i++)
262  {
263  pts[i].X = points[i].X + offset.X;
264  pts[i].Y = points[i].Y + offset.Y;
265  }
266  return pts;
267  }
static PointF [] ImageResizer.Util.PolygonMath.NormalizePoly ( PointF[]  poly)
inlinestatic

Moves the polygon so that the upper-left corner of its bounding box is located at 0,0.

Parameters
poly
Returns

Definition at line 151 of file PolygonMath.cs.

152  {
153  RectangleF box = GetBoundingBox(poly);
154  return PolygonMath.MovePoly(poly, new PointF(-box.X, -box.Y));
155  }
static RectangleF GetBoundingBox(PointF[] points)
Returns a bounding box for the specified set of points.
Definition: PolygonMath.cs:218
static double ImageResizer.Util.PolygonMath.NormalizeTo90Intervals ( double  d)
inlinestatic

Normalizes the given angle to a positive multiple of 90 degrees between 0 and 270.

Parameters
d
Returns

Definition at line 678 of file PolygonMath.cs.

678  {
679  d = d % 360;
680  if (d < 0) d += 360;
681 
682  if (d >= 315 && d < 360) return 0;
683  if (d >= 0 && d < 45) return 0;
684  if (d >= 45 && d < 135) return 90;
685  if (d >= 135 && d < 225) return 180;
686  if (d >= 225 && d < 315) return 270;
687 
688  throw new Exception("Impossible");
689  }
static PointF [] ImageResizer.Util.PolygonMath.RotatePoly ( PointF[]  poly,
double  degrees 
)
inlinestatic

Rotates the specified polygon (or set of points) around the origin.

Parameters
poly
degrees
Returns

Definition at line 98 of file PolygonMath.cs.

99  {
100  PointF[] pts = new PointF[poly.Length];
101  for (int i = 0; i < poly.Length; i++)
102  pts[i] = RotateVector(poly[i], degrees * Math.PI / 180);
103  return pts;
104  }
static PointF RotateVector(PointF v, double radians)
Rotates the specified point around the origin.
Definition: PolygonMath.cs:162
static PointF [] ImageResizer.Util.PolygonMath.RotatePoly ( PointF[]  poly,
double  degrees,
PointF  origin 
)
inlinestatic

Rotates the specified polygon (or set of points) around the origin.

Parameters
poly
degrees
origin
Returns

Definition at line 113 of file PolygonMath.cs.

113  {
114  PointF[] pts = new PointF[poly.Length];
115  for (int i = 0; i < poly.Length; i++)
116  pts[i] = RotateVector(poly[i], degrees * Math.PI / 180,origin);
117  return pts;
118  }
static PointF RotateVector(PointF v, double radians)
Rotates the specified point around the origin.
Definition: PolygonMath.cs:162
static PointF ImageResizer.Util.PolygonMath.RotateVector ( PointF  v,
double  radians 
)
inlinestatic

Rotates the specified point around the origin.

Parameters
v
radians
Returns

Definition at line 162 of file PolygonMath.cs.

163  {
164 /*
165  * 2D Rotation
166  * A point <x,y> can be rotated around the origin <0,0> by running it through the following equations
167  * to get the new point <x',y'> :
168  * x' = cos(theta)*x - sin(theta)*y //cos(90) or cos(-90) = 0
169  * y' = sin(theta)*x + cos(theta)*y //sin(90) or sin(-90) = +/- 1
170 */
171  return new PointF(
172  (float)(Math.Cos(radians) * v.X - Math.Sin(radians) * v.Y),
173  (float)(Math.Sin(radians) * v.X + Math.Cos(radians) * v.Y));
174  }
static PointF ImageResizer.Util.PolygonMath.RotateVector ( PointF  v,
double  radians,
PointF  origin 
)
inlinestatic

Rotates the specified point around the specified origin.

Parameters
v
radians
origin
Returns

Definition at line 183 of file PolygonMath.cs.

184  {
185 /*
186  * 2D Rotation
187  * A point <x,y> can be rotated around the origin <0,0> by running it through the following equations
188  * to get the new point <x',y'> :
189  * x' = cos(theta)*x - sin(theta)*y //cos(90) or cos(-90) = 0
190  * y' = sin(theta)*x + cos(theta)*y //sin(90) or sin(-90) = +/- 1
191 */
192  return new PointF(
193  (float)(Math.Cos(radians) * (v.X - origin.X) - Math.Sin(radians) * (v.Y - origin.Y)) + origin.X,
194  (float)(Math.Sin(radians) * (v.X - origin.X) + Math.Cos(radians) * (v.Y - origin.Y)) + origin.Y);
195  }
static PointF [] ImageResizer.Util.PolygonMath.RoundPoints ( PointF[]  a)
inlinestatic

Rounds the elements of the specified array [not used]

Parameters
a
Returns

Definition at line 20 of file PolygonMath.cs.

Referenced by ImageResizer.ImageBuilder.EndLayout().

21  {
22 
23  ForEach(a, delegate(object o){
24  PointF p = (PointF)o;
25  p.X = (float)Math.Round(p.X);
26  p.Y = (float)Math.Round(p.Y);
27  return p;
28  });
29  return a;
30  }
static void ForEach(Array a, ForEachFunction func)
Modifies the specified array by applying the specified function to each element.
Definition: PolygonMath.cs:57
static PointF [,] ImageResizer.Util.PolygonMath.RoundPoints ( PointF  a[,])
inlinestatic

Rounds the elements of the specified array [not used]

Parameters
a
Returns

Definition at line 37 of file PolygonMath.cs.

38  {
39 
40  ForEach(a, delegate(object o)
41  {
42  PointF p = (PointF)o;
43  p.X = (float)Math.Round(p.X);
44  p.Y = (float)Math.Round(p.Y);
45  return p;
46  });
47  return a;
48  }
static void ForEach(Array a, ForEachFunction func)
Modifies the specified array by applying the specified function to each element.
Definition: PolygonMath.cs:57
static SizeF ImageResizer.Util.PolygonMath.ScaleInside ( SizeF  inner,
SizeF  bounding 
)
inlinestatic

Scales 'inner' to fit inside 'bounding' while maintaining aspect ratio. Upscales and downscales.

Parameters
inner
bounding
Returns

Definition at line 364 of file PolygonMath.cs.

365  {
366  double innerRatio = inner.Width / inner.Height;
367  double outerRatio = bounding.Width / bounding.Height;
368 
369  if (outerRatio > innerRatio)
370  {
371  //Width is wider - so bound by height.
372  return new SizeF((float)(innerRatio * bounding.Height), (float)(bounding.Height));
373  }
374  else
375  {
376  //Height is higher, or aspect ratios are identical.
377  return new SizeF((float)(bounding.Width), (float)(bounding.Width / innerRatio));
378  }
379  }
static SizeF ImageResizer.Util.PolygonMath.ScaleOutside ( SizeF  innerBounds,
SizeF  outer 
)
inlinestatic

Scales 'outer' to be equal or larger than 'innerBounds' while maintaining aspect ratio. Upscales and downscales.

Parameters
innerBounds
outer
Returns

Definition at line 387 of file PolygonMath.cs.

Referenced by ImageResizer.Plugins.CropAround.CropAroundPlugin.LayoutImage().

387  {
388 
389  double innerRatio = innerBounds.Width / innerBounds.Height;
390  double outerRatio = outer.Width / outer.Height;
391 
392  if (outerRatio > innerRatio) {
393  //Width is wider - so bound by height.
394  return new SizeF((float)(outerRatio * innerBounds.Height), (float)(innerBounds.Height));
395  } else {
396  //Height is higher, or aspect ratios are identical.
397  return new SizeF((float)(innerBounds.Width), (float)(innerBounds.Width / outerRatio));
398  }
399  }
static RectangleF ImageResizer.Util.PolygonMath.ScaleRect ( RectangleF  rect,
double  ExpandX,
double  ExpandY 
)
inlinestatic

Expands the given rectangle using the specified scale multipliers, leaving it centered

Parameters
rect
ExpandX
ExpandY
Returns

Definition at line 823 of file PolygonMath.cs.

Referenced by ImageResizer.Plugins.Faces.FaceDetection.DetectFeatures().

823  {
824  var dx = (float)(rect.Width * ExpandX);
825  var dy = (float)(rect.Height * ExpandY);
826  return new RectangleF(rect.X - (dx / 2), rect.Y - (dy / 2), rect.Width + dx, rect.Height + dy);
827  }
static PointF [] ImageResizer.Util.PolygonMath.ToPoly ( RectangleF  rect)
inlinestatic

Returns a clockwise array of points on the rectangle. Point 0 is top-left.

Parameters
rect
Returns

Definition at line 137 of file PolygonMath.cs.

138  {
139  PointF[] r = new PointF[4];
140  r[0] = rect.Location;
141  r[1] = new PointF(rect.Right, rect.Top);
142  r[2] = new PointF(rect.Right, rect.Bottom);
143  r[3] = new PointF(rect.Left, rect.Bottom);
144  return r;
145  }
static Rectangle ImageResizer.Util.PolygonMath.ToRectangle ( RectangleF  r)
inlinestatic

Rounds a floating-point rectangle to an integer rectangle using System.Round

Parameters
r
Returns

Definition at line 581 of file PolygonMath.cs.

582  {
583  return new Rectangle((int)Math.Round(r.X), (int)Math.Round(r.Y), (int)Math.Round(r.Width),(int)Math.Round( r.Height));
584  }

The documentation for this class was generated from the following file: