L8. Iteration and Graphics Introduce Matlab Graphics More practice with iteration and boolean-type thinking Warm-up for functions and arrays.

Slides:



Advertisements
Similar presentations
Lab # 03- SS Basic Graphic Commands. Lab Objectives: To understand M-files principle. To plot multiple plots on a single graph. To use different parameters.
Advertisements

Laboratory Study II October, Java Programming Assignment  Write a program to calculate and output the distance traveled by a car on a tank of.
RAPTOR Syntax and Semantics By Lt Col Schorsch
Introduction to Graphing Using MATLAB. Line Graphs  Useful for graphing functions  Useful for displaying data trends over time  Useful for showing.
PHY-102 SAPIntroductory GraphicsSlide 1 Introductory Graphics In this section we will learn how about how to draw graphics on the screen in Java:  Drawing.
Insight Through Computing 13. More on Arrays Square Bracket Notation Subscripts Plotting and color Built-In Functions: ginput, fill, sum, axis.
Representing a Game Board In a game, we represent the action taking place using an array – In a very simple game, we use individual variables to represent.
Mrs. Chapman. Tabs (Block Categories) Commands Available to use Script Area where you type your code Sprite Stage All sprites in this project.
6. More on the For-Loop Using the Count Variable Developing For-Loop Solutions.
Loop Structures.
Insight Through Computing 8. More Practice with Iteration and Conditionals Through Graphics For-Loop Problems Introduce While-Loops.
Chapter 5 Linear Inequalities and Linear Programming Section 2 Systems of Linear Inequalities in Two Variables.
Graphs of the Linear Function: Graphing Calculator TI-84 Plus
2 What is pyGame? A set of Python modules to make it easier to write games. –home page: –documentation:
CGMB 314 Intro to Computer Graphics Fill Area Primitives.
1 iSee Player Tutorial Using the Forest Biomass Accumulation Model as an Example ( Tutorial Developed by: (
ELG 3120 Signal and System Analysis 1 Introduction to MATLAB TAs Wei Zhang Ozgur Ekici (Section A)(Section B) ELG 3120 Lab Tutorial 1.
Find the probability and odds of simple events.
Chapter 2 - Algorithms and Design
Solving and Graphing Inequalities in Two Variables.
Computer Science 112 Fundamentals of Programming II Graphics Programming.
Lecture 15: Intro to Graphics Yoni Fridman 7/25/01 7/25/01.
BACS 287 Programming Logic 1. BACS 287 Programming Basics There are 3 general approaches to writing programs – Unstructured – Structured – Object-oriented.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Vectors and the Geometry of Space 2015
Using Pro-Engineer to Create 3 Dimensional Shapes Kevin Manner Kevin Manner Tim Reynolds Tim Reynolds Thuy Tran Thuy Tran Vuong Nguyen Vuong Nguyen.
Chapter 4 Working with Frames. Align and distribute objects on a page Stack and layer objects Work with graphics frames Work with text frames Chapter.
Graphic Basics in C ATS 315. The Graphics Window Will look something like this.
Loops & Graphics IP 10 Mr. Mellesmoen Recall Earlier we wrote a program listing numbers from 1 – 24 i=1 start: TextWindow.WriteLine(i) i=i+1 If.
1 Building Java Programs Supplement 3G: Graphics These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold,
PowerPoint Basics Tutorial 3: Graphics In this tutorial we’ll be looking at graphics, and the various types of illustrations that can be included in a.
Homework 3 Can you divide 36 balls into 9 groups such that each group has odd number of balls? 36 ÷ 9 = 4, 4 is even What if we change things around a.
Algorithms Writing instructions in the order they should execute.
GRAPHICS MODULE 14 STUDY BOOK. Graphic commands SCREEN - puts the screen into graphics mode WINDOW - allows scaling of the screen LINE - 3 formats –LINE.
Creating a Simple Game in Scratch Barb Ericson Georgia Tech June 2008.
CSC 1010 Programming for All Lecture 7 Input, Output & Graphics.
1Barnett/Ziegler/Byleen Finite Mathematics 12e Learning Objectives for Section 5.2  The student will be able to solve systems of linear inequalities graphically.
EGR 115 Introduction to Computing for Engineers 3D animation in MATLAB Monday 17 Nov 2014 EGR 115 Introduction to Computing for Engineers.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Computer Programming Modeling a Passive Solar Home.
1 Arrays of Arrays An array can represent a collection of any type of object - including other arrays! The world is filled with examples Monthly magazine:
Introduction to Drafting and Design In order to begin our drawing we have to set the drawing limits or the paper size.
Video in Macromedia Flash (Optional) – Lesson 121 Video in Macromedia Flash (Optional) Lesson 12.
CS305j Introduction to Computing Simple Graphics 1 Topic 11 Simple Graphics "What makes the situation worse is that the highest level CS course I've ever.
Introduction to Graphics. Graphical objects To draw pictures, we will use three classes of objects: –DrawingPanel : A window on the screen. –Graphics.
Midterm: Question 1 (35%) (30 minutes) Write an assembly program to draw a rectangle. – (5%) Show a message to ask for information of the rectangle – (5%)
Scratch for Interactivity Dr. Ben Schafer Department of Computer Science University of Northern Iowa.
PyGame - Unit 1 PyGame Unit – – Introduction to PyGame.
CS100A, Fall 1998, Lecture 201 CS100A, Fall 1998 Lecture 20, Tuesday Nov 10 More Matlab Concepts: plotting (cont.) 2-D arrays Control structures: while,
Lesson 5-4 Example Example 1 Draw an array to model and find 21 ÷ 3. 1.Write the answer if you know it. Otherwise, draw an array.
Creating a Simple Game in Scratch Barb Ericson Georgia Tech May 2009.
CompSci 4 Java 4 Apr 14, 2009 Prof. Susan Rodger.
How to use MATLAB (using M-files) Double click this icon To start Matlab 6.5.
Introduction to Illustrator
Course Information Do not rely on color to distinguish curves (due to black-white printout). Use different line styles to distinguish curves (solid, dashed,
Lecture 25: Exploring data
Lesson One: The Beginning Chapter 1: Pixels Learning Processing Daniel Shiffman Presentation by Donald W. Smith Graphics from
Images Presentation Name Course Name Unit # – Lesson #.# – Lesson Name
Basic Graphics Drawing Shapes 1.
به نام مهربانترین In the name of the most compassionate
The WHITE Cross Lesson 2.
CSc 110, Spring 2018 Lecture 9: Parameters, Graphics and Random
The WHITE Corners Lesson 3.
Go to =>
Here are four triangles. What do all of these triangles have in common
L13. More on Arrays Square Bracket Notation Subscripts
Images Presentation Name Course Name Unit # – Lesson #.# – Lesson Name
Plotting Signals in MATLAB
Creating a Simple Game in Scratch
Presentation transcript:

L8. Iteration and Graphics Introduce Matlab Graphics More practice with iteration and boolean-type thinking Warm-up for functions and arrays.

We will Draw Pictures Using Three User-Defined* Graphics Functions DrawRect Rectangles DrawDisk Circles DrawStar 5-pointed Stars *As opposed to built-in functions like sqrt and rem.

Example

Why? Get more practice with loops and if. Warm-up to using Matlab’s graphics functions which use arrays Warm-up to writing your own user- defined functions

DrawRect DrawRect(-1,-2,6,3,’y’)

DrawDisk DrawDisk(1,3,4,’r’)

DrawStar DrawStar(1,3,4,’g’)

A Simple 3-line Script Draw a black square. Then a magenta disk. Then a yellow star.

Solution close all figure axis equal off hold on DrawRect(-1,-1,2,2,'k') DrawDisk(0,0,1,'m') DrawStar(0,0,1,'y') hold off

A General Framework close all figure axis equal off hold on hold off shg Fragment involving DrawRect ’s, DrawDisk ’s and/or DrawStar ’s

Some Matlab Graphics Commands % Close all figure windows… close all % Open a new figure window figure % Set x and y scaling to be the % same and do not display axes axis equal off % “Add-in mode” is on… hold on IGNORE FOR NOW

Some Matlab Graphics Commands % Exit the add-in mode… hold off % Bring the figure window to the % front… shg IGNORE FOR NOW

Syntax Let’s look at the rules associated with using DrawRect, DrawDisk, and DrawStar.

DrawRect(-1,-2,6,3,’y’) DrawRect Input 3 (-1,-2) 6 A yellow 6x3 rectangle at (-1,-2) ‘y’ Output

DrawRect DrawRect(,,,, ) Coordinates of lower left corner length width color

Color Options White ‘w’ Black ‘k’ Red ‘r’ Blue ‘b’ Green ‘g’ Yellow ‘y’ Magenta ‘m’ Cyan ‘c’

Question Time What is the area of the red region? for k=1:3 if rem(k,2)==1 DrawRect(0,0,k,k,’r’) % red else DrawRect(0,0,k,k,’w’) % white end A. 1 B. 3 C. 6 D. 9

DrawDisk(-1,-2,6,’m’) DrawDisk Input A magenta disk with radius 6 & center at (-1,-2) -2 6 ‘m’ Output (-1,-2) 6

DrawDisk DrawDisk(,,, ) Coordinates of center radius color

DrawStar(-1,-2,6,’g’) DrawStar Input A green star with radius 6 and center at (-1,-2) -2 6 ‘g’ Output (-1,-2) 6

DrawStar DrawDisk(,,, ) Coordinates of center radius color

Now Let’s Solve 3 Problems Star Array Nested Stars PaintBall

The Framework (Again) close all figure axis equal off hold on hold off shg Fragment involving DrawRect ’s, DrawDisk ’s and/or DrawStar ’s We Focus On this part

Blue 12-by-6 rectangle with lower left corner at (0,0). White radius-1 stars with centers at (2,4), (4,4), (6,4), (8,4) (10,4) (2,2), (4,2), (6,2), (8,2) (10,2) Problem 1: Star Array

 Top Row: y = 4  Bot Row: y = The x-value in the k-th column is 2k  column  x-value Preliminary Notes

Pseudocode Draw the blue rectangle for k = 1:5 Draw the kth star in the top row end for k = 1:5 Draw the kth star in the bottom row end

Pseudocode Draw the blue rectangle for k = 1:5 Draw the kth star in the top row Draw the kth star in the bottom row end

Refinement Draw the blue rectangle Draw the blue 12-by-6 rectangle with lower left corner at (0,0). DrawRect(0,0,12,6,’b’)

Refinement Draw the k-th star in the top row Draw a white star with radius 1 and center (2k,4) DrawStar(2*k,4,1,’w’)

Refinement Draw the k-th star in the bottom row Draw a white star with radius 1 and center (2k,2) DrawStar(2*k,2,1,’w’)

Solution DrawRect(0,0,12,6,’b’) for k = 1:5 DrawStar(2*k,4,1,’w’) DrawStar(2*k,2,1,’w’) end

Problem 2: Nested Stars Draw black square with center (0,0) & side 2.1 Draw radius 1 magenta star with center (0,0) Draw nested sequence of yellow and magenta stars, each with center (0,0) and radius reduced by a factor of 1.2. Stop when radius <=.1

Preliminary Notes Star #1 : DrawStar(0,0,1,’m’) Star #2 : DrawStar(0,0,1/1.2,’y’) Star #3 : DrawStar(0,0,1/(1.2)^2,’m’) Star #4 : DrawStar(0,0,1/(1.2)^3,’y’)

Preliminary Notes R = 1 Star #1 : DrawStar(0,0,R,’m’) R = R/1.2 Star #2 : DrawStar(0,0,R,’y’) R = R/1.2 Star #3 : DrawStar(0,0,R,’m’) R = R/1.2 Star #4 : DrawStar(0,0,R,’y’)

Pseudocode Draw the Black Square R = 1; k = 1; Repeat while R > 0.1 Draw the k-th star Update R and k

Refinement Draw the black square Draw a black square with side 2.1 And center (0,0) s = 2.1; DrawRect(-s/2,-s/2,s,s,’k’)

Refinement R = 1; k = 1; Repeat while R >.1 Draw the k-th star Update R and k R = 1; k = 1; while R >.1 Draw the k-th star R = R/1.2; k= k+1; end

Refinement if k is odd Magenta, radius R. center (0,0) otherwise Yellow, radius R, center (0,0) Draw the kth star

Refinement if rem(k,2)==1 DrawStar(0,0,R,’m’) else DrawStar(0,0,R,’y’) end

Solution R = 1; k = 1; while R >.1 if rem(k,2)==1 DrawStar(0,0,R,’m’) else DrawStar(0,0,R,’y’) end R = R/1.2; k= k+1; end

Problem 3: Paintball Draw a black unit square with lower left corner at (0,0). Draw a radius.03 disk with center randomly located in square.

Problem 3: Paintball If the disk is entirely in square, randomly color it ‘c’, ‘y’, or ‘m’ with equal probability. Otherwise, color it White. Repeat this process until 50 white disks drawn.

DrawDisk(-1,-2,6,’m’) DrawDisk Input A magenta disk with radius 6 & center at (-1,-2) -2 6 ‘m’ Output (-1,-2) 6

Preliminary Notes Dot: radius r, center (x,y) y+r > 1 x+r > 1 x-r < 0 y-r < 0 “Edge Hits”

Preliminary Notes How we simulate a 3-way random event? If ink = rand(1), then 1/3 the time we have: 0 < ink < 1/3 1/3 the time we have: 1/3 <= ink < 2/3 1/3 the time we have: 2/3 <= ink < 1 Check the inequalities and do the right thing.

Pseudocode Draw black square. Repeat until 50 white disks: Locate a random disk. If the disk is in the square then randomly color it’c’, ‘y’, or ‘m’. Otherwise, color it ‘w’ end

Refinement “Draw the black square” Draw a unit black square With lower left corner at (0,0) DrawRect(0,0,1,1,’k’)

Pseudocode DrawRect(0,0,1,1,’k’) EdgeHits = 0; while EdgeHits < 50 Locate a random disk. If the disk is in the square then randomly color it’c’, ‘y’, or ‘m’. Otherwise, color it ‘w’ EdgeHits = EdgeHits + 1; end

Variable Definition We use a variable EdgeHits to keep track of the number of disks that intersect the square’s boundary.

Refinement “Locate a random disk” x = rand; y = rand; The center (x,y) satisfies 0<x<1 and 0<y<1.

If the disk is in the square then randomly color it’c’, ‘y’, or ‘m’. Otherwise, color it ‘w’ EdgeHits = EdgeHits + 1; end Refinement How do we check that?

Dot: radius r, center (x,y) y+r > 1 x+r > 1 x-r < 0 y-r < 0 None of these conditions hold.

Dot: radius r, center (x,y) y+r <= 1 x+r <= 1 x-r >= 0 y-r >= 0 All of these conditions hold.

y+r <= 1 x+r <= 1 x-r >= 0 y-r >= 0 All of these conditions hold. y+r =0 && y-r>=0

Question Time Want to count upper right corner hits. Which of these boolean conditions guarantees that (1,1) is covered? (i) x + r >= 1 && y + r >= 1 (ii) x + y >= 2 - 2*r A. Neither C. Both B. (i) only D. (ii) only

AnswerTime The case x = 1, y = 1 – r , fools Condition (ii). (i) x + r >= 1 && y + r >= 1 (ii) x + y >= 2 – 2*r A. Neither C. Both B. (i) only D. (ii) only

If the disk is in the square then randomly color it ’c’, ‘y’, or ‘m’. Otherwise, color it ‘w’ EdgeHits = EdgeHits + 1; end Refinement How do we do that?

randomly color it ‘c’, ‘y’, or ‘m’ Refinement 1/3 of the time the disk should be ‘m’ 1/3 of the time the disk should be ‘y’ 1/3 of the time the disk should be ‘c’

Refinement ink = rand(1); if ink < 1/3; DrawDisk(x,y,r,’m’) elseif 1/3 <= ink && ink < 2/3 DrawDisk(x,y,r,’y’) else DrawDisk(x,y,r,’c’) end