## Graph Drawing [2/8]

### Graph Drawing

This program draws vertical lines. The lines are 50 pixels apart. Top line ends are at coordinate y = 10, and the other line ends are at y = 710.

### Exercise:

Change the program so that the lines are 8 pixels apart. Also, reduce the sleep time so that the image is drawn faster.

The solution is on the next slide.

In this program, top line ends are at coordinate y = 10, and the bottom line ends are at y = 710.

### Exercise:

Change the program so that the bottom (i.e. second) line ends are at coordinate y = 500.

The solution is on the next slide.

In this program, top line ends are at coordinate y = 10, and the bottom line ends are at y = 500.

### Exercise:

Change the program so that top (i.e. first) line ends are at coordinate y = i.

The solution is on the next slide.

### Exercise:

Change the program so that first line ends are at coordinate y = i*i/600. Place the other line ends at y = 500.

The solution is on the next slide.

### Exercise:

Change the program so that lines are 1 pixel apart instead of 8 pixels. Also, position the second line ends at coordinate y = i , and make the image be drawn even faster.

The solution is on the next slide.

Open-ended exercise:

Waving Curve

To make a waving curve: Change the program so that first line ends are at coordinate y = 200*sin(i/70)+500. Place the other line ends at y = 500.

The sin (sine) function can be used to create waves. Don't be afraid of the sine function, even if you don't know anything about it. Try changing the numbers 200, 70 and 500 in the given formula to see how it affects the shape of the wave. You can also try to find which numbers produce a shape matching the one in the image below.

This exercise is very easy to solve if you follow the instructions. In this exercise, you don't need to analyze the given program in detail (but you can if you want to), and you don't have to be familiar with the mathematical concepts involved.

Run the given program first.

This program draws a graph of mathematical function x *x. Values of this function are calculated by expression x*x and stored in the variable fval. Find this expression in the source code of the program.

### Exercise:

Make the program draw a graph of mathematical function

- x + 2

Solution

#num fval = **x + 2**;

What happens when the variables x0 and y0 are initialized to some other values? Try it out.

This exercise should also be easy. Run the given program first.

What happens when the variable unit is initialized to some other value? Try out values 70, 140, 200, 40.

You can change the initial values of variables x0 and y0 to move the graph around the screen. Change the initial value of variable unit to zoom in or out.

### Exercises:

Make the program draw a graph of mathematical function

- x * 3

Solution

#num fval = **x * 3**;

Make the program draw a graph of the following mathematical functions:

- x*2
- x*1
- x*0.5
- x*0 + 3
- x.round
- x.int
- sin(x)
- 2*sin(3*x)
- 3*sin(5*x)+1
- sin(x*x)
- x*sin(x*x)
- -x*x + 5
- 1/x

(For mathematicians only) The last one might present a problem due to
**division by zero**. There are numerous ways to solve it.
Can you find a way?

Solution for 1/x:

It is probably the easiest to move the origin by half a pixel:

#num x0 = 480**.5;**

This slide ends the optional chapter ** Graph Drawing**.

<< F2:Prev - - F4:Next >>