Week 8 - Monday. What did we talk about last time? StdAudio.
-
Upload
nathan-claiborne -
Category
Documents
-
view
260 -
download
0
Transcript of Week 8 - Monday. What did we talk about last time? StdAudio.
![Page 1: Week 8 - Monday. What did we talk about last time? StdAudio.](https://reader033.fdocuments.in/reader033/viewer/2022061501/56649c755503460f94928d23/html5/thumbnails/1.jpg)
CS 121Week 8 - Monday
![Page 2: Week 8 - Monday. What did we talk about last time? StdAudio.](https://reader033.fdocuments.in/reader033/viewer/2022061501/56649c755503460f94928d23/html5/thumbnails/2.jpg)
Last time
What did we talk about last time?StdAudio
![Page 3: Week 8 - Monday. What did we talk about last time? StdAudio.](https://reader033.fdocuments.in/reader033/viewer/2022061501/56649c755503460f94928d23/html5/thumbnails/3.jpg)
Questions?
![Page 4: Week 8 - Monday. What did we talk about last time? StdAudio.](https://reader033.fdocuments.in/reader033/viewer/2022061501/56649c755503460f94928d23/html5/thumbnails/4.jpg)
Project 3
![Page 5: Week 8 - Monday. What did we talk about last time? StdAudio.](https://reader033.fdocuments.in/reader033/viewer/2022061501/56649c755503460f94928d23/html5/thumbnails/5.jpg)
StdDraw
![Page 6: Week 8 - Monday. What did we talk about last time? StdAudio.](https://reader033.fdocuments.in/reader033/viewer/2022061501/56649c755503460f94928d23/html5/thumbnails/6.jpg)
StdDraw
StdDraw is a library of Java code developed by Robert Sedgewick and Kevin Wayne
StdDraw allows you to draw output on the screen easily
You can draw points, lines, and polygons in various colors
You can clear and resize the drawing area and even save the results
StdDraw is not standard Java that everyone uses, but it’s a nice tool for graphics
![Page 7: Week 8 - Monday. What did we talk about last time? StdAudio.](https://reader033.fdocuments.in/reader033/viewer/2022061501/56649c755503460f94928d23/html5/thumbnails/7.jpg)
Lines and Points
![Page 8: Week 8 - Monday. What did we talk about last time? StdAudio.](https://reader033.fdocuments.in/reader033/viewer/2022061501/56649c755503460f94928d23/html5/thumbnails/8.jpg)
Lines and points
The simplest things you can draw with StdDraw are lines and points
The first thing you should be aware of is that the canvas is drawn like Quadrant I of a Cartesian plane
(0,0)
(0,1) (1,1)
(1,0)
![Page 9: Week 8 - Monday. What did we talk about last time? StdAudio.](https://reader033.fdocuments.in/reader033/viewer/2022061501/56649c755503460f94928d23/html5/thumbnails/9.jpg)
Line and point methods
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)
![Page 10: Week 8 - Monday. What did we talk about last time? StdAudio.](https://reader033.fdocuments.in/reader033/viewer/2022061501/56649c755503460f94928d23/html5/thumbnails/10.jpg)
Line example
Let’s draw a box then divide it into two halves, like so:
![Page 11: Week 8 - Monday. What did we talk about last time? StdAudio.](https://reader033.fdocuments.in/reader033/viewer/2022061501/56649c755503460f94928d23/html5/thumbnails/11.jpg)
Circles and Polygons
![Page 12: Week 8 - Monday. What did we talk about last time? StdAudio.](https://reader033.fdocuments.in/reader033/viewer/2022061501/56649c755503460f94928d23/html5/thumbnails/12.jpg)
Who wants to take all that time to make a square?
There are built in commands for drawing: Circles Squares Arbitrary polygons Filled versions of each one of these
We won’t bother with the arbitrary polygons
It is also possible to set the color
![Page 13: Week 8 - Monday. What did we talk about last time? StdAudio.](https://reader033.fdocuments.in/reader033/viewer/2022061501/56649c755503460f94928d23/html5/thumbnails/13.jpg)
Shape methods
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
![Page 14: Week 8 - Monday. What did we talk about last time? StdAudio.](https://reader033.fdocuments.in/reader033/viewer/2022061501/56649c755503460f94928d23/html5/thumbnails/14.jpg)
Colors
Eventually you will be able to define your own colors
For now you are limited to 13 presets
For example, to make something magenta, you would use the value StdDraw.MAGENTA
BLACK BLUE CYAN DARK_GRAY GRAY
GREEN LIGHT_GRAY MAGENTA ORANGE PINK
RED WHITE YELLOW
![Page 15: Week 8 - Monday. What did we talk about last time? StdAudio.](https://reader033.fdocuments.in/reader033/viewer/2022061501/56649c755503460f94928d23/html5/thumbnails/15.jpg)
Screen saver?
Let’s write some code for making 100 circles at random locations with random sizes and random colors
Location is easy Size is easy, we just decide on the
range of sizes we want and do some math
Color is more painful We need a switch statement with 13
choices
![Page 16: Week 8 - Monday. What did we talk about last time? StdAudio.](https://reader033.fdocuments.in/reader033/viewer/2022061501/56649c755503460f94928d23/html5/thumbnails/16.jpg)
What about a chessboard?
We just want to make a pattern of black and white squares on the screen
Hint: We need two loops
![Page 17: Week 8 - Monday. What did we talk about last time? StdAudio.](https://reader033.fdocuments.in/reader033/viewer/2022061501/56649c755503460f94928d23/html5/thumbnails/17.jpg)
Controlling the Draw Area
![Page 18: Week 8 - Monday. What did we talk about last time? StdAudio.](https://reader033.fdocuments.in/reader033/viewer/2022061501/56649c755503460f94928d23/html5/thumbnails/18.jpg)
Controls
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 show(int delay) Delay for delay ms
![Page 19: Week 8 - Monday. What did we talk about last time? StdAudio.](https://reader033.fdocuments.in/reader033/viewer/2022061501/56649c755503460f94928d23/html5/thumbnails/19.jpg)
Scales
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
![Page 20: Week 8 - Monday. What did we talk about last time? StdAudio.](https://reader033.fdocuments.in/reader033/viewer/2022061501/56649c755503460f94928d23/html5/thumbnails/20.jpg)
Canvas size
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 of the canvas in terms of screen pixels
![Page 21: Week 8 - Monday. What did we talk about last time? StdAudio.](https://reader033.fdocuments.in/reader033/viewer/2022061501/56649c755503460f94928d23/html5/thumbnails/21.jpg)
What is this show() thing?
The show() 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
![Page 22: Week 8 - Monday. What did we talk about last time? StdAudio.](https://reader033.fdocuments.in/reader033/viewer/2022061501/56649c755503460f94928d23/html5/thumbnails/22.jpg)
Applications
![Page 23: Week 8 - Monday. What did we talk about last time? StdAudio.](https://reader033.fdocuments.in/reader033/viewer/2022061501/56649c755503460f94928d23/html5/thumbnails/23.jpg)
Cannon simulator
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]
![Page 24: Week 8 - Monday. What did we talk about last time? StdAudio.](https://reader033.fdocuments.in/reader033/viewer/2022061501/56649c755503460f94928d23/html5/thumbnails/24.jpg)
Plotting functions
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
![Page 25: Week 8 - Monday. What did we talk about last time? StdAudio.](https://reader033.fdocuments.in/reader033/viewer/2022061501/56649c755503460f94928d23/html5/thumbnails/25.jpg)
Function plotting algorithm
1. Ask the user for the coefficients of the four terms (ax3 + bx2 + cx + d)
2. Ask the user for an x range3. Run through the function and find the
minimum and maximum y values hit4. Rescale the drawing area to show
everything5. Plot the function
![Page 26: Week 8 - Monday. What did we talk about last time? StdAudio.](https://reader033.fdocuments.in/reader033/viewer/2022061501/56649c755503460f94928d23/html5/thumbnails/26.jpg)
Upcoming
![Page 27: Week 8 - Monday. What did we talk about last time? StdAudio.](https://reader033.fdocuments.in/reader033/viewer/2022061501/56649c755503460f94928d23/html5/thumbnails/27.jpg)
Next time…
Static methods
![Page 28: Week 8 - Monday. What did we talk about last time? StdAudio.](https://reader033.fdocuments.in/reader033/viewer/2022061501/56649c755503460f94928d23/html5/thumbnails/28.jpg)
Reminders
Read Chapter 8 of the textbookGet an early start on Project 3
It's harder than the previous two!