Advertisement
Guest User

Untitled

a guest
Oct 24th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.31 KB | None | 0 0
  1. // This is the CPP file you will edit and turn in. (TODO: Remove this comment!)
  2.  
  3. #include "fractals.h"
  4. #include "gmath.h"
  5. #include "recursion.h"
  6. #include "console.h"
  7. using namespace std;
  8.  
  9. /*
  10. * This method uses the pythag. theorm to convert the
  11. * side length of an equilateral triangle to the height
  12. */
  13. double getHeightFromEquilateralSide(double size) {
  14. return sqrt(pow(size, 2) - pow(size / 2, 2));
  15. }
  16.  
  17. void drawTriangle(GWindow& window, double x, double y, double size) {
  18. double height = getHeightFromEquilateralSide(size);
  19. window.drawLine(x, y, x + size, y);
  20. window.drawLine(x, y, x + (size / 2), y + height);
  21. window.drawLine(x + size, y, x + (size / 2), y + height);
  22. }
  23.  
  24. void drawSierpinskiTriangle(GWindow& window, double x, double y, double size, int order) {
  25. if(x < 0 || y < 0 || size < 0 || order < 0) {
  26. throw "Invalid input parameters.";
  27. }
  28. else if(order == 0) { //base case
  29. return;
  30. } else if(order == 1) { //1 is base case to draw the triangle
  31. drawTriangle(window, x, y, size);
  32. } else { //Recursive case: draw 3 sierpinski triangles
  33. int newSize = size / 2;
  34. drawSierpinskiTriangle(window, x, y, newSize, order - 1);
  35. drawSierpinskiTriangle(window, x + newSize, y, newSize, order - 1);
  36. drawSierpinskiTriangle(window, x + newSize / 2, y + getHeightFromEquilateralSide(newSize), newSize, order - 1);
  37. }
  38. }
  39.  
  40. void drawTree(GWindow& window, double x, double y, double size, int order, int angle) {
  41. if(order == 0) {
  42. return; //base case
  43. } else {
  44. //Manage color
  45. if(order == 1) {
  46. //If it's the last thing on the tree, make it green
  47. window.setColor("#2e8b57");
  48. } else {
  49. window.setColor("#8b7765"); //Set color to brown
  50. }
  51.  
  52. //Draw this line
  53. window.drawPolarLine(x, y, size, angle);
  54.  
  55. int newSize = size / 2;
  56. int newX = x + cosDegrees(angle) * size;
  57. int newY = y - sinDegrees(angle) * size;
  58.  
  59. for(int angleDelta = -45; angleDelta <=45; angleDelta+=15) {
  60. int newAngle = angle + angleDelta;
  61. drawTree(window, newX, newY, newSize, order - 1, newAngle);
  62. }
  63. }
  64. }
  65.  
  66. void drawTree(GWindow& window, double x, double y, double size, int order) {
  67. if(x < 0 || y < 0 || size < 0 || order < 0) {
  68. throw "Invalid input parameters.";
  69. }
  70. drawTree(window, x + size / 2, y + size, size / 2, order, 90); //draw the tree, starting with a straight line (angle 90)
  71. }
  72.  
  73. //Store direction so we don't redraw last pixel
  74. enum direction {NONE, UP, DOWN, LEFT, RIGHT};
  75.  
  76. int floodFill(GWindow& window, int x, int y, int startingColor, int color, int filled, direction last) {
  77. if(!window.inCanvasBounds(x, y)) {
  78. return filled;
  79. }
  80. else if(window.getPixel(x, y) != startingColor) {
  81. return filled;
  82. } else {
  83. window.setPixel(x, y, color);
  84. filled++;
  85.  
  86. //Look up, down, left, right. Loop would be nicer but not allowed for problem.
  87. if(last != RIGHT) {
  88. floodFill(window, x + 1, y, startingColor, color, filled, RIGHT);
  89. }
  90. if(last != LEFT) {
  91. floodFill(window, x - 1, y, startingColor, color, filled, LEFT);
  92. }
  93. if(last != DOWN) {
  94. floodFill(window, x, y + 1, startingColor, color, filled, DOWN);
  95. }
  96. if(last != UP) {
  97. floodFill(window, x, y - 1, startingColor, color, filled, UP);
  98. }
  99. }
  100. return filled;
  101. }
  102.  
  103. int floodFill(GWindow& window, int x, int y, int color) {
  104. if(x < 0 || y < 0 || !window.inCanvasBounds(x, y)) {
  105. throw "Invalid input parameters.";
  106. }
  107. int initialColor = window.getPixel(x, y);
  108. if(initialColor == color) {
  109. return 0;
  110. }
  111. return floodFill(window, x, y, initialColor, color, 0, NONE);
  112. }
  113.  
  114. /*
  115. * This method converts coordinates from a real plane (x, y) to
  116. * a complex number (returns Complex) within range min to max
  117. */
  118. Complex getComplex(int x, int y, int size, const Complex& min, const Complex& max) {
  119. float a = ((float)(x)/ (float)size) * (max.real()-min.real()) + min.real();
  120. float b = ((float)(y)/ (float)size) * (max.imag()-min.imag()) + min.imag();
  121. return Complex(a, b);
  122. }
  123.  
  124. /*
  125. * This method returns whether or not a specific pixel (represented by initialPoint)
  126. * is in the mandelbrot set (given `iterations` tries to test for convergance)
  127. */
  128. bool isInMandelbrot(Complex& z, const Complex& initialPoint, int iterations) {
  129. if(z.abs() > 4) {
  130. return false;
  131. }
  132. else if(iterations <= 0) {
  133. return true;
  134. }
  135. else {
  136. iterations--;
  137. z = (z + initialPoint);
  138. z = z * z;
  139. return isInMandelbrot(z, initialPoint, iterations);
  140. }
  141. }
  142.  
  143. void drawMandelbrotSet(GWindow& gw, double leftX, double topY, double size,
  144. const Complex& min, const Complex& max,
  145. int iterations, int color) {
  146. for(int x = 0; x < size; x++) {
  147. for(int y = 0; y < size; y++) {
  148. Complex point = getComplex(x, y, size, min, max);
  149. Complex initial = Complex(0, 0);
  150.  
  151. if(isInMandelbrot(initial, point, iterations)) {
  152. gw.drawPixel(x + leftX, y + topY, color);
  153. }
  154. }
  155. }
  156.  
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement