# Difference between revisions of "Math:Coordinate Systems"

(→Cartesian to Spherical: More C++ code samples) |
(→Cartesian to Cylindrical) |
||

Line 103: | Line 103: | ||

</source> | </source> | ||

− | === Cartesian | + | === Cartesian and Cylindrical === |

+ | Conversion between cartesian and cylindrical systems are exactly the same as polar-cartesian conversions; though the new z coordinate exists, it will not be changed through the conversion. |

## Latest revision as of 07:38, 23 March 2013

In mathematics, points in space can be represented using several different methods.

## Contents

## Coordinates in 2D

### Cartesian

The Cartesian coordinate system is the most common system used to represent points in space. Generally, it's represented like this:

A point in this coordinate system takes the form ; x is the distance on the horizontal axis, and y is the distance on the vertical axis. For example, point on the Cartesian plane is 2 units to the right and 3 units upward:

Each quadrant of the Cartesian plane, separated by the axes, is numbered:

### Polar

Polar coordinates aren't used as often as Cartesian, but they can still be very useful, especially in games. Where Cartesian coordinates work with rectangles, polar coordinates work with circles:

Points in the polar system are in the form of . r is the distance (radius) from the origin, and *θ* (theta) is the counter-clockwise angle with 0 being straight right. There are two main units for measuring the angle: degrees, of which there are 360 in a circle; and radians, of which there are 2π. Note that r cannot be negative.

## Coordinates in 3D

### Cartesian

### Spherical

### Cylindrical

## Conversion Between Systems

### Cartesian and Polar

To convert Cartesian to polar, we first need the distance from what will be the origin. This is done with an extension of the Pythagorean theorem: . Next, to get the angle, we use . However, arc tangent can only give an angle between -90 and 90 degrees; most languages should have an atan2 function, but if not, you can still get it by adding 180° to the arc tangent result if Δx is negative.

Here is a C++ example demonstrating how one might implement this conversion routine:

PointPolar CartesianToPolar(Point2f point) { PointPolar nPoint; if (point.x != 0.0f || point.y != 0.0f) { nPoint.magnitude = point.GetLength(); nPoint.direction = atan2(point.y, point.x); } return nPoint; }

Converting from polar to Cartesian is somewhat more simple: .

Point2f PolarToCartesian(PointPolar point) { Point2f nPoint; nPoint.x = cos(point.direction) * point.magnitude; nPoint.y = -sin(point.direction) * point.magnitude; return nPoint; }

### Cartesian and Spherical

PointSpherical CartesianToSpherical(Point3f point) { PointSpherical nPoint; nPoint.magnitude = point.GetLength(); if (point.x != 0.0f || point.y != 0.0f) { nPoint.direction = atan2(point.y, point.x); } if (point.z != 0.0f) { nPoint.pitch = asin(point.z/magnitude); } return nPoint; }

Point3f SphericalToCartesian(PointSpherical point) { float zPlaneScalar = cos(point.pitch); Point3f nPoint; nPoint.z = sin(point.pitch) * point.magnitude; nPoint.x = cos(point.direction) * point.magnitude * zPlaneScalar; nPoint.y = -sin(point.direction) * point.magnitude * zPlaneScalar; return nPoint; }

### Cartesian and Cylindrical

Conversion between cartesian and cylindrical systems are exactly the same as polar-cartesian conversions; though the new z coordinate exists, it will not be changed through the conversion.