# Align polygon in arbitrary plane to xy world plane

Hi, I have a list of points in the 3D space that represents the vertices of a polygon (points are ordered). The points are coplanar, but they are in an arbitrary plane. I need to align these points so that they are on plane xy of the world. I want to remove the 3rd dimension, but I cant just project them removing the z axis because I want to keep the angles and distances between the points).

I thought I would be able to do it in a simple way, but I’m not having the result I intended. What I did:

I transladed the points of my polygon to the origin subtracting the first point (vertices[0]) from all other points, then used other two points of my list of points to estimate the plane normal using:

`Vector3 A = vertices[1]; Vector3 B = vertices[2]; Vector3 planeNormal = Vector3.Cross(A, B) / Vector3.Magnitude(Vector3.Cross(A, B))`

then, with this normal, I intended to align it with the world z-axis.

I created some spheres inside a gameObject (objectSpheres) with my vertices, and I set the local axis of the object in the same direction of the plane normal (example image 1):

`objSpheres.transform.rotation = Quaternion.LookRotation(planeNormal);`

and by pausing the Unity game scene, I can select objSpheres and go to the Inspector and set the local angles in Transform Rotation x,y and z to zero, and then my spheres are aligned. That’s the result I want, I can see my spheres aligned with the plane (example image 2).

But when I try to set these values by code with

`objSpheres.transform.localEulerAngles = new Vector3(0, 0, 0);`

it doesnt work at all, looks like I only transform the objSpheres axis, but the other spheres do not move (example image 3).
What am I doing wrong?

I just figured out. I will publish it here to help others that may have a similar problem.

Here is the function to align a list of vertices (considering they are colinear in an arbitrary plane) into the XY plane. You may want to set the reduced dimension to zero to avoid the floating point approximation to zero.

``````    List<Vector3> alignVerticesOnPlane(List<Vector3> vertices)
{
List<Vector3> verticesOnPlane = new List<Vector3>();

//1. Subtract vertices[0] from all points
for (int i = 0; i < vertices.Count; i++)
``````

//2. Find the normalized plane normal
Vector3 A = verticesOnPlane[1];
Vector3 B = verticesOnPlane[2];
Vector3 n = (Vector3.Cross(A, B) / Vector3.Magnitude(Vector3.Cross(A, B)));

//3. Create a quaternion with the normal of the plane and the direction you want it to face
Quaternion rotation = Quaternion.FromToRotation(n, Vector3.forward); //change Vector3.forward to align with other planes

//4. Apply the quaternion rotation to all points
for (int i = 0; i < verticesOnPlane.Count; i++)
verticesOnPlane = rotation * verticesOnPlane*;*

return verticesOnPlane; // =)
}