`x`

, `y`

), and I want to move from there `d`

, this just adds to my `x`

coordinate; I end up at (`x + d`

, `y`

). Or, if I want to move `d`

, I am adding to my `y`

coordinate, ending up at (`x`

, `y + d`

). This idea of moving some distance, in some direction, is called a It is possible to combine displacements in two directions at once. For example, suppose that I know the the top of a rectangle is at (10, 30) and it is 100 wide and 50 tall. If I want to figure out where the opposite corner is, I think to myself, "I can get to the opposite corner by moving 100 to the right, and 50 down. This means I have to add 100 to the

`x`

of the corner, and 50 to the `y`

." I end up at (10 + 100, 30 + 50) = (110, 80).Of course, if I can move to the right, I can move to the left as well, since moving a negative amount to the right is the same as moving to the left. Similarly, I can add a negative amount to my y to move up.

The thing you should notice is that right now, we only have rules for how to move some distance that is

`x`

axis, with every `y`

becoming -`y`

.There is another system of angles that you need to know about, because it is what the computer uses in many of its functions. It is called the

The actual distance that I go in the

`x`

and in the `y`

would have to be some fraction of 50, something less than the full distance, because I am not going directly in either direction. You can probably guess that the distance I would go in the `y`

would be greater than the distance in the `x`

, because 60° is closer to vertical than horizontal. But how do I find out what fraction of 50 I go in the x, and what fraction in the y?In math, there are special functions to calculate exactly these pieces of information. They are called the "sine" and "cosine" functions.

If I start at (`x` , `y` ) and go a distance `d` at an angle `a` , I will end up at: |

In Java, there are methods called

`Math.sin()`

and `Math.cos()`

that will find the sine or cosine of an angle. Unfortunately, they work with angles in radians, so I need to convert my angles before I put them in.So, suppose that I want to draw a line that starts at (10, 20) and goes 50 pixels away at an angle of 30°. To do this in Java, I need to convert the angle into radians; 30° is π/6 radians. I would write something like this:

Location point1 = new Location(10, 20);

Location point2 = new Location(10 + 50 * cos(Math.PI / 6), 20 + 50 * Math.sin(Math.PI / 6));

new Line(point1, point2);

Let's make this more general by writing a program that will draw a line from the center of the screen, to a point that is some distance and angle away:

public class AngleLine {

double d = 50;

double angle = 80 * Math.PI / 180;

Location center = new Location(canvas.getWidth() / 2, canvas.getHeight() / 2);

Location point2 = new Location(center.getX() + d * Math.cos(angle), center.getY() + d * Math.sin(angle));

new Line(center, point2, canvas);

Here, I could change the value I assign to

`d`

or `angle`

, and it would draw a different line.The nice thing about this angle formula is that it allows me to think about

public void onMouseMove(Location point) {

// Every time the mouse moves, increase the angle a little bit, recalculate the end point,

// and redraw the line

canvas.clear();

angle = angle + Math.PI / 180;

Location point2 = new Location(center.getX() + d * Math.cos(angle), center.getY() + d * Math.sin(angle));

new Line(center, point2, canvas);

}