Slightly mathematical programming problem.

J

Jason Heyes

class Point
{
double x, y;

public:
Point(double x_, double y_) : x(x_), y(y_) { }

double get_x() const { return x; }

double get_y() const { return y; }

// returns an angle between 0 and 180 degrees
// pre: y is non-negative
double get_angle() const;
};

double Point::get_angle() const; // TODO: implement this!

// returns the point with greatest angle
// pre: all points have non-negative y-components and points is non-empty
Point find_max_angle(std::vector<Point> points)
{
Point max_point = points[0];
double max_angle = max_point.get_angle();

std::vector<Point>::size_type i;
for (i=1; i < points.size(); i++)
{
Point point = points;
double angle = point.get_angle();

if (angle > max_angle)
{
max_point = point;
max_angle = angle;
}
}

return max_point;
}

Not sure how to implement Point::get_angle. Clues anyone?
 
R

Raghu Uppalli

I guess the angle you are trying to find is between X axis and a line
from the origin (0,0) to the point(x,y). Is that correct?

If so, the angle should be

atan(y/x)

or something like that. I dont remember my trigonometry well enough..

hth,
Raghu
 
M

manuthomas23

You mean the slope made by the vector joining origin(0,0) and point p?
You can take atan(y/x) * 180 / PI, PI=>3.14159...
 
J

Jason Heyes

Raghu Uppalli said:
I guess the angle you are trying to find is between X axis and a line
from the origin (0,0) to the point(x,y). Is that correct?

If so, the angle should be

atan(y/x)

or something like that. I dont remember my trigonometry well enough..

I don't think that works. I need an angle between 0 and 180 degrees. You can
measure the angle in radians if you like but I thought degrees would be
clearer.
 
J

Jason Heyes

You mean the slope made by the vector joining origin(0,0) and point p?
You can take atan(y/x) * 180 / PI, PI=>3.14159...

Wouldn't that give me an angle between -90 and 90 degrees? I need an angle
between 0 and 180 degrees.
 
N

Niels Dybdahl

class Point
{
double x, y;

public:
Point(double x_, double y_) : x(x_), y(y_) { }

double get_x() const { return x; }

double get_y() const { return y; }

// returns an angle between 0 and 180 degrees
// pre: y is non-negative
double get_angle() const;
};

double Point::get_angle() const; // TODO: implement this!

You should do it this way:

double Point::get_angle() const {
const double PI=acos(-1);
double a=atan2(y, x);
return a*180/PI;
}

atan2 or similar is defined in most math libraries. I do not know if it is
standard C++.

Niels Dybdahl
 
J

Jason Heyes

Niels Dybdahl said:
You should do it this way:

double Point::get_angle() const {
const double PI=acos(-1);
double a=atan2(y, x);
return a*180/PI;
}

atan2 or similar is defined in most math libraries. I do not know if it is
standard C++.

Hey thanks a bunch. This solves my problem perfectly. I am so grateful!
 
J

Jeff Flinn

Jason Heyes said:
Hey thanks a bunch. This solves my problem perfectly. I am so grateful!

And you might want to replace your function with the following which will
properly handle the case of an empty vector:

typedef std::vector<Point> tPts;

tPts::iterator find_max_angle( tPts& aPts )
{
return std::max_element( aPts.begin()
, aPts.end()
, boost::bind
( std::less<double>()
, boost::bind( &Point::get_angle, _1 )
, _2
)
);
}

You can replace boost::bind above, with std::bind1st and std::mem_fn_ref(?)
where appropriate. You may also consider creating an overload to take a
const tPts& with a tPts::const_iterator return type as well.

Jeff Flinn
 
J

Jeff Flinn

Jeff Flinn said:
And you might want to replace your function with the following which will
properly handle the case of an empty vector:

typedef std::vector<Point> tPts;

tPts::iterator find_max_angle( tPts& aPts )
{
return std::max_element( aPts.begin()
, aPts.end()
, boost::bind
( std::less<double>()
, boost::bind( &Point::get_angle, _1 )
, _2

Oops: replace the previous line with:
, boost::bind( &Point::get_angle, _2 )

)
);
}

You can replace boost::bind above, with std::bind1st and
std::mem_fn_ref(?) where appropriate. You may also consider creating an
overload to take a const tPts& with a tPts::const_iterator return type as
well.

Other options are to use the non-predicate form of std::max_element with
boost's transform_iterator, or to provide and operator< for your Point
class.

Jeff Flinn
 
J

Jason Heyes

Jeff Flinn said:
Oops: replace the previous line with:


