unity中计算三角形的外接圆

//三角形类
public class Triangle
    {
    	//点1
        public Vector2 a;
        //点2
        public Vector2 b;
       	//点3
        public Vector2 c;
        //外接圆圆心
        public Vector2 center;
        //半径
        public float radius;
    }
public static void GetTrangleOutCircle(Triangle triangle)
        {
            double a = triangle.a.x - triangle.b.x;
            double b = triangle.a.y - triangle.b.y;
            double c = triangle.a.x - triangle.c.x;
            double d = triangle.a.y - triangle.c.y;
            double aa = Mathf.Pow(triangle.a.x, 2) - Mathf.Pow(triangle.b.x, 2);
            double bb = Mathf.Pow(triangle.b.y, 2) - Mathf.Pow(triangle.a.y, 2);
            double cc = Mathf.Pow(triangle.a.x, 2) - Mathf.Pow(triangle.c.x, 2);
            double dd = Mathf.Pow(triangle.c.y, 2) - Mathf.Pow(triangle.a.y, 2);
            double E = (aa - bb) / 2;
            double F = (cc - dd) / 2;
            double resultY = (a * F - c * E) / (a * d - b * c);
            double resultX = (F * b - E * d) / (b * c - a * d);
            double resultR = Mathf.Sqrt(Mathf.Pow(triangle.a.x - (float)resultX, 2)+ (Mathf.Pow((triangle.a.y - (float)resultY), 2)));

            triangle.center = new Vector2((float)resultX, (float)resultY);
            triangle.radius = (float)resultR;
        }
//使用
Triangle triangle = new Triangle();
        triangle.a = new Vector2(2, 4);
        triangle.b = new Vector2(2, 0);
        triangle.c = new Vector2(0, 2);

        RoomLinkMgr.GetTrangleOutCircle(triangle);
        Debug.Log(triangle.center + " " + triangle.radius);

输出:
在这里插入图片描述
输入:

Triangle triangle = new Triangle();
        triangle.a = new Vector2(1, 4);
        triangle.b = new Vector2(-1, 0);
        triangle.c = new Vector2(1,-1);

        RoomLinkMgr.GetTrangleOutCircle(triangle);
        Debug.Log(triangle.center + " " + triangle.radius);

输出:
在这里插入图片描述

参考资料:
https://blog.csdn.net/weixin_42117950/article/details/86063083
https://blog.csdn.net/liyuanbhu/article/details/52891868#commentBox

你可以通过在Unity使用Mesh来绘制圆角三角形。下面是一个简单的示例代码,演示如何创建一个圆角三角形的Mesh: ```csharp using UnityEngine; public class RoundedTriangle : MonoBehaviour { public float radius = 1f; public int segments = 30; void Start() { MeshFilter meshFilter = gameObject.AddComponent<MeshFilter>(); MeshRenderer meshRenderer = gameObject.AddComponent<MeshRenderer>(); Mesh mesh = new Mesh(); meshFilter.mesh = mesh; Vector3[] vertices = new Vector3[segments + 2]; int[] triangles = new int[segments * 3]; vertices[0] = Vector3.zero; // 心点 float angleIncrement = 360f / segments; float currentAngle = 0f; for (int i = 1; i < segments + 2; i++) { float x = Mathf.Cos(Mathf.Deg2Rad * currentAngle) * radius; float y = Mathf.Sin(Mathf.Deg2Rad * currentAngle) * radius; vertices[i] = new Vector3(x, y, 0f); currentAngle += angleIncrement; } for (int i = 0; i < segments; i++) { triangles[i * 3] = 0; triangles[i * 3 + 1] = i + 1; triangles[i * 3 + 2] = i + 2; } mesh.vertices = vertices; mesh.triangles = triangles; mesh.RecalculateNormals(); // 可以在这里设置材质等其他属性 } } ``` 将脚本挂载到一个空的GameObject上,然后在Inspector面板调整radius和segments属性来控制圆角三角形的大小和分段数。运行游戏后,你将在场景看到绘制的圆角三角形。 注意:此示例仅绘制了一个平面的圆角三角形,如果你需要在3D空间绘制一个具有深度的圆角三角形,你可能需要使用更复杂的算法来生成相应的顶点和三角形
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值