# Getting the topleft, topright, bottomleft and bottomright points of a shape in Unity3D?

I have a 4-point shape that is defined in screen space by the user. They can draw the 4 points, one at a time, in any order they wish.

I am using a List of Vector2's to store the screen co-ordinates of each point.

Now what I'm trying to do is figure out how to get which of each of those 4 points are the topleft, topright, bottomleft and bottomright points of this shape, but I'm not having any luck.

I've tried using topleft = MinX, MinY etc, but obviously this doesn't work as the topleft point doesn't necessarily contain both the MinX and MinY value.

Can anyone point me in the right direction.

Thanks

supposing you already do some validation like Lars Viklund said, i suggest this aproach.

Let p0 , p1 , p2 , p3 be your points ordered by Y from top to bottom (or by X from L to R) :

//(very) pseudocode
If p0.X < p1.X
p0 is topleft
p1 is topright
else
p0 is topright
p1 is topleft
end if
If p2.X < p3.X
p2 is bottomleft
p3 is bottomright
else
p2 is bottomright
p3 is bottomleft
end if


I would say that it's important that you determine the parameters within you're willing to accept shapes, and figure out how you can indicate this to the user.

There are many corner cases:

• shapes like a <> diamond,
• shapes where the lines cross (if lines are predetermined);
• shapes where points coincide.

A validation would probably be something like this:

• reject shape if line segments overlap;
• reject shape if points are too close or lines are too short;
• reject shape if angles seem too strange.

Once you've got a reasonably validated shape, you can attempt to classify points.

A sketch for a classification would be to compute an axis-aligned bounding box for your points and find the point that most closely agrees with a corner. Given a candidate point, you can iterate around the shape and verify the next point against the next corner.