Transforming Geometry Groundhog Day. Drawing Quads In a 300 by 200 window, draw two quads of...

19
Transforming Geometry Groundhog Day

Transcript of Transforming Geometry Groundhog Day. Drawing Quads In a 300 by 200 window, draw two quads of...

Page 1: Transforming Geometry Groundhog Day. Drawing Quads In a 300 by 200 window, draw two quads of different colors. Don’t show the grey grid.

Transforming Geometry

Groundhog Day

Page 2: Transforming Geometry Groundhog Day. Drawing Quads In a 300 by 200 window, draw two quads of different colors. Don’t show the grey grid.

Drawing Quads

In a 300 by 200 window, draw two quads of different colors.

Don’t show the grey grid.

Page 3: Transforming Geometry Groundhog Day. Drawing Quads In a 300 by 200 window, draw two quads of different colors. Don’t show the grey grid.

Adding a face

Add eyes, nose, and mouth.

The picture below is the top of the groundhog.

The grid units are 100/12.

Page 4: Transforming Geometry Groundhog Day. Drawing Quads In a 300 by 200 window, draw two quads of different colors. Don’t show the grey grid.

Multiple groundhogs

• To repeat at a different location– Use variables for X and Y points

quad(ghX+150, ghY,

ghX+100, ghY+200, ….)

• To repeat at a different size– Use variable for scale

float scale = size/200 ;

quad(int(150*scale), 0,

int(100*scale), int(200*scale), ….)

Page 5: Transforming Geometry Groundhog Day. Drawing Quads In a 300 by 200 window, draw two quads of different colors. Don’t show the grey grid.

Multiple groundhogs

• To repeat at a different location and size– Use variables for X, Y, and scale

float scale = size/200 ;

quad(ghx+int(150*scale), ghY,

ghx+int(100*scale), ghy+int(200*scale),

….)

• To repeat at a 45 angle– Take out a good geometry book!

Page 6: Transforming Geometry Groundhog Day. Drawing Quads In a 300 by 200 window, draw two quads of different colors. Don’t show the grey grid.

Better yet

• Use transformation matrices– Without really understanding the math

• Linear transformations

• Affine transformations

• Linear algebra

• Matrices

• Euclidean geometry

• Cartesian coordinates

• You’ll be able to– Draw slanted ellipses– Draw big slanted groundhogs– Draw tiny groundhogs looking in mirrors– Make Toy Story 4

Page 7: Transforming Geometry Groundhog Day. Drawing Quads In a 300 by 200 window, draw two quads of different colors. Don’t show the grey grid.

The translate function

size(400, 400) ;

ellipse(100, 100, 80, 40) ;

translate(10, 10) ;

ellipse(100, 100, 80, 40) ;

Page 8: Transforming Geometry Groundhog Day. Drawing Quads In a 300 by 200 window, draw two quads of different colors. Don’t show the grey grid.

Why not three!

size(400, 400) ;

ellipse(100, 100, 80, 40) ;

translate(10, 10) ;

ellipse(100, 100, 80, 40) ;

translate(20, 20) ;

ellipse(100, 100, 80, 40) ;

Perhaps not what you expected

Page 9: Transforming Geometry Groundhog Day. Drawing Quads In a 300 by 200 window, draw two quads of different colors. Don’t show the grey grid.

Are these the same?

int x = 200 ;int y = 200 ; ellipse(x+100, y+100, 80, 40) ;

int x = 200 ;int y = 200 ;translate(x, y) ; ellipse(100, 100, 80, 40) ;

for (int x=0; x<=200; x+=100) { for (int y=0; y<=200; y+=100) { ellipse(x+100, y+100, 80, 40) ; }}

for (int x=0; x<=200; x+=100) { for (int y=0; y<=200; y+=100) { translate(x, y) ; ellipse(100, 100, 80, 40) ; }}

Page 10: Transforming Geometry Groundhog Day. Drawing Quads In a 300 by 200 window, draw two quads of different colors. Don’t show the grey grid.

Controlling transformations

• To discard current “transformation matrix” and return to the old matrixpopMatrix() ;

• To start a new current matrix and save a copy of the current matrixpushMatrix() ;

