COSC 1P02 Introduction to Computer Science 7.1 Cosc 1P02 Week 7 Lecture slides "There are two ways...

10
COSC 1P02 Introduction to Computer Science 7.1 Cosc 1P02 Week 7 Lecture slides "There are two ways of constructing a software design; one way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult." - C. A. R. Hoare

Transcript of COSC 1P02 Introduction to Computer Science 7.1 Cosc 1P02 Week 7 Lecture slides "There are two ways...

Page 1: COSC 1P02 Introduction to Computer Science 7.1 Cosc 1P02 Week 7 Lecture slides "There are two ways of constructing a software design; one way is to make.

COSC 1P02

Introduction to Computer Science 7.1

Cosc 1P02

Week 7 Lecture slides

"There are two ways of constructing a software design; one way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult."

- C. A. R. Hoare

Page 2: COSC 1P02 Introduction to Computer Science 7.1 Cosc 1P02 Week 7 Lecture slides "There are two ways of constructing a software design; one way is to make.

COSC 1P02

Introduction to Computer Science 7.2

Choosing a Color

Set color of each pixel Sequence through all pixels

Picture is a collection for-each

ASCIIPrompter to get RGB values Pixel class

methods to get & set color of pixels Example

Color constructor allows setting RGB Black & white Gray

if R=G=B we get gray different shades with different intensities

Page 3: COSC 1P02 Introduction to Computer Science 7.1 Cosc 1P02 Week 7 Lecture slides "There are two ways of constructing a software design; one way is to make.

COSC 1P02

Introduction to Computer Science 7.4

Grayscale

Convert a picture into grayscale essentially what we call a black and white picture shades of gray indicating the luminance of the picture

Gray if R=G=B set RGB to same value

average of RGB values Color methods Example Note method may change the color of the pixels of the

Picture parameter they stay changed just like a method moving a turtle changes its position

Page 4: COSC 1P02 Introduction to Computer Science 7.1 Cosc 1P02 Week 7 Lecture slides "There are two ways of constructing a software design; one way is to make.

COSC 1P02

Introduction to Computer Science 7.6

Color Blindness

Inability do distinguish some colors Red-green color blindness affects 5% of males

deuteranopia – red and green receptors trigger at same level

test Example

set red and green values to same level (as green) PictureDisplayer(picture) constructor creates

displayer that fits the picture and places the picture on displayer

note display now declared in method not class depends on picture loaded local to method constructor does no initialization

Page 5: COSC 1P02 Introduction to Computer Science 7.1 Cosc 1P02 Week 7 Lecture slides "There are two ways of constructing a software design; one way is to make.

COSC 1P02

Introduction to Computer Science 7.9

Random Paint

Create a picture by setting randomly chosen pixels to randomly chosen colors

Start with a white canvas (picture) Select a random pixel

pixels are arranged in two dimensions (width x height) can access a particular pixel via getPixel(x,y)

x is x-coordinate (column) y is y-coordinate (row) top left corner is (0,0) bottom right corner is (height,width)

Example picture attributes (width, height) random color

random R, G, and B values

Page 6: COSC 1P02 Introduction to Computer Science 7.1 Cosc 1P02 Week 7 Lecture slides "There are two ways of constructing a software design; one way is to make.

COSC 1P02

Introduction to Computer Science 7.10

Rotating a Picture

90 degree rotation to the left Rotated picture has different dimensions Pixel movement

e.g. 3x2 picture (x,y) -> (y,width-1-x)

Sequence through pixels of original producing new by setting pixels

nested for loops y indexes through rows x indexes through columns

Example displayer that sizes to picture need two displayers

pictures different shapes garbage collection

when display no longer references first displayer with no references, the storage for the object is

reclaimed

Page 7: COSC 1P02 Introduction to Computer Science 7.1 Cosc 1P02 Week 7 Lecture slides "There are two ways of constructing a software design; one way is to make.

COSC 1P02

Introduction to Computer Science 7.12

Redeye Correction

Flash reflecting from back of eye shows red in picture Change red to black (pupil)

cannot change all red pixels or real red gets changed Select area to apply correction

use some tool (e.g. PictureInspector) to determine area

What is red? many shades of red, not only (255,0,0) want colors close to red

Color distance distance in 3 dimensional (R,G,B) space

Example constant declaration (TOLERANCE) indexing over subrange of picture distance method

sqrt & pow from Math

Page 8: COSC 1P02 Introduction to Computer Science 7.1 Cosc 1P02 Week 7 Lecture slides "There are two ways of constructing a software design; one way is to make.

COSC 1P02

Introduction to Computer Science 7.15

Picture Methods

method meaning o = new Picture() constructor: creates a picture object

loading pixels from a file selected via a file open dialog

o = new Picture(width,height) constructor: creates a picture object with specified height and width with all pixels white

i = getHeight() returns height (in pixels) of picture p = getPixel(x,y) returns pixel in column x of row y i = getWidth() returns width (in pixels) of picture save() present file save dialog to allow user

to save picture as modified

Page 9: COSC 1P02 Introduction to Computer Science 7.1 Cosc 1P02 Week 7 Lecture slides "There are two ways of constructing a software design; one way is to make.

COSC 1P02

Introduction to Computer Science 7.16

PictureDisplayer Methods

method meaning o = new PictureDisplayer() constructor: creates a new displayer with

canvas 200x200 o = new PictureDisplayer(pic) constructor: creates a new displayer with

canvas to fit pic and with pic placed on displayer

close() wait until user presses Close button and close displayer

placePicture(pic) place pic on the displayer waitForUser() wait until user presses OK before

continuing

Page 10: COSC 1P02 Introduction to Computer Science 7.1 Cosc 1P02 Week 7 Lecture slides "There are two ways of constructing a software design; one way is to make.

COSC 1P02

Introduction to Computer Science 7.17

The end