PolyLine

The class PolyLine implements a piecewise linear function.

../../images/PolyLine.png

Given a sequence of coordinate pairs \((x_i,v_i)\) the PolyLine is constructed by the line segments between \((x_i,v_i)\) and \((x_{i+1},v_{i+1})\) for \(i < N\), where \(N\) is the number of coordinate pairs.

We define

\(f(x) = v_0\) for \(x < x_0\),
\(f(x) = v_N\) for \(x >= x_{N-1}\),
\(f(x) = v_{i-1} + (x - x_{i-1})(v_i - v_{i-1})/(x_i - x_{i-1})\) for \(x_{i-1} <= x < x_i\) (linear interpolation)
Code:

Public Methods

constructors

PolyLine(unsigned int iNumSegments);
PolyLine(PolyLine *pPL);
iNumSegments

Number of segments of poly line.

pPL

Pointer to a poly line.

The constructors allocate memory for the x-cordinates, the y-coordinates and the slopes.

destructor

~PolyLine();

addPoint

void addPoint(unsigned int i, double fX, double fV);
i

index of the new point.

fX

x-coordinate of the new point.

fV

y -coordinate of the point (value).

Adds the i-th point as (fX, fV), and calculates the slope of the line from the previous point.

constructor

double getVal(double fX);
fX

x-coordinate

Calculates the value of the poly line by interpolating the segment whose projection to the x-axis contains x.

write

void write(FILE *fOut);
fOut

Output file handle.

Writes a simple textfile containing the coordinate pairs of the poly line.

diplay

void display(const char *pIndent, const std::string sCaption);
pIndent

Indentation of output.

sCaption

Caption for output.

Writes the poly line data to screen (used for debugging)

readFromString

static PolyLine *readFromString(const std::string sData);
sData

String containing PolyLine data in a suitable format ( <x1> <v1> <x2> <v2> … <xn> <vn>).