• To add transforms to the current matrixtranslate()– And a few more to come

Page 11: Transforming Geometry Groundhog Day. Drawing Quads In a 300 by 200 window, draw two quads of different colors. Don’t show the grey grid.

One translation at a time

size(400, 400) ;

for (int x=0; x<=200; x+=100) {

for (int y=0; y<=200; y+=100) {

pushMatrix() ;

translate(x, y) ;

ellipse(100, 100, 80, 40) ;

popMatrix() ;

}

}

Page 12: Transforming Geometry Groundhog Day. Drawing Quads In a 300 by 200 window, draw two quads of different colors. Don’t show the grey grid.

Too clever a translation

size(400, 400) ;

for (int i=0; i<3; i++) {

pushMatrix() ;

for (int j=0; j<3; j++) {

ellipse(100, 100, 80, 40) ;

translate(0, 100) ;

}

popMatrix() ;

translate(100, 0) ;

}

Page 13: Transforming Geometry Groundhog Day. Drawing Quads In a 300 by 200 window, draw two quads of different colors. Don’t show the grey grid.

A different slant

size(400, 400) ;

rotate(radians(30)) ;

fill(255,0,0) ;

ellipse(300, 100, 100, 50) ;

rotate(PI/6) ;

fill(0, 255, 0) ;

ellipse(100, 0, 100, 50) ;

Page 14: Transforming Geometry Groundhog Day. Drawing Quads In a 300 by 200 window, draw two quads of different colors. Don’t show the grey grid.

May I supersize that?

size(400, 400) ;

ellipse(50, 50, 10, 10) ;

scale(6) ;

ellipse(50, 50, 10, 10) ;

Page 15: Transforming Geometry Groundhog Day. Drawing Quads In a 300 by 200 window, draw two quads of different colors. Don’t show the grey grid.

Translate and Rotatesize(600, 600) ;

ellipse(100, 100, 10, 10) ;

pushMatrix() ;

fill(0,255,0) ;

translate(100, 100) ;

rotate(PI/6) ;

ellipse(300, 300, 200, 100) ;

popMatrix() ;

pushMatrix() ;

fill(0,127,0) ;

rotate(PI/6) ;

translate(100, 100) ;

ellipse(300, 300, 200, 100) ;

popMatrix() ;

Page 16: Transforming Geometry Groundhog Day. Drawing Quads In a 300 by 200 window, draw two quads of different colors. Don’t show the grey grid.

Translate and Scalesize(600, 600) ;

ellipse(100, 100, 10, 10) ;

pushMatrix() ;

fill(255,0,0) ; // red

translate(100, 100) ;

scale(0.5) ;

ellipse(300, 300, 200, 100) ;

popMatrix() ;

pushMatrix() ;

fill(127,0,0) ;

scale(0.5) ;

translate(100, 100) ;

ellipse(400, 400, 200, 100) ;

popMatrix() ;

Page 17: Transforming Geometry Groundhog Day. Drawing Quads In a 300 by 200 window, draw two quads of different colors. Don’t show the grey grid.

Scale and rotate

• It’s the same in either order

size(600, 600) ;

ellipse(100, 100, 10, 10) ;

pushMatrix() ;

fill(0,0,255) ;

rotate(PI/6) ;

scale(0.5) ;

ellipse(400, 400, 200, 100) ;

popMatrix() ;

Page 18: Transforming Geometry Groundhog Day. Drawing Quads In a 300 by 200 window, draw two quads of different colors. Don’t show the grey grid.

In-Class Exercise: Draw six groundhogs

• Make the window 600 by 600

• Draw a 2 by 3 collection of groundhogsfor (int i=0; i<2; i++) {

for (int j=0; j<3; j++) {

• Consider the following– Vary the groundhog position– Change the groundhog size– Rotating the groundhog

Page 19: Transforming Geometry Groundhog Day. Drawing Quads In a 300 by 200 window, draw two quads of different colors. Don’t show the grey grid.

Assignment 3 - Due Feb 10

• Write a program that repeats a pattern of groundhogs in several places throughout the screen. Try to put a little variation in the pattern.

• Use a screen size of 400x400.• You must use translate(),

rotate(), and scale().• Remember to comment your

code and to submit only your source code.