2
\$\begingroup\$

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.

enter image description here

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

\$\endgroup\$
2
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

A completely unambigious answer to your question cannot be found.

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.