computer science 111

20
Computer Science 111 Fundamentals of Programming I Colors, Tuples, Nested Loops, and Grids

Upload: myrna

Post on 07-Jan-2016

15 views

Category:

Documents


1 download

DESCRIPTION

Computer Science 111. Fundamentals of Programming I Colors, Tuples, Nested Loops, and Grids. Representing Colors. When a color is digitized, it is represented as a triple of three integer values, (r, g, b) The integers represent the intensities of red, green, and blue mixed into the color - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Computer Science 111

Computer Science 111

Fundamentals of Programming IColors, Tuples, Nested Loops, and Grids

Page 2: Computer Science 111

Representing Colors

• When a color is digitized, it is represented as a triple of three integer values, (r, g, b)

• The integers represent the intensities of red, green, and blue mixed into the color

• Also called the RGB system

Page 3: Computer Science 111

Range of RGB Values

• Each integer ranges from 0 to 255, for 256 distinct intensity values

• Thus, there are 2563 or 224 or 16,777,216 distinct colors

• 0 is the absence of intensity, 255 is the total saturation of intensity

Page 4: Computer Science 111

Some Example Colors

Color RGB value

Black (0, 0, 0)

Red (255, 0, 0)

Green (0, 255, 0)

Blue (0, 0, 255)

Yellow (255, 255, 0)

Gray (127, 127, 127)

White (255, 255, 255)

Page 5: Computer Science 111

Tuples

• A tuple is an ordered collection of two or more elements

• Represented in Python as

• Often used to hold three values, such as RGB color components

(<element-1>, . . ., <element-n>)

Page 6: Computer Science 111

Examples

Build a tuple of three integers and look it up

>>> red = (255, 0, 0)

>>> red(255, 0, 0)

>>> (r, g, b) = red>>> r255>>> g0>>> b0>>> (r, g, b)(255, 0, 0)

Use variables to extract the component values

Page 7: Computer Science 111

Simple for Loops

Just iterate through a sequence of values

The sequences are linear

for i in xrange(5): # Prints 0 1 2 3 4 print i,

sum = 0for element in [20, 30, 40]: sum += element # Sum is 90 at end

for element in myfile: sum += int(element)

Page 8: Computer Science 111

Nested Loops

Often used to process grid-like structures

• Game boards• Bitmaps of images• Matrices• Maps and diagrams

for row in xrange(7): for column in xrange(7): <do something with row and column>

0 1 2 3 4 5 6

0

12

3

4

5

6

Page 9: Computer Science 111

Grid Positions as (row,col) Pairs

In this 3 by 3 imaginary grid, each row has 3 positions and each column has 3 positions

for row in xrange(3): for col in xrange(3): print (row, col), print

#col0 col1 col2

(0,0) (0,1) (0,2) # row0(1,0) (1,1) (1,2) # row1(2,0) (2,1) (2,2) # row2

Page 10: Computer Science 111

Or Use a while Loop

row = 0while row < 3: col = 0 while col < 3: print (row, col), col += 1 print row += 1

#col0 col1 col2

(0,0) (0,1) (0,2) # row0(1,0) (1,1) (1,2) # row1(2,0) (2,1) (2,2) # row2

Page 11: Computer Science 111

Representing a Grid as Data

• Sometimes called a two-dimensional list or matrix

• Access each position with the methods get and put

• Usually process the grid with a nested loop

Page 12: Computer Science 111

A Grid Type

• Like the list type or the string type, the grid type is actually a set of operations on a set of data values (grids)

• Define a Grid class and its operations in a module

• Import the module to create and use grids

• The module hides the details of the data structures and operations used to represent and manipulate a grid

Page 13: Computer Science 111

The grid Module’s FunctionsOperation What It Does

g = Grid(rows, cols, value) Returns a grid with the given number of rows, columns, and fill value. The default number of rows and columns is 3 and the default fill value is 1.

g.getHeight() Returns the number of rows.

g.getWidth() Returns the number of columns.

g.put(row, column, value) Inserts value at the given row and column.

g.get(row, column) Returns the element at the given row and column.

Page 14: Computer Science 111

Creating a Grid

g = Grid(3, 3, 1) # Create a 3 by 3 grid of ones

g = Grid(3, 3) # Ditto

g = Grid(3) # Ditto

g = Grid() # Ditto

from grid import Grid

Page 15: Computer Science 111

Print a Grid

>>> print g1 1 11 1 11 1 1

print automatically runs str, which formats the elements in the grid in two dimensions

Page 16: Computer Science 111

Print # of Rows and Columns

>>> print g.getHeight(), g.getWidth()3 3

Page 17: Computer Science 111

Sum the Elements

sum = 0for row xrange(g.getHeight()): for col in xrange(g.getWidth()): sum += g.get(row, col)print sum # Prints 9

Page 18: Computer Science 111

Increment the Elements

for row xrange(g.getHeight()): for col in xrange(g.getWidth()): g.put(row, col, g.get(row, col) + 1)

Page 19: Computer Science 111

Represent an Image

>>> image = Grid(100, 100, (255, 0, 0))

This image is 100 pixels high and 100 pixels wide.

Each pixel is red.

Page 20: Computer Science 111

Insert a Blue Line Across the Middle of the Image

row = image.getHeight() / 2

for col in xrange(image.getWidth()): image.put(row, col, (0, 0, 255))

Much of image processing is just manipulating color values in a grid.