Oh, well. :)

There's still NaNs if you want to screw with the relational operators.

Given a and b, where a == NaN:

(a < b) == false

(b < a) == false

(a == b) == false

Printable View

- 02-15-2008CornedBee
Oh, well. :)

There's still NaNs if you want to screw with the relational operators.

Given a and b, where a == NaN:

(a < b) == false

(b < a) == false

(a == b) == false - 02-15-2008dudeomanodude
Ya know, even applying these changes to the operator still leaves too many holes to fix in trying to sort things counter-clockwise. I think I'm going to redesign like this:

1. Get the three coordinates from user.

2. Compute theof the triangle.**centroid**

3. Use theas the origin, then sort with respect to theta.**centroid**

Geez, ya know this really simplifies things, unfortunately I've written a lot of code trying to fix these stupid "special cases". Oh well, I guess the more elegant solution always prevails. - 02-15-2008dudeomanodude
Ok, my previous post is what I have to do now. But how can I do this with sort()?

cpjust suggested creating a predicate function like:

Code:`bool compare(const Coord& lhs, const Coord& rhs){`

return lhs.theta < rhs.theta;

}

So once i know where the centroid is, and how far dy, dx it is from (0,0) it needs to look like this:

Code:`bool compare(const Coord& lhs, const Coord& rhs, double dx, double dy){`

// adjust lhs and rhs by dy, dx

// then return:

return lhs.theta < rhs.theta;

}

How can I accomplish this? - 02-15-2008cpjust
I don't deal with graphics, so most of what you guys are talking about it is like reading Egyptian hieroglyphics, but without understanding what you're trying to do, could you create two sort predicates and sort your data twice (once with each predicate)?

- 02-15-2008dudeomanodude
No need for the predicates. Sorting with origin at centroid worked out better than I thought.

Here's Coord:

Code:`class Coord{`

public:

Coord(double x, double y) : x_coord(x), y_coord(y), theta(0) {};

bool operator<(const Coord& rhs){ return theta < rhs.theta; };

double get_theta(){ return theta; };

double get_x(){ return x_coord; };

double get_y(){ return y_coord; };

void calc_theta(double, double);

private:

double x_coord;

double y_coord;

double theta;

};

void Coord::calc_theta(double dy, double dx){

double temp_x = x_coord - dx;

double temp_y = y_coord - dy;

if(temp_x < 0){

theta = atan(temp_y/temp_x) + PI;

}

else{

theta = atan(temp_y/temp_x);

}

}

Code:`void compute_centroid_thetas(list<Coord>& m_list, double& dy, double& dx){`

double x1, x2, x3, y1, y2, y3;

list<Coord>::iterator it = m_list.begin();

x1 = it->get_x();

y1 = it->get_y();

++it;

x2 = it->get_x();

y2 = it->get_y();

++it;

x3 = it->get_x();

y3 = it->get_y();

dy = (y1 + y2 + y3)/3;

dx = (x1 + x2 + x3)/3;

for(it = m_list.begin(); it != m_list.end(); ++it){

it->calc_theta(dy, dx);

}

}

Everything works flawlessly now. Thanks everyone for your help!