If a second step was warranted I'd probably roll this functionality up into a class, but I'm not sure that would make things substantially better (or, better at all, in fact -). Pathcodes = pathcodes.replace("F", "FLFRFLF")Īngle = decode(angle, coords, jump) if it needs to get back in it should be a bit more explicit, IMHO): import mathĪngles = ĭef grow(steps, length=200, startPos=(0,0)): as a first step (I've removed the try/except because I don't know what you're trying to ward against. I don't see it as particularly ugly and I'd only refactor it incrementally, e.g. My Code: from math import sin, cos, radiansĭef grow(steps, length = 200, startPos = (0,0)):įor i in xrange(steps): set=set.replace("F", "FLFRFLF")ĮDIT: due to lazy copying, i forgot the import It can be in python, or your favorite language. My challenge for you is to either improve my code or come up with a more elligant solution of your own. My implementation is in python and i basically ripped off the python turtle implementation but replaced the turtle specific stuff with basic trig. So i set out to make my own that returned a list or coordinates. For more such things, visit my blog was looking at the wikipedia page for the Koch Snowflake ( here) and was bothered by the all the examples all being in the logo/turtle style. I hope that this excites you for more programming, algorithms and fractals. It was executed on a mobile device using C4Droid, a program for running C++ programs on Android. SDL_Quit() //Clearing all SDL resources return 0 įollowing images show the result. KochFractal(lines) //Applying Koch Fractalįor( auto itr = lines.begin() itr != lines.end() itr++)ĭelete (*itr) //Deleting all lines at the end of a program SDL_Delay(2500) //Delay to show each iteration SDL_RenderPresent(renderer) //Updating screen SDL_RenderClear(renderer) //Clearing renderer for( auto itr = lines.begin() itr != lines.end() itr++) While(SDL_PollEvent(&in)) //Polling Events SDL_SetRenderDrawColor(renderer,0,0,0,255) //Setting default screen color //Horizontal line //lines.push_back( new Line(SCR_W-10,SCR_H/2.0, SCR_W-20,180.0) ) //Vertical line //lines.push_back( new Line(SCR_W/1.5,10, SCR_H-20,90.0) ) //Equilateral Triangle for forming Koch Snowflake Renderer = SDL_CreateRenderer( window, -1, SDL_RENDERER_ACCELERATED ) //Creating renderer SDL_WINDOWPOS_UNDEFINED, SCR_W, SCR_H, SDL_WINDOW_SHOWN ) //Creating window 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) Worth noting: It's a heavy function! void kochFractal( std::list & lines )
0 Comments
Leave a Reply. |