Trying to make life worth

Showing posts with label 3d Graphics. Show all posts
Showing posts with label 3d Graphics. Show all posts

Sunday, December 11, 2016

How to sort 3D vertices of a polygon in order?

we are given multiple random coplanar 3D points. Compute normal by any 3 Point. we need to rotate all these point in X-Y plane. So compute rotation matrix normal to (0,0,1). rotate all these in X-Y plane.


      double[] normal = PlaneUtils.getNormal(tverts);
      Vector3 srcNormal = new Vector3(normal[0], normal[1], normal[2]).normalize();
      Vector3 destNormal = new Vector3(0, 0, 1);
      // Rotation Matrix of Arbitrary Polygon From Current Plane to XY Plane
      Matrix4 rotationMatrix = Matrix4.rotate(destNormal, srcNormal);

      for (int i = 0; i < PointArrVal.length; i++) {
        PointArrVal[i].timesEquals(rotationMatrix);
      }

Now Compute Mean of All X-coordinate and mean of All Y-coordinate.
Mean of X= Sum of All X-coordinate/number of Point and in same way compute mean of Y.

Compute Atan2 angle between Mean Point and given coordinate.

Math.atan2(clipCornerPoint[i].y - cY, clipCornerPoint[i].x - cX), clipCornerPoint[i]
Store computed angle with vertices in Ordered Multi Map like

TreeMap <Double, List<Point3>> multimap = new TreeMap<Double, List<Point3>>();
      for (int i = 0; i < clipCornerPoint.length; i++) {
        putObjects(multimap, Math.atan2(clipCornerPoint[i].y - cY, clipCornerPoint[i].x - cX), clipCornerPoint[i]);
      }

Sort map angle as key and it will give you vertices in order.
now reverse your point in same previous Arbitrary Axis by inverse Rotation Matrix.