通过两点构建圆弧

由于在计算中缺少了一个点,来构建唯一的三角形,用以确定内切圆,故默认过这两个点的内切圆点切线相互垂直

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Test : MaskableGraphic
{
    [SerializeField]
    private Vector3 start, end;

    [SerializeField]
    private float size,angle;
    
    [SerializeField]
    private int count;

    [SerializeField]
    private Color32 color1,color2,color3;

    [SerializeField]
    private Vector3 n1,n2,target;

    protected override void OnPopulateMesh(VertexHelper vh)
    {
        vh.Clear();

        //  计算两点连线的中点
        Vector3 line = end - start;

        Vector3 midPoint = start + line.normalized * line.magnitude / 2;

        //  计算两点连线的法线垂直于外
        Vector3 midPointNormal = Vector3.Cross(Vector3.forward, line).normalized;

        //  构建等腰直角三角形,确定内接圆切线方向 上方定点
        Vector3 topPoint = midPoint + midPointNormal * line.magnitude / 2;

        //  圆心
        Vector3 center = midPoint - midPointNormal * line.magnitude / 2;

        Vector3 lastStart = start ;
        //  角度
        float degree = 90.0f / count;
        for(int i = 1; i <= count;++i)
        {
            Vector3 end = Quaternion.AngleAxis(-degree * i, Vector3.forward) * (start - center) + center;
            DrawLine(vh, lastStart, end, 2, color1);
            lastStart = end;
        }
        //  绘制两条线
        DrawLine(vh, start, topPoint, 1, color2);
        DrawLine(vh, topPoint, end, 1, color2);
        //  两条半径
        DrawLine(vh, start, center, 1, color3);
        DrawLine(vh, center, end, 1, color3);
        //ChartDrawer.DrawLine(vh, mid + (n1 + n2).normalized * 20, end, 2, color2);
    }


    private void DrawLine(VertexHelper vh, Vector3 p1, Vector3 p2, float size, Color32 color)
    {
        UIVertex[] vertex = new UIVertex[4];
        if (p1 == p2) return;
        Vector3 v = Vector3.Cross(p2 - p1, Vector3.forward).normalized * size;
        vertex[0].position = p1 - v;
        vertex[1].position = p2 - v;
        vertex[2].position = p2 + v;
        vertex[3].position = p1 + v;

        for (int j = 0; j < 4; j++)
        {
            vertex[j].color = color;
            vertex[j].uv0 = Vector2.zero;
        }
        vh.AddUIVertexQuad(vertex);
    }
}

你可能感兴趣的:(通过两点构建圆弧)