0

I would like to reproduce the beautiful Moroccan Mosaic's as seen here. The code is using Turtle. I would like to translate that to Tkinter. I have had some luck modifying the the code in an answer here to draw on to a canvas. Of course there is a lot missing as there are no lines being drawn from the origin. I don't know how to do this. One thought is that I could possibly overlay n polygon, each though rotated some 2 * pi/n degrees. This still leaves the problem of getting the line from the origin to the initial and final points for each polygon. I don't think this would quite solve everything but it is a great start.

enter image description here

Or maybe it is simpler to somehow to get the polygon's to rotate around the center?

from tkinter import *
import math
canvas_width = 400
canvas_height =400
python_green = "#476042"

def polygon(canvas,sides=10, radius=100, rotation=0, translation=None, outline=python_green, fill='White', width = 1):
   one_segment = math.pi * 2 / sides
   points = [
       (math.sin(one_segment * i + rotation) * radius + canvas_width/2,
        math.cos(one_segment * i + rotation) * radius + canvas_height/2)
       for i in range(sides)]

   if translation:
       points = [[sum(pair) for pair in zip(point, translation)]
                 for point in points]


   canvas.create_polygon(points, outline=outline, fill='', width=width)

master = Tk()
w = Canvas(master, width=canvas_width, height=canvas_height)
w.pack()
polygon(w,outline='green', width=2)

mainloop()
Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80
Relative0
  • 1,567
  • 4
  • 17
  • 23

1 Answers1

1

You can use the same code without the need to "translate" it to tkinter - all you need is to create a canvas and then use RawTurtle to draw on the canvas directly.

import turtle
import tkinter as tk

root = tk.Tk()
canvas = tk.Canvas(root,width=600,height=600)
canvas.pack()

#below code same from the linked source
myPen = turtle.RawTurtle(canvas) #changed from turtle.Turtle()
myPen.shape("arrow")
myPen.speed(1000)

def drawMosaic(color1, numberOfSides1, size1, color2, numberOfSides2, size2, numberOfIterations):
    for i in range(0, numberOfIterations):
        myPen.color(color1)
        for j in range(0, numberOfSides1):
            myPen.forward(size1)
            myPen.left(360 / numberOfSides1)
        myPen.color(color2)
        for k in range(0, numberOfSides2):
            myPen.forward(size2)
            myPen.left(360 / numberOfSides2)

        myPen.left(360 / numberOfIterations)

drawMosaic("#980C6B", 8, 80, "#DD6BB8", 5, 70, 20)

root.mainloop()
Henry Yik
  • 22,275
  • 4
  • 18
  • 40
  • That works, but I believe the OP's desire is to build his mosaic from rotated and translated polygons, rather than through turtle differential geometry. – Reblochon Masque Jun 27 '19 at 10:33
  • 1
    Might be - I'll just keep the answer for reference then. – Henry Yik Jun 27 '19 at 12:36
  • Sure, it is a great approach. – Reblochon Masque Jun 27 '19 at 12:46
  • I am getting an extra tk window when I include the code to turn the tracer off and make the pen invisible i.e. screen = turtle.Screen() myPen = turtle.Turtle(visible=False) screen.tracer(False) Do you know how to solve this? – Relative0 Jun 29 '19 at 09:30
  • The reason I am trying to use Polygons is that I have had a very difficult time trying to get Turtle in to an image (other than postscript) which would have a transparent background. Then I could import it in to PIL. There seem to be a few workarounds, none of which I can get to work. – Relative0 Jun 29 '19 at 09:34