powerfun-unity/Assets/AR/GeometryUtil.cs
2023-04-18 17:10:48 +08:00

35 lines
1.4 KiB
C#

using System;
using UnityEngine;
namespace Assets.AR
{
public static class GeometryUtil
{
public static bool CircleCenter(Vector3 aP0, Vector3 aP1, Vector3 aP2, out Vector3 center)
{
var lhs = aP1 - aP0;
var vector3 = aP2 - aP0;
var rhs = Vector3.Cross(lhs, vector3);
if ((double)rhs.sqrMagnitude < 9.9999998245167E-15)
{
center = Vector3.zero;
return false;
}
rhs.Normalize();
if ((double)rhs.sqrMagnitude < 9.9999998245167E-15)
{
center = Vector3.zero;
return false;
}
var normalized1 = Vector3.Cross(lhs, rhs).normalized;
var normalized2 = Vector3.Cross(vector3, rhs).normalized;
var from = (lhs - vector3) * 0.5f;
var num1 = Vector3.Angle(normalized1, normalized2);
var num2 = Vector3.Angle(from, normalized1);
var num3 = from.magnitude * Mathf.Sin(num2 * ((float)Math.PI / 180f)) / Mathf.Sin(num1 * ((float)Math.PI / 180f));
center = (double)Vector3.Dot(lhs, aP2 - aP1) <= 0.0 ? aP0 + vector3 * 0.5f + normalized2 * num3 : aP0 + vector3 * 0.5f - normalized2 * num3;
return !float.IsInfinity(center.x) && !float.IsNaN(center.x) && !float.IsInfinity(center.y) && !float.IsNaN(center.y) && !float.IsInfinity(center.z) && !float.IsNaN(center.z);
}
}
}