# Difference between revisions of "Math:Coordinate Systems"

(added info on polar coordinates and fixed some stuff in cartesian) |
(→Cartesian and Polar: Added C++ code samples) |
||

Line 40: | Line 40: | ||

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: <math>d = \sqrt{\Delta x^2 + \Delta y^2}</math>. Next, to get the angle, we use <math>\arctan \frac{\Delta y}{\Delta x}</math>. 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. | 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: <math>d = \sqrt{\Delta x^2 + \Delta y^2}</math>. Next, to get the angle, we use <math>\arctan \frac{\Delta y}{\Delta x}</math>. 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: | ||

+ | |||

+ | <source type="cpp"> | ||

+ | 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; | ||

+ | } | ||

+ | </source> | ||

Converting from polar to Cartesian is somewhat more simple: <math>\left ( x, y \right ) = \left ( r * \cos \theta , r * \sin \theta \right )</math>. | Converting from polar to Cartesian is somewhat more simple: <math>\left ( x, y \right ) = \left ( r * \cos \theta , r * \sin \theta \right )</math>. | ||

+ | |||

+ | <source type="cpp"> | ||

+ | Point2f PolarToCartesian(PointPolar point) | ||

+ | { | ||

+ | Point2f nPoint; | ||

+ | nPoint.x = cos(point.direction) * point.magnitude; | ||

+ | nPoint.y = -sin(point.direction) * point.magnitude; | ||

+ | |||

+ | return nPoint; | ||

+ | } | ||

+ | </source> | ||

+ | |||

=== Cartesian to Spherical === | === Cartesian to Spherical === | ||

=== Cartesian to Cylindrical === | === Cartesian to Cylindrical === |

## Revision as of 06:30, 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; }