In the examples you'll see in the following pages there will be many snippets of code that are repeated between examples. For instance the algorithm for finding the distance between two points in a two-dimensional space is used MANY times. In practice you may wish to place these repeated parts of the code into individual functions in your own custom made library so that you can reuse them between different parts of an engine.

For the sake of these examples however we will more often then not leave the code in a raw format within each example to make it clear exactly what needs to happen to complete said example.

Finding Slope of 2 Points

The "Slope" of a line or line segment refers to the ratio of a vector representing an angle from an axis.

Basically it's the angle of a line on a graph.

Finding a Square Root

The Square Root of a given number "x" is the amount "y" times itself.

In other words, for example, "the square roots of 9 is 3 mulitplied by 3". This may seem inconsequential at first but be assured this figure will be included in many popular equations. For instance it is the foundation of the algorithm to find the distance between two points. Imagine how much advanced math would suddenly fail if we were unable to find the distance between objects!

float beer

Truth be told, it is probably better to use an IDE's built-in square root equations, should they be available. What that means is that if you want to find a square root in, for instance, JavaScript, then you are probably better off invoking JavaScript's "Math.sqrt(x);" function then trying to code your own. This is for two reasons.

In the case of popular programs like the Chrome Web Browser you are basically taking advantage of the engineering ability of the programmers who work for Google itself, the producers of Chrome, whose job it is to make the most effecient interpretation of this function possible.

In the case of some othe languages like C++ or C# simply writing "sqrt(x);" will, with most compilers, be able to take advantage of some low level instructions that might have even been built into the CPU hardware itself. You can't get more optimized than that.

Area of Primitive Shapes

The approach to finding the total area of primitive shapes will change depending on the shape. Thankfully none of them are very complex. Keep in mind that by "primitive" we mean shapes like rectangles, circles, and triangles, etc.

For more complex objects that have an arbitrary shape see the next section.

Let's start wtih rectangles.

area = width * height

Rectangles

Most people are aware that to find the area of a rectangle you simply multiply the width by the height. What is probably the most important thing to realize about rectangles in computer science is that they are not always defined by width and height. Every object stored in a computer's memory has to have an origin point. The question is; where is that origin point? There are a few approaches.

  1. Origin in one corner of the rectangle is point 1. Opposite diagonal corner is point 2. Both points are are defined by x and a y values.
  2. Origin in one corner of the rectangle is point 1. Opposite diagonal corner is point 2. Point 1 is measured in x and y values but point 2 is measured in width and height.
  3. Origin is the center of the rectangle as point 1. Width and height are kept as well but are divided in half to find the extent to the side edges.
  4. Instead of two points we keep four values. These values define the top, bottom, left, and right extents. This form of rectangle is really only usable in axis aligned rectangles.

The beauty of any of these systems is that any of them can also be applied to 3D boxes by simply adding a "Z" property to each of the points (or "front" and "back" values in the case of the last option).

For pretty much all of the examples found throughout this website we'll only use one of the first two options unless otherwise noted. Converting between the two is simple enough if needed.

  1. To find the rightmost extent of a rectangle stored as an origin point along with width and height values we would simply add the "X" value of the origin and the "Width" value together.
  2. To find the width of a rectangle stored as an origin point and the opposite corner point, we simply subtract p1 from p2.

And of course you can do the same with heights and y values.

The example below is a simple visualization of the process. In this case we've stored the rectangle as two points and the width and height values are found by looking for the absolute of the difference between those two points.

Circles

Here...

A=πr2

That's it. That's the area of a circle. You don't need the center point position, you only need the radius, and the value of Pi either as a constant or written out. For instance since we can't actually write the symbol for "Pi" we can just use the shorthand constant (or just write out the number). Like so...

area = radius * radius * 3.14159

You'll notice that in the example above we can't define a negative value for the radius. There's probably never a reason for you to ever store a negative radisu but it wouldn't make sense anyway. If for whatever reason your radius is set to below zero then you'll want to either constantly check for that and raise it to a positive value or use an "absolute" function to ensure a positive radius value.

Triangles

Triangles, depending on the type, are fairly easy as well. Especially when you realize that you can picture almost all triangles as rectangles that have been cut diagonally. And even if you aren't dealing with a right angle triangle (a triangle with one 90 degree angle and two acute angles) you can divide the triangle into two sections which can be seen as two rectangles sitting next to one another.

Luckily the formula we'll be using doesn't care if you're dealing with a Right triangle, an Equilateral triangle (in which all three sides are equal), or an Acute triangle (three acute angles).

A = hb / 2
Or...
Area = Height * Base / 2

Area of Polygons

Under constructions