floco.py

Created by numworks-pt

Created on December 01, 2020

883 Bytes

floco(n) traça o floco de Koch para n etapas [n inteiro positivo] na cor definida em seg(). seg(xa, ya, xb, yb, r, v, b) desenha um segmento final A(xa, ya) e B(xb, yb) na cor (r, v, b). Podemos apenas pedir seg(xa, ya, xb, yb): a cor será então magenta por defeito.

Este script foi traduzido do script criado original pelo utilizador telpe51.


from math import *
from kandinsky import *
from cmath import *
def seg(xa,ya,xb,yb,r=250,v=0,b=250):
  col=color(r,v,b)
  if xa==xb :
    if ya<yb:
      y=ya
      bs=yb
    else :
      y=yb
      bs=ya
    while y<=bs:
      set_pixel(int(xa),int(y),col)
      y=y+1
  else:
    m=(yb-ya)/(xb-xa)
    p=ya-m*xa
    if xa<xb:
      x=xa
      bs=xb
    else :
      x=xb
      bs=xa
    while x<=bs:
      y=m*x+p
      set_pixel(int(x),int(y),col)
      x=x+1
    
def motivo(za, zb, n):
  if n==0:
      seg(za.real,za.imag,zb.real,zb.imag)
  else:
      zc=za+(zb-za)/3
      zd=za+2*(zb-za)/3
      ze=zc+(zd-zc)*exp(pi/3*1j)
      motivo(za, zc, n-1)
      motivo(zc, ze, n-1)
      motivo(ze, zd, n-1)
      motivo(zd, zb, n-1)

def floco(n):
  z1=50+165*1j
  z2=230+165*1j
  z3=z2+(z1-z2)*exp(pi/3*1j)
  motivo(z1, z2, n)
  motivo(z2, z3, n)
  motivo(z3, z1, n)

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.