How to find the corners of a 2D array such that it can be reliably and correctly rotated

23 views Asked by At

I have an array called Existing_Array from a Geotiff LiDAR file, see screen shot below.

Existing Unrotated 2D Array

I am attempting to find a reliable way to rotate the image such that the side with the largest values (pink/red) is East facing and flush top to bottom.

my thought process is to use indexing to grab the Top Right and Bottom Right corners of the array using the below code snip.

for i, x in enumerate(Existing_Array[-1]):
    if x > 1:
        Bottom_Right = x
        
for i, x in reversed([*enumerate(Existing_Array[-1])]):
    if x > 1:
        Top_Right = x

Then calculate the angle between them and use that to apply a rotation to the whole array to get the below rotated array.

enter image description here

My issue is that using indexing doesn't always work because the 2D array could be drawn in a way that for example: The Top Left of the array could be at a lower or higher row/column index than the Top Right point that I am looking for, meaning the generic indexing code could grab that point instead and throw off the angle calculation to rotate the array to the below screenshot.

Is there a reliable way to calculate the angle between two points so I can orient the array as depicted in the screenshot if I do not know the shape of the array, only that the largest values will be to the right and the smallest values will be on the left?

0

There are 0 answers