Hyperbola.cs 4.73 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Hyperbola : 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 = "Hyperbola"
        };
    }

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

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

        Vector3[] vectors = new Vector3[202];
        int k = 0;
        float step = a / 100;
        float x = -a;
        while (k < 51)
        {
            float y = x * x / (a * a) - 1;
            y *= (b * b);
            y = Mathf.Sqrt(y);
            vectors[50 + k] = new Vector3(x, y, 0);
            if (y != 0)
            {
                vectors[50 - k] = new Vector3(x, -y, 0);
            }
            x -= step;
            step *= 1.1f;
            k++;
        }

        x = a;
        step = a / 100;
        k = 0;
        while (k < 51)
        {
            float y = x * x / (a * a) - 1;
            y *= (b * b);
            y = Mathf.Sqrt(y);
            vectors[151 + k] = new Vector3(x, y, 0);
            if (y != 0)
            {
                vectors[151 - k] = new Vector3(x, -y, 0);
            }
            x += step;
            step *= 1.1f;
            k++;
        }
        mesh.vertices = vectors;

        int[] triangle = new int[400];
        for (int i = 0; i < 100; ++i)
        {
            triangle[2 * i] = i;
            triangle[2 * i + 1] = i + 1;
        }
        for (int i = 100; i < 200; ++i)
        {
            triangle[2 * i] = i + 1;
            triangle[2 * i + 1] = i + 2;
        }

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

        GetComponent<MeshFilter>().mesh = mesh;
    }
BlackAngle233's avatar
BlackAngle233 committed
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187

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

        Vector3[] vectors = new Vector3[404];
        int k = 0;
        float step = a / 100;
        float x = -a;
        while (k < 101)
        {
            float y = x * x / (a * a) - 1;
            y *= (b * b);
            y = Mathf.Sqrt(y);
            vectors[100 + k] = new Vector3(x, y, -5);
            if (y != 0)
            {
                if(k % 2 == 0)
                    vectors[100 - k] = new Vector3(x, -y, -5);
                else
                    vectors[102 - k] = new Vector3(x, -y, -5);
            }
            k++;
            vectors[100 + k] = new Vector3(x, y, 5);
            if (y != 0)
            {
                if (k % 2 == 0)
                    vectors[100 - k] = new Vector3(x, -y, 5);
                else
                    vectors[102 - k] = new Vector3(x, -y, 5);
            }
            x -= step;
            step *= 1.1f;
            k++;
        }

        x = a;
        step = a / 100;
        k = 0;
        while (k < 101)
        {
            float y = x * x / (a * a) - 1;
            y *= (b * b);
            y = Mathf.Sqrt(y);
            vectors[301 + k] = new Vector3(x, y, -5);
            if (y != 0)
            {
                if (k % 2 == 0)
                    vectors[301 - k] = new Vector3(x, -y, -5);
                else
                    vectors[303 - k] = new Vector3(x, -y, -5);
            }
            k++;
            vectors[301 + k] = new Vector3(x, y, 5);
            if (y != 0)
            {
                if (k % 2 == 0)
                    vectors[301 - k] = new Vector3(x, -y, 5);
                else
                    vectors[303 - k] = new Vector3(x, -y, 5);
            }
            x += step;
            step *= 1.1f;
            k++;
        }
        mesh.vertices = vectors;

        Vector2[] uvs = new Vector2[vectors.Length];
        for (int i = 0; i < uvs.Length; i++)
        {
            uvs[i] = new Vector2(0, vectors[i].y / 100);
        }
        mesh.uv = uvs;

        int[] triangle = new int[1200];
        for (int i = 0; i < 99; ++i)
        {
            triangle[6 * i] = 2 * i;
            triangle[6 * i + 1] = 2 * i + 1;
            triangle[6 * i + 2] = 2 * i + 2;
            triangle[6 * i + 3] = 2 * i + 1;
            triangle[6 * i + 4] = 2 * i + 3;
            triangle[6 * i + 5] = 2 * i + 2;
        }
        for (int i = 101; i < 200; ++i)
        {
            triangle[6 * i] = 2 * i;
            triangle[6 * i + 1] = 2 * i + 1;
            triangle[6 * i + 2] = 2 * i + 2;
            triangle[6 * i + 3] = 2 * i + 1;
            triangle[6 * i + 4] = 2 * i + 3;
            triangle[6 * i + 5] = 2 * i + 2;
        }

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

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

    public void MeshClear()
    {
        mesh.Clear();
        GetComponent<MeshFilter>().mesh = mesh;
    }
BlackAngle233's avatar
BlackAngle233 committed
188
}