Search code examples
c#.netbitmapgdi+system.drawing

Filter Rectangles with 2:1 Aspect Ratio from an array of rectangles


I have a rectangle array defined like this

Rectangle[] recArray = new Rectangle[100];

I need a way to filter out rectangles that have 1:2 aspect ratio like the one below

enter image description here

Please advice me the best way to do this.


Solution

  • You can use a linq query like this:

    var result = recArray.Where(x => x.Height / x.Width == 2).ToList();
    

    The result is a List<Rectangle> which you can draw them or do whatever you need with them.

    If you need an Array instead of the List use .ToArray() method instead of ToList().

    Also don't forget to add using System.Linq;