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

This is a discussion on *Help w/sort and operator overloading* within the **C++ Programming** forums, part of the General Programming Boards category; Oh, well.
There's still NaNs if you want to screw with the relational operators.
Given a and b, where a ...

- 02-15-2008 #16
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) == falseAll 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

- 02-15-2008 #17
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.Ubuntu Desktop

GCC/G++

Geany (for quick projects)

Anjuta (for larger things)

- 02-15-2008 #18
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?Ubuntu Desktop

GCC/G++

Geany (for quick projects)

Anjuta (for larger things)

- 02-15-2008 #19

- 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)?

- 02-15-2008 #20
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!Ubuntu Desktop

GCC/G++

Geany (for quick projects)

Anjuta (for larger things)

- Exactly how to get started with C++ (or C) today
- C Tutorial
- C++ Tutorial
- 5 ways you can learn to program faster
- The 5 Most Common Problems New Programmers Face
- How to set up a compiler
- 8 Common programming Mistakes
- What is C++11?
- Creating a game, from start to finish

- How to create a shared library on Linux with GCC - December 30, 2011
- Enum classes and nullptr in C++11 - November 27, 2011
- Learn about The Hash Table - November 20, 2011
- Rvalue References and Move Semantics in C++11 - November 13, 2011
- C and C++ for Java Programmers - November 5, 2011
- A Gentle Introduction to C++ IO Streams - October 10, 2011