Ellipse.cs 1.29 KB
Newer Older
BlackAngle233's avatar
BlackAngle233 committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Ellipse : MonoBehaviour
{
    Mesh mesh;
    public Vector3 center;
    public float a;
    public float b;
    // Start is called before the first frame update
    void Start()
    {
        mesh = new Mesh
        {
            name = "Ellipse"
        };
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    public void DrawEllipse()
    {
        mesh.Clear();

        Vector3[] vectors = new Vector3[200];
        int k = 0;
        float step = a / 50;
        for (float i = -a; i <= a; i += step)
        {
            float y = 1 - i * i / (a * a);
            y *= (b * b);
            y = Mathf.Sqrt(y);
            vectors[k] = new Vector3(i, y, 0);
            if (y != 0)
            {
                vectors[200 - k] = new Vector3(i, -y, 0);
            }
            k++;
        }
        mesh.vertices = vectors;

        int[] triangle = new int[400];
        for (int i = 0; i < vectors.Length - 1; ++i)
        {
            triangle[2 * i] = i;
            triangle[2 * i + 1] = i + 1;
        }
        triangle[398] = 199;
        triangle[399] = 0;

        mesh.SetIndices(triangle, MeshTopology.Lines, 0);

        GetComponent<MeshFilter>().mesh = mesh;
    }
}