Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This is the CPP file you will edit and turn in. (TODO: Remove this comment!)
- #include "fractals.h"
- #include "gmath.h"
- #include "recursion.h"
- #include "console.h"
- using namespace std;
- /*
- * This method uses the pythag. theorm to convert the
- * side length of an equilateral triangle to the height
- */
- double getHeightFromEquilateralSide(double size) {
- return sqrt(pow(size, 2) - pow(size / 2, 2));
- }
- void drawTriangle(GWindow& window, double x, double y, double size) {
- double height = getHeightFromEquilateralSide(size);
- window.drawLine(x, y, x + size, y);
- window.drawLine(x, y, x + (size / 2), y + height);
- window.drawLine(x + size, y, x + (size / 2), y + height);
- }
- void drawSierpinskiTriangle(GWindow& window, double x, double y, double size, int order) {
- if(x < 0 || y < 0 || size < 0 || order < 0) {
- throw "Invalid input parameters.";
- }
- else if(order == 0) { //base case
- return;
- } else if(order == 1) { //1 is base case to draw the triangle
- drawTriangle(window, x, y, size);
- } else { //Recursive case: draw 3 sierpinski triangles
- int newSize = size / 2;
- drawSierpinskiTriangle(window, x, y, newSize, order - 1);
- drawSierpinskiTriangle(window, x + newSize, y, newSize, order - 1);
- drawSierpinskiTriangle(window, x + newSize / 2, y + getHeightFromEquilateralSide(newSize), newSize, order - 1);
- }
- }
- void drawTree(GWindow& window, double x, double y, double size, int order, int angle) {
- if(order == 0) {
- return; //base case
- } else {
- //Manage color
- if(order == 1) {
- //If it's the last thing on the tree, make it green
- window.setColor("#2e8b57");
- } else {
- window.setColor("#8b7765"); //Set color to brown
- }
- //Draw this line
- window.drawPolarLine(x, y, size, angle);
- int newSize = size / 2;
- int newX = x + cosDegrees(angle) * size;
- int newY = y - sinDegrees(angle) * size;
- for(int angleDelta = -45; angleDelta <=45; angleDelta+=15) {
- int newAngle = angle + angleDelta;
- drawTree(window, newX, newY, newSize, order - 1, newAngle);
- }
- }
- }
- void drawTree(GWindow& window, double x, double y, double size, int order) {
- if(x < 0 || y < 0 || size < 0 || order < 0) {
- throw "Invalid input parameters.";
- }
- drawTree(window, x + size / 2, y + size, size / 2, order, 90); //draw the tree, starting with a straight line (angle 90)
- }
- //Store direction so we don't redraw last pixel
- enum direction {NONE, UP, DOWN, LEFT, RIGHT};
- int floodFill(GWindow& window, int x, int y, int startingColor, int color, int filled, direction last) {
- if(!window.inCanvasBounds(x, y)) {
- return filled;
- }
- else if(window.getPixel(x, y) != startingColor) {
- return filled;
- } else {
- window.setPixel(x, y, color);
- filled++;
- //Look up, down, left, right. Loop would be nicer but not allowed for problem.
- if(last != RIGHT) {
- floodFill(window, x + 1, y, startingColor, color, filled, RIGHT);
- }
- if(last != LEFT) {
- floodFill(window, x - 1, y, startingColor, color, filled, LEFT);
- }
- if(last != DOWN) {
- floodFill(window, x, y + 1, startingColor, color, filled, DOWN);
- }
- if(last != UP) {
- floodFill(window, x, y - 1, startingColor, color, filled, UP);
- }
- }
- return filled;
- }
- int floodFill(GWindow& window, int x, int y, int color) {
- if(x < 0 || y < 0 || !window.inCanvasBounds(x, y)) {
- throw "Invalid input parameters.";
- }
- int initialColor = window.getPixel(x, y);
- if(initialColor == color) {
- return 0;
- }
- return floodFill(window, x, y, initialColor, color, 0, NONE);
- }
- /*
- * This method converts coordinates from a real plane (x, y) to
- * a complex number (returns Complex) within range min to max
- */
- Complex getComplex(int x, int y, int size, const Complex& min, const Complex& max) {
- float a = ((float)(x)/ (float)size) * (max.real()-min.real()) + min.real();
- float b = ((float)(y)/ (float)size) * (max.imag()-min.imag()) + min.imag();
- return Complex(a, b);
- }
- /*
- * This method returns whether or not a specific pixel (represented by initialPoint)
- * is in the mandelbrot set (given `iterations` tries to test for convergance)
- */
- bool isInMandelbrot(Complex& z, const Complex& initialPoint, int iterations) {
- if(z.abs() > 4) {
- return false;
- }
- else if(iterations <= 0) {
- return true;
- }
- else {
- iterations--;
- z = (z + initialPoint);
- z = z * z;
- return isInMandelbrot(z, initialPoint, iterations);
- }
- }
- void drawMandelbrotSet(GWindow& gw, double leftX, double topY, double size,
- const Complex& min, const Complex& max,
- int iterations, int color) {
- for(int x = 0; x < size; x++) {
- for(int y = 0; y < size; y++) {
- Complex point = getComplex(x, y, size, min, max);
- Complex initial = Complex(0, 0);
- if(isInMandelbrot(initial, point, iterations)) {
- gw.drawPixel(x + leftX, y + topY, color);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement