+ All Categories
Home > Documents > 7 Polar Coordinate Systems

7 Polar Coordinate Systems

Date post: 06-Jan-2016
Category:
Upload: dung-trinh
View: 17 times
Download: 0 times
Share this document with a friend
Description:
Polar Coordinate Systems
Popular Tags:

of 67

Transcript

Lecture Notes for Chapter 7: Polar Coordinate Systems

Chapter 7Polar Coordinate Systems

Ian ParberryUniversity of North TexasFletcher DunnValve Software3D Math Primer for Graphics & Game Development What Youll See in This ChapterThis chapter describes the polar coordinate system. It is divided into four sections. Section 7.1 describes 2D polar coordinates.Section 7.2 gives some examples where polar coordinates are preferable to Cartesian coordinates. Section 7.3 shows how polar space works in 3D and introduces cylindrical and spherical coordinates. Section 7.4 makes it clear that polar space can be used to describe vectors as well as positions.Chapter 7 Notes3D Math Primer for Graphics & Game Dev2Word CloudChapter 7 Notes3D Math Primer for Graphics & Game Dev3

Section 7.1:2D Polar CoordinatesChapter 7 Notes3D Math Primer for Graphics & Game Dev4Polar Coordinate SpaceRecall that 2D Cartesian coordinate space has an origin and two axes that pass through the origin.A 2D polar coordinate space also has an origin (known as the pole), which has the same basic purpose: it defines the center of the coordinate space.A polar coordinate space only has one axis, sometimes called the polar axis, which is usually depicted as a ray from the origin. It is customary in math literature for the polar axis to point to the right in diagrams, and thus it corresponds to the +x axis in a Cartesian system.It's often convenient to use different conventions than this, as we'll discuss later in this lecture. Until then, well use the traditional conventions of the math literature.Chapter 7 Notes3D Math Primer for Graphics & Game Dev5Chapter 7 Notes3D Math Primer for Graphics & Game Dev6

Polar CoordinatesIn Cartesian coordinates we described a 2D point using the using two signed distances, x and y. Polar coordinates use a distance and an angle. By convention, the distance is usually called r (which is short for radius) and the angle is usually called . The polar coordinate pair (r, ) species a point in 2D space as follows:Start at the origin, facing in the direction of the polar axis, and rotate by angle . Positive values of are usually interpreted to mean counterclockwise rotation, with negative values indicating clockwise rotation.Now move forward from the origin a distance of r units. Chapter 7 Notes3D Math Primer for Graphics & Game Dev7Chapter 7 Notes3D Math Primer for Graphics & Game Dev8

ExamplesChapter 7 Notes3D Math Primer for Graphics & Game Dev9

Polar DiagramsThe grid circles show lines of constant r. The straight grid lines that pass through the origin show lines of constant , consisting of points that are the same direction from the origin.Chapter 7 Notes3D Math Primer for Graphics & Game Dev10

Angular MeasurementIt really doesn't matter whether you use degrees or radians (or grads, mils, minutes, signs, sextants, or Furmans) to measure angles, so long as you keep it straight. In the text of our book we almost always give specific angular measurements in degrees and use the symbol after the number.We do this because we are human beings, and most humans who are not math professors find it easier to deal with whole numbers rather than fractions of . Indeed, the choice of the number 360 was specifically designed to make fractions avoidable in many common cases.However, computers prefer to work with angles expressed using radians, and so the code snippets in our book use radians rather than degrees.Chapter 7 Notes3D Math Primer for Graphics & Game Dev11Some Ponderable QuestionsCan the radial distance r ever be negative?Can ever go outside of 180 180?The value of the angle directly west of the origin (i.e. for points where x < 0 and y = 0 using Cartesian coordinates) is ambiguous. Is equal to +180 or 180 for these points?The polar coordinates for the origin itself are also ambiguous. Clearly r = 0, but what value of should we use? Wouldn't any value work?Chapter 7 Notes3D Math Primer for Graphics & Game Dev12AliasingThe answer to all of these questions is yes.In fact, for any given point, there are infinitely many polar coordinate pairs that can be used to describe that point.This phenomenon is known as aliasing. Two coordinate pairs are said to be aliases of each other if they have different numeric values but refer to the same point in space. Notice that aliasing doesn't happen in Cartesian space. Each point in space is assigned exactly one (x, y) coordinate pair.A given point in polar space corresponds to many coordinate pairs, but a coordinate pair unambiguously designates exactly one point.Chapter 7 Notes3D Math Primer for Graphics & Game Dev13Creating AliasesOne way to create an alias for a point (r, ) is to add a multiple of 360 to . Thus (r, ) and (r, + k360) describe the same point, where k is an integer. We can also generate an alias by adding 180 to and negating r; which means we face the other direction, but we displace by the opposite amount.In general, for any point (r, ) other than the origin, all of the polar coordinates that are aliases for (r, ) be expressed as:

Chapter 7 Notes3D Math Primer for Graphics & Game Dev14

Canonical Polar CoordinatesA polar coordinate pair (r, ) is in canonical form if all of the following are true:Chapter 7 Notes3D Math Primer for Graphics & Game Dev15

Algorithm to Make (r, ) Canonical If r = 0, then assign = 0.If r < 0, then negate r, and add 180 to .If 180, then add 360 until > 180If > 180, then subtract 360 until 180.Chapter 7 Notes3D Math Primer for Graphics & Game Dev16Chapter 7 Notes3D Math Primer for Graphics & Game Dev17

A Nit-Picky ObservationPicky readers may notice that while this code ensures that is in range radians, it does not explicitly avoid the case where = . The value cannot be represented exactly in floating point. In fact, because is irrational, it can never be represented exactly with any finite number of digits in any base! The value of the constant PI in our code is not exactly equal to , it's the closest number to that can be represented by a float. While double precision arithmetic is closer, its not exact. So, you can think of this function as returning a value from to , exclusive.Chapter 7 Notes3D Math Primer for Graphics & Game Dev18Converting from Polar to Cartesian Coordinates in 2DConverting polar coordinates (r, ) to the corresponding Cartesian coordinates (x, y) follows from the definition of sin and cos.x = r cos y = r sin Chapter 7 Notes3D Math Primer for Graphics & Game Dev19

Converting from Cartesian to Polar Coordinates in 2DComputing polar coordinates (r, ) from Cartesian coordinates (x, y) is slightly tricky. Due to aliasing, there isn't only one right answer; there are infinitely many (r, ) pairs that describe the point (x, y).Usually, we want canonical coordinates.We can easily compute r using Pythagoras's theorem

Chapter 7 Notes3D Math Primer for Graphics & Game Dev20

Solve for Computing r was pretty easy. Now solve for :Chapter 7 Notes3D Math Primer for Graphics & Game Dev21

Pause for ThoughtThere are two problems with this approach. The first is that if x = 0, then the division is undefined. The second is that arctan has a range from 90 to +90. The basic problem is that the division y/x effectively discards some useful information when x = y. Both x and y can either be positive or negative, resulting in four different possibilities, corresponding to the four different quadrants that may contain the point. But the division y/x results in a single value. If we negate both x and y, we move to a different quadrant in the plane, but the ratio x/y doesn't change.Because of these problems, the complete equation for conversion from Cartesian to polar coordinates requires some if statements to handle each quadrant, and is a bit of a mess for math people.Chapter 7 Notes3D Math Primer for Graphics & Game Dev22atan2Luckily, programmers have the atan2 function, which properly computes the angle for all x and y except for the pesky case at the origin. Borrowing this notation, let's define an atan2 function we can use in these notes in our math notation.Chapter 7 Notes3D Math Primer for Graphics & Game Dev23atan2Chapter 7 Notes3D Math Primer for Graphics & Game Dev24

Two Key ObservationsTwo key observations about this definition. First, following the convention of the atan2 function as found in the standard libraries of most computer languages, the arguments are in reverse order: y, x. You can either just remember that it's reversed, or you might find it handy to remember that atan2(y, x) is similar to arctan(y/x). Or remember that tan = sin / cos , and = atan2(sin , cos ).Second, in many software libraries, the atan2 function is undefined at the origin, when x = y = 0. The atan2 function we are defining for use in our equations in these notes is defined such that atan2(0, 0) = 0. In our code snippets we'll use atan2 and explicitly handle the origin as a special case, but in our equations, we'll use atan2 which is defined at the origin. (Note the difference in typeface.)Chapter 7 Notes3D Math Primer for Graphics & Game Dev25Computing Back to the task at hand: computing the polar angle from a set of 2D Cartesian coordinates.Armed with the atan2 function, we can easily convert 2D Cartesian coordinates to polar form.Chapter 7 Notes3D Math Primer for Graphics & Game Dev26

Code SnippetChapter 7 Notes3D Math Primer for Graphics & Game Dev27

Section 7.2:Why Use Polar Coordinates?Chapter 7 Notes3D Math Primer for Graphics & Game Dev28Why Use Polar Coordinates?Theyre better for humans (eg. I live 22 miles NNE of Dallas, TX)Theyre useful in video games (for cameras and turrets and assassins arms, oh my).Sometimes we even use 3D spherical coordinates for locating things on the globe latitude and longitude. More coming upChapter 7 Notes3D Math Primer for Graphics & Game Dev29Section 7.3:3D Polar SpaceChapter 7 Notes3D Math Primer for Graphics & Game Dev303D Polar SpaceThere are two kinds in common use:Cylindrical coordinates 1 angle and 2 distancesSpherical coordinates 2 angles and 1 distanceChapter 7 Notes3D Math Primer for Graphics & Game Dev31

3D Cylindrical SpaceTo locate the point described by cylindrical coordinates (r, , z), start by processing r and just like we would for 2D polar coordinates, and then move up or down the z axis by z.Chapter 7 Notes3D Math Primer for Graphics & Game Dev32

3D Spherical CoordinatesAs with 2D polar coordinates, 3D spherical coordinates also work by defining a direction and distance.The only difference is that in 3D it takes two angles to define a direction. There are two polar axes in 3D spherical space. The first axis is horizontal and corresponds to the polar axis in 2D polar coordinates or +x in our 3D Cartesian conventions. The other axis is vertical, corresponding to +y in our 3D Cartesian conventions.Chapter 7 Notes3D Math Primer for Graphics & Game Dev33Notational ConfusionDifferent people use different conventions and notation for spherical coordinates, but most math people have agreed that the two angles are named and . Math people also are in general agreement about how these two angles are to be interpreted to define a direction. You can imagine it like this:Chapter 7 Notes3D Math Primer for Graphics & Game Dev34Finding the Point (r, , )Begin by standing at the origin, facing the direction of the horizontal polar axis. The vertical axis points from your feet to your head.Rotate counterclockwise by the angle (the same way that we did for 2D polar coordinates).Point your arm straight up, in the direction of the vertical polar axis.Rotate your arm downward by the angle . Your arm now points in the direction specified by the polar angles , .Displace from the origin along this direction by the distance r, and we've arrived at the point described by the spherical coordinates (r, , ).Chapter 7 Notes3D Math Primer for Graphics & Game Dev35Chapter 7 Notes3D Math Primer for Graphics & Game Dev36

Azimuth, Zenith, Lat, and LongThe horizontal angle is known as the azimuth, and is the zenith. Other terms that you've probably heard are longitude and latitude. Longitude is basically , and latitude is the angle of inclination, 90 .So you see, the latitude/longitude system for describing locations on planet Earth is actually a type of spherical coordinate system. We're often only interested in describing points on the planet's surface, and so the radial distance r, which would measure the distance to the center of the Earth, isn't necessary. Chapter 7 Notes3D Math Primer for Graphics & Game Dev37Visualizing Polar CoordinatesThe spherical coordinate system described in the previous section is the traditional right handed system used by math people. We'll soon see that the formulas for converting between Cartesian and spherical coordinates are rather elegant under these assumptions. However, if you are like most people in the video game industry, you probably spend more time visualizing geometry than manipulating equations, and for our purposes these conventions carry a few irritating disadvantages:Chapter 7 Notes3D Math Primer for Graphics & Game Dev38Irritating Disadvantage 1The default horizontal direction at = 0 points in the direction of +x. This is unfortunate, since for us, +x points to the right or east, neither of which are the default directions in most people's mind. Similar to the way that numbers on a clock start at the top, it would be nicer for us if the horizontal polar axis pointed towards +z, which is forward or north.Chapter 7 Notes3D Math Primer for Graphics & Game Dev39Irritating Disadvantage 2The conventions for the angle are unfortunate in several respects.It would be nicer if the 2D polar coordinates (r, ) were extended into 3D simply by adding a third coordinate of zero, like we extend the Cartesian system from 2D to 3D. But the spherical coordinates (r, , 0) don't correspond to the 2D polar coordinates (r, ) as we'd like. In fact, assigning = 0 puts us in the awkward situation of Gimbal lock, a singularity we'll describe later. Instead, the points in the 2D plane are represented as (r, , 90).It might have been more intuitive to measure latitude, rather than zenith. Most people think of the default as horizontal and up as the extreme case.Chapter 7 Notes3D Math Primer for Graphics & Game Dev40Irritating Disadvantage 3No offense to the Greeks, but and take a little while to get used to. The symbol r isn't so bad because at least it stands for something meaningful: radial distance or radius. Wouldn't it be great if the symbols we used to denote the angles were similarly short for English words, rather than completely arbitrary Greek symbols?Chapter 7 Notes3D Math Primer for Graphics & Game Dev41Irritating Disadvantages 4 and 5It would be nice if the two angles for spherical coordinates were the same as the first two angles we use for Euler angles, which are used to describe orientation in 3D. (We're not going to discuss Euler angles until Chapter 8.)It's a right-handed system, and we use a left-handed system.Chapter 7 Notes3D Math Primer for Graphics & Game Dev42Spherical Coordinates for GamersThe horizontal angle we will rename to h, which is short for heading and is similar to a compass heading. A heading of zero indicates a direction of forward or to the north, depending on the context. This matches the standard aviation conventions. If we assume our 3D cartesian conventions described in Chapter 1, then a heading of zero (and thus our primary polar axis) corresponds to +z. Also, since we prefer a left-handed coordinate system, positive rotation will rotate clockwise when viewed from above.Chapter 7 Notes3D Math Primer for Graphics & Game Dev43Spherical Coordinates for GamersThe vertical angle is renamed to p, which is short for pitch and measures how much we are looking up or down. The default pitch value of zero indicates a horizontal direction, which is what most of us intuitively expect. Perhaps not-so-intuitively, positive pitch rotates downward, which means that pitch actually measures the angle of declination. This might seem to be a bad choice, but it is consistent with the left-hand rule. Later we'll see how consistency with the left-hand rule bears fruit worth suffering this small measure of counter-intuitiveness.Chapter 7 Notes3D Math Primer for Graphics & Game Dev44Chapter 7 Notes3D Math Primer for Graphics & Game Dev45

Aliasing in 3D Spherical CoordinatesThe first sure-fire way to generate an alias is to add a multiple of 360 to either angle. This is really the most trivial form of aliasing and is caused by the cyclic nature of angular measurements.The other two forms of aliasing are a bit more interesting, because they are caused by the interdependence of the coordinates. In other words, the meaning of one coordinate r depends on the values of the other coordinate(s) the angles) This dependency created a form of aliasing and a singularity:Chapter 7 Notes3D Math Primer for Graphics & Game Dev46The Aliasing and the SingularityThe aliasing in 2D polar space could be triggered by negating the radial distance r and adjusting the angle so that the opposite direction is indicated. We can do the same with spherical coordinates. Using our heading and pitch conventions, all we need to do is flip the heading by adding an odd multiple of 180, and then negate the pitch.The singularity in 2D polar space occurred at the origin, since the angular coordinate is irrelevant when r = 0. With spherical coordinates, both angles are irrelevant at the origin.

Chapter 7 Notes3D Math Primer for Graphics & Game Dev47Thats Not All, FolksSo spherical coordinates exhibit similar aliasing behavior because the meaning of r changes depending on the values of the angles. However, spherical coordinates also suffer additional forms of aliasing because the pitch angle rotates about an axis that varies depending on the heading angle. This creates an additional form of aliasing and an additional singularity, analogous to those caused by the dependence of r on the direction.Chapter 7 Notes3D Math Primer for Graphics & Game Dev48More Aliasing and SingularitiesDifferent heading and pitch values can result in the same direction, even excluding trivial aliasing of each individual angle. An alias of (h, p) can be generated by (h180, 180 p). For example, instead of turning right 90(facing east) and pitching down 45, we could turn left 90(facing west) and then pitch down 135. Although we would be upside down, we would still be looking in the same direction.A singularity occurs when the pitch angle is set to 90 (or any alias of these values). In this situation, known as Gimbal lock, the direction indicated is purely vertical (straight up or straight down), and the heading angle is irrelevant. We'll have a great deal more to say about Gimbal lock when we discuss Euler angles in Chapter 8.Chapter 7 Notes3D Math Primer for Graphics & Game Dev49Canonical Spherical CoordinatesJust as we did in 2D, we can define a set of canonical spherical coordinates such that any given point in 3D space maps unambiguously to exactly one coordinate triple within the canonical set. We place similar restrictions on r and h as we did for polar coordinates. Two additional constraints are added related to the pitch angle. Pitch is restricted to be in the range 90 to 90. Since the heading value is irrelevant when pitch reaches the extreme values of Gimbal lock, we force h = 0 in that case.Chapter 7 Notes3D Math Primer for Graphics & Game Dev50Conditions for Canonical Spherical CoordinatesChapter 7 Notes3D Math Primer for Graphics & Game Dev51

Algorithm to Make (r, p, h) Canonical If r = 0, then assign h = p = 0If r < 0, then negate r, add 180 to h, and negate pIf p < 90, then add 360 to p until p 90If p > 270, then subtract 360 from p until p 270If p > 90, add 180 to h and set p = 180 pIf h 180, then add 360 to h until h > 180If h > 180, then subtract 360 from h until h 180Chapter 7 Notes3D Math Primer for Graphics & Game Dev52Chapter 7 Notes3D Math Primer for Graphics & Game Dev53

Chapter 7 Notes3D Math Primer for Graphics & Game Dev54

Chapter 7 Notes3D Math Primer for Graphics & Game Dev55

Converting Spherical Coordinates to 3D Cartesian Coordinates.Let's see if we can convert spherical coordinates to 3D Cartesian coordinates.For now, our discussion will use the traditional right-handed conventions for both Cartesian and spherical spaces. Later we'll show conversions applicable to our left-handed conventions.Examine the figure on the next slide, which shows both spherical and Cartesian coordinates.Chapter 7 Notes3D Math Primer for Graphics & Game Dev56Chapter 7 Notes3D Math Primer for Graphics & Game Dev57

What is d?Notice that we've introduced a new variable d, the horizontal distance between the point and the vertical axis.Chapter 7 Notes3D Math Primer for Graphics & Game Dev58

Computing zFrom the right triangle with hypotenuse r and legs d and z, we see that z/r = cos , that is, z = r cos .Chapter 7 Notes3D Math Primer for Graphics & Game Dev59

Computing x and yConsider that if = 90, we basically have 2D polar coordinates.Let x' and y' stand for the x and y coordinates that would result if = 90.As in 2D, x' = r cos , y' = r sin .Notice that when = 90, d = r. As decreases, d decreases, and by similar triangles x/x' = y/y' = d/r. Looking at triangle dzr again, we observe that d/r = sin .

Chapter 7 Notes3D Math Primer for Graphics & Game Dev60

Putting it All TogetherPutting all this together, we have:x = r sin cos y = r sin sin z = r cos Using our gamer conventions:x = r cos p sin hy = r sin pz = r cos p cos hChapter 7 Notes3D Math Primer for Graphics & Game Dev61Continuingr is easy:

As before, the singularity at the origin when r = 0 is handled as a special case.The heading angle is surprisingly simple to compute using our atan2 function, h = atan2(x, z).This trick works because atan2 only uses the ratio of its arguments and their signs. Examine the equations x = r cos p sin h, y = r sin p, and z = r cos p cos h and notice that the scale factor of r cos p is common to both x and z. Furthermore, by using canonical coordinates we are assuming r > 0 and 90 p 90, thus cos p 0 and the common scale factor is always nonnegative.Chapter 7 Notes3D Math Primer for Graphics & Game Dev62

FinallyFinally, once we know r, we can solve for p from y.y = r sin py/r = sin pp = arcsin(y/r)The arcsin function has a range of 90 to 90, which fortunately coincides with the range for p within the canonical set.Chapter 7 Notes3D Math Primer for Graphics & Game Dev63Chapter 7 Notes3D Math Primer for Graphics & Game Dev64

Section 7.4:Using Polar Coordinates to Specify VectorsChapter 7 Notes3D Math Primer for Graphics & Game Dev65Using Polar Coords to Specify VectorsWe've seen how to describe a point using polar coordinates, and how to describe a vector using cartesian coordinates. It's also possible to use polar form to describe vectors.Polar coordinates directly describe the two key properties of a vector, its direction and length.As for the details of how polar vectors work, we've actually already covered them.Chapter 7 Notes3D Math Primer for Graphics & Game Dev66That concludes Chapter 7. Next, Chapter 8:Rotation in Three DimensionsChapter 7 Notes3D Math Primer for Graphics & Game Dev67


Recommended