csc 112introduction to media computation 1 rotating images

10
CSC 112 Introduction to Media Compu tation 1 Rotating Images

Upload: harry-cooper

Post on 18-Jan-2018

220 views

Category:

Documents


0 download

DESCRIPTION

Introduction to Media Computation3 CSC 112 Rotating: How it works We increment the same, but we use targetX for the Y coordinate and targetY for the X coordinate

TRANSCRIPT

Page 1: CSC 112Introduction to Media Computation 1 Rotating Images

CSC 112 Introduction to Media Computation 1

Rotating Images

Page 2: CSC 112Introduction to Media Computation 1 Rotating Images

Introduction to Media Computation 2CSC 112

Copy and Rotatedef copySideways(): # This is the version from the book (Recipe 29) that # simply swithches targetX and targetY in the call to # setColor to accomplish the rotation. # # Set up the source and target pictures file=getMediaPath("flower1.jpg") picture = makePicture(file) canvasf = getMediaPath("320x240.jpg") canvas = makePicture(canvasf) # Now, do the actual copying targetX = 1 for sourceX in range(1,getWidth(picture)): targetY = 1 for sourceY in range(1,getHeight(picture)): color = getColor(getPixel(picture,sourceX,sourceY)) setColor(getPixel(canvas,targetY,targetX), color) targetY = targetY + 1 targetX = targetX + 1 # Display the results show(picture) show(canvas) return canvas

Page 3: CSC 112Introduction to Media Computation 1 Rotating Images

Introduction to Media Computation 3CSC 112

Rotating: How it works We increment the same,

but we use targetX for the Y coordinate and targetY for the X coordinate

Page 4: CSC 112Introduction to Media Computation 1 Rotating Images

Introduction to Media Computation 4CSC 112

Rotating: How it works 2 We increment sourceY

and targetY just the same.

Page 5: CSC 112Introduction to Media Computation 1 Rotating Images

Introduction to Media Computation 5CSC 112

Rotate: How it ends Same amount of

increment, even same values in the variables, but a different result.

First column of the original became the first for of the copy, and so on.

Page 6: CSC 112Introduction to Media Computation 1 Rotating Images

Introduction to Media Computation 6CSC 112

Take a closer look:

The flower was reversed left-to-right as it was copied. Why?

The first column of the original became the first row of the copy, when it really should be the last row to avoid reversing the image.

Page 7: CSC 112Introduction to Media Computation 1 Rotating Images

Introduction to Media Computation 7CSC 112

Copy with Counterclockwise Rotationdef copyCounterclockwise(): # This version works like Recipe 30 from the book, but it # uses the names targetX and targetY for the values that # will be used for the X and Y values in setColor. file=getMediaPath("flower1.jpg") picture = makePicture(file) canvasf = getMediaPath("320x240.jpg") canvas = makePicture(canvasf) # The picture on the canvas will be filled in from bottom # to top with the columns from the original picture, as # targetY goes from getWidth(pictue) - 1 to 0 targetY = getWidth(picture) - 1 for sourceX in range(1,getWidth(picture)): targetX = 1 for sourceY in range(1,getHeight(picture)): color = getColor(getPixel(picture,sourceX,sourceY)) setColor(getPixel(canvas,targetX,targetY), color) targetX = targetX + 1 targetY = targetY - 1 # Display the results show(picture) show(canvas) return canvas

Page 8: CSC 112Introduction to Media Computation 1 Rotating Images

Introduction to Media Computation 8CSC 112

Other rotations We can rotate clockwise instead of counterclockwise by

counting down through the values of targetX instead of targetY

Inversion is accomplished by counting down through both ranges. targetX will be associated with the sourceX loop in this case, since the copied picture will be the same shape as the original.

Page 9: CSC 112Introduction to Media Computation 1 Rotating Images

Introduction to Media Computation 9CSC 112

Copy with Clockwise Rotationdef copyClockwise(): # Like copyContercloskwise, this function uses the names targetX and # targetY for the values that will be used for the X and Y in setColor file=getMediaPath("flower1.jpg") picture = makePicture(file) canvasf = getMediaPath("320x240.jpg") canvas = makePicture(canvasf) # The picture on the canvas will be filled in from top # to bottom with the columns from the original picture targetY = 1 for sourceX in range(1,getWidth(picture)): # Each row of the new picture will be filled from right to left, # as targetX decreases from getHeight(picture) - 1 to 0 targetX = getHeight(picture) - 1 for sourceY in range(1,getHeight(picture)): color = getColor(getPixel(picture,sourceX,sourceY)) setColor(getPixel(canvas,targetX,targetY), color) targetX = targetX - 1 targetY = targetY + 1 show(picture) show(canvas) return canvas

Page 10: CSC 112Introduction to Media Computation 1 Rotating Images

Introduction to Media Computation 10CSC 112

Copy and Invertdef copyInverted(): # Set up the source and target pictures file=getMediaPath("flower1.jpg") picture = makePicture(file) canvasf = getMediaPath("320x240.jpg") canvas = makePicture(canvasf) # The columns of the new picture will be added from # right to left, since targetX is counting down targetX = getWidth(picture) - 1 for sourceX in range(1,getWidth(picture)): # Each column will be filled from bottom to top, # since targetY is counting down targetY = getHeight(picture) - 1 for sourceY in range(1,getHeight(picture)): color = getColor(getPixel(picture,sourceX,sourceY)) setColor(getPixel(canvas,targetX,targetY), color) targetY = targetY - 1 targetX = targetX - 1 # Display the results show(picture) show(canvas) return canvas