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

public class Logarithmic : MonoBehaviour
{
    Mesh mesh;

    public int maxX;
    public float factor;
    public float a;
    // Start is called before the first frame update
    void Start()
    {
        mesh = new Mesh
        {
            name = "Logarithmic"
        };
    }

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

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

        float step = 0.01f;
        Vector3[] vectors = new Vector3[100 * maxX];
        int k = 0;
        float down = Mathf.Log(a);
        for (float i = step; i <= maxX; i = i + step)
        {
            float y = Mathf.Log(i) / down;
            y *= factor;
            vectors[k] = new Vector3(i, y, 0);
            k++;
        }
        vectors[100 * maxX - 1] = new Vector3(maxX, Mathf.Log(maxX) / down * factor, 0);
        mesh.vertices = vectors;

        int[] triangle = new int[100 * maxX * 2 - 2];
        for (int i = 0; i < vectors.Length - 1; ++i)
        {
            triangle[2 * i] = i;
            triangle[2 * i + 1] = i + 1;
        }

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

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

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

        float step = 0.01f;
        Vector3[] vectors = new Vector3[200 * maxX];
        int k = 0;
        float down = Mathf.Log(a);
        for (float i = step; i <= maxX; i = i + step)
        {
            float y = Mathf.Log(i) / down;
            y *= factor;
            vectors[k] = new Vector3(i, y, -5);
            k++;
            vectors[k] = new Vector3(i, y, 5);
            k++;
        }
        vectors[200 * maxX - 2] = new Vector3(maxX, Mathf.Log(maxX) / down * factor, -5);
        vectors[200 * maxX - 1] = new Vector3(maxX, Mathf.Log(maxX) / down * factor, 5);
        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[100 * maxX * 6 - 6];
        for (int i = 0; i < 100 * maxX - 1; ++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;
    }
}