Skip to content

Commit

Permalink
Merge pull request #40 from fredokun/empty-image
Browse files Browse the repository at this point in the history
gfx lib updated
  • Loading branch information
fredokun authored Oct 13, 2017
2 parents c0c7455 + c1c9589 commit b066e31
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
6 changes: 6 additions & 0 deletions mrpython/StudentRunner.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,18 @@
def install_locals(locals):
#locals = { k:v for (k,v) in locs.items() }
locals['draw_line'] = studentlib.gfx.image.draw_line
locals['line'] = studentlib.gfx.image.draw_line
locals['draw_triangle'] = studentlib.gfx.image.draw_triangle
locals['triangle'] = studentlib.gfx.image.draw_triangle
locals['fill_triangle'] = studentlib.gfx.image.fill_triangle
locals['filled_triangle'] = studentlib.gfx.image.fill_triangle
locals['draw_ellipse'] = studentlib.gfx.image.draw_ellipse
locals['ellipse'] = studentlib.gfx.image.draw_ellipse
locals['fill_ellipse'] = studentlib.gfx.image.fill_ellipse
locals['filled_ellipse'] = studentlib.gfx.image.fill_ellipse
locals['overlay'] = studentlib.gfx.image.overlay
locals['underlay'] = studentlib.gfx.image.underlay
locals['empty_image'] = studentlib.gfx.image.empty_image
locals['show_image'] = studentlib.gfx.img_canvas.show_image
return locals

Expand Down
3 changes: 3 additions & 0 deletions mrpython/studentlib/gfx/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ def fill_ellipse(x0, y0, x1, y1, color="black"):
# TODO: check arguments
return Image((('fill-ellipse', x0, y0, x1, y1, color),))

def empty_image():
return Image((('empty-image', True),))

def overlay(*images):
# TODO : check arguments
objects = ()
Expand Down
2 changes: 2 additions & 0 deletions mrpython/studentlib/gfx/img_canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ def draw_image(self, image):
elif elem[0] == 'draw-ellipse':
x1, y1, x2, y2, color = elem[1:]
self.draw_ellipse(x1, y1, x2, y2, color)
elif elem[0] == 'empty-image':
pass # nothing to do
else:
raise ValueError("Cannot draw image element: unsupported '{}' type (elem={})".format(elem[0], elem))

Expand Down
59 changes: 59 additions & 0 deletions tests/pyramide.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@

def filled_rectangle(x, y, largeur, hauteur, couleur):
""" float * float * float * float * str -> Image
Hypothèse: (largeur > 0.0) and (hauteur > 0.0)
Retourne l'image d'un rectangle au coin bas
gauche en (x,y) et de la largeur et hauteur
spécifiées.
"""

return overlay(filled_triangle(x, y, x + largeur, y, x + largeur, y + hauteur, couleur),
filled_triangle(x + largeur, y + hauteur, x, y + hauteur, x, y, couleur))


def pyramide(x, y, largeur_min, largeur_max, hauteur, nb_etages, couleur):
""" float * float * float * float * float * int * str -> Image
Hypothèse: (largeur_min > 0.0) and (largeur_max > largeur_min) and (hauteur > 0.0)
Hypothèse: nb_etages > 0
Retourne un monument égyptien bien dimensionné.
"""

# x_incr : float (incrément en x à chaque étape)
x_incr = ((largeur_max - largeur_min) / nb_etages) / 2.0

# y_incr : float (incrément en y à chaque étape)
y_incr = hauteur / nb_etages

# x_courant : float
x_courant = x

# y_courant : float
y_courant = y

# l_courant : float
l_courant = largeur_max

# img : Image
img = empty_image()

# i : int (etage)
for i in range(nb_etages):
img = overlay(img,
filled_rectangle(x_courant, y_courant, l_courant, y_incr, couleur))
x_courant = x_courant + x_incr
y_courant = y_courant + y_incr
l_courant = l_courant - 2 * x_incr

return img

#r1 = filled_rectangle (-0.5, -0.5, 0.8, 0.6, "black")
#show_image(r1)

pyra = pyramide(-0.5, -0.5, 0.2, 1.2, 1.4, 32, "blue")
#print(pyra)
show_image(pyra)



0 comments on commit b066e31

Please sign in to comment.