// These "include" code from the C++ library and SFML too
/* drawPolarLine ()
Point drawPolarLine ( double x0 , double y0 , double r , double theta )
virtual inherited ? ( not )
Draws a line using polar coordinates that begins at the
given (x,y) point and extends from there by the given angle
and radius . Returns the end point where the line ends . */
*/
#include "stdafx.h"
#define _USE_MATH_DEFINES
#define wWidth  760
#define wHeight 760
#include <iostream>
#include <cmath> // sin()
#include <vector>
using std::vector;
#include <SFML/Graphics.hpp>

struct Point {
	float x, y, r, theta;
	Point(float xin, float yin) : x(xin), y(yin) {
		r = sqrt(x*x + y * y);
		theta = atan2(y, x);
	}
	Point() {};
};

Point drawPolarLine(sf::RenderWindow&, double x0, double y0, double r, double theta);


std::ostream& operator<<(std::ostream& os, const Point& p) {
	os << "(x,y) = (" << p.x << ',' << p.y << ")\n"
		<< "(r,theta) = (" << p.r << ',' << p.theta << ")";
	return os;
}

/*std::istream& operator>>(std::istream& is, Point& p) {
	char ch1;
	float x, y;
	is << ch1;
	if (ch1 != '(') cout << "That's wrong!" << '\n';
	is >> x;
	is >> ch1; // eat the comma
	is >> y;
	is >> ch1; // eat the closing paren
	p = Point(x, y);
	return is;
}*/

/*void rdraw(sf::RenderWindow& w,
	vector<Point>& points,
	float factor,
	unsigned nsides = 3,
	int level = 5) {
	sf::VertexArray lines(sf::LineStrip, nsides + 1);
	//std::cout << "here\n";
	//for (unsigned i = 0; i < nsides; ++i)
	//lines[i].position = sf::Vector2f(points[i].x, points[i].y);
	for (unsigned i = 0; i <= nsides; ++i)
		lines[i].position = sf::Vector2f(points[i%nsides].x, points[i%nsides].y);

	w.draw(lines);
	if (level > 1) {//recurse
		for (unsigned k = 0; k < nsides; k++) {
			float xdot = points[k].x;
			float ydot = points[k].y; //to optimize
			points[k].x = xdot + factor * (points[(k + 1) % nsides].x - xdot);
			points[k].y = ydot + factor * (points[(k + 1) % nsides].y - ydot);
		}
		// tie up the end
		//points[nsides-1].x = points[nsides-1].x + factor*(points[0].x-points[nsides-1].x);
		//points[nsides-1].y = points[nsides-1].y + factor*(points[0].y-points[nsides-1].y);
		//std::cout << "level = " << level << '\n';
		//std::cin.get();
		rdraw(w, points, factor, nsides, level - 1); // recursive call
	}//if
	else for (unsigned i = 0; i < nsides; ++i)
		points[i] = Point(wWidth / 2 * (1 + cos(2 * M_PI*i / nsides)),
			wHeight / 2 * (1 + sin(2 * M_PI*i / nsides)));
}//draw */

int main() {
	int n{ 1 }; //recursive depth
	double s{ 100.f }; //N-gon
	double x, y, r, theta;

	sf::RenderWindow window(sf::VideoMode(wWidth, wHeight), "Recursive Figure");
	window.setFramerateLimit(1);

	std::cout << "What is the edge length of the big triangle?\n";
	std::cin >> s;


	std::cout << "How deep goes your fractal?\n";
	std::cin >> n;

	std::cout << "Where does your line start? ";
	std::cin >> x >> y;
	
	std::cout << "How far do you want to go? ";
	std::cin >> r;

	std::cout << "In what direction? ";
	std::cin >> theta;



	// set coordinates of initial vertices
	
	// 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 the scene
		drawPolarLine(window, x, y, r, theta);
		// Show everything we just drew
		
		window.display();
	}
}


Point drawPolarLine(sf::RenderWindow& w, double x0, double y0, double r, double theta) {
	sf::VertexArray line(sf::LineStrip, 2);
	line[0].position = sf::Vector2f(x0, y0);
	line[1].position = sf::Vector2f(x0 + r * cos(theta), y0 + r * sin(theta));
	w.draw(line);
	return Point(x0 + r * cos(theta), y0 + r * sin(theta));
}