Math in Unity : difference between Lines, Segments and Rays

Matteo Lo Piccolo
2 min readNov 2, 2021

--

Before moving on to 3D, we will talk about lines, segments and rays.
It will be just some basic notions to better understand how some mechanisms work.
This is because, as we have seen, formulas are incredibly useful, but sometimes we have to use little “tricks” to adapt them to the code.

We know that the Vector is represented by an “arrow” in space.
He does not have a specific position, but he has his own DIRECTION and MAGNITUDE.

A line, on the other hand, is represented by two POINTS,
a point A and a point B.
The important thing to know is that :

LINE goes through these points, but continues TO INFINITY ON BOTH SIDES.

A segment, on the other hand, is a small part of the LINE, but has a BEGINNING AND AN END.

So, in the example above, Point A is the start and Point B is the end of the segment.

Finally we have the RAYS.

The RAYS resemble the Vectors: they have a starting point, a direction, but CONTINUE TO INFINITY.

Now, if we look at this picture

The mathematical formula that represents a LINE is

y = mx + q

In this formula, the mx represents the SLOPE with respect to the x axis,
while the q represents the intersection point on the y axis.

I leave you a reference video where, in my opinion, the LINE formula is very well explained
https://www.youtube.com/watch?v=uOboYVGdwVg

The slope formula will be very useful later on as well, and it is calculated like this:

Put simply, if we have two Vectors A and B,
one (1, 2) and the other (3, 4)

slope = (4 -2 ) / (3 -1)
slope = 2 / 2
slope = 1

Normally, a slope of 1 equals an angle of 45 °.

Slope formula reference
https://www.khanacademy.org/math/cc-eighth-grade-math/cc-8th-linear-equations-functions/8th-slope/a/slope-formula

In the next article we talk about linear interpolation.

--

--

Matteo Lo Piccolo
Matteo Lo Piccolo

Written by Matteo Lo Piccolo

Always in love with programming, even if late (I'm already 39 years old) I decided to follow my dream! We will see how far my passion will take me!

No responses yet