Find corners in array of points

Hi!

I have an array of points, describing a rectangular shaped polygon. And I am looking for a function to extract the four corners of that polygon.

The difficulty I am facing is that the top-left corner is not necessarily the point where x & y are the closest to 0.

Y can be higher on the middle if the polygon is bent. And X isn't n necessarily the same on both right corners if the polygon is a parallelogram.

What i need is an algorithm that detects the two bends furthest out on both sides of the polygon, and I have no idea how to do this.

Has anyone done something similar?

//John Erlandsson