Thread: Help w/sort and operator overloading

  1. #16
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    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
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  2. #17
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    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.
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  3. #18
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    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;
    }
    But it will have to take into account that I want to sort with origin at the centroid.

    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;
    }
    I don't think it will work like I've got it there, but can you see what I'm trying to do?

    How can I accomplish this?
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  4. #19
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    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)?

  5. #20
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    Quote Originally Posted by cpjust View Post
    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:
    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);
    	}
    }
    Here's how I compute the thetas with origin at centroid:
    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);
    	}
    }
    than I just call sort(). No special cases (except adding PI to theta in Quadrants 2 and 3).

    Everything works flawlessly now. Thanks everyone for your help!
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

Popular pages Recent additions subscribe to a feed