// ----------------------- // Public Constructors // ----------------------- /// <summary> /// Creates a RectangleF from PointF and SizeF values. /// </summary> /// <param name="location">Location.</param> /// <param name="size">Size.</param> public RectangleF(PointF location, SizeF size) { x = location.X; y = location.Y; width = size.Width; height = size.Height; }
/// <summary> /// Clamps point coordinate according to the specified size (0,0, size.Width, size.Height). /// </summary> /// <param name="point">The point to clamp.</param> /// <param name="size">The valid region.</param> /// <returns>Clamped point.</returns> public static PointF Clamp(this PointF point, SizeF size) { return new PointF { X = System.Math.Min(System.Math.Max(0, point.X), size.Width), Y = System.Math.Min(System.Math.Max(0, point.Y), size.Height) }; }
/// <summary> /// Inflates the rectangle by specified width and height (can be negative) and automatically clamps rectangle coordinates. /// </summary> /// <param name="rect">Rectangle to inflate.</param> /// <param name="widthScale">Horizontal scale.</param> /// <param name="heightScale">Vertical scale.</param> /// <param name="constrainedArea">If specified rectangle region will be clamped.</param> /// <returns>Inflated rectangle.</returns> public static RectangleF Inflate(this RectangleF rect, double widthScale, double heightScale, SizeF constrainedArea = default(SizeF)) { RectangleF newRect = new RectangleF { X = (float)(rect.X - rect.Width * widthScale / 2), Y = (float)(rect.Y - rect.Height * heightScale / 2), Width = (float)(rect.Width + rect.Width * widthScale), Height = (float)(rect.Height + rect.Height * heightScale) }; if (constrainedArea.IsEmpty == false) newRect.Intersect(new RectangleF(new PointF(), constrainedArea)); return newRect; }
/// <summary> /// Changes rectangle's location in order to fit the specified area. /// </summary> /// <param name="rect">Rectangle.</param> /// <param name="area">Valid bounding box.</param> /// <param name="translatedRectangle">Translated rectangle.</param> /// <returns>True if the translation exist, false otherwise.</returns> public static bool MoveToFit(this RectangleF rect, SizeF area, out RectangleF translatedRectangle) { var leftOffset = (rect.X < 0) ? -rect.X : 0; var topOffset = (rect.Y < 0) ? -rect.Y : 0; rect.X += leftOffset; rect.Y += topOffset; var rightOffset = (rect.Right > area.Width) ? (area.Width - rect.Right) : 0; var bottomOffset = (rect.Bottom > area.Height) ? (area.Height - rect.Bottom) : 0; rect.X += rightOffset; rect.Y += bottomOffset; translatedRectangle = rect; if (rect.X < 0 || rect.Y < 0 || rect.Right > area.Width || rect.Bottom > area.Height) return false; return true; }
/// <summary> /// Creates new structure from area and angle. /// </summary> /// <param name="center">Box2D center.</param> /// <param name="size">Box 2D size.</param> /// <param name="angle">Angle in degrees.</param> public Box2D(PointF center, SizeF size, float angle) { this.Center = new PointF(center.X, center.Y); this.Size = size; this.Angle = angle; }
/// <summary> /// Translates a PointF by the negative of a specified size. /// </summary> /// <param name="pt">Point.</param> /// <param name="sz">Offset.</param> /// <returns>PointF structure.</returns> public static PointF Subtract(PointF pt, SizeF sz) { return(new PointF(pt.X - sz.Width, pt.Y - sz.Height)); }
/// <summary> /// Produces a Size structure from a SizeF structure by /// rounding the Width and Height properties. /// </summary> /// <param name="value">Floating-point size structure.</param> /// <returns>Integer size structure.</returns> public static Size Round(SizeF value) { int w, h; checked { w = (int)Math.Round(value.Width); h = (int)Math.Round(value.Height); } return new Size(w, h); }
/// <summary> /// Translates a PointF by the negative of a specified size. /// </summary> /// <param name="pt">Point.</param> /// <param name="sz">Offset.</param> /// <returns>PointF structure.</returns> public static PointF Subtract(PointF pt, SizeF sz) { return new PointF(pt.X - sz.Width, pt.Y - sz.Height); }
/// <summary> /// Creates a SizeF from an existing SizeF value. /// </summary> /// <param name="sz">Size.</param> public SizeF(SizeF sz) { width = sz.Width; height = sz.Height; }
public static SizeF Subtract(SizeF sz1, SizeF sz2) { return(new SizeF(sz1.Width - sz2.Width, sz1.Height - sz2.Height)); }
public static SizeF Add(SizeF sz1, SizeF sz2) { return(new SizeF(sz1.Width + sz2.Width, sz1.Height + sz2.Height)); }
/// <summary> /// SizeF Constructor /// </summary> /// /// <remarks> /// Creates a SizeF from an existing SizeF value. /// </remarks> public SizeF(SizeF size) { width = size.Width; height = size.Height; }
/// <summary> /// Changes rectangle scale by a minimum amount in term of area change to match the scale of the user-specified size scale. /// </summary> /// <param name="rect">Rectangle.</param> /// <param name="other">Size from which the scale is taken.</param> /// <param name="correctLocation">Moves rectangle to minimize the impact of scaling regarding original location.</param> /// <returns>Rectangle that has the same scale as </returns> public static RectangleF ScaleTo(this RectangleF rect, SizeF other, bool correctLocation = true) { return ScaleTo(rect, other.Width / other.Height); }
/// <summary> /// Adds two SizeF structures. /// </summary> /// <param name="sz1">First structure.</param> /// <param name="sz2">Second structure.</param> /// <returns>SizeF structure.</returns> public static SizeF Add(SizeF sz1, SizeF sz2) { return new SizeF(sz1.Width + sz2.Width, sz1.Height + sz2.Height); }
/// <summary> /// Translates a PointF by the positive of a specified size. /// </summary> /// <param name="pt">Point.</param> /// <param name="sz">Offset.</param> /// <returns>PointF structure.</returns> public static PointF Add(PointF pt, SizeF sz) { return new PointF(pt.X + sz.Width, pt.Y + sz.Height); }
/// <summary> /// Subtracts two SizeF structures. /// </summary> /// <param name="sz1">First structure.</param> /// <param name="sz2">Second structure.</param> /// <returns>SizeF structure.</returns> public static SizeF Subtract(SizeF sz1, SizeF sz2) { return new SizeF(sz1.Width - sz2.Width, sz1.Height - sz2.Height); }
/// <summary> /// Produces a Size structure from a SizeF structure by /// taking the ceiling of the Width and Height properties. /// </summary> /// <param name="value">Floating-point size structure.</param> /// <returns>Integer size structure.</returns> public static Size Ceiling(SizeF value) { int w, h; checked { w = (int)Math.Ceiling(value.Width); h = (int)Math.Ceiling(value.Height); } return new Size(w, h); }
/// <summary> /// Enlarges this rectangle by the specified amount. /// </summary> /// <param name="size">The amount to inflate this rectangle.</param> public void Inflate(SizeF size) { x -= size.Width; y -= size.Height; width += size.Width * 2; height += size.Height * 2; }
/// <summary> /// Produces a Size structure from a SizeF structure by /// truncating the Width and Height properties. /// </summary> /// <param name="value">Floating-point size structure.</param> /// <returns>Integer size structure.</returns> public static Size Truncate(SizeF value) { int w, h; checked { w = (int)value.Width; h = (int)value.Height; } return new Size(w, h); }
/// <summary> /// Translates a PointF by the positive of a specified size. /// </summary> /// <param name="pt">Point.</param> /// <param name="sz">Offset.</param> /// <returns>PointF structure.</returns> public static PointF Add(PointF pt, SizeF sz) { return(new PointF(pt.X + sz.Width, pt.Y + sz.Height)); }