Learning outcomes. COMPSCI 101 Principles of Programming. Drawing 2D shapes using Characters. Printing a Row of characters

Similar documents
CISC 1600, Lab 3.1: Processing

CISC 1600, Lab 2.1: Processing

Name: Partner: Python Activity 9: Looping Structures: FOR Loops

Guided Problem Solving

CISC 1600 Lecture 3.1 Introduction to Processing

CSE11 Lecture 20 Fall 2013 Recursion

CS3240 Human-Computer Interaction Lab Sheet Lab Session 2

Loops. Variable Scope Remapping Nested Loops. Donald Judd. CS Loops 1. CS Loops 2

1. Complete these exercises to practice creating user functions in small sketches.

CompSci 105 S2 C - ASSIGNMENT TWO -

For loops, nested loops and scopes. Jordi Cortadella Department of Computer Science

Name: Class: Date: 2. I have four vertices. I have four right angles and all my sides are the same length.

Outline. For loops, nested loops and scopes. Calculate x y. For loops. Scopes. Nested loops. Algorithm: repeated multiplication x x x x

Creating a Java class with a main method

Area and Perimeter Name: Date:

COMP Summer 2015 (A01) Jim (James) Young jimyoung.ca

Iteration in Programming

Student Outcomes. Classwork. Opening Exercises 1 2 (5 minutes)

Nested Loops ***** ***** ***** ***** ***** We know we can print out one line of this square as follows: System.out.

Control Flow: Loop Statements

Warm-up. Translations Using arrow notation to write a rule. Example: 1) Write a rule that would move a point 3 units to the right and 5 units down.

mith College Computer Science CSC103 How Computers Work Week 7 Fall 2017 Dominique Thiébaut

3. Area and perimeter.notebook November 13, All rectangles with area 12cm 2 have the same perimeter. True or false?

Chapter 4: Control structures. Repetition

Student Outcomes. Lesson Notes. Classwork. Opening Exercise (3 minutes)

MOVING A VERTEX. How must Kirsten move vertex A to be sure that the area of her new triangle is

Question/Answer Booklet COMPSCI 101 THE UNIVERSITY OF AUCKLAND. SECOND SEMESTER, 2016 Campus: City COMPUTER SCIENCE. Principles of Programming

Geometry. Set 1. Daily Practice And Answer Keys

Chapter 4: Control structures

Lesson 22: Surface Area

LAB 5: SELECTION STATEMENTS

Warping & Blending AP

