How to get biggest possible rectangle from Array of Vector2i

Godot Version

4.4.1

Question

I am looking for built-in methods to handle this otherwise i need to write everything from scratch. Assume a grid, and bunch of points such as ((1,2), (2,0), (6,4),(5,5)). I want to create biggest possible rectangle by looking max,min of x and y of every point.

So possible biggest rectangle of these 4 points is 36 cells.
I tried Rect2i.expand() but rect2i calculates every area from 0.0, i can not give starting point such as (1,0) here.
Tried array.min() and array.max() but it returns (6,4) not (5,5), so i am missing an extra column(6 cells).

-----------------------------------------------------------------------------------------
Another example


Biggest rectangle area of above points should be 15 cells.

--------------------------------------------------------------------------------------
If points are in straight line such as array of points of (1,2),(1,3),(1,4)
then it should return 3

With what methods should i achieve what i want? I have Array[Vector2i]

So on the Array page it says this

To find the maximum value using a custom comparator, you can use reduce().

Since it’s a custom function you can also use it for min as well.

if you used .expand() like this

var r = Rect2i.new() # r = (p: (0,0), s: (0,0) )
r = r.expand()

it counted the area from (0,0), because Rect was initially located at (0,0)

Try to create a Rect2i at the position of one of the points from the array

func expand_to_array(array : Array[Vector2i]) -> Rect2i:
	var r = Rect2i(array[0], Vector2.ZERO)
	for point in array:
		r = r.expand(point)
	return r
1 Like