Week 8 - Monday What did we talk about last time? StdDraw The - - PowerPoint PPT Presentation

week 8 monday what did we talk about last time stddraw
SMART_READER_LITE
LIVE PREVIEW

Week 8 - Monday What did we talk about last time? StdDraw The - - PowerPoint PPT Presentation

Week 8 - Monday What did we talk about last time? StdDraw The following methods can be used to draw lines and points Method Use void line(double x0, double y0, Draw a line from (x0,y0) to double x1, double y1) (x1,y1) void


slide-1
SLIDE 1

Week 8 - Monday

slide-2
SLIDE 2

 What did we talk about last time?  StdDraw

slide-3
SLIDE 3
slide-4
SLIDE 4
slide-5
SLIDE 5
slide-6
SLIDE 6

 The following methods can be used to draw lines and points

Method Use void line(double x0, double y0, double x1, double y1) Draw a line from (x0,y0) to (x1,y1) void point(double x, double y) Draw a point at (x,y)

slide-7
SLIDE 7

 Here are some methods for drawing circles and squares and

setting the color for doing so:

Method Use

void circle(double x, double y, double r) Draw a circle centered at (x,y) with radius r void filledCircle(double x, double y, double r) Draw a filled circle centered at (x,y) with radius r void square(double x, double y, double r) Draw a square centered at (x,y) with edges 2r void filledSquare(double x, double y, double r) Draw a filled square centered at (x,y) with edges 2r void setPenColor(Color c) Start drawing with color c

slide-8
SLIDE 8

 We just want to make a pattern of black and white squares on

the screen

 Hint: We need two loops

slide-9
SLIDE 9

 A number of methods are given to give us more control over

the display

Method Use void setXscale(double x0, double x1) Set the x scale void setYscale(double y0, double y1) Set the y scale void setPenRadius(double r) Set the pen radius void setCanvasSize(int w, int h) Set canvas size void clear() Clear canvas to white void clear(Color c) Clear canvas to color c void pause(int delay) Delay for delay ms

slide-10
SLIDE 10

 As you have seen, the default scale of the canvas is in the

range [0,1] for both x and y

 We can use the setXscale() method to set the minimum

and maximum x values

 We can use the setYscale() method to set the minimum

and maximum y values

 Useful for plotting functions

slide-11
SLIDE 11

 Note that changing the scale doesn't change the size of the

window, just what is shown in it

 If you want to change the size of the window, use the

setCanvasSize() method to set the width and the height

  • f the canvas in terms of screen pixels
slide-12
SLIDE 12

 The pause() method lets you specify a delay in milliseconds

before things are drawn on the screen

 You can use it to slow down or speed up animations  To use it right, you need to first call

StdDraw.enableDoubleBuffering()

 Then, whenever you want to actually show all the drawing

that's been done, you call StdDraw.show()

 Doing so draws everything off-screen and then shows it all at

  • nce, which is both more efficient and more attractive for

animations

slide-13
SLIDE 13

 Can we simulate a cannon being fired?  Let the user enter an initial velocity in m/s  Let the user an angle between 0° and 90°  Assume each iteration takes 1/10 of a second  Assume an initial height of 20 m  We draw the path of the cannon ball as it flies through the air  Let's also set the x and y scales to both be [0,100]

slide-14
SLIDE 14

 Plotting functions is really useful  Getting smooth curves is hard  Instead, we just pick a whole bunch of x points and figure out

the function value

  • We can just draw dots to plot those values
  • We can connect them with lines for a more connected look

 Let's write some code to draw cubic polynomials

slide-15
SLIDE 15
  • 1. Ask the user for the coefficients of the four terms (ax3 + bx2

+ cx + d)

  • 2. Ask the user for an x range
  • 3. Run through the function and find the minimum and

maximum y values hit

  • 4. Rescale the drawing area to show everything
  • 5. Plot the function
slide-16
SLIDE 16
slide-17
SLIDE 17

 Keep reading Chapter 8  Static methods

slide-18
SLIDE 18

 Keep reading Chapter 8 of the textbook  Keep working on Project 3

  • Due next Friday