Trigonometric.cs 2.44 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Trigonometric : MonoBehaviour
{
    Mesh mesh;

    public int minX;
    public int maxX;

    public TriType triType;
    public float a;
    public float b;
    public float factor;
    public enum TriType
    {
        Sin,
        Cos,
        Tan
    }
    // Start is called before the first frame update
    void Start()
    {
        mesh = new Mesh
        {
            name = "Trigonometric"
        };
    }

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

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

        float step = 0.01f;
        Vector3[] vectors = new Vector3[100 * (maxX - minX) + 1];
        int k = 0;
        for (float i = minX; i <= maxX; i = i + step)
        {
            float y = a * i + b;
            switch (triType)
            {
                case TriType.Sin:
                    y = Mathf.Sin(y);
                    break;
                case TriType.Cos:
                    y = Mathf.Cos(y);
                    break;
                case TriType.Tan:
                    y = Mathf.Tan(y);
                    break;
                default:
                    break;
            }
            y *= factor;
            vectors[k] = new Vector3(i, y, 0);
            k++;
        }
        switch (triType)
        {
            case TriType.Sin:
                vectors[100 * (maxX - minX)] = new Vector3(maxX, Mathf.Sin(a * maxX + b) * factor, 0);
                break;
            case TriType.Cos:
                vectors[100 * (maxX - minX)] = new Vector3(maxX, Mathf.Cos(a * maxX + b) * factor, 0);
                break;
            case TriType.Tan:
                vectors[100 * (maxX - minX)] = new Vector3(maxX, Mathf.Tan(a * maxX + b) * factor, 0);
                break;
            default:
                break;
        }
        mesh.vertices = vectors;

        int[] triangle = new int[100 * (maxX - minX) * 2];
        for (int i = 0; i < vectors.Length - 1; ++i)
        {
            if(vectors[i].y * vectors[i + 1].y >= 0)
            {
                triangle[2 * i] = i;
                triangle[2 * i + 1] = i + 1;
            }
            else
            {
                triangle[2 * i] = i;
                triangle[2 * i + 1] = i;
            }
        }
        mesh.SetIndices(triangle, MeshTopology.Lines, 0);

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