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
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
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 the centroid of the triangle.
3. Use the centroid as the origin, then sort with respect to theta.
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.
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:
But it will have to take into account that I want to sort with origin at the centroid.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:
I don't think it will work like I've got it there, but can you see what I'm trying to do?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?
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)?
No need for the predicates. Sorting with origin at centroid worked out better than I thought.
Here's Coord:
Here's how I compute the thetas with origin at centroid: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);
}
}
than I just call sort(). No special cases (except adding PI to theta in Quadrants 2 and 3).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!