`Three sides of a 500 square foot rectangle are fenced. Express the fence s length f as a function of height x.

Find the area and perimeter of these shapes: Draw another shape with area a smaller perimeter. a larger perimeter.

Nets and Drawings for Visualizing Geometry. Unit 1 Lesson 1

Outline. CIS 110: Introduction to Computer Programming. Announcements. For Loops. Redundancy in Patterns. A Solution with Our Current Tools

Answer Key Lesson 5: Area Problems

CSCI 135 Exam #0 Fundamentals of Computer Science I Fall 2012

Lesson 2 Fractions on a Number Line

Drawing a Circle. 78 Chapter 5. geometry.pyde. def setup(): size(600,600) def draw(): ellipse(200,100,20,20) Listing 5-1: Drawing a circle

Lab 2. Hanz Cuevas Velásquez, Bob Fisher Advanced Vision School of Informatics, University of Edinburgh Week 3, 2018

CIS 110: Introduction to Computer Programming

CSE120 Wi18 Final Review

Applications of Integration. Copyright Cengage Learning. All rights reserved.

CAS London CPD Day February 16

Meet #2. Park Forest Math Team. Self-study Packet

Informatics 1 Functional Programming Lecture 9. Algebraic Data Types. Don Sannella University of Edinburgh

EDEXCEL NATIONAL CERTIFICATE UNIT 4 MATHEMATICS FOR TECHNICIANS OUTCOME 1

Solution Notes. COMP 151: Terms Test

Measures of Dispersion

UNIT 19 Similarity Lesson Plan 1

Three-Dimensional Shapes

CCBC Math 081 Geometry Section 2.2

Nested Loops. Chapter 11

Skill. 69 Graph Ordered Pairs. (First Quadrant) Using Skill 69 COMMON ERRORS. 285 Holt Mathematics. OBJECTIVE Graph ordered pairs (first quadrant)

An Introduction to Processing

Self-Teach Exercises: Getting Started Turtle Python

EEN118 LAB TWO. 1. A Five-Pointed Star.

CS 139 Practice Midterm Questions #2

LabVIEW Case and Loop Structures ABE 4423/6423 Dr. Filip To Ag and Bio Engineering, Mississippi State University

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #16 Loops: Matrix Using Nested for Loop

Assignment #2: Simple Java Programs Due: 11AM PST on Monday, April 23 rd

Mathematics Success Grade 6

HO-1: INTRODUCTION TO FIREWORKS

Three Dimensional Figures. TeacherTwins 2015

Mid Unit Review. Of the four learning outcomes for this unit, we have covered the first two. 1.1 LO1 2.1 LO2 LO2

Step 1: Create A New Photoshop Document

+. n is the function parameter and the function returns the sum.

Lesson 24: Surface Area

Applications. 44 Stretching and Shrinking

BUILD YOUR VOCABULARY

WORD Creating Objects: Tables, Charts and More

GM1.1 Consolidation Worksheet Answers

Lesson 10.1 Enrich Line Art Use geometric fi gures to draw each of the following Enrich E76 Grade 4

Inserting a table plus all related tips on the table

(a) Assume that in a certain country, tax is payable at the following rates:

CHAPTER SOL PROBLEMS

Directions: Working with your group, cut out the shapes below and sort them into 3 groups based on similar characteristics.

Decision Making in C

= 3 + (5*4) + (1/2)*(4/2)^2.

2. What are the measures of the 3 angles in the second triangle? 3. What is the relationship between the angles of each triangle?

Measuring Triangles. 1 cm 2. 1 cm. 1 cm

CK-12 Geometry: Similar Polygons

Test #2 October 8, 2015

S206E Lecture 13, 5/22/2016, Grasshopper Math and Logic Rules

S8.6 Volume. Section 1. Surface area of cuboids: Q1. Work out the surface area of each cuboid shown below:

University of Cape Town ~ Department of Computer Science Computer Science 1015F ~ June Exam


Simple Java YEAH Hours. Brahm Capoor and Vrinda Vasavada

Area. Domain 4 Lesson 25. Getting the Idea

Vocabulary: Looking For Pythagoras

MD5-26 Stacking Blocks Pages

Lesson 26: Volume of Composite Three-Dimensional Objects

Khan Academy JavaScript Study Guide

mith College Computer Science CSC103 How Computers Work Week 6 Fall 2017 Dominique Thiébaut

Describe Plane Shapes

Graphical User Interfaces

Downloaded from

Staff Microsoft VISIO Training. IT ESSENTIALS Creating Flowcharts Using Visio 2013 (ST562) June 2015

Algebra 1. Standard 11 Operations of Expressions. Categories Combining Expressions Multiply Expressions Multiple Operations Function Knowledge

Transcription:

Learning outcomes At the end of this lecture, students should be able to draw 2D shapes using characters draw 2D shapes on a Canvas COMPSCI 101 Principles of Programming Lecture 25 - Using the Canvas widget to draw rows and columns of shapes 2 Drawing 2D shapes using Characters We write programs to draw 2D shapes using characters (e.g. asterisks) The way to conceptualize this is to think about the shape as a sequence of rows and to think carefully about how to describe the i th row, e.g. drawing a triangle. These kinds of problems will help you learn how to write loops by finding appropriate formulas to describe each iteration of the loop in terms of the iteration variable. Printing a Row of characters The following example prints only one row of # characters using a SINGLE for loop. def print_row(number_of_cols): for j in range(number_of_cols): print('#', end="") Print a new line character (i.e. ) Example00.py ### 3 4

Printing Multiple Rows of Characters To create rows and columns of shapes we need nested loops That is, loops within loops to execute lines of code. draw a single character The first (outer) loop is looping through rows, the inner loop is looping through columns. As we go through each column of a given row, we print an asterisk. The result is that we can build any size rectangle we want. 1) Printing a Rectangle of Characters To print a rectangle, we need two parameters: number of columns = 3 columns for in loop draw 3 asterisks The outer for loop contains two statements: 1) inner for loop 2) : move cursor to the next line The inner for loop contains one statement: statement which prints a character 5 6 1) Printing a Rectangle of Characters To print a rectangle, we need two parameters: number of columns = 3 columns Example01.py for in range 4 rows for in range 3 columns draw 1 asterisk def print_square(number_of_rows, number_of_cols): for i in range(number_of_rows): for j in range(number_of_cols): print('', end="") 2) Printing a right-angle Triangle To print a right-angle triangle, we need one parameter: The outer for loop contains two statements: 1) inner for loop 2) : move cursor to the next line The inner for loop contains one statement: for in loop 4 rows if it is the first row, draw 1 asterisk if it is the second row, draw 2 asterisks if it is the i th row, draw i asterisks statement which prints one or more character(s) 7 8

Example02.py 2) Printing a right-angle Triangle To print a right-angle triangle, we need one parameter: Task: Exercise 1 Complete the following code fragment to print for in range 4 rows for in range row = 0, number of columns = 1 row = 1, number of columns = 2 row = 2, number of columns = 3 def print_right_angle_triangle(number_of_rows): for row in range(number_of_rows): Case 1: Case 2: def print_right_angle_triangle(number_of_rows): for row in range(number_of_rows): for column in range(row+1): print('', end="") 9 10 Program skeleton All the programs in this lecture have the following code skeleton. The draw_shapes() function is different for each exercise. def main(): root = Tk() root.title("my first Canvas") root.geometry("400x300+10+20") a_canvas = Canvas(root) a_canvas.config(background="pink") #some colour a_canvas.pack(fill=both, expand = True) draw_shapes(a_canvas) root.mainloop() main() Drawing 2D shapes on a Canvas In order to draw a 2D shape (e.g. multiples of squares) on a canvas, we need: The number of rows and number of columns Size of each square (size=50) Start point (x_margin, y_margin) = (20, 30) Nested loops Coordinates of the top left corner of each square Example: 1 st (20, 30), (70, 30), (120,30) 2 nd (20, 80), (70,80), (120, 80) (70,30) Size of the squares is 50 pixels by 50 pixels (120,30) 11 12

Example 3 Let s look at ONE row of the shape FIRST: x = 20 (starts at 20 on each row) 13 Coordinates of the first square: (20, 30, 70, 80) Second square: (70, 30, 120, 80) Third square(120, 30, 170, 80) (70,30) (120,30) for j in range(number_of_colums): rect = (x_left, y_down, x_left + size, y_down modify x-coordinate of the square in each iteration + size) Now, we look at the entire shape. We need nested loops! The outer loop iterates number of rows. 14 1 st row : coordinate of the top left corner: (20, 30) and the next one is (70, 30) and (120, 30) 2 nd row: coordinate of the top left corner: (20, 80) and the next one is (70, 80) and (120, 80) 3 rd row: : coordinate of the top left corner: (20, 130) and the next one is (70, 130) and (120, 130) (20,130) (20,80) We put them together: Outer loop: for i in range(number_of_rows): draw a single shape change the x value to move along the row change the y value ready for the next row down reset the starting position of each row for j in range(number_of_columns): Inner loop: adjust the y y_down += size coordinates 15 reset the starting position of each row y_down += size y_down += size Algorithm: 16 (20,80) (20,130) (70, 30) (70, 80) (70, 130) draw a single shape change the x value to move along the row change the y value ready for the next row down Example03.py (120, 30) (120, 80) (120, 130)

Example 4 What should we do in order to draw the following shapes? First row: Fill, draw, fill, draw Second row: Draw, fill, draw, fill Third row Fill, draw, fill, draw a_canvas.create_rectangle(rect, fill="blue") Command to create the filled square 4) Using a Boolean variable First row: True, False, True, False Second row: False, True, False, True Third row True, False, True, False False False False True True True True False False False False True True True False False False True True True True 17 18 4) What is the output of the following code fragment? We put them together: is_filled = True for i in range(5): print(is_filled, end=" ") is_filled = not is_filled True False True False True i is_filled True 0 False 1 True 2 False 3 True 4 False Outer loop: x-margin, y-margin, width, height, first_in_row_filled=true set up y-position set up x-positon, is_filled draw a single shape change the x value to move along the row modify the is_filled boolean change the y value ready for the next row down modify the first_in_row_filled boolean Inner loop 19 20

Example04.py Example 5 Nested Loops: first_in_row_filled = True for i in range(number_of_rows): is_filled = first_in_row_filled for j in range(number_in_row): if is_filled: a_canvas.create_rectangle(rect, fill="blue") else: x_left = x_left + size is_filled = not is_filled Steps: 1 st iteration of outer loop -> repeat 5 iterations in the inner loop 2 nd iteration of outer loop -> repeat 4 iterations in the inner loop 3 rd iteration of outer loop -> repeat 3 iterations in the inner loop 4 th iteration of outer loop -> repeat 2 iterations in the inner loop 5 th iteration of outer loop -> repeat 1 iteration in the inner loop y_down = y_down + size first_in_row_filled = not first_in_row_filled 21 22 is_circle boolean Exercise 2 first_is_circle is_circle Draw the canvas True True False True False True False False True False True True True False True False False True True True def draw_shapes(a_canvas): number_of_rows = 6 size = 30 y_down = 0 left_hand_side = size for number_along_row in range(1, number_of_rows + 1): for j in range(number_along_row): rect = (x_left + 2, y_down + 2, x_left + size 2, y_down + size 2) a_canvas.create_oval(rect, fill="blue") x_left = x_left + size 2 gridlines are of size 30 pixels 23 24 y_down = y_down + size

def draw_shapes(a_canvas): number_of_rows = 5 left_hand_side = 0 y_down = 0 size = 50 first_is_circle = True for number_to_do in range(1, number_of_rows + 1): is_circle = first_is_circle for j in range(number_to_do): rect = (x_left + 3, y_down + 3, x_left + size 3, y_down + size 3) if is_circle: a_canvas.create_oval(rect, fill="blue") else: x_left = x_left + size 2 is_circle = not is_circle gridlines are of size 50 pixels 25 y_down = y_down + size first_is_circle = not first_is_circle