Get bounding rectangle from multiple rectangles

I have up to 4 rectangles in the image, for each of these rectangles I know their top left X, Y coordinate and their width, height. I want to create an Int32Rect with dimensions from the top-left rectangle to the bottom-most right rectangle. The main problem is that you can create System.Windows.Int32Rect with x, y, width, height parameters. Any ideas how I can accomplish this with my known values ​​today?

EDIT: Trying to clarify, I want to create an Int32Rect that is the dimension of all the other "rectangles" in my image. Thus, one large Int32Rect that starts with a "rectangle" in the upper left of the image and expands to a "rectangle" that is in the lower right of the image.

Here's the code that does it for one rectangle:

var topLeftOfBox = new Point(centerOfBoxRelativeToImage.X - Box.Width/2,
                centerOfBoxRelativeToImage.Y - Box.Height/2);
return new CroppedBitmap(originalBitmap, new Int32Rect(topLeftOfBox.X, topLeftOfBox.Y, Box.Width, Box.Height));

      

Thanks for the help and ideas everyone, I found Aybe in charge to work better for me.

+3


source to share


3 answers


You need to grab x / y mins / maxs for each rectangle and construct a rectangle from those values:

using System.Linq;
using System.Windows;

internal class Class1
{
    public Class1()
    {
        var rect1 = new Int32Rect(10, 10, 10, 10);
        var rect2 = new Int32Rect(30, 30, 10, 10);
        var rect3 = new Int32Rect(50, 50, 10, 10);
        var rect4 = new Int32Rect(70, 70, 10, 10);

        var int32Rects = new[] { rect1, rect2, rect3, rect4 };
        var int32Rect = GetValue(int32Rects);
    }

    private static Int32Rect GetValue(Int32Rect[] int32Rects)
    {
        int xMin = int32Rects.Min(s => s.X);
        int yMin = int32Rects.Min(s => s.Y);
        int xMax = int32Rects.Max(s => s.X + s.Width);
        int yMax = int32Rects.Max(s => s.Y + s.Height);
        var int32Rect = new Int32Rect(xMin, yMin, xMax - xMin, yMax - yMin);
        return int32Rect;
    }
}

      



Result:

{10,10,70,70}

      

+2


source


The question is somewhat unclear, so I'll start by stating what you are looking for. When I read your question, you have a collection of rectangles and are looking for the smallest rectangle containing all the rectangles in your collection.

Here's what you need to know to do this:



  • The top left rectangle is equal (X, Y)

    and the bottom right is (X+Width, Y+Height)

    .
  • The width of the rectangle is determined Right - Left

    . The height is given by the symbol Bottom - Top

    .

Now that you can convert between these values, the rest is easy. Find the minimum top value, minimum left value, maximum right value, and maximum bottom value. Load these values ​​into the equations in the second mark above and you're done.

0


source


You can save yourself and not worry about the "world knowledge" of which the Rectangle has the greatest value /top.most left-most / right-most:

public static class RectangleExtensions
{
    public static Rectangle RectsGetBounds(this Rectangle rect, params Rectangle[] rects)
    {
        Rectangle rbase = rect;

        for (int i = 0; i < rects.Length; i++)
        {
            rbase = Rectangle.Union(rbase, rects[i]);
        }

        return rbase;
    }

    public static Rectangle ControlsGetBounds(this Control cntrl, params Control[] cntrls)
    {
        return RectsGetBounds(cntrl.Bounds, cntrls.Select(c => c.Bounds).ToArray());
    }
}

      

0


source







All Articles