在Unity3D中利用描点法画圆

直接法:

调用函数即可

//N为描点的个数,CirclePoint圆心距离,radius半径, mycolor为颜色
void MyDrawCircles(int N, Vector3 CirclePoint, float radius, Color mycolor)
    {
        float x = CirclePoint.x - radius;
        for (int i = 0; i < N; i++)
        {                      
            float x1 = x + i * (2f*radius / N);
            float x2 = x + (i + 1) * (2f*radius / N);
            Debug.DrawLine(new Vector3(x1, CirclePoint.y + Mathf.Sqrt(radius * radius - Mathf.Pow(x1 - CirclePoint.x, 2)), Pos_Z),
                new Vector3(x2, CirclePoint.y + Mathf.Sqrt(radius * radius - Mathf.Pow(x2 - CirclePoint.x, 2)), Pos_Z), mycolor);
            Debug.DrawLine(new Vector3(x1, CirclePoint.y - Mathf.Sqrt(radius * radius - Mathf.Pow(x1 - CirclePoint.x, 2)), Pos_Z),
               new Vector3(x2, CirclePoint.y - Mathf.Sqrt(radius * radius - Mathf.Pow(x2 - CirclePoint.x, 2)), Pos_Z), mycolor);
        }
    }

PS:for循环中,i++可以改成 i=i+5等等,这样便能画出一个虚线的圆

 

1.利用LineRender画圆,下面是以物体position为圆心,半径为R,在xz平面上的画圆

    public GameObject CirclePoint;//圆心
    public float R;//半径
    public int N;   //描点个数
    public float width;  //圆的粗细
    public Material CircleMaterial; //材质

    void Start () {          
        gameObject.AddComponent<LineRenderer>();
        GetComponent<LineRenderer>().positionCount = (N + 1);//这里要加1,达成闭合曲线
    }
	
	void Update () {
        GetComponent<LineRenderer>().startWidth = width;
        GetComponent<LineRenderer>().material = CircleMaterial;

        for (int i = 0; i < N + 1; i++)
        {
 float x = R * Mathf.Cos((360f / N * i) * Mathf.Deg2Rad) + transform.position.x; //确定x坐标
 float z = R * Mathf.Sin((360f / N * i) * Mathf.Deg2Rad) + transform.position.z; //确定z坐标
 GetComponent<LineRenderer>().SetPosition(i, new Vector3(x, transform.position.y, z));
        }
      
    }

 

 

使用C# 泛型List二维数组画圆(可简化)

二维数组的使用举例:

List <List <int >> array1 = new List <List <int >>();
        List <int> array2 = new List <int>();          

        array2.Add(2);
        array2.Add(3);
        array2.Add(6);
        array2.Add(6);
        array2.Add(6);
        List <int> array3 = new List <int>();
        array3.Add(1);
        array3.Add(4);
        array3.Add(5);
        array3.Add(12);
        array3.Add(32);
        array1.Add(数组2);
        array1.Add(ARRAY3);

        List <string> array4 = array1 [0];
        List <string> array5 = array1 [1];
 

相当于数组里套了一个数组(N维数组同理)。

泛型的基本用法:https://blog.csdn.net/weixin_42513339/article/details/83188987

 

画圆代码如下:

    int Pos_Z = 0;
    public int N; //半圆采样点个数

    List<List<float>> RoundPoints1 = new List<List<float>>();
    List<float> xPoints1 = new List<float>();
    List<float> yPoints1 = new List<float>();

    List<List<float>> RoundPoints2 = new List<List<float>>();
    List<float> xPoints2 = new List<float>();
    List<float> yPoints2 = new List<float>();

    // Use this for initialization
    void Start () {
        for (int i = 0; i < N; i++)
        {
            float x1 = -1.3f + i * (2f / N);
            float y1 = Mathf.Sqrt(1 - Mathf.Pow(x1 + 0.3f, 2));
            xPoints1.Add(x1);
            yPoints1.Add(y1);
        }
        for (int i = N; i < 2*N+1; i++)
        {
            float x1 = 0.7f - (i-N) * (2f / N);
            float y1 = -Mathf.Sqrt(1 - Mathf.Pow(x1 + 0.3f, 2));
            xPoints1.Add(x1);
            yPoints1.Add(y1);
        }

        for (int i = 0; i < N; i++)
        {
            float x2 = 0 + i * (2f / N);
            float y2 = Mathf.Sqrt(1 - Mathf.Pow(x2 - 1f, 2));
            xPoints2.Add(x2);
            yPoints2.Add(y2);
        }

        for (int i = N; i < 2*N+1; i++)
        {
            float x2 = 2 - (i-N) * (2f / N);
            float y2 = -Mathf.Sqrt(1 - Mathf.Pow(x2 - 1f, 2));
            xPoints2.Add(x2);
            yPoints2.Add(y2);
        }

        RoundPoints1.Add(xPoints1);
        RoundPoints1.Add(yPoints1);

        RoundPoints2.Add(xPoints2);
        RoundPoints2.Add(yPoints2);
    }
	
	// Update is called once per frame
	void Update () {

        for (int i = 0; i < xPoints1.Count-1; i++)
        {
            Debug.DrawLine(new Vector3(xPoints1[i], yPoints1[i], Pos_Z),
                new Vector3(xPoints1[i + 1], yPoints1[i + 1], Pos_Z), Color.red);
        }
        for (int i = 0; i < xPoints2.Count-1; i++)
        {
            Debug.DrawLine(new Vector3(xPoints2[i], yPoints2[i], Pos_Z),
                new Vector3(xPoints2[i + 1], yPoints2[i + 1], Pos_Z), Color.red);
        }
     }

上图是画两个圆的函数,如果画一个,那么删除一部分即可。

下图便是画出圆的形状,当然如果想要画的圆更加光滑,那么只需要增大采样点N的个数即可。

而且本人只是调试用的圆,所以直接用了Debug.DrawLine() 

如果想在场景运行的时候显示,那么可以是用LineRender();

PS:两种上述画线函数具体用法请点击这里,泛型函数点击这里

本人接下去可能会写一些关于两个圆融合的程序。(https://blog.csdn.net/weixin_42513339/article/details/83212575

  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值