(A global constant for the number of degrees in a full circle is necessary, unfortunately, because Turtle allows you to change this using the degrees function, but doesn't have a documented way of getting the current value. Sides - number of sides of the snowflake. I would write: # Number of degrees in a full circle.ĭef snowflake(depth=4, size=300, sides=3): One way to reduce the number of constants is to turn them into function parameters. The exterior angle \$θ\$ of a regular polygon can be computed from the number of sides \$k\$, like this: \$θ = \$. The function uses two constants, but these are not independent. The term you want here is exterior angle. It has the value 120, but the interior angle of an equilateral triangle is actually 60°. The constant EQUILATERAL_TRIANGLE_INTERNAL_ANGLE is wrongly named. For example, what if you wanted to draw several snowflakes before updating the screen? It is usually best if a function does just one thing: then you have more flexibility about how you use it. The function does two things: it draws the snowflake, and it updates the screen. What does it do? What is the meaning of the arguments? Just looking at this function: def snowflake(n, size=300): Note the use of SIZES_LIMIT which will then create the range object RANGE_OF_SIZES.Īll of these last "constants" are only used within the if _name_ = '_main_' so it's pretty easy to adapt. EQUILATERAL_TRIANGLE_INTERNAL_ANGLE = 120Īnd use argparse to provide default values or custom ones from the command line for:.I personally would keep the following constants: Maybe that allowing to change the latter group using the command line could be more intuitive to tweak the parameters of the display. Of your 12 constants, a few are really constant, some are arbitrary fixed for the purpose of your application, and some could be tweaked to achieve a better (prettier?) result. You can even make the two sensible parts two functions if you want to separate the concerns better. This way, the main part would look like: if _name_ = "_main_": Considering its value, it won't hurt anyone in an interactive session and is still needed in a program for people to be able to read the text. I would also put time.sleep(WRITING_SLEEPING_INTERVAL) at the end of write_as_title. I would have put a call to costum_reset (costum? really?) at the beginning of both write_as_title and snowflake since both drawings need a clear state to begin with. On reducing the similarities between the two drawings Code Issues Pull requests Computer Graphics Project 1 opengl graphics koch-snowflake Updated on C grypesc / KochFractals Star 1 Code Issues Pull requests Low level combination of C and x86/圆4 assembly code that turns a curve entered by the user into a Kochs fractal. you allow to parametrize write_as_title with a custom font but nothing similar is done with other kind of parameters.you don't need the newline continuation ( \) inside parenthesis, they already act as an implicit one.a docstring would help understanding the recursion involved in snowflake_edge.whitespace in expressions is weird sometimes.On top of also finding that you might have too much constant (more on that in a little bit), I just have a few nitpicks: Your code reads well and is easily understandable. Snowflake_edge(n - 1, size / SIDES_OF_AN_EQUILATERAL_TRIANGLE) Lambda: turtle.left(EQUILATERAL_TRIANGLE_INTERNAL_ANGLE // 2)): Lambda: turtle.right(EQUILATERAL_TRIANGLE_INTERNAL_ANGLE), Turtle.right(EQUILATERAL_TRIANGLE_INTERNAL_ANGLE)įor movement in (lambda: turtle.left(EQUILATERAL_TRIANGLE_INTERNAL_ANGLE // 2), They are similar but I cannot quite abstract the similarity out.ĮQUILATERAL_TRIANGLE_INTERNAL_ANGLE = 120įor _ in range(SIDES_OF_AN_EQUILATERAL_TRIANGLE): Snowflake(DEPTH_WHEN_CONSTANT_SIZE, size) "Constant recursion depth, size increasing.", font=FONT) Snowflake(deepness, SIZE_WHEN_CONSTANT_DEPTH) "Constant size, recursion depth increasing.", font=FONT) Have I got too many constants? I like them because they make the program easy to customize, but 12 constants looks like a lot.Īny idea to avoid repetition in these blocks of code? write_as_title(\ Constant recursion depth, size increasing.Constant size, recursion depth increasing.If (!gl) else if (script.After finding a piece of code on StackOverflow that drew the Koch snowflake fractal, I made a ton of modifications to it and used it to produce an animation divided in two parts: I have the following WebGL code which draws a triangle: "use strict" Ĭanvas = document.getElementById("gl-canvas")
0 Comments
Leave a Reply. |