1

Is there such an algorithm to sort an array of 3D points in clockwise order? I'm specifically dealing with right triangle in my case so only 3 points. (for build mesh)

Knaus Irina
  • 789
  • 5
  • 15
  • 35
  • In order to sort your array, you would define a default vector, then you get the angle between that vector and (position - vector.origin). This will get a float between 0 and 180. Then you had 180 if the position is to the left of the default vector. http://forum.unity3d.com/threads/unity2d-collision-get-left-right-side-of-collision.282374/ Finally you order by result of angle. – Everts Jan 21 '16 at 07:59
  • If any of the answers worked for you, it would be helpful for others if you accept that answer. If not, feel free to comment on the answers given, or update your question. – Thomas Hilbert Jan 27 '16 at 16:42

2 Answers2

1

Assume you have two edges connecting your three vertices.

E1 = V2 - V1
E2 = V1 - V3

They span a triangle. You can calculate the triangle's normal N like this:

N = cross(E1, E2)

This tells you which direction the triangle is facing. You can calculate whether the triangle is facing towards, or away from, a certain point of view P by projecting N onto the distance of your triangle from P.

D = V1 - P
d = dot(N, D)

If d is positive, the triangle looks away from P, if it is negative, it faces P.

You are now able to judge for every set of (V1,V2,V3) whether they are sorted correctly or not. If not, just swap V2 and V3 and they will be.

There is one pitfall though. If you are trying to build the hull of a closed mesh, the requirement is that all triangles are facing towards the outside. This can not be modelled by trying to make all triangles face a certain point, because that point would have to be different for each triangle. If the mesh is convex through, you can model it by requiring that all triangles face away from a certain point, which lies inside the convex mesh.

Thomas Hilbert
  • 3,559
  • 2
  • 13
  • 33
0

The algorithm for sorting is not difficult. The problem is, what plane that those points lie on. And which side it facing

Only a bunch of points cannot clockwise or counter clockwise by itself. You need a plane and a side to reference those point

edit : Actually what I used to said is a bit inaccurate. What you really need is a position and direction to reference, not a plane

Thaina Yu
  • 1,372
  • 2
  • 16
  • 27