Use LINQ to find a bounding box for a list of PointF in C#

Use LINQ to find a bounding box

This example shows how to use LINQ to find a bounding box for a list of PointF. When you click on the program’s PictureBox, it adds points to the list and shows the bounding box.

The following method finds the bounding box.

// Find the list's bounding box.
private Rectangle BoundingBox(IEnumerable<Point> points)
{
    var x_query = from Point p in points select p.X;
    int xmin = x_query.Min();
    int xmax = x_query.Max();

    var y_query = from Point p in points select p.Y;
    int ymin = y_query.Min();
    int ymax = y_query.Max();

    return new Rectangle(xmin, ymin, xmax - xmin, ymax - ymin);
}

Notice that the method takes as a parameter an IEnumerable<Point>, so it can take any enumerable object as a parameter. In this example, the program uses a List<Point>, but the method could also process an array of Point or some other collection class holding Point.

The method first makes a LINQ query to select the points’ X coordinates. It then uses the query’s Min and Max methods to get the minimum and maximum X values.

It repeats those steps to get the minimum and maximum Y values. It finishes by using the minimum and maximum values to build a Rectangle, which it returns.

Note that LINQ defers execution until the query is actually needed. In this case, that means the query is not actually executed until the program calls Min to find xmin.

The following code shows how the program uses the BoundingBox method.

// Draw the points and the bounding box.
private void picCanvas_Paint(object sender, PaintEventArgs e)
{
    e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
    // Draw the points.
    foreach (Point point in Points)
        e.Graphics.DrawEllipse(Pens.Red,
            point.X - 2, point.Y - 2, 5, 5);

    // Draw the bounding box.
    if (Points.Count > 1)
        e.Graphics.DrawRectangle(Pens.Green,
            BoundingBox(Points));
}

This code loops through the points and draws them. Then if the Points list holds more than 1 Point, the code calls the BoundingBox method and draws the Rectangle it returns.


Download Example   Follow me on Twitter   RSS feed   Donate




This entry was posted in graphics, LINQ and tagged , , , , , , , , , , , . Bookmark the permalink.

2 Responses to Use LINQ to find a bounding box for a list of PointF in C#

  1. R. Hoek says:

    This method is elegant, but not very efficient. The linq expressions now loop the point collection 4 times to get the min/max values of resp. x and y.
    It would be nice to see an example (not sure you can use linq for this) which loops only once thru the point collection en gets the min/max values of x and y in this single loop.
    But this is a good example for using linq expressions.

    • RodStephens says:

      The whole thing can be summed up with the words, “Not very efficient” and “LINQ.” LINQ is never efficient; it just makes your code simpler (sometimes). If you have a large list of points, you’ll be better off using your own code to loop through them.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.