RobotMath: Solving Triangles, Part 1

From Tekkotsu Wiki

Jump to: navigation, search

Contents

Significance of Triangles

Head constructed from a triangular mesh

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 every computer game with 3D graphics. Trigonometry (Greek for measurement of triangles) underlies much of the higher mathematics used in engineering, physics, and graphics. In this lesson you will learn some of the mathematical properties of triangles, how to reason about them, and how to calculate these properties 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.

File:Tri-abc.png

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

File:Angle-arm.png

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.

File:Tri_normal.png

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

File:Right-triangle.png

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), with the hypotenuse being the line from (0,0) to (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. Prove that sin2(θ) + cos2(θ) = 1. Hint: start with the Pythagorean theorem, plus x = r cos(θ) and y = r sin(θ).
  5. Solve for x given r and sin(θ). Hint: use sin2(θ) + cos2(θ) = 1.
  6. Harder: Solve for x given r and tan(θ). Hint: use the Pythagorean theorem plus y=x tan(θ).
  7. 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?
  8. 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° - θ.

File:Right-triangle-phi.png

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.

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 32+42=52. What are the angles θ and φ in this triangle?

\tan \theta = \frac{\mbox{opposite}_\theta}{\mbox{adjacent}_\theta} = \frac{4}{3}
\theta = \arctan(4/3) = 53.13^\circ
\phi = \arctan(3/4) = 36.87^\circ

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:

\tan \theta = \frac{y}{x} = \frac{0.05}{1.9}
 \theta = \arctan (0.05/1.9) = 1.5^\circ

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) incorrectly 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 if the x axis points North. Therefore, the two turns and the distance can be calculated by:

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

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

  1. 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?
  2. 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

  1. Summarize what you feel are the key points of this lesson.
  2. Make up a problem of your own that can be solved using the techniques you've learned, and show how to solve it.
  3. 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?
  4. Is there such a thing as an arc cosecant? How would you define it?

Next: RobotMath: Trigonometry on the Unit Circle