Other options are to use the non-predicate form of std::max_element with
boost's transform_iterator, or to provide and operator< for your Point
class.

Yes. I was going to ask for a briefer version of find_max_angle. You are one
step ahead of me. Since I would also like to use the original interface, I
will need this:

Point find_max_angle(std::vector<Point> points)
{
return *find_max_angle(points);
}

Everyone is happy!
 
J

Jeff Flinn

Jason said:
Yes. I was going to ask for a briefer version of find_max_angle. You
are one step ahead of me. Since I would also like to use the original
interface, I will need this:

Point find_max_angle(std::vector<Point> points)
{
return *find_max_angle(points);
}

Everyone is happy!

I don't think the above will compile due to overload ambiguity. In both
cases, a more explicit naming of the functions would be advised.

Except when points is empty. The at best you'll get an access violation, at
worst some problem in a totally unrelated area of the code that you'll spend
days trying to track down. You'll need to:

tPts::iterator lItr = find_max_angle(points);

if( lItr != points.end() )
{
return *lItr;
}
else
{
throw "points is empty";
}

Also, do you realize that the std::vector points is being copied?

Jeff Flinn
 
J

Jerry Coffin

Jason Heyes wrote:

[ ... ]
double Point::get_angle() const; // TODO: implement this!

If you care much about efficiency, you may NOT want to implement this
as stated. I'd just compute the slopes instead of the actual angles --
this is basically a single division (y/x) so it's quite fast. You're
discarding most of the values anyway, so computing the actual angle
instead of the slope doesn't really gain you much.
 
R

Richard Herring

In message said:
Jason Heyes wrote:

[ ... ]
double Point::get_angle() const; // TODO: implement this!

If you care much about efficiency, you may NOT want to implement this
as stated. I'd just compute the slopes instead of the actual angles --
this is basically a single division (y/x) so it's quite fast. You're
discarding most of the values anyway, so computing the actual angle
instead of the slope doesn't really gain you much.

But look out for the pathological cases. When x is 0, y/x is undefined,
but atan2(y, x) is fine. (Unless y is also 0 !)
 
J

Jason Heyes

Jerry Coffin said:
Jason Heyes wrote:

[ ... ]
double Point::get_angle() const; // TODO: implement this!

If you care much about efficiency, you may NOT want to implement this
as stated. I'd just compute the slopes instead of the actual angles --
this is basically a single division (y/x) so it's quite fast. You're
discarding most of the values anyway, so computing the actual angle
instead of the slope doesn't really gain you much.

The slope is not enough. You need the signs of x and y and you must handle
pathological cases (i.e., when x = 0). In the end you do no better than
atan2 efficiency-wise.
 
J

Jason Heyes

Jeff Flinn said:
I don't think the above will compile due to overload ambiguity. In both
cases, a more explicit naming of the functions would be advised.

Yes. I should have renamed the function.
Except when points is empty. The at best you'll get an access violation,
at worst some problem in a totally unrelated area of the code that you'll
spend days trying to track down. You'll need to:

tPts::iterator lItr = find_max_angle(points);

if( lItr != points.end() )
{
return *lItr;
}
else
{
throw "points is empty";
}

A precondition was stated so an if-statement is unnecessary.
Also, do you realize that the std::vector points is being copied?

Yes. This is not a concern unless points is large.
 
J

Jerry Coffin

Jason Heyes wrote:

[ ... ]
The slope is not enough. You need the signs of x and y and you must
handle pathological cases (i.e., when x = 0). In the end you do no
better than atan2 efficiency-wise.

Unfortunately, doing a bit of testing shows I was even further wrong
than this: after a little testing, it looks like even if you ignore the
pathological cases, atan2 is actually _substantially_ faster than just
the floating point division on its own (at least with the
compilers/libraries I usually use).

To make a long story short, what I previously said was clearly wrong,
and badly so at that. I apologize for the misinformation.
 
J

Jason Heyes

Jerry Coffin said:
Jason Heyes wrote:

[ ... ]
The slope is not enough. You need the signs of x and y and you must
handle pathological cases (i.e., when x = 0). In the end you do no
better than atan2 efficiency-wise.

Unfortunately, doing a bit of testing shows I was even further wrong
than this: after a little testing, it looks like even if you ignore the
pathological cases, atan2 is actually _substantially_ faster than just
the floating point division on its own (at least with the
compilers/libraries I usually use).

Interesting. I guess the atan2 routine works without floating point
division.
To make a long story short, what I previously said was clearly wrong,
and badly so at that. I apologize for the misinformation.

Not a problem. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top