Window = SDL_CreateWindow( "Koch Fractal", SDL_WINDOWPOS_UNDEFINED, SDL_Init(SDL_INIT_EVERYTHING) //Initializing SDL2 Phew! A big implementation really! Now, time for the action in the int main(): Lines.remove( (*itr) ) //Deleting new Line*(s) delete (*itr) Lines.push_back( (*itr) ) //Adding new Line*(s) for( auto itr = delLines.begin() itr != delLines.end() itr++) NewLines.push_back( new Line( x_l4, y_l4, len_l4, ang_l4 ) ) įor( auto itr = newLines.begin() itr != newLines.end() itr++) NewLines.push_back( new Line( x_l3, y_l3, len_l3, ang_l3 ) ) NewLines.push_back( new Line( x_l2, y_l2, len_l2, ang_l2 ) ) NewLines.push_back( new Line( x_l1, y_l1, len_l1, ang_l1 ) ) All four lines properties are setted above! //Fixing bug - Changing Triangle Forming Orientation Std::list newLines //For getting new Line*(s) N_points = size(flake_points,1) % No.//Worth noting: It's a heavy function! void kochFractal( std::list & lines ) This is a matter of preference though.Īlso, personal preference again, but I like it when comments are aligned (if possible): length = 1 % Original side length of triangle i is by default the imaginary unit in MATLAB (so is j, 1i, 1j). I suggest substituting the iterator i to for instance ii if you ever work with complex numbers, to avoid potential errors and confusion. If you want to use the number of degrees, and convert it to radians, you might want to use deg2rad. I guess you think 60*pi/180 is easier to read than pi/3, but I disagree. theta = 60*pi/180 % Angle of the equilateral triangle in radians Note that you should always use the function numel instead of length, but it's still not a good idea to overload it. This can cause a lot of errors that are hard to find. I strongly recommend you not to use length as a variable name as it's a very commonly used builtin function. length = 1 % Original side length of triangle You can skip all the clearing in the beginning and substitute it with: function = koch_snowflake(iterations) % orĪnd add an end at the bottom of your script. It requires very little alterations, and will make the function a lot easier to use. I suggest you put this into a function, that takes n as an input argument instead of hardcoding it. % Returns the unit vector p_vec, which is perpendicular to the input Str = sprintf('Iteration: %d',iteration) Įdit - This function is also required: function p_vec = perp(vec) Hold on % Plot last data point with 1st (ie, close the loop) % so we now skip to the next empty spaces N = n + 4 % Three new points have been inserted, New_flake_points(n+1:n+3,:) = new_triangle_points(index:index+2,:) New_flake_points(n,:) = flake_points(i,:) New_flake_points = nan(n_points*3+n_points,2) Top = p + 1/2*vector + perp(vector)*length/3*sin(theta) Vector = flake_points(i+1,:)-flake_points(i,:) ī1 = p + 1/3*vector % Base point 1 of the new triangleī2 = p + 2/3*vector % Base point 2 of the new triangle % Vector between next point and current point Vector = flake_points(1,:)-flake_points(i,:) New_triangle_points = nan(n_points*3,2) % New points to be added % Points of the original equilateral triangleįlake_points = % Vertices of the snowflake Theta = 60*pi/180 % Angle of the equilateral triangle in radians Length = 1 % Original side length of triangle Here is my attempt to plot this in MATLAB. This process then repeats on each edge of the new shape. Beginning with an equilateral triangle, a smaller equilateral triangle is placed halfway along each edge of the shape. The Koch snowflake is a well known fractal.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |