# RobotMath: Solving Triangles, Part 1

### From Tekkotsu Wiki

## Contents |

### Significance of Triangles

Triangles are the bricks of cyberspace. 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. Trigonometry (Greek for *measurement of triangles*) underlies much of the higher mathematics used in engineering, physics, and computer graphics. In this lesson you will learn some of the mathematical properties of triangles and how to calculate them in computer code.

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°.**

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 *l _{1}*, and a

*lower arm*, shown in blue, of length

*l*. The tip of the arm is called the

_{2}*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 up, along the *x* axis. And when θ_{2} = 0°, the lower arm will be aligned with the upper arm, and the entire arm can be viewed as one segment of length *l _{1}*+

*l*pointing in direction θ

_{2}_{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 a particular θ_{2} that puts the end effector 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 *l*_{1}, *l*_{2}, 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
- Therefore
- And our knowledge about supplementary angles tells us that
- Therefore

### 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.

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.

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

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

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:

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:**

- Given
*x*and θ, solve for*y*using the tangent function. - Given
*x*and θ, solve for*y*using cosine and the Pythagorean theorem. - What is the value of sin(θ) divided by cos(θ) ?
- Harder: given
*r*and tan θ, solve for*x*. - Prove that sin
^{2}(θ)+cos^{2}(θ)=1. Hint: start with the Pythagorean theorem, plus*x*=*r*cos(θ) and*y*=*r*sin(θ). - 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?
- 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:

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.

**Example:** A common illustration of the Pythagorean Theorem is a right triangle with sides of length 3 (adjacent), 4 (opposite), and 5 (hypotenuse). Notice that 3^{2}+4^{2}=5^{2}. What are the angles θ and φ in this triangle?

The arctan function is used frequently in robot programming.

**Problem**: A robot is told to travel forward 2 meters. It ends up going forward by 1.9 meters and drifting left by 50 millimeters. What is the drift angle?

**Solution**:

### Programming With Trigonometric Functions

C++ provides built-in trigonometric functions **sin' , **

*cos*,

**, and**tan**, and arc functions**asin**acos**, and

**atan**. There is also a

**sqrt**function for computing the square root, which is useful for applying the Pythagorean Theorem. All of these functions, and several additional ones, are defined in the C math library, math.h. This library is included in Tekkotsu, so you don't need to explicitly #include it in your own code.

The trig functions expect their arguments to be of type float or double. Integer arguments will be converted, but if your calculation involves a division you don't want to be using integer arithmetic. For example, tan(3/4) returns 0 because the integer division 3/4 returns 0, which is then converted to the float value 0.0. In contrast, tan(3.0/4.0) returns 0.9316.

**Problem:** A robot at the origin (0,0) facing North is told to travel to the point (120,18) and then face due East. The robot can only turn or move foward; it can't do both at once. Thus it will have to turn, advance, and then turn again. Calculate the amount of each turn, and the forward distance the robot must travel.

**Solution:** The destination point is 120 mm ahead of the origin and 18 mm to the left, so x=128 and y=18. We can use **arctan** to calculate the turn angle. The desired final heading is to the East, which is an angle of -π/2 relative to the *x* axis.

AngSignPi turn1 = atan(18.0/120.0); AngSignPi turn2 = -M_PI/2 - turn1; float distance = sqrt(120*120 + 18*18);

**Questions:** Write C++ code to solve each of these.

- A robot with a camera on a pan/tilt mount located directly above its arm sticks its arm straight out. The horizontal distance from its shoulder to its fingertip is 0.8 meters, and the vertical distance from its shoulder to the camera is 0.5 meters. Assuming a tilt angle of 0° means the camera is pointing out at the horizon, how far must the robot tilt the camera down to point it at its fingertip?
- A robot with a damaged tread drifts rightward at an angle of 15° when it tries to move forward. If the robot advances a distance of 8 meters, how far will it have traveled along its intended track, and how far will it have drifted off course?

### Reflection

- Summarize what you feel are the key points of this lesson.
- Make up a problem of your own that can be solved using the techniques you've learned, and show how to solve it.
- The mathematical abbreviations for cotangent, secant, and cosecant are
**cot**,**sec**, and**csc**. Why do you think these functions were omitted from the C math library? - Is there such a thing as an arc cosecant? How would you define it?