Reputation: 125
My task is to print symmetric hexagons on a line.
I have to create a function like this : def hexagone(point, longueur,c):
Where point is the X-axis and Y-axis. "longueur" is the distance between the center of a hexagon and any of his corners . I do use this distance to calculate and obtain every x / y coordinate for my turtle.gotto().
"C" represents the color for each part of the hexagone.
So far I can perfectly draw the hexagon at point 0,0 :
import turtle
from math import pi, sin, cos
def hexagone(point, longueur,c):
l= point[0] + longueur
turtle.up()
turtle.goto(point)
turtle.color(c[0]) #black
turtle.down()
turtle.begin_fill()
turtle.goto(l * cos(4 / 3 * pi), l * sin(4 / 3 * pi))
turtle.goto(l * cos(5 / 3 * pi), l * sin(5 / 3 * pi))
turtle.goto(l * cos(0), l * sin(0))
turtle.goto(point)
turtle.end_fill()
turtle.color(c[1]) #blue
turtle.begin_fill()
turtle.goto(l * cos(0), l * sin(0))
turtle.goto(l * cos(pi / 3), l * sin(pi / 3))
turtle.goto(l * cos(pi * 2 / 3), l * sin(pi * 2 / 3))
turtle.goto(point)
turtle.end_fill()
turtle.color(c[2]) #red
turtle.begin_fill()
turtle.goto(l * cos(pi * 2 / 3), l * sin(pi * 2 / 3))
turtle.goto(-l, point[1])
turtle.goto(l * cos(4 / 3 * pi), l * sin(4 / 3 * pi))
turtle.goto(point)
turtle.end_fill()
turtle.up()
return True
hexagone((0,0), 50, ("black",("blue"),("red")))
turtle.done()
The first hexagon is perfect. I print it at origin(0,0) .
However, if I try to simply increment the point[0]->(x) by, let's say 100 :
i += 100 hexagone((i,0), 50, ("black",("blue"),("red")))
it's printing hexagon over hexagon and getting bigger and bigger.
Something is obliviously wrong with my formulas and it's scaling incorrectly .
Does anyone know how could I make this work in order to archive the result from the example?
Upvotes: 1
Views: 276
Reputation: 41895
I'd take a different approach to solving this -- since the hexagon is composed of three segments of the same shape, define that shape as the cursor and draw the hexagon via rotation and stamping:
from turtle import Screen, Turtle
from math import pi, cos, sin
LONGUEUR = 25
COLORS = ['red', 'blue', 'black']
def define_shape(longueur):
polygon = ((0, 0), \
(longueur * cos(pi * 2 / 3), longueur * sin(pi * 2 / 3)), \
(- longueur, 0), \
(longueur * cos(4 / 3 * pi), longueur * sin(4 / 3 * pi)), \
)
screen.register_shape('diamond', polygon)
def hexagone(point, colors):
turtle.setposition(point)
for color in colors:
turtle.left(120)
turtle.color(color)
turtle.stamp()
screen = Screen()
screen.mode('logo')
define_shape(LONGUEUR)
turtle = Turtle(shape='diamond', visible='False')
turtle.speed('fastest') # because I have no patience
turtle.penup()
for x in range(-15 * LONGUEUR, 16 * LONGUEUR, 3 * LONGUEUR):
hexagone((x, 0), COLORS)
screen.exitonclick()
We might be able to go further and make the entire hexagon, colors included, the cursor by defining a compound shape. Then we would only need to move the cursor and stamp where needed.
Upvotes: 2
Reputation: 142814
If you want to display in (100,50)
then you have to use (0,0)
for all calculations and after calculations you have to add 100
to every x
and 50
to every y
which you use in goto()
- so you create it for (0,0)
and then you move it by (100,50)
In code I use 0
instead of point[0]
and point[1]
because I make calculation for (0,0)
And later I get x,y
from point
(which will be 100
and 50)
and use in every goto()
goto(... +x, ... +y)
Full code
import turtle
from math import pi, sin, cos
def hexagone(point, longueur, c):
l = 0 + longueur # 0 instead of point[0]
x, y = point
turtle.up()
turtle.goto(point)
turtle.color(c[0]) #black
turtle.down()
turtle.begin_fill()
turtle.goto(l * cos(4 / 3 * pi )+x, l * sin(4 / 3 * pi)+y)
turtle.goto(l * cos(5 / 3 * pi)+x, l * sin(5 / 3 * pi)+y)
turtle.goto(l * cos(0)+x, l * sin(0)+y)
turtle.goto(point)
turtle.end_fill()
turtle.color(c[1]) #blue
turtle.begin_fill()
turtle.goto(l * cos(0)+x, l * sin(0)+y)
turtle.goto(l * cos(pi / 3)+x, l * sin(pi / 3)+y)
turtle.goto(l * cos(pi * 2 / 3)+x, l * sin(pi * 2 / 3)+y)
turtle.goto(point)
turtle.end_fill()
turtle.color(c[2]) #red
turtle.begin_fill()
turtle.goto(l * cos(pi * 2 / 3)+x, l * sin(pi * 2 / 3)+y)
turtle.goto(-l+x, 0+y) # 0 instead of point[1]
turtle.goto(l * cos(4 / 3 * pi)+x, l * sin(4 / 3 * pi)+y)
turtle.goto(point)
turtle.end_fill()
turtle.up()
return True
hexagone((0,0), 50, ("black",("blue"),("red")))
hexagone((100,0), 50, ("black",("blue"),("red")))
hexagone((0,100), 50, ("black",("blue"),("red")))
hexagone((100,100), 50, ("black",("blue"),("red")))
turtle.done()
Upvotes: 2