/// <summary> /// Create new vector from two distance values /// </summary> /// <param name="x">X value</param> /// <param name="y">Y value</param> /// <param name="width">Width value</param> /// <param name="height">Height value</param> public Rectangle2f(DistanceF x, DistanceF y, DistanceF width, DistanceF height) { this.x = x.NativeValue; this.y = y.GetIn(x.NativeUnit); this.w = width.GetIn(x.NativeUnit); this.h = height.GetIn(x.NativeUnit); this.unit = x.NativeUnit; }
static void Main(string[] args) { // new distance in meters DistanceF a = new DistanceF(10, Unit.Meters); // new distance in centimeters DistanceF b = new DistanceF(5, Unit.Centimeters); // new distance in inches DistanceF c = new DistanceF(14, Unit.Inches); // new distance in feet DistanceF d = new DistanceF(1.5f, Unit.Feet); // sum of them (all converted automatically) DistanceF sum = a + b + c + d; Console.WriteLine("Sum of " + a + ", " + b + ", " + c + ", " + d + " equals " + sum); // get distance converted to millimeters DistanceF mm = sum.Converted(Unit.Millimeters); // get distance as float float distanceAsFloat = mm.Decimeters; Console.WriteLine(mm + " in decimeters is equal to " + distanceAsFloat); // get distance as pixels (you need to specify dpi (dots per inch)) int pixels = (int)b.Pixels(92); Console.WriteLine(b + " is " + pixels + " pixels on 92 dpi monitor"); // create distance from pixels DistanceF distanceFromPixels = DistanceF.FromPixels(1024, 92); Console.WriteLine("Screen width is " + distanceFromPixels + " on 1024px wide 92dpi display"); // compare distances if (a > b) { Console.WriteLine(a + " is more than " + b); } // compare distances if (b <= c) { Console.WriteLine(b + " is less or equal to " + c); } // create vectors from distances Vector2f v1 = new Vector2f(a, b); Vector2f v2 = new Vector2f(b, c); Vector2f v3 = new Vector2f(c, d); // sum of vectors Vector2f vSum = v1 + v2; Console.WriteLine("(Vector " + v1 + ") + (Vector " + v2 + ") equals (Vector " + vSum + ")"); // subtract vectors Vector2f vSub = v3 - v2; Console.WriteLine("(Vector " + v3 + ") - (Vector " + v2 + ") equals Vector (" + vSub + ")"); // calculate dot product of two vectors float vDot = v2 * v3; Console.WriteLine("(Vector " + v2 + ") dot (Vector " + v3 + ") equals " + vDot); // find vector which is perpendicular to v1 Vector2f vPerp = v1.Perpendicular; Console.WriteLine("(Vector " + v1 + ") is perpendicular to (Vector " + vPerp + ")"); // Create rectangle in eal units Rectangle2f rect = new Rectangle2f(10, 15, 90, 80, Unit.Millimeters); // Crop rectangle top and right margins by 1 cm Rectangle2f rectCropped = rect.Cropped(DistanceF.Zero, new DistanceF(1, Unit.Centimeters), new DistanceF(1, Unit.Centimeters), DistanceF.Zero); Console.WriteLine("(Rectangle " + rect + ") with top and right cm cropped is equal to (Rectangle " + rectCropped + ")"); Console.ReadKey(); }
/// <summary> /// Return cropped rectangle by specified amounts from all sides /// </summary> /// <param name="left">How much to crop from left</param> /// <param name="right">How much to crop from right</param> /// <param name="top">How much to crop from top</param> /// <param name="bottom">How much to crop from bottom</param> /// <returns>New cropped rectangle</returns> public Rectangle2f Cropped(DistanceF left, DistanceF right, DistanceF top, DistanceF bottom) { float l = left.GetIn(unit); float t = top.GetIn(unit); return new Rectangle2f(x + l, y + t, w - l - right.GetIn(unit), h - t - bottom.GetIn(unit), unit); }
/// <summary> /// Create new vector from two distance values /// </summary> /// <param name="x">X value</param> /// <param name="y">Y value</param> public Vector2f(DistanceF x, DistanceF y) { this.x = x.NativeValue; this.y = y.GetIn(x.NativeUnit); this.unit = x.NativeUnit; }
/// <summary> /// Returns scaled vector which maches specified length /// </summary> /// <param name="newLength">New vector length</param> /// <returns>New vector of specified length</returns> public Vector2f Scaled(DistanceF newLength) { float scale = newLength.GetIn(unit) / NativeLength; return this * scale; }
/// <summary> /// Scales vector to match specified length /// </summary> /// <param name="newLength">New vector length</param> public void Scale(DistanceF newLength) { float scale = newLength.GetIn(unit) / NativeLength; x *= scale; y *= scale; }