Giter Site home page Giter Site logo

von-koch-flake's Introduction

Von-Koch-Flake โ„๏ธ

How to create the Von Koch flake with python ?

Import libraries ๐Ÿ“—

import matplotlib.pyplot as plt
import math as mt
  • You need two libraries :
  1. math : To use math funciton ( there are a lot in this project ! )
  2. matplotlib : To draw the flake

Let's do some math to start ๐Ÿ“

  • If you hate math, don't worry, skip this part...
  • You start with a segment of 1, and for each step, you need to split in 3 part, and generate an isosceles triangle at the middle
  • Let's see a pretty schema about this : Alt text
  • You can do that in different way, but I choose to take two lists of position X,Y about each point.
  • For example, at the beginning, i take X = [0,1] & Y = [0,0] and at the second itteration, you can add 3 points between the previous two.
  • However, if you want to try this on your own, I advise you to take a pen and a piece of paper, and state the problem. Good Luck !

Main function โšก

  • This function generate the X & Y lists.
def vonkoch(order,X=[0,1],Y=[0,0]):
    if order==0:
        return (X,Y)

    for l in range(order):
        i=0
        for betwX in range(len(X)-1):
            "Math part ... "
            u = [ (X[i+1]-X[i])/3 , (Y[i+1]-Y[i])/3 ]
            v = [ 2*(X[i+1]-X[i])/3 , 2*(Y[i+1]-Y[i])/3 ]
            w = [ (X[i+1]-X[i])/2 , (Y[i+1]-Y[i])/2 ]
            n = [ -(Y[i+1]-Y[i]) , (X[i+1]-X[i]) ]
            norm_n = mt.sqrt( n[0]**2 + n[1]**2 )
            N =  [ -(Y[i+1]-Y[i])/norm_n , (X[i+1]-X[i])/norm_n ]
            L = mt.sqrt( abs(X[i+1]-X[i])**2 + abs(Y[i+1]-Y[i])**2 )
            I = [ X[i]+w[0] , Y[i]+w[1] ]
            c = L/6 * mt.tan(mt.pi/3)

            "insert at 1/3"
            X.insert(i+1, X[i]+u[0])
            Y.insert(i+1, Y[i]+u[1])

            "insert at 2/3"
            X.insert(i+2, X[i]+v[0])
            Y.insert(i+2, Y[i]+v[1])

            "insert the top"
            X.insert(i+2, I[0]+N[0]*c)
            Y.insert(i+2, I[1]+N[1]*c)

            i+= 4

    return (X,Y)
  • You need to make a double loops, one for each operation to do in the order n, and an other to add points on all the segments.
  • Furthermore, you have a part of math ( it's scary I agree ) and other part to insert new points.

Draw one curve of Von Koch โœ๏ธ

  • Just a function use for draw a part of the flake with matplotlib.
def trace(order):
    Xf = vonkoch(order,[0,1],[0,0])[0]
    Yf = vonkoch(order,[0,1],[0,0])[1]
    plt.plot(Xf ,Yf, color="black")
    plt.axis('equal')
    plt.show()
    plt.close()

Draw the flake ๐Ÿ’™

  • You need to do the same previous thing but with two other segments to create an isosceles triangle
def flake(order):
    X1 = vonkoch(order,[0,1],[0,0])[0]
    Y1 = vonkoch(order,[0,1],[0,0])[1]
    X2 = vonkoch(order,[1,1/2],[0,-mt.sqrt(3)/2])[0]
    Y2 = vonkoch(order,[1,1/2],[0,-mt.sqrt(3)/2])[1]
    X3 = vonkoch(order,[1/2,0],[-mt.sqrt(3)/2,0])[0]
    Y3 = vonkoch(order,[1/2,0],[-mt.sqrt(3)/2,0])[1]

    Xf = X1+X2+X3 ; Yf = Y1+Y2+Y3

    plt.plot(Xf ,Yf, color="black")
    plt.axis('equal')
    plt.show()
    plt.close()
  • Enjoy ! You can draw a pretty lake with this command :
flake(7)
  • Here, n =7 it's largely enough but if you have a powerful computer, you can do better !
  • Preview of the result :

Alt text

  • You can zoom in to see beautiful geometry !

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.