The output of this program is an SVG file depicting a Koch snowflake. To create a Koch Curve : from turtle import def snowflake (lengthSide, levels): if levels 0: forward (lengthSide) return lengthSide / 3. The basic unit Start the project by making an empty file koch.py. To create the Koch snowflake, one would use F F F (an equilateral triangle) as the axiom. And it introduces the computer science idea of recursion. It assumes you know about for-loops and functions. Generate Kock Curve (_SIZE_, _ITERATIONS_) The Koch Snowflake This project draws a fractal curve, with only a few lines of turtle graphics code. Subroutine CopoNieveKoch(longitud, recursionfondo) Finish_Path ( Close_Path => True, Rendering => Fill, Rule => Even_Odd ) Doc. 3 loop Koch ( Level, Length ) Direction := Direction 120.0 end loop Doc. Margins ( Margins_Type '( Left => Cm_2_5, others => One_cm )) Doc. 7" ) Put_Line ( "open koch.pdf to view ouput" ) return end if Level := Level_Type ' Value ( Argument ( 1 )) Doc. Line ( Corner Current ) else Koch ( Level - 1, Length / 3.0 ) Direction := Direction - 60.0 Koch ( Level - 1, Length / 3.0 ) Direction := Direction 120.0 Koch ( Level - 1, Length / 3.0 ) Direction := Direction - 60.0 Koch ( Level - 1, Length / 3.0 ) end if end Koch begin if Argument_Count /= 1 then Put_Line ( "koch_curve " ) Put_Line ( " 0. 7 Purple : constant Color_Type := ( 0.7, 0.0, 0.5 ) Length : constant Real := 400.0 Corner : constant Point := ( 90.0, 580.0 ) Level : Level_Type Current : Point := ( 0.0, 0.0 ) Direction : Angle_Deg := Angle_Deg '( 60.0 ) Doc : PDF_Out_File procedure Koch ( Level : Level_Type Length : Real ) is begin if Level = 0 then Current := Current Length * Point '( Sin ( Direction, 360.0 ), Cos ( Direction, 360.0 )) Doc. Text_IO subtype Angle_Deg is Real type Level_Type is range 0. With Ada.Command_Line with _Elementary_Functions with Ada.Text_IO with PDF_Out procedure Koch_Curve is package Real_Math is new _Elementary_Functions (PDF_Out.Real ) use Real_Math, PDF_Out, Ada. PROC DrawKoch(INT x,y REAL POINTER len BYTE depth) Triangle_top = 1/2*line_start 1/2*line_end \ Triangle_right = 1/3*line_start 2/3*line_end # 2/3 of this line Triangle_left = 2/3*line_start 1/3*line_end # 1/3 of this line # find the three points of the "triangle" used to define the segments (screen,, line_start, line_end, LINE_WIDTH ) The concept behind this, is to break a line into two while leaving one third of the space between them where we fit two more lines forming an equilateral triangle without the base. If ( line_end - line_start ) / 3 < MIN_LINE_LENGTH: Koch Fractal is a simple algorithm which produces snowflake from a triangle. # draw_koch_line: (approximately) draws a Koch line between the points specifiedĭef draw_koch_line(screen, line_start, line_end ): # vector_from_polar: constructs a vector from its angle and mangitudeĭef vector_from_polar( magnitude, angle ): Snowflake_right = screen_center vector_from_polar( radius, pi/2 4*pi/3 )ĭraw_koch_line( screen, snowflake_top, snowflake_left )ĭraw_koch_line( screen, snowflake_left, snowflake_right )ĭraw_koch_line( screen, snowflake_right, snowflake_top ) Snowflake_left = screen_center vector_from_polar( radius, pi/2 2*pi/3 ) Snowflake_top = screen_center vector_from_polar( radius, pi/2 ) Radius = WINDOW_SIZE/2 # not really radius? but distance from center to corner Screen_center = numpy.array(/2, WINDOW_SIZE/2 ]) # an equilateral triangle centered on the screen # calculate the three corners of the snowflake, MIN_LINE_LENGTH = 1 # when to terminate recursion This main loop is based on the pygame.draw example but feels a bit clunky with done=False while not done etc. The snowflake is constantly redrawn in preparation for the future. All other feedback is of course also greatly appreciated. I'm new to the Python world and would appreciate all feedback on how to make the code more Pythonic as I'm not used to these idioms, styles and modules. I've drawn a Koch snowflake with Python 3.
0 Comments
Leave a Reply. |