Brak opisu

snowflake.cpp 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #include "drawingWindow.h"
  2. #include <cmath>
  3. /// \fn void DrawingWindow::snowflake(int x, int y, int size, double angle, int level, QColor c) {
  4. /// \~English
  5. /// \brief Recursive figure, the snowflake presented at:
  6. /// https://sites.google.com/a/wellesley.edu/wellesley-cs118-spring13/lectures-labs/lab-6-turtle-recursion
  7. /// sf = sf(n-1) + 60 degrees sf(n-1) + 60 degrees sf(n-1) + s(n-1)
  8. /// sf(0) = line
  9. /// \param x initial coordinate x
  10. /// \param y initial coordinate y
  11. /// \param size size of the snowflake
  12. /// \param angle angle of a line of the snowflake
  13. /// \param level recursion depth
  14. /// \param c color of the lines (snowflake)
  15. /// \~Spanish
  16. /// \brief Figura recursiva, el copo de nieve presentado en:
  17. /// https://sites.google.com/a/wellesley.edu/wellesley-cs118-spring13/lectures-labs/lab-6-turtle-recursion
  18. /// sf = sf(n-1) + 60 grados sf(n-1) + 60 grados sf(n-1) + s(n-1)
  19. /// sf(0) = line
  20. /// \param x coordenada inicial x
  21. /// \param y coordenada inicial y
  22. /// \param size tamano del copo de nieve
  23. /// \param angle angulo de la linea del copo de nieve
  24. /// \param level profundidad de la recursion
  25. /// \param c color de las lineas (copo de nieve)
  26. void DrawingWindow::snowflake(int x, int y, int size, double angle, int level, QColor c) {
  27. int chunkSize = round(size/3);
  28. if (level == 0) {
  29. addLinePolar(x,y,size,angle,1,c);
  30. return;
  31. }
  32. snowflake(x, y, chunkSize, angle, level-1, c);
  33. x = x + round( chunkSize * cos(angle * M_PI/180));
  34. y = y + round( chunkSize * sin(angle * M_PI/180));
  35. snowflake(x, y, chunkSize, angle + 60, level-1, c);
  36. snowflake(x + round(chunkSize * cos((angle + 60) * M_PI/180)),
  37. y + round(chunkSize * sin((angle + 60) * M_PI/180)),
  38. chunkSize, angle - 60, level-1, c);
  39. x = x + round(chunkSize * cos(angle * M_PI/180));
  40. y = y + round(chunkSize * sin(angle * M_PI/180));
  41. snowflake(x, y, chunkSize, angle, level-1, c);
  42. }
  43. /// \fn void DrawingWindow::snowHelper(int size, int level)
  44. /// \~English
  45. /// \brief interface function for the snowflake recursive function
  46. /// \param size snowflake size
  47. /// \param level recursion depth
  48. /// \~Spanish
  49. /// \brief function de interface para la funcion recursiva snowflake
  50. /// \param size tamano del copo de nieve
  51. /// \param level profundidad de la recursion
  52. void DrawingWindow::snowHelper(int size, int level) {
  53. snowflake(50,size,size,0,level,Qt::blue);
  54. snowflake(round(size*cos(-60*M_PI/180))+50,size+round(size*sin(-60*M_PI/180)),size,120,level,Qt::blue);
  55. snowflake(size+50,size,size,-120,level,Qt::blue);
  56. }