rosace.py

Created by andreanx

Created on May 23, 2019

1006 Bytes

Draws a rhombic rosette using the turtle module, with the current turtle position. The construction method is the one with an equilateral polygon in rotation around one of its vertices.

Function : rosace(n,r,v) n = order of the rosette r = radius of the circumscribed circle c = True to use different colors for each rotation of the polygon v = True to draw the underlying web.


from math import *
from kandinsky import *
from turtle import *

def hsv2rgb(h,s=1,v=1):
  h,c=(h/180)%2,v*s
  x,m,k=c*(1-abs((h%(2/3))*3-1)),v-c,int(h*3)
  return round(255*(m+x*(k%3==1)+c*(k%5==0))),round(255*(m+c*(k==1 or k==2)+x*(k%3==0))),round(255*(m+x*(k%3==2)+c*(k==3 or k==4)))

def rosace(n=12,r=111,c=True,v=True):
  ne=n+(n%2)
  a=2*pi/n
  ca,sa,l=cos(a),sin(a),r*sin(a/2)
  a*=180/pi
  cb=191,191,191
  for d in range(n):
    if(v):
      color(cb)
      forward(r)
      forward(-r)
      left(a/2)
      forward(r)
      forward(-r)
      left(-a/2)
    if(c):cf=hsv2rgb(heading())
    else:cf=0,0,0
    for k in range(ne):
      if v and k>0 and k<ne/2:
        color(cb)
        x,y=position()
        x2,y2=ca*x-sa*y,sa*x+ca*y
        at,lt=atan2(y2-y,x2-x)*180/pi-heading(),sqrt((x2-x)**2+(y2-y)**2)
        left(at)
        forward(lt)
        forward(-lt)
        left(-at)
      color(cf)
      forward(l)
      left(a/(1+(n!=ne and (k==ne/2-1 or k==ne-1))))
    left(a)