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