def test_rectangle(): from zyxxy_shape_functions import draw_a_rectangle, clone_a_shape from zyxxy_canvas import create_canvas_and_axes from zyxxy_utils import is_the_same_contour #, is_the_same_point axes = create_canvas_and_axes(canvas_width = 18, canvas_height = 12) def compare_contours(s1, s2, **kwargs): global n p1 = s1.get_xy() if isinstance(s1, Shape) else s1 p2 = s2.get_xy() if isinstance(s2, Shape) else s2 assert is_the_same_contour(p1=p1, p2=p2, **kwargs), "failed test " + str(n) print("succeeded test " + str(n)) n+=1 # same rectangle, then with 0 turn, then stretched and turned, then with alternative diamonds r = draw_a_rectangle(ax=axes, centre_x=5, centre_y=15, width=10, height=2) r0 = draw_a_rectangle(ax=axes, centre_x=5, centre_y=15, width=10, height=2, turn=0) r2 = clone_a_shape(r) r2.stretch(stretch_x=1/5, stretch_y=5) r2.turn(turn=3) r3 = draw_a_rectangle(ax=axes, left=0, top=16, width=10, height=2) r4 = draw_a_rectangle(ax=axes, right=10, bottom=14, width=10, height=2) for other_r, kwargs in [[r0, {}], [r2, {'start_1':1}], [r3, {}], [r4, {}]]: try: compare_contours(s1=r, s2=other_r, **kwargs) except: raise Exception(n, r.get_xy()) # make sure shift works for other_r_init in [r, r3, r4]: other_r = clone_a_shape(other_r_init) other_r.shift(shift=[101, 202]) compare_contours(s1=r.get_xy() + [101, 202], s2=other_r) # make sure flipping works r001 = clone_a_shape(r) ; r001.flip_upside_down() r301 = clone_a_shape(r3); r301.flip_upside_down() r401 = clone_a_shape(r4); r401.flip_upside_down() compare_contours(r001.get_xy() + [0, 2], r301.get_xy()) compare_contours(r001.get_xy() + [0,-2], r401.get_xy()) # make sure rotation works r31 = clone_a_shape(r3); r31.turn(turn=6) r41 = clone_a_shape(r4); r41.turn(turn=6) compare_contours(r31.get_xy() + [20, -4], r41.get_xy()) # make sure parameters update works r502 = clone_a_shape(r); r502.stretch(stretch_x=1/5, stretch_y=50) r5033 = clone_a_shape(r);r5033.update_shape_parameters(width=2, height=100) compare_contours(r5033, r502) with pytest.raises(Exception): x = 1 / 0
def example_us_flag(axes=None): create_canvas_and_axes(canvas_width = 19*13*2, canvas_height = 10*13*2, axes=axes) for stripe_nb in range(7): draw_a_rectangle(centre_x=19*13, centre_y=10+2*20*stripe_nb, width=19*13*2, height=20, colour='red') draw_a_rectangle(centre_x=100, centre_y=190, width=200, height=140, colour='navy') for row in range(9): # there are 9 rows of stars # let's define how many stars are in this row # and where is the centre_x of the first star if row%2==0: # if row number is even stars_qty=6 first_star_centre_x = 15 else: # if row number is odd stars_qty=5 first_star_centre_x = 33 # centre_y=260-(row+1)*14 because we are counting star rows from the top for column in range(stars_qty): draw_a_star(centre_x=first_star_centre_x+column*34, centre_y=260-(row+1)*14, radius_1=9, radius_2=3, ends_qty=5, colour='white') show_drawing_and_save_if_needed()
def example_cuban_flag(axes=None): create_canvas_and_axes(canvas_width=30, canvas_height=20, axes=axes) draw_a_rectangle(left=0, centre_y=10, width=30, height=4, colour='blue') draw_a_rectangle(left=0, bottom=0, width=30, height=4, colour='blue') draw_a_rectangle(left=0, top=20, width=30, height=4, colour='blue') draw_a_triangle(tip_x=17, tip_y=10, width=20, height=17, colour='red', turn=9) draw_a_star(centre_x=6, centre_y=10, radius_1=3, radius_2=1, ends_qty=5, colour='white') show_drawing_and_save_if_needed()
def example_croc(axes=None): ######################################################### ## CREATING THE DRAWING! ## ######################################################### # Creating the canvas! ## axes = create_canvas_and_axes(canvas_width = 190, canvas_height = 100, background_colour = 'PastelBlue', axes = axes) ####################################################### # Now let's draw the shapes! ## left_body = 30 bottom_body = 30 right_body = 120 right_head = 180 top_body = 50 top_head = bottom_body+15 tail_right = 35 + left_body tail_width = 10 centre_backside = 50 r_nostrils = 3 lip_y = 0.5 * (top_head + bottom_body) lip_r = 3 teeth_length = 5 nb_teeth = 7 leg_width = 10 leg_length = 15 feet_height = 5 feel_length = 20 ####################################################### # Now let's draw the shapes! ## set_default_patch_style(colour='BrightGreen') # legs leg_layer_nb = new_layer() for shift, colour in [[0, 'green'], [-17, 'BrightGreen']]: for x in [50, 100]: # draw a leg draw_a_rectangle(ax=axes, left=x+shift, top=bottom_body, height=leg_length, width=leg_width, colour=colour) # draw a feet draw_a_sector(ax=axes, centre_x=x+shift+feel_length/2, centre_y=bottom_body-leg_length, radius=feel_length/2, stretch_y=feet_height/(feel_length/2), angle_start=9, angle_end=15, colour=colour) # body body_layer_nb = new_layer() draw_a_rectangle(ax=axes, left=left_body, bottom=bottom_body, height=top_body-bottom_body, width=right_body-left_body) # backside backside_clip_contour = build_a_circle(radius=centre_backside-bottom_body) + [left_body, centre_backside] draw_a_sector( ax=axes, centre_x=left_body, centre_y=(2*centre_backside-bottom_body-tail_width+top_body)/2, radius=(2*centre_backside-bottom_body-tail_width+top_body)/2-bottom_body, radius_2=(2*centre_backside-bottom_body-tail_width-top_body)/2, angle_start=6, angle_end=12, clip_outline=backside_clip_contour) # tail draw_a_rectangle(ax=axes, left=left_body, top=2*centre_backside-bottom_body, height=tail_width, width=tail_right-left_body) draw_a_sector(ax=axes, centre_x=tail_right, centre_y=2*centre_backside-bottom_body, radius=tail_width,angle_start=3, angle_end=6) # lower teeth and jaw upper_teeth = build_a_zigzag(width=right_head - (right_body-lip_r), height=teeth_length, angle_start=3, nb_segments=2*nb_teeth) + [right_body-lip_r, lip_y] lower_teeth = upper_teeth[1:-1] + [0, teeth_length] draw_a_polygon(ax=axes, contour=lower_teeth, colour='white') draw_a_rectangle(ax=axes, left=right_body, bottom=bottom_body, height=lip_y-bottom_body, width=right_head-right_body) # upper jaw upper_jaw_layer_nb = new_layer() draw_a_rectangle(ax=axes, left=right_body, bottom=lip_y, height=top_head-lip_y, width=right_head-right_body) # ... and the eyes, white circles with black circles on top eye_y = top_body for radius, colour in [[8, 'BrightGreen'], [5, 'white'], [3, 'black']]: for eye_x in [right_body, right_body+12]: draw_a_circle(ax=axes, centre_x=eye_x, centre_y=eye_y, radius=radius, colour=colour) # ... and the eyelids. Saving them in array for future use eyelids = [] eyelid_width = 12 for eye_x in [right_body, right_body+12]: for td in [-1, 1]: mid_y = td * eyelid_width / 2 eyelid = draw_an_eye(ax=axes, centre_x=eye_x, centre_y=eye_y, width=eyelid_width, depth_1=mid_y, depth_2=mid_y, colour='green') eyelids.append(eyelid) # ... and the nostrils nostril_y = top_head for nostril_x in [right_head-r_nostrils, right_head-3*r_nostrils]: draw_a_circle(ax=axes, centre_x=nostril_x, centre_y=nostril_y, radius=r_nostrils) draw_a_circle(ax=axes, centre_x=nostril_x, centre_y=nostril_y, radius=1, colour='green') # ... and the teeth and the lip # teeth draw_a_polygon(ax=axes, contour=upper_teeth, colour='white') # upper lip lipline_arc = build_an_arc(radius=lip_r, angle_start=6, angle_end=9) + [right_body-lip_r, lip_y+lip_r] lipline = link_contours([[right_head, lip_y]], lipline_arc) draw_a_broken_line(ax=axes, contour=lipline, colour='green', linewidth=2) upper_jaw_diamond = [right_body-lip_r, lip_y+lip_r] return leg_layer_nb, body_layer_nb, upper_jaw_layer_nb, eyelids, upper_jaw_diamond
def example_yellow_cat(axes=None, cat_colour = 'Yellow', background_colour = 'SeaWave'): ####################################################### ## CREATING THE DRAWING! ## ####################################################### ## Creating the canvas! ## axes = create_canvas_and_axes(canvas_width = 120, canvas_height = 120, background_colour = background_colour, axes = axes, make_symmetric = 'x') ####################################################### # Now let's draw the shapes! ## # settings set_default_outline_style(linewidth=2) set_default_line_style(linewidth=2) set_default_patch_style(colour=cat_colour)#darkorange # the tail tail_length = [30, 22, 20, 12, 10] for i, tl in enumerate(tail_length): triangle_tail = draw_a_triangle(ax=axes, tip_x=38, tip_y=30, height=tl, width=tl/2, turn=7) if i%2 == 1: triangle_tail.set_style(colour='black') # the body height_body = [60, 57, 54, 38, 35, 19, 16] for i, bh in enumerate(height_body): triangle_body = draw_a_triangle(ax=axes, tip_x=0, tip_y=60, height=bh, width=bh, turn=6) if i%2 == 1: triangle_body.set_style(colour='black') # the feet draw_a_triangle(ax=axes, tip_x=-12, tip_y=20, height=20, width=20, turn=6) draw_a_triangle(ax=axes, tip_x= 12, tip_y=20, height=20, width=20, turn=6) head_layer = new_layer() # the ears et = 4.5 draw_a_triangle(ax=axes, tip_x=-30, tip_y=114, height=50, width=30, turn=et) draw_a_triangle(ax=axes, tip_x=-22, tip_y=106, height=40, width=24, colour='black', turn=et) draw_a_triangle(ax=axes, tip_x= 30, tip_y=114, height=50, width=30, turn=-et) draw_a_triangle(ax=axes, tip_x= 22, tip_y=106, height=40, width=24, colour='black', turn=-et) #head head_circle = draw_a_circle(ax=axes, centre_x=0, centre_y=85, radius=25) #from this line, the default colour is black set_default_patch_style(colour='black') # neck draw_a_circle(ax=axes, centre_x=0, centre_y=60, radius=1) neck_coords = [0, 60] # stripes on the face # vertical stripes for c, b in [[-10, 101], [-5, 100], [0, 101]]: draw_a_rectangle(ax=axes, centre_x=c, bottom=b, width=3, height=20, clip_outline = head_circle) # horizontal stripes for c, x in [[70, 16], [75, 15], [80, 18]]: draw_a_rectangle(ax=axes, right=-x, centre_y=c, width=20, height=3, clip_outline = head_circle) draw_a_rectangle(ax=axes, left=+x, centre_y=c, width=20, height=3, clip_outline = head_circle) # eyes eyes = [] for centre_x in [-12, 12]: eye_white= draw_an_eye(ax=axes, centre_x=centre_x, centre_y=90, width=16, depth_1=-8, depth_2=8, colour='white') draw_an_ellipse(ax=axes, centre_x=centre_x, centre_y=90, width=8, height=16, colour='BrightGreen', clip_outline = eye_white) draw_a_circle(ax=axes, centre_x=centre_x, centre_y=90, radius=3, colour='black', clip_outline = eye_white) # the following line is needed for animation eyes.append(eye_white) # nose draw_a_triangle(ax=axes, tip_x=0, tip_y=72, height=8, width=10, colour='BubblePink') # smile draw_a_segment(ax=axes, start_x=0, start_y=72, length=7, turn=6) smile = draw_a_smile(ax=axes, centre_x=0, centre_y=69, depth=4, width=20) return head_layer, neck_coords, eyes, smile
from zyxxy_canvas import create_canvas_and_axes, show_drawing_and_save_if_needed from zyxxy_all_EXAMPLES import example_british_flag from zyxxy_shape_functions import draw_a_rectangle ####################################################### # Creating the canvas! ## create_canvas_and_axes(canvas_width = 18, canvas_height = 12, tick_step = 1, title = "Flag Of The U.K.", model = example_british_flag, outlines_colour = "cyan") ####################################################### # Now let's draw the shapes! ## draw_a_rectangle(centre_x=9, centre_y=2, width=22, height=1, colour='red', turn=1) draw_a_rectangle(centre_x=3, centre_y=2, width=22, height=2, colour='red', turn=3) draw_a_rectangle(centre_x=9, centre_y=6, width=8, height=6, colour='navy') draw_a_rectangle(centre_x=9, centre_y=6, width=22, height=3, colour='white', turn=6) show_drawing_and_save_if_needed()
def example_british_flag(axes=None): create_canvas_and_axes(canvas_width=18, canvas_height=12, axes=axes) draw_a_rectangle(centre_x=9, centre_y=6, width=18, height=12, colour='navy') draw_a_rectangle(centre_x=9, centre_y=6, width=22, height=3, colour='white', turn=7) draw_a_rectangle(centre_x=9, centre_y=6, width=22, height=1, colour='red', turn=7) draw_a_rectangle(centre_x=9, centre_y=6, width=22, height=3, colour='white', turn=5) draw_a_rectangle(centre_x=9, centre_y=6, width=22, height=1, colour='red', turn=5) draw_a_rectangle(centre_x=9, centre_y=6, width=18, height=4, colour='white') draw_a_rectangle(centre_x=9, centre_y=6, width=4, height=12, colour='white') draw_a_rectangle(centre_x=9, centre_y=6, width=18, height=2, colour='red') draw_a_rectangle(centre_x=9, centre_y=6, width=2, height=12, colour='red') show_drawing_and_save_if_needed()
def example_finnish_flag(axes=None): create_canvas_and_axes(canvas_width=36, canvas_height=22, axes=axes) draw_a_rectangle(centre_x=13, bottom=0, width=6, height=22, colour='midnightblue') draw_a_rectangle(left=0, centre_y=11, width=36, height=6, colour='midnightblue') show_drawing_and_save_if_needed()
from zyxxy_canvas import create_canvas_and_axes, show_drawing_and_save_if_needed from zyxxy_all_EXAMPLES import example_cuban_flag from zyxxy_shape_functions import draw_a_triangle, draw_a_rectangle, draw_a_star ####################################################### # Creating the canvas! ## create_canvas_and_axes( canvas_width = 30, canvas_height = 20, tick_step = 2, title = "Flag Of Cuba", model = example_cuban_flag, outlines_colour = "cyan") ####################################################### # Now let's draw the shapes! ## draw_a_triangle(tip_x=5, tip_y=10, width=20, height=17, colour='red', turn=3) draw_a_rectangle(centre_x=15, centre_y=12, width=30, height=4, colour='blue') draw_a_star(centre_x=15, centre_y=8, radius_1=3, radius_2=1, ends_qty=8, colour='white') show_drawing_and_save_if_needed()
# grass for i in range(25): draw_a_triangle(tip_x=aquarium_side + 2 + i / 3, tip_y=height_ground + height_grass, width=0.5, height=height_grass, colour='springgreen', turn=6, opacity=0.7) set_default_outline_style(linewidth=2) # a table draw_a_rectangle(left=0, bottom=0, width=width, height=height_ground, colour='MidBrown') # an aquarium draw_a_square(left=0, bottom=height_ground, side=aquarium_side, colour='PastelBlue') # fish draw_a_crescent(centre_x=10, centre_y=13, width=8, depth_1=1, depth_2=-1,
title = "Excited Cat", model = 'https://i.pinimg.com/564x/de/21/fd/de21fdb9fa70d9d54ce7e4c0f07910d5.jpg', model_zoom = 1.3, background_colour='aliceblue') ####################################################### # Now let's draw the shapes! ## # the body set_default_patch_style(colour="yellow", layer_nb=2) set_default_outline_style(linewidth=4, layer_nb=1) body = draw_an_egg(ax=ax, power=3, height_widest_point=3, width=4, height=5, tip_x=0, tip_y=14, stretch_x=3, stretch_y=-3) for y in [1.5, 6, 10.5]: draw_a_rectangle(ax=ax, width=16, height=2, centre_x=0, centre_y=y, opacity=0.3, clip_outline=body, colour='orange', outline_linewidth=0) # the head set_default_patch_style( layer_nb=4) set_default_outline_style(layer_nb=3) head = draw_a_circle(ax=ax, centre_x=0, centre_y=14, radius=4.2) # the ears, using the "eye" shape draw_a_crescent(ax=ax, width=6, depth_1=1, depth_2=3, centre_x=-4, centre_y=16, turn=9) draw_a_crescent(ax=ax, width=6, depth_1=1, depth_2=3, centre_x=4, centre_y=16, turn=3) new_layer() # the stripes
body_shape[:, 1] += body_height + body_bottom body = draw_a_polygon(contour=body_shape) #gradient rectangles gradient_colours = create_gradient_colours(rgb_start=[0, 0, 255], rgb_end=(255, 0, 255)) gradient_bottom = body_bottom - tail_coeff * tail_height grh = (ear_height + body_height + tail_coeff * tail_height) / (len(gradient_colours) - 1) for i, gc in enumerate(gradient_colours): draw_a_rectangle(width=30, height=grh, centre_x=0, centre_y=gradient_bottom + i * grh, opacity=1, clip_outline=body, colour=gc, outline_linewidth=0) draw_a_rectangle(width=30, height=grh, centre_x=0, centre_y=gradient_bottom + i * grh, opacity=1, clip_outline=tail_shape, colour=gc, outline_linewidth=0) # a vertical line draw_a_segment(start_x=0, start_y=eye_y, turn=6, length=1.5)
####################################################### # Creating the canvas! ## create_canvas_and_axes(canvas_width=19 * 13 * 2, canvas_height=10 * 13 * 2, tick_step=50, title="Flag Of The U.S.A.", model=example_us_flag) #set_diamond_size_factor(0.75) ####################################################### # Now let's draw the shapes! ## for stripe_nb in range(3): draw_a_rectangle(left=0, centre_y=15 + 30 * stripe_nb, width=19 * 13 * 2, height=15, colour='red') draw_a_rectangle(left=0, centre_y=190, width=90, height=80, colour='navy') for row in range(7): # there are 9 rows of stars # let's define how many stars are in this row # and where is the centre_x of the first star if row % 2 == 0: # if row number is even stars_qty = 9 first_star_centre_x = 15 else: # if row number is odd stars_qty = 8 first_star_centre_x = 33 # centre_y=260-(row+1)*14 because we are counting star rows from the top
from zyxxy_canvas import create_canvas_and_axes, show_drawing_and_save_if_needed from zyxxy_all_EXAMPLES import example_finnish_flag from zyxxy_shape_functions import draw_a_rectangle ####################################################### # Creating the canvas! ## create_canvas_and_axes(canvas_width=36, canvas_height=22, tick_step=2, title="Flag Of Finland", model=example_finnish_flag, outlines_colour="red") ####################################################### # Now let's draw the shapes! ## draw_a_rectangle(left=22, centre_y=11, width=6, height=10, colour='midnightblue') show_drawing_and_save_if_needed()