// G. Hagopian making an ellipse with tangent lines
#define _USE_MATH_DEFINES

#include <SFML/Graphics.hpp>
#include <random>
#include <iostream>
#include <cmath>
//#include <ctime>
using namespace std;

mt19937 mt_rand(time(0));

void drawLine(sf::RenderWindow& w, sf::Vector2f pt, sf::Vertex vertices[], unsigned int keyPresses) {
	//pick random point on big circle
	unsigned int angle = mt_rand() % 360;
	sf::Vector2f rand_pt(360 * cos(angle*M_PI / 180), 360 * sin(angle*M_PI / 180));
	sf::Vector2f M((pt.x + rand_pt.x) / 2, (pt.y + rand_pt.y) / 2);
	float m = (pt.x - rand_pt.x) / (rand_pt.y - pt.y);
	float a = 1 + m * m;
	float b = 2 * m*(M.y - m * M.x);
	float c = (M.y - m * M.x)*(M.y - m * M.x) - 129600;
	float D = b * b - 4 * a*c;
	float u1 = (-b + sqrt(D)) / (2 * a);
	float u2 = (-b - sqrt(D)) / (2 * a);
	float v1 = M.y + m * (u1 - M.x);
	float v2 = M.y + m * (u2 - M.x);
	/*sf::VertexArray line(sf::Lines, 2);
	line[0].position = sf::Vector2f(u1,v1);
	line[1].position = sf::Vector2f(u2,v2);
	line[0].color(sf::Color::Red);*/

	vertices[2 * keyPresses] = sf::Vertex(sf::Vector2f(400+u1,400+v1), sf::Color::Red, sf::Vector2f(0, 0));
	vertices[2 * keyPresses + 1] = sf::Vertex(sf::Vector2f(400+u2,400+v2), sf::Color::Red, sf::Vector2f(0, 0));
	// draw it
	w.draw(vertices, 2*keyPresses, sf::Lines);
}

int main() {
	// Make a window that is 800 by 200 pixels
	// And has the title "Hello from SFML"
	sf::RenderWindow window(sf::VideoMode(800, 800), "Ellipse From Tangents");
	window.setFramerateLimit(1);
	unsigned int keyPresses{ 0 };


	//mt19937 mt_rand(time(0));
	unsigned int angle = mt_rand() % 360;
	unsigned int dist = mt_rand() % 360;
	cout << angle << '\n' << dist;
	sf::CircleShape circle(360, 60);
	circle.setOrigin(360, 360);
	circle.setPosition(400, 400);
	circle.setOutlineColor(sf::Color::Cyan);
	circle.setFillColor(sf::Color::Cyan);
	sf::Vector2f rand_pt(dist*cos(angle*M_PI / 180), dist*sin(angle*M_PI / 180));
	sf::Vertex vertices[1000];
	sf::CircleShape point(8);
	point.setOrigin(4, 4);
	point.setPosition(400 + rand_pt.x, 400 + rand_pt.y);
	point.setFillColor(sf::Color::Red);
	// This "while" loop goes round and round- perhaps forever
	while (window.isOpen()) {
		// The next 6 lines of code detect if the window is closed
		// And then shuts down the program
		sf::Event event;
		while (window.pollEvent(event)) {
			if (event.type == sf::Event::Closed)
				// Someone closed the window- bye
				window.close();

		}

		// Clear everything from the last run of the while loop
		window.clear();

		// Draw our circle and point
		window.draw(circle);
		window.draw(point);
		if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) {
			drawLine(window, sf::Vector2f(400 + rand_pt.x, 400 + rand_pt.y),
				vertices, keyPresses);
			++keyPresses;
		}

		// Draw our game scene here
		// Just a message for now

		// Show everything we just drew
		window.display();
	}// This is the end of the "while" loop

	return 0;
}