//from video at https://youtu.be/6C1NPy321Nk
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Sinewave : MonoBehaviour
{
    public LineRenderer myLineRenderer;
    public int points;
    public float amplitude = 1;
    public float frequency = 1;
    public Vector2 xLimits = new Vector2(0, 1);
    public Vector2 direction = Vector2.right;
    public float movementSpeed = 1;
    [Range(0, 2 * Mathf.PI)]
    public float radians;
    void Start()
    {
        myLineRenderer = GetComponent<LineRenderer>();
    }

    void Draw()
    {
        float xStart = xLimits.x;
        float Tau = 2 * Mathf.PI;
        float xFinish = xLimits.y;

        float angle = Vector2.Dot(Vector2.right, direction);
        Debug.Log(Mathf.Abs(angle));

        myLineRenderer.positionCount = points;
        for (int currentPoint = 0; currentPoint < points; currentPoint++)
        {
            float progress = (float)currentPoint / (points - 1);
            float x = Mathf.Lerp(xStart, xFinish, progress);
            float y = amplitude * Mathf.Sin((Tau * frequency * x) + (Time.timeSinceLevelLoad * movementSpeed));
            //Vector2 sinVector = new Vector2(x, y);

            // Apply rotation matrix based on angle
            float xRot = x * Mathf.Cos(angle) - y * Mathf.Sin(angle); 
            float yRot = x * Mathf.Sin(angle) + y * Mathf.Cos(angle); 

            myLineRenderer.SetPosition(currentPoint, new Vector3(xRot, yRot, 0));
        }
    }

    void Update()
    {
        Draw();
    }
}

