Search code examples
arrays3dpoint

How to order 3d points in clockwise order?


I have bunch of 3d points (an array) not ordered in some particular order and not restricted to some axis/plane. Based on the coordinates of these points I want to order the array in clockwise order, like in the image. At moment I am clueless where to start. One idea is to find for each the closest point and somehow figure out the direction.

enter image description here


Solution

  • 3Dave has already said this, but it completely depends on where the camera is.

    There is no answer unless you specify the frustrum.

    Note that circles are 2D, not 3D objects. "Clockwise" relates to circles.


    Assuming that you mean on a plane:

    This is a problem with two parts.

    The first part is incredibly difficult.

    The second part is relatively easy.

    First part: indeed, you are doing object recognition: you have to find a circle.

    For this, investigate the existing technology for shape recognition, or read up on stuff like https://link.springer.com/article/10.1007/s11042-018-6167-2

    For the second part: (which is almost irrelevant after the first part). Just get the coords of each point relative to the center of the circle you found, simply calculate the angle of each from the top, and sort them.


    Cheap game-type solution

    If you want the cheap solution, which you can use if the points are "reasonable" ..

    1. find the centroid of all the points (it's just the average of all)

    2. write each point as a vector from the centroid to the point

    3. pick any one point as being the "top"

    4. use something like this https://docs.unity3d.com/ScriptReference/Vector3.Angle.html to get the angle of each from the "top" one

    5. voila! just put them in order

    In practice you'll likely need these things also:

    1. find the "plane" the points are on (find the "average plane" they are on, it's relatively easy to do this, look it up!)

    2. make an axis through the centroid which is perpendicular to the plane