Can I use the local x,y transforms of a plane?

Hi Unity,

I’m in the process of writing a script that calculates 3D circles from 3 points. As these points are coplanar I’m trying to move these to a new x,y coordinate system on a Plane where I’ll be able to calculate the circles centre and radius.

Currently, this looks a bit like this:
`
private void CalcCircle()

    {

        plane.Set3Points(points[0], points[1], points[2]);

        for (int i = 0; i < points.Length; i++)

        {

            localPoints <em>= /* calculate points _as local point on plane */;_</em>

}
FindCircle(localPoints[0], localPoints[1], localPoints[2], out centre, out radius);
/* convert centre back to world coords */
}
`
If anyone is interested, I can post the code for finding the circle :slight_smile:
Thanks in advance!

You shouldn’t bother with converting the points into 2d coordinates. Just using some vector math you can directly calculate the circle center point. In the process you also get the normal of the plane the circle is located in. I quickly created this method:

public static Vector3 CircleCenter(Vector3 aP0, Vector3 aP1, Vector3 aP2, out Vector3 normal)
{
    // two circle chords
    var v1 = aP1 - aP0;
    var v2 = aP2 - aP0;

    normal = Vector3.Cross(v1,v2);
    if (normal.sqrMagnitude < 0.00001f)
        return Vector3.one * float.NaN;
    normal.Normalize();

    // perpendicular of both chords
    var p1 = Vector3.Cross(v1, normal).normalized;
    var p2 = Vector3.Cross(v2, normal).normalized;
    // distance between the chord midpoints
    var r = (v1 - v2) * 0.5f;
    // center angle between the two perpendiculars
    var c = Vector3.Angle(p1, p2);
    // angle between first perpendicular and chord midpoint vector
    var a = Vector3.Angle(r, p1);
    // law of sine to calculate length of p2
    var d = r.magnitude * Mathf.Sin(a * Mathf.Deg2Rad) / Mathf.Sin(c*Mathf.Deg2Rad);
    if (Vector3.Dot(v1, aP2-aP1)>0)
        return aP0 + v2 * 0.5f - p2 * d;
    return aP0 + v2 * 0.5f + p2 * d;
}

It returns the 3d center position of the circle as well as the plane normal the circle is located in. I simply used it like this inside Update: p0, p1, p2 and C are Transform references to 4 sphere objects.

    // inside update
    Vector3 norm;
    var center = CircleCenter(p0.position, p1.position, p2.position, out norm);
    if (!float.IsNaN(center.x))
    {
        C.position = center;
        var r = p0.position - center;
        int count = 100;
        var q = Quaternion.AngleAxis(360f/count, norm);
        for(int i = 0; i < count; i++)
        {
            var p = r;
            r = q * r;
            Debug.DrawLine(center+p,center+r, Color.yellow);
        }
    }

The result looks like this:

Three points on a plane can only determine a circle, and that is with the requirement that those 3 points do not lie on the same line. You cannot get a unique sphere from 3 coplanar points; that requires 4 points.


edit Derivations:

Thankyou for your answer @TreyH , I am aware of the above; what I’m doing is creating a circle in 3D space, so one that can be at any orientation (think a frisbee on a tilt).

In order to do that I’m using a calculation based on a 2D circle, then rotating it; to do this I’m trying to create a new coordinate system based on the plane created by three points, I hope that clarifies my question.