# RobotMath: Solving Triangles, Part 1

Jump to: navigation, search

## Contents

### Significance of Triangles

Head constructed from a triangular mesh

Triangles are everywhere. They are the subject of trigonometry (Greek for measurement of triangles), which underlies the higher mathematics used in engineering, physics, and computer graphics. The human head at right is constructed from a mesh of triangles, as are all the characters, objects, and scenery in your favorite computer game.

The triangle below has sides of length a, b, and c and angles A, B, and C. You can see that angle C is formed by sides a and b, placing it opposite side c. Similarly, angle B is formed by sides a and c and is opposite side b.

Given enough information about a triangle, it is possible to calculate all the remaining values. For example, if we know the lengths a and b and the angle C, we can calculate the angles A and B and the length c. Or, if we know the length c and the angles A and B, we can calculate the lengths a and b and the angle C. Some of these calculations are easier than others, but none are terribly difficult. Many simple robotics problems reduce to solving a triangle, as you'll see below.

Here's a simple rule that will help you solve for the angles: The angles of a triangle always sum to 180°.

$A + B + C = 180^\circ = \pi\,\mbox{radians}$

So if you know any two angles of a triangle, you immediately know the third angle as well.

### Solving A Robot Arm

The robot arm below has two joints: a shoulder and an elbow, and two links: an upper arm of length l1, and a lower arm, shown in blue, of length l2. The tip of the arm is called the end effector. It might be a gripper, or a tool such as a screwdriver or a welding torch, but for our purposes we will treat it as just a point.

We denote the shoulder angle by θ1 and the elbow angle by θ2. We adopt the convention that the shoulder joint is at the origin of our coordinate system, and a joint angle of zero means the associated link is pointing straight out, not bent. So when θ1 = 0° the upper arm is pointing straight ahead along the x axis. And when θ2 = 0°, the lower arm will be pointing in the same direction as the upper arm and the entire arm can be viewed as one segment of length l1+l2 pointing in direction θ1.

Suppose we rotate the shoulder joint to the right so that the upper arm is at angle θ1 relative to the x axis. Then we rotate the elbow joint to the left so that the lower arm is at angle θ2 relative to the upper arm. We will choose θ2 such that the end effector at the tip of the arm lies on the x axis, as shown. (You'll learn later how to calculate θ2 to put the end-effector where you want it.) So the shoulder, elbow, and end-effector form a triangle, and the line from the shoulder to the end-effector conveniently lies on the x axis.

Problem: What is the value of the angle β as a function of θ1 and θ2? Note that angles α and θ2 are supplementary, meaning they sum to 180° or π radians.

Solution: The key insight is to see this as a triangle solving problem. The triangle's three vertices are the shoulder joint, the elbow joint, and the end effector. Its three sides have lengths l1, l2, and S, and its three angles are θ1, α, and β. We're given θ1 and θ2 and are asked to solve for β.

Our knowledge about the sum of the angles of a triangle tells us that $\theta_1 + \alpha + \beta = \pi \quad$
Therefore $\beta = \pi - \theta_1 - \alpha \quad$
And our knowledge about supplementary angles tells us that $\alpha = \pi - \theta_2 \quad$
Therefore $\beta = \pi - \theta_1 - (\pi-\theta_2) = \theta_2-\theta_1 \quad$

### Solving Based on Lengths of Sides

Solving for the lengths of the sides of a triangle, such as side S in the problem above, or for the angles when only one angle and two sides are initially known, requires a little more work. The trick is to divide the triangle into two right triangles, as shown below. Any triangle can be decomposed into two right triangles by dropping a perpendicular h from a vertex to the opposite side. The little square in the diagram below indicates that the angle between h and the line it intersects is 90°. We can solve any triangle by decomposing it into two right triangles this way. You'll see how in RobotMath: Solving Triangles, Part 2. First we need to learn more about right triangles.

### Right Triangles

Right angles have nice mathematical properties that make them easy to work with. The side opposite the right angle is the longest side of the triangle, and is called the hypotenuse. Pick either one of the other two angles and call it θ. The other two sides are called the adjacent side and the opposite side because of their relationship to θ.

The most common way to draw a triangle in a math textbook is with the adjacent side extending from the origin in the positive direction along the x axis, and the opposite side parallel to the positive y axis, as shown above. If the lengths of the sides are x, y, and r, then the vertices are at (0,0), (x,0), and (x,y). The best-known property of right triangles is that they obey the Pythagorean Theorem: the square of the hypotenuse is equal to the sum of the squares of the other two sides.

$r^2 = x^2 + y^{2\quad}$

There are many proofs of the Pythagorean Theorem, but they are too complicated to go into here. See [Wikipedia] for details.

Another important property of right triangles is that the various ratios of their sides x, y, and r are completely determined by θ, so if we know θ plus the length of just one of the sides, we can determine the lengths of the other two sides. To do this, we make use of the sine, cosine, and tangent functions.

### Solving Right Triangles with Sine, Cosine, and Tangent

The sine of the angle θ (written sin θ in mathematical shorthand) is determined by drawing a right triangle with angle θ and measuring the ratio of the "opposite" side (i.e., the side opposite θ) to the hypotenuse. The triangle can be of any size; the ratio of the sides will be the same as long as θ is the same.

$\sin\theta = \frac{\mbox{opposite}_\theta}{\mbox{hypotenuse}} = \frac{y}{r}$

Given any right triangle, if we know r and θ, we can solve for y:

$y = r\sin\theta^{\quad}$

Or if we know y and θ, we can solve for r:

$r = \frac{y}{\sin\theta}$

Actually it's not enough to know θ to solve for y or r: we need to know sin θ. In earlier times people looked up values of trigonometric functions such as sine in lengthy printed tables, but today they are built in to calculators and computers.

The cosine and tangent functions are defined similarly:

$\cos\theta = \frac{\mbox{adjacent}_\theta}{\mbox{hypotenuse}} = \frac{x}{r}$

$\tan\theta = \frac{\mbox{opposite}_\theta}{\mbox{adjacent}_\theta} = \frac{y}{x}$

We don't actually need all three of these functions to solve a right triangle; using just sin and the Pythagorean theorem we can solve for any side given any other side plus the value of θ. But it's convenient to have all three functions at our disposal.

Problems:

1. Given x and θ, solve for y using the tangent function.
2. Given x and θ, solve for y using cosine and the Pythagorean theorem.
3. What is the value of sin(θ) divided by cos(θ) ?
4. Harder: given r and tan θ, solve for x.
5. Prove that sin2(θ)+cos2(θ)=1. Hint: start with the Pythagorean theorem, plus x = r cos(θ) and y = r sin(θ).
6. Draw a diagram to help you solve this problem. A robot facing north detects an east-west wall 3 meters ahead. If it turns by 30 degrees, how far can it travel before it hits the wall?
7. A flying robot finds itself 1000 meters above a dry lakebed. If it cuts its engines and descends on a 7° glidepath, how far will it travel over the ground before it lands?

### An Aside: The Third Angle

The functions sin(θ), cos(θ), and tan(θ) are defined based on a triangle containing an angle θ and another angle equal to 90°, making it a right triangle. What about the third angle, which we'll call φ? There are some neat symmetries between φ and θ. For starters, obviously φ = 90° - θ.

Notice that the side adjacent to θ (side x) is opposite to φ, and the side opposite θ (side y) is adjacent to φ. Therefore:

$\sin\phi = \frac{\mbox{opposite}_\phi}{\mbox{hypotenuse}} = \frac{x}{r} = \cos\theta$

$\cos\phi = \frac{\mbox{adjacent}_\phi}{\mbox{hypotenuse}} = \frac{y}{r} = \sin\theta$

$\tan\phi = \frac{\mbox{opposite}_\phi}{\mbox{adjacent}_\phi} = \frac{x}{y} = \frac{1}{\tan\theta} = \cot\theta$

Here, cot is the abbreviation for the cotangent function, which is the reciprocal of the tangent. Less commonly seen are the reciprocal of cosine, called the secant, and the reciprocal of sine, called the cosecant.

### Inverse (Arc) Functions

The tangent function, given an angle θ, tells us the ratio y/x. The inverse function, called the arc tangent (or arctan for short), does the opposite: given a ratio y/x, it tells us the angle θ. The arc sine and arc cosine functions are likewise the inverses of the sine and cosine functions, respectively.

Problem: given a right triangle with sides of length 3 (adjacent), 4 (opposite), and 5 (hypotenuse), what is the angle θ?

Solution:

$\tan \theta = \frac{y}{x} = \frac{4}{3}$
$\theta = \arctan (4/3) = 53.13^\circ$

### Programming With Trigonometric Functions

To be written. Introduce sin/cos/tan and asin/acos/atan functions. Save atan2 for part 2. Introduce sqrt for Pythagorean theorem.