Make all variables in french
This commit is contained in:
parent
af3fc330cc
commit
388ebdb2b9
248
betterTurtle.py
248
betterTurtle.py
@ -4,10 +4,9 @@
|
|||||||
A new turtle which is faster.
|
A new turtle which is faster.
|
||||||
|
|
||||||
>>> t = Turtle()
|
>>> t = Turtle()
|
||||||
>>> t.Init()
|
>>> t.init()
|
||||||
>>> t.setPosition((0, 0))
|
>>> t.set_position((0, 0))
|
||||||
>>> t.fractale.outline(6, 2, 5)
|
>>> t.fractal.outline(6, 2, 5)
|
||||||
>>> t.mainloop()
|
|
||||||
|
|
||||||
.. autoclass:: Figures
|
.. autoclass:: Figures
|
||||||
:members:
|
:members:
|
||||||
@ -25,7 +24,7 @@ import time
|
|||||||
from PIL import Image, ImageDraw
|
from PIL import Image, ImageDraw
|
||||||
|
|
||||||
|
|
||||||
class Figures():
|
class Figures:
|
||||||
"""A lot of function to create some well-know shapes
|
"""A lot of function to create some well-know shapes
|
||||||
|
|
||||||
:param master: turtle2 to use for draw
|
:param master: turtle2 to use for draw
|
||||||
@ -38,7 +37,7 @@ class Figures():
|
|||||||
self.canvas = master
|
self.canvas = master
|
||||||
|
|
||||||
def _outline_trace(self, number_of_iterations, length, number_of_sides):
|
def _outline_trace(self, number_of_iterations, length, number_of_sides):
|
||||||
"""Internal fonction to draw outline of a recursive shape
|
"""Internal function to draw outline of a recursive shape
|
||||||
|
|
||||||
:param number_of_iterations: Number of iteration used to draw
|
:param number_of_iterations: Number of iteration used to draw
|
||||||
:param length: Size of a single side
|
:param length: Size of a single side
|
||||||
@ -50,18 +49,18 @@ class Figures():
|
|||||||
:returns: Nothing
|
:returns: Nothing
|
||||||
:rtype: None"""
|
:rtype: None"""
|
||||||
|
|
||||||
# Stop the recursivity if the number of iteration is equal to zero
|
# Stop the recursion if the number of iteration is equal to zero
|
||||||
if number_of_iterations == 0:
|
if number_of_iterations == 0:
|
||||||
self.canvas.avance(length)
|
self.canvas.forward(length)
|
||||||
else:
|
else:
|
||||||
self._outline_trace(number_of_iterations - 1, length, number_of_sides)
|
self._outline_trace(number_of_iterations - 1, length, number_of_sides)
|
||||||
self.canvas.droite(360. / number_of_sides)
|
self.canvas.right(360. / number_of_sides)
|
||||||
self._outline_trace(number_of_iterations - 1, length, number_of_sides)
|
self._outline_trace(number_of_iterations - 1, length, number_of_sides)
|
||||||
self.canvas.droite(-360. / number_of_sides)
|
self.canvas.right(-360. / number_of_sides)
|
||||||
self._outline_trace(number_of_iterations - 1, length, number_of_sides)
|
self._outline_trace(number_of_iterations - 1, length, number_of_sides)
|
||||||
self.canvas.droite(-360. / number_of_sides)
|
self.canvas.right(-360. / number_of_sides)
|
||||||
self._outline_trace(number_of_iterations - 1, length, number_of_sides)
|
self._outline_trace(number_of_iterations - 1, length, number_of_sides)
|
||||||
self.canvas.droite(360. / number_of_sides)
|
self.canvas.right(360. / number_of_sides)
|
||||||
self._outline_trace(number_of_iterations - 1, length, number_of_sides)
|
self._outline_trace(number_of_iterations - 1, length, number_of_sides)
|
||||||
|
|
||||||
def regular_polygon(self, number_of_sides, length):
|
def regular_polygon(self, number_of_sides, length):
|
||||||
@ -76,9 +75,8 @@ class Figures():
|
|||||||
:rtype: None"""
|
:rtype: None"""
|
||||||
angle = 360. / number_of_sides
|
angle = 360. / number_of_sides
|
||||||
for i in range(number_of_sides):
|
for i in range(number_of_sides):
|
||||||
self.canvas.avance(length)
|
self.canvas.forward(length)
|
||||||
self.canvas.droite(angle)
|
self.canvas.right(angle)
|
||||||
i = i
|
|
||||||
|
|
||||||
def poly_repeat(self, length, number_of_side, density):
|
def poly_repeat(self, length, number_of_side, density):
|
||||||
"""Draw a repetition of a regular polygon
|
"""Draw a repetition of a regular polygon
|
||||||
@ -95,16 +93,15 @@ class Figures():
|
|||||||
angle = 360. / density
|
angle = 360. / density
|
||||||
for i in range(density):
|
for i in range(density):
|
||||||
self.regular_polygon(number_of_side, length)
|
self.regular_polygon(number_of_side, length)
|
||||||
self.canvas.droite(angle)
|
self.canvas.right(angle)
|
||||||
i = i
|
|
||||||
|
|
||||||
def outline(self, number_of_iteration, length, number_of_sides):
|
def outline(self, number_of_iteration, length, number_of_sides):
|
||||||
"""Draw outline of a recursive shape
|
"""Draw outline of a recursive shape
|
||||||
|
|
||||||
:param number_of_iterations: Number of iteration used to draw
|
:param number_of_iteration: Number of iteration used to draw
|
||||||
:param length: Length of a single side
|
:param length: Length of a single side
|
||||||
:param number_of_sides: Number of sides of the initial shape
|
:param number_of_sides: Number of sides of the initial shape
|
||||||
:type number_of_iterations: int
|
:type number_of_iteration: int
|
||||||
:type length: int
|
:type length: int
|
||||||
:type number_of_sides: int
|
:type number_of_sides: int
|
||||||
|
|
||||||
@ -112,8 +109,7 @@ class Figures():
|
|||||||
:rtype: None"""
|
:rtype: None"""
|
||||||
for i in range(number_of_sides):
|
for i in range(number_of_sides):
|
||||||
self._outline_trace(number_of_iteration, length, number_of_sides)
|
self._outline_trace(number_of_iteration, length, number_of_sides)
|
||||||
self.canvas.droite(360. / number_of_sides)
|
self.canvas.right(360. / number_of_sides)
|
||||||
i = i
|
|
||||||
|
|
||||||
def tree(self, length, angles, factor=1.5, min_size=5):
|
def tree(self, length, angles, factor=1.5, min_size=5):
|
||||||
"""Draw a tree recursively
|
"""Draw a tree recursively
|
||||||
@ -132,19 +128,20 @@ class Figures():
|
|||||||
if length < min_size:
|
if length < min_size:
|
||||||
return ""
|
return ""
|
||||||
else:
|
else:
|
||||||
self.canvas._etat["couleur"] = (int(length), int(length), int(length))
|
self.canvas._state["color"] = (int(length), int(length), int(length))
|
||||||
for angle in angles:
|
for angle in angles:
|
||||||
pos = self.canvas.getPosition()
|
pos = self.canvas.get_position()
|
||||||
anglebase = self.canvas.getAngle()
|
base_angle = self.canvas.get_angle()
|
||||||
self.canvas.droite(angle)
|
self.canvas.right(angle)
|
||||||
self.canvas.avance(length)
|
self.canvas.forward(length)
|
||||||
self._tree(length / factor, angles, factor=factor, min_size=min_size)
|
self._tree(length / factor, angles, factor=factor, min_size=min_size)
|
||||||
self.canvas.setPosition(pos)
|
self.canvas.set_position(pos)
|
||||||
self.canvas.setAngle(anglebase)
|
self.canvas.set_angle(base_angle)
|
||||||
|
|
||||||
def dragon(self, length, number_of_iteration, angle=1):
|
def dragon(self, length, number_of_iteration, angle=1):
|
||||||
"""Draw the dragon curve
|
"""Draw the dragon curve
|
||||||
|
|
||||||
|
:param angle: Start angle
|
||||||
:param length: Length of a side
|
:param length: Length of a side
|
||||||
:param number_of_iteration: Number of iteration for the curve
|
:param number_of_iteration: Number of iteration for the curve
|
||||||
:type length: int
|
:type length: int
|
||||||
@ -153,187 +150,168 @@ class Figures():
|
|||||||
:returns: Nothing
|
:returns: Nothing
|
||||||
:rtype: None"""
|
:rtype: None"""
|
||||||
if number_of_iteration == 0:
|
if number_of_iteration == 0:
|
||||||
self.canvas.avance(length)
|
self.canvas.forward(length)
|
||||||
else:
|
else:
|
||||||
self.dragon(length, number_of_iteration - 1, 1)
|
self.dragon(length, number_of_iteration - 1, 1)
|
||||||
self.canvas.gauche(angle * 90)
|
self.canvas.left(angle * 90)
|
||||||
self.dragon(length, number_of_iteration - 1, -1)
|
self.dragon(length, number_of_iteration - 1, -1)
|
||||||
|
|
||||||
def power(self, length, power, base=1.5):
|
def power(self, length, power, base=1.5):
|
||||||
k = base
|
k = base
|
||||||
L = []
|
list_powers = []
|
||||||
for i in range(power):
|
for i in range(power):
|
||||||
k = 10 * (k * 0.15 - int(k * 0.15))
|
k = 10 * (k * 0.15 - int(k * 0.15))
|
||||||
n = int((k - int(k)) * 10)
|
n = int((k - int(k)) * 10)
|
||||||
L.append(n)
|
list_powers.append(n)
|
||||||
for i in L:
|
for i in list_powers:
|
||||||
angle = 36 * i
|
angle = 36 * i
|
||||||
self.canvas.droite(angle)
|
self.canvas.right(angle)
|
||||||
self.canvas.avance(length)
|
self.canvas.forward(length)
|
||||||
|
|
||||||
def turning_tree(self, length, angles):
|
def turning_tree(self, length, angles):
|
||||||
while True:
|
while True:
|
||||||
self.tree(length, angles)
|
self.tree(length, angles)
|
||||||
time.sleep(0.1)
|
time.sleep(0.1)
|
||||||
i = 0
|
i = 0
|
||||||
for a in angles:
|
for _ in angles:
|
||||||
angles[i] += 1
|
angles[i] += 1
|
||||||
i += 1
|
i += 1
|
||||||
|
|
||||||
def _koch_curve(self, length):
|
def _koch_curve(self, length):
|
||||||
self.canvas.avance(length)
|
self.canvas.forward(length)
|
||||||
self.canvas.gauche(60)
|
self.canvas.left(60)
|
||||||
self.canvas.avance(length)
|
self.canvas.forward(length)
|
||||||
self.canvas.droite(120)
|
self.canvas.right(120)
|
||||||
self.canvas.avance(length)
|
self.canvas.forward(length)
|
||||||
self.canvas.gauche(60)
|
self.canvas.left(60)
|
||||||
self.canvas.avance(length)
|
self.canvas.forward(length)
|
||||||
|
|
||||||
def koch_curve(self, length, number_of_iteration):
|
def koch_curve(self, length, number_of_iteration):
|
||||||
if number_of_iteration > 0:
|
if number_of_iteration > 0:
|
||||||
self.koch_curve(length / 3., number_of_iteration - 1)
|
self.koch_curve(length / 3., number_of_iteration - 1)
|
||||||
self.canvas.gauche(60)
|
self.canvas.left(60)
|
||||||
self.koch_curve(length / 3., number_of_iteration - 1)
|
self.koch_curve(length / 3., number_of_iteration - 1)
|
||||||
self.canvas.droite(120)
|
self.canvas.right(120)
|
||||||
self.koch_curve(length / 3., number_of_iteration - 1)
|
self.koch_curve(length / 3., number_of_iteration - 1)
|
||||||
self.canvas.gauche(60)
|
self.canvas.left(60)
|
||||||
self.koch_curve(length / 3., number_of_iteration - 1)
|
self.koch_curve(length / 3., number_of_iteration - 1)
|
||||||
else:
|
else:
|
||||||
self._koch_curve(length)
|
self._koch_curve(length)
|
||||||
|
|
||||||
|
|
||||||
class Turtle():
|
class Turtle:
|
||||||
|
|
||||||
### Fonctions internes ###
|
@staticmethod
|
||||||
|
def _calc_center(size):
|
||||||
|
return size[0] / 2, size[1] / 2
|
||||||
|
|
||||||
def _calcCentre(self, taille):
|
def _forward(self, distance):
|
||||||
return (taille[0] / 2, taille[1] / 2)
|
AB = (distance * math.cos(math.radians(self._state.get("angle")))) + self._state.get("coordinate_x")
|
||||||
|
AC = (distance * math.sin(math.radians(self._state.get("angle")))) + self._state.get("coordinate_y")
|
||||||
|
self._forward_image(distance)
|
||||||
|
self._set_coordinates((AB, AC))
|
||||||
|
|
||||||
# self.canvas.update()
|
def _forward_image(self, distance):
|
||||||
|
AB = (distance * math.cos(math.radians(self._state.get("angle")))) + self._state.get("coordinate_x")
|
||||||
|
AC = (distance * math.sin(math.radians(self._state.get("angle")))) + self._state.get("coordinate_y")
|
||||||
|
self.draw.line((self.get_position('x') * self.resolution, self.get_position('y') * self.resolution,
|
||||||
|
AB * self.resolution, AC * self.resolution), fill=self._state.get("colour"))
|
||||||
|
|
||||||
def _avance(self, distance):
|
def _turn(self, angle):
|
||||||
AB = (distance * math.cos(math.radians(self._etat.get("angle")))) + \
|
self._set_angle(self._state.get("angle") + angle)
|
||||||
self._etat.get("coordx")
|
|
||||||
AC = (distance * math.sin(math.radians(self._etat.get("angle")))) + \
|
|
||||||
self._etat.get("coordy")
|
|
||||||
self._avanceIMG(distance)
|
|
||||||
self._setCoords((AB, AC))
|
|
||||||
# self.canvas.update()
|
|
||||||
# self.canvas.update()
|
|
||||||
|
|
||||||
def _avanceIMG(self, distance):
|
def _set_angle(self, angle):
|
||||||
AB = (distance * math.cos(math.radians(self._etat.get("angle")))) + \
|
self._state["angle"] = angle
|
||||||
self._etat.get("coordx")
|
while self._state.get("angle") >= 360:
|
||||||
AC = (distance * math.sin(math.radians(self._etat.get("angle")))) + \
|
self._state["angle"] = self._state.get("angle") - 360
|
||||||
self._etat.get("coordy")
|
|
||||||
self.draw.line(
|
|
||||||
(self.getPosition('x') *
|
|
||||||
self.resolution,
|
|
||||||
self.getPosition('y') *
|
|
||||||
self.resolution,
|
|
||||||
AB *
|
|
||||||
self.resolution,
|
|
||||||
AC *
|
|
||||||
self.resolution),
|
|
||||||
fill=self._etat.get("couleur"))
|
|
||||||
|
|
||||||
def _tourne(self, angle):
|
|
||||||
self._setAngle(self._etat.get("angle") + angle)
|
|
||||||
# self.canvas.update
|
|
||||||
|
|
||||||
def _setAngle(self, angle):
|
|
||||||
self._etat["angle"] = angle
|
|
||||||
while self._etat.get("angle") >= 360:
|
|
||||||
self._etat["angle"] = self._etat.get("angle") - 360
|
|
||||||
|
|
||||||
def _clear(self):
|
def _clear(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
### Fonction publiques ###
|
### Fonction publiques ###
|
||||||
|
|
||||||
def _clearIMG(self):
|
def _clear_img(self):
|
||||||
self.image = Image.new(
|
self.image = Image.new(
|
||||||
'1', (self._config.get("taille")), (255, 255, 255))
|
'1', (self._config.get("size")), (255, 255, 255))
|
||||||
self.draw = ImageDraw.Draw(self.image)
|
self.draw = ImageDraw.Draw(self.image)
|
||||||
|
|
||||||
def Init(self, titre="Turtle", taille=(
|
def __init__(self, titre="Turtle", size=(
|
||||||
400, 400), sauvegarde=False, resolution=10):
|
400, 400), resolution=10):
|
||||||
self._config = {"titre": titre,
|
self._config = {"titre": titre,
|
||||||
"taille": taille,
|
"size": size,
|
||||||
"tailleIMG": (taille[0] * resolution, taille[1] * resolution),
|
"size_IMG": (size[0] * resolution, size[1] * resolution),
|
||||||
"centre": self._calcCentre(taille),
|
"center": self._calc_center(size),
|
||||||
}
|
}
|
||||||
self._etat = {"angle": 0,
|
self._state = {"angle": 0,
|
||||||
"coordx": self._config.get("centre")[0],
|
"coordinate_x": self._config.get("center")[0],
|
||||||
"coordy": self._config.get("centre")[1],
|
"coordinate_y": self._config.get("center")[1],
|
||||||
"couleur": (0, 0, 0),
|
"colour": (0, 0, 0),
|
||||||
}
|
}
|
||||||
self.fractale = Figures(self)
|
self.fractal = Figures(self)
|
||||||
self.image = Image.new(
|
self.image = Image.new(
|
||||||
'RGB',
|
'RGB',
|
||||||
(self._config.get("tailleIMG")),
|
(self._config.get("size_IMG")),
|
||||||
(255,
|
(255,
|
||||||
255,
|
255,
|
||||||
255))
|
255))
|
||||||
self.draw = ImageDraw.Draw(self.image)
|
self.draw = ImageDraw.Draw(self.image)
|
||||||
self.resolution = resolution
|
self.resolution = resolution
|
||||||
|
|
||||||
def avance(self, distance):
|
def forward(self, distance):
|
||||||
self._avance(distance)
|
self._forward(distance)
|
||||||
|
|
||||||
def recule(self, distance):
|
def backward(self, distance):
|
||||||
self._avance(-distance)
|
self._forward(-distance)
|
||||||
if self.sauvegarde:
|
if self.sauvegarde:
|
||||||
self._avanceIMG(-distance)
|
self._forward_image(-distance)
|
||||||
|
|
||||||
def droite(self, angle):
|
def right(self, angle):
|
||||||
self._tourne(angle)
|
self._turn(angle)
|
||||||
|
|
||||||
def gauche(self, angle):
|
def left(self, angle):
|
||||||
self._tourne(-angle)
|
self._turn(-angle)
|
||||||
|
|
||||||
def goto(self, coordonnees):
|
def goto(self, coordinates):
|
||||||
self._setCoords(coordonnees)
|
self._set_coordinates(coordinates)
|
||||||
self.draw.line(coordonnees)
|
self.draw.line(coordinates)
|
||||||
|
|
||||||
def _setCoords(self, coordonnees):
|
def _set_coordinates(self, coordinates):
|
||||||
self._etat["coordx"] = coordonnees[0]
|
self._state["coordinate_x"] = coordinates[0]
|
||||||
self._etat["coordy"] = coordonnees[1]
|
self._state["coordinate_y"] = coordinates[1]
|
||||||
|
|
||||||
def clear(self):
|
def clear(self):
|
||||||
self._clear()
|
self._clear()
|
||||||
|
|
||||||
# ## Accès aux variables ##
|
# ## Accès aux variables ##
|
||||||
|
|
||||||
def setPosition(self, coordonnees):
|
def set_position(self, coordinates):
|
||||||
self._setCoords(coordonnees)
|
self._set_coordinates(coordinates)
|
||||||
|
|
||||||
def getPosition(self, typeCOORD=''):
|
def get_position(self, type_coord=''):
|
||||||
if typeCOORD == 'x':
|
if type_coord == 'x':
|
||||||
return self._etat.get("coordx")
|
return self._state.get("coordinate_x")
|
||||||
elif typeCOORD == "y":
|
elif type_coord == "y":
|
||||||
return self._etat.get("coordy")
|
return self._state.get("coordinate_y")
|
||||||
return (self._etat.get("coordx"), self._etat.get("coordy"))
|
return self._state.get("coordinate_x"), self._state.get("coordinate_y")
|
||||||
|
|
||||||
def setAngle(self, angle):
|
def set_angle(self, angle):
|
||||||
self._setAngle(angle)
|
self._set_angle(angle)
|
||||||
|
|
||||||
def getAngle(self):
|
def get_angle(self):
|
||||||
return self._etat.get("angle")
|
return self._state.get("angle")
|
||||||
|
|
||||||
def getEtat(self):
|
def get_state(self):
|
||||||
texte = ""
|
text = ""
|
||||||
for i in self._etat.items():
|
for i in self._state.items():
|
||||||
texte = texte + "\n" + str(i[0]) + ":" + str(i[1])
|
text = text + "\n" + str(i[0]) + ":" + str(i[1])
|
||||||
return texte
|
return text
|
||||||
|
|
||||||
def save(self, chemin, typeIMG=None):
|
def save(self, path, type_img=None):
|
||||||
self.image.save(chemin, typeIMG)
|
self.image.save(path, type_img)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
t = Turtle()
|
t = Turtle()
|
||||||
t.Init()
|
t.set_position((0, 0))
|
||||||
t.setPosition((0, 0))
|
t.fractal.outline(6, 2, 5)
|
||||||
t.fractale.outline(6, 2, 5)
|
|
||||||
t.save("test.bmp")
|
t.save("test.bmp")
|
||||||
|
Loading…
Reference in New Issue
Block a user