Advertisement
Guest User

Untitled

a guest
Apr 16th, 2014
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.58 KB | None | 0 0
  1. #pragma once
  2. #include <SFML/Graphics/Rect.hpp>
  3. #include <SFML/System/Vector2.hpp>
  4. #include <cstdint>
  5. #include <complex>
  6. #include <functional>
  7. #include <vector>
  8.  
  9. using namespace std;
  10.  
  11. template<class T>
  12. class Fractal
  13. {
  14. public:
  15. Fractal(void);
  16. ~Fractal(void);
  17.  
  18. //the most important function
  19. vector<uint32_t> evaluate(const sf::Rect<T>& area, const sf::Vector2u& subdivisions);
  20.  
  21. //set the iterative function
  22. typedef function<void(complex<T>&)> iterative_function;
  23. void setIterativeFunction(iterative_function func);
  24.  
  25. //set the domain function
  26. typedef function<bool(complex<T>&)> domain_function;
  27. void setDomainFunction(domain_function func);
  28.  
  29. //set the maximum number of escape iterations
  30. void setMaxIterations(const uint32_t iterations);
  31.  
  32. //get maximum iterations
  33. uint32_t getMaxIterations() const;
  34.  
  35. //a coordinates generator
  36. //generates the coordinates to evaluate the fractal
  37. class CoordinatesGenerator
  38. {
  39. public:
  40. CoordinatesGenerator(const sf::Rect<T>& area, const sf::Vector2u& subdivisions);
  41. ~CoordinatesGenerator();
  42.  
  43. complex<T> operator()();
  44. private:
  45. const sf::Rect<T>& area_;
  46. const sf::Vector2u& subdivisions_;
  47. complex<T> coord_;
  48. sf::Vector2u pixel_;
  49. };
  50. private:
  51. //the number of escape iterations
  52. uint32_t max_iterations_;
  53.  
  54. //the tolerance where z must change
  55. T tolerance_;
  56.  
  57. //the formula used for the iterative system
  58. iterative_function iter_function_;
  59.  
  60. //the formula that decides either the given complex is inside or not the domain
  61. domain_function domain_function_;
  62.  
  63. //returns the number of iterations that z has to do to escape
  64. uint32_t getIterations(complex<T> z) const;
  65. };
  66.  
  67. template<class T>
  68. Fractal<T>::Fractal()
  69. {
  70. //setting max iterations to 1000 by default
  71. max_iterations_ = 1000;
  72.  
  73. //setting standard Manderbot iterative function
  74. iter_function_ = iterative_function([](complex<T>& z)
  75. {
  76. z = z*z + complex<T>(1,0);
  77. });
  78.  
  79. //setting standard Manderbot domain function
  80. domain_function_ = domain_function([](complex<T>& z)
  81. {
  82. return abs(z) < 2;
  83. });
  84. }
  85.  
  86. // Fractal<T>::setIterativeFunction
  87. // iterative_function func : the function on which the system iterates
  88. // must match this signature : void(Complex<T>&)
  89. template<class T>
  90. void Fractal<T>::setIterativeFunction(iterative_function func)
  91. {
  92. iter_function_ = func;
  93. }
  94.  
  95. // Fractal<T>::setDomainFunction
  96. // domain_function func : the function that determines if complex is inside domain
  97. // must match this signature : bool(Complex<T>&)
  98. template<class T>
  99. void Fractal<T>::setDomainFunction(domain_function func)
  100. {
  101. domain_function_ = func;
  102. }
  103.  
  104. // Fractal<T>::setMaxIterations
  105. // iterations : set the maximum iterations for escape
  106. template<class T>
  107. void Fractal<T>::setMaxIterations(const uint32_t iterations)
  108. {
  109. max_iterations_ = iterations;
  110. }
  111.  
  112. // vector<uint32_t> Fractal<T>::evaluate(const sf::Rect<T>& area, const sf::Vector2u& subdivisions)
  113. // area: the fractal area to evaluate
  114. // subdivisions : the number of subdivisions to evaluate
  115. // return a vector of the number of iterations
  116. // the vector is construction from x = 0 ... n, y = 0 ... n
  117. template<class T>
  118. vector<uint32_t> Fractal<T>::evaluate(const sf::Rect<T>& area, const sf::Vector2u& subdivisions)
  119. {
  120. uint32_t temp;
  121. complex<T> z(area.left,area.top);
  122. uint32_t num_coordinates = (subdivisions.x)*(subdivisions.y);
  123.  
  124. vector<uint32_t> result;
  125. vector<complex<T>> coordinates(num_coordinates);
  126. CoordinatesGenerator generator(area,subdivisions);
  127. generate(coordinates.begin(),coordinates.end(),generator);
  128.  
  129. for(auto& z: coordinates)
  130. {
  131. temp = getIterations(z);
  132. result.push_back(temp);
  133. }
  134. return result;
  135. }
  136.  
  137. // uint32_t Fractal<T>::getIterations(complex<T> z) const
  138. // z : the complex number to evaluate
  139. // return the number of iterations that z escapes domain
  140. // using iterative and domain functions
  141. template<class T>
  142. uint32_t Fractal<T>::getIterations(complex<T> z) const
  143. {
  144. static uint32_t result;
  145. result = 0;
  146.  
  147. while(domain_function_(z) && result < max_iterations_)
  148. {
  149. iter_function_(z);
  150. result++;
  151. }
  152. return result;
  153. }
  154.  
  155. // Fractal<T>::CoordinatesGenerator::CoordinatesGenerator(const sf::Rect<T>& area, const sf::Vector2u& subdivisions)
  156. // area : the fractal area to evaluate
  157. // subdivisions : the number of subdivisions
  158. // used by STL algorithm
  159. template<class T>
  160. Fractal<T>::CoordinatesGenerator::CoordinatesGenerator(const sf::Rect<T>& area, const sf::Vector2u& subdivisions):
  161. area_(area),subdivisions_(subdivisions)
  162. {
  163. coord_ = complex<T>(area_.left,area_.top);
  164. pixel_.x = 0;
  165. pixel_.y = 0;
  166. }
  167.  
  168. template<class T>
  169. Fractal<T>::CoordinatesGenerator::~CoordinatesGenerator()
  170. {
  171. }
  172.  
  173. // complex<T> Fractal<T>::CoordinatesGenerator::operator()()
  174. // Generate coordinates to evaluate the fractal
  175. // used by STL algorithm
  176. template<class T>
  177. complex<T> Fractal<T>::CoordinatesGenerator::operator()()
  178. {
  179. //getting the variation of X and Y
  180. T deltaX = area_.width/static_cast<T>(subdivisions_.x);
  181. T deltaY = area_.height/static_cast<T>(subdivisions_.y);
  182.  
  183. //creating the coordinate
  184. coord_ = complex<T>(static_cast<T>(pixel_.x)*deltaX + area_.left,static_cast<T>(pixel_.y)*deltaY + area_.top);
  185.  
  186. //applying some changes to generate the next coordinate
  187. pixel_.x++;
  188.  
  189. if(pixel_.x >= subdivisions_.x)
  190. {
  191. pixel_.y++;
  192. pixel_.x = 0;
  193. }
  194.  
  195. return coord_;
  196. }
  197.  
  198. template<class T>
  199. Fractal<T>::~Fractal()
  200. {
  201. }
  202.  
  203. template<class T>
  204. uint32_t Fractal<T>::getMaxIterations() const
  205. {
  206. return max_iterations_;
  207. }
  208.  
  209. vector<uint32_t>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement