## Video Game Math: Parametrics

## What are Parametrics?

Parametrics are unusual in the fact that although they’re considerably more complicated when written out than your typical algebra equation.

Most programmers actually use them without even realizing that they’re doing it – often without even knowing that they’re a thing!

A parametric equation is a “Parameterized Equation”.

In ordinary algebraic functions, we normally see a “dependent variable” and an “independent variable” – for example:

y = 2x + 5

In this case, ‘y’ is the dependent variable, with x being the independent – we can figure out any value for y if we know an x value. This equation, when graphed, also gives us a line (making it a “linear equation”, although for this article’s purposes that doesn’t matter).

Although we could rewrite the equation to invert this notion – solving a value for X if we know the applicable Y value – it doesn’t solve “cleanly” in this state without adding additional steps. Functionally speaking, before a Y value can give us an X value, we would need to rewrite the equation as:

x = (y – 5)/2

This is great when you’re working with things like lines, where the equations are reversible – and when you’re working with any Function. Functions, in both computers and math, are algorithms where inputing a value/values will return a single value. In the above equation, a “proper” function form would be:

F(x) = 2x + 5

Or

G(y) = (y – 5)/2

## But what if we need to deal with a curve?

A vertical parabola is pretty easy – because they still pass the ‘function test’ (there’s only one Y value per X value) and you can “cheat” past a horizontal parabola (make a function that returns a single X based on a Y value) – but what about a Circle?

In programming, variables can only hold a single value (classes/structs can have multiple components, but each of those can only hold a single value). Therefore, all math performed in programming must only assign single values… this means that all programming math MUST be mathematical functions. So how can we possibly solve an object like a circle, where every X value has two Y values, and vice versa?

The basic equation for a circle is:

(𝑋−𝐶𝑥)2+(𝑌−𝐶𝑦)2=𝐶𝑟2

No matter how you distribute it, there’s no way to get “X = ?” or “Y = ?” out of that. Any function you would write would give you two values – the positive and negative root of a quadratic:

𝑋=2𝐶𝑥±√(2𝐶𝑥)2−4𝐶𝑥2−4𝑦2+8𝐶𝑦𝑌−4𝐶𝑦2+4𝐶𝑟22

Note the ‘±’ – making this algorithm return two values, so it’s useless to us as a programmer.

## What if we need an object to move in a circle or draw a circle on the screen?

The problem with the circle equation above is that both values are “dependent” on each other – there’s no independent variable to work with. So we need to create one.

In the case of a circle, there’s another variable we can’t see in the equation that can help us – if you remember math class in high school, most of your teachers probably had a “unit circle” on the board. One of the special things about the circle is that for every angle there is a corresponding sine and cosine – and if you use basic trigonometry (see the first installment of this series) you’ll remember that those values correlate to triangles.

In short – if we know the radius of a circle, the cosine of any angle multiplied by the radius will give you how far away that point is in the X axis – and the sine of any angle multiplied by the radius will give you the Y value. That works great if the circle is centered on the origin, but we also have to add the coordinates of the circle’s center if the circle isn’t on the origin. This gives us two smaller equations in a parametric “System of Equations” that will allow us to plot the values of X and Y independently. If we use θ to represent the angle as our independent variable, the system looks like:

{𝑥=𝐶𝑟cos(𝜃)+𝐶𝑥𝑦=𝐶𝑟sin(𝜃)+𝐶𝑦

In programming, we could then write a “For” loop to iterate through all possible angles (at whatever level of precision/timing necessary) to give us a circle. A similar (albeit more complex pattern) would allow us to parameterize a sphere in 3D.

Although this article is explicitly addressing curves – which leads into the discussion on Polar/Spherical Equations next week – parametric equations can be used in most situations where a variable can’t be conveniently isolated. They can also be used in situations where there is an independent variable, although this is usually over-complicating in those situations.