Line segment
A line is the shortest straight path that goes through two points. A line extends infinitely in both directions. Like its 2D counterpart, the 3D line we are going to implement will actually be a Line Segment. We define this line segment using a Start point and an End point:
Getting ready
We are going to define a Line
structure that holds start and end points. This structure represents a line segment. We will also implement two helper functions, Length
and LengthSq
. These functions will help us find the length and squared length of the line segment.
How to do it…
Follow these steps to implement a 3D line segment:
Add the declaration of
Line
toGeometry3D.h
:typedef struct Line { Point start; Point end; inline Line() {} inline Line(const Point& s, const Point& e) : start(s), end(e) { } } Line;
Declare the helper functions
Length
andLengthSq
inGeometry3D.h
:float Length(const Line& line); float LengthSq(const Line& line);
Create a new file,
Geometry3D...