Listing 1: Segment class and a function object that orders the segments
based on a scanline position
class Segment
{
public:
// Return y-coord for point on segment given its x-coord.
int evaluate(const int xcoord) const
{
return lower_y + slope() * (xcoord - lower_x);
}
// other functions not shown
private:
int lower_x, lower_y, upper_x, upper_y;
// other members not shown
};
class fCompare
{
public:
fCompare(int scan_pos) : m_scan_pos(scan_pos) {}
bool operator() (const Segment& lhs, const Segment& rhs) const
{
if (lhs.evaluate(m_scan_pos) < rhs.evaluate(m_scan_pos))
return true;
else
// handle ties
}
private:
int m_scan_pos;
};