[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

creation of a set of segments, please help

aaragon

11/13/2008 11:08:00 PM

Hello everybody,

I have an interesting problem for which I still don't have a solution.
Imagine that you're working with points in two-dimesional space, so
the point class should be (simplifying):

class Point {

double x_,y_;

public:

Point(double x, double y) : x_(x), y_(y) {}
// other constructors and member functions
};

Now, imagine that you're also working with segments in 2D. A segment
consists of its endpoints:

class Segment {

Point source_, target_;
public:
Segment(const Point& s, const Point& t) : source_(s), target_(t)
{}
// other constructors and member functions
};


Now, I want to enforce that if I have points P1, P2, the segment P1-P2
is the same as the segment P2-P1. That is, the orientation of the
segment doesn't matter so as long as the endpoints are the same, the
segments are equal. This could be enforced by operator== in the
Segment class:

bool Segment::operator==(const Segment& s)
{ return ((source_ == s.source_ && target_ == s.target_) || (target_
== s.source_ && source_ == s.target_)); }


Now, I would like to create a set of segments. Of course, the segment
does not have to have duplicates. However, sets are defined by using
operator< which is not defined here.

So the question is, how do I create a std::set of segments? Any ideas?
I could just write operator< so that it compares the smaller point
from both segments, but I thought that someone could have a better
solution.

Thank you all,

aa
4 Answers

SG

11/13/2008 11:25:00 PM

0

On 14 Nov., 00:08, aaragon <alejandro.ara...@gmail.com> wrote:
> bool Segment::operator==(const Segment& s)
> { return ((source_ == s.source_ && target_ == s.target_) || (target_
> == s.source_ && source_ == s.target_)); }

You have to be very careful with comparing doubles -- especially in
case of many geometric algorithms. They work fine on paper but when it
comes to the implementation rounding errors can be a pain in the a**.

> Now, I would like to create a set of segments. Of course, the segment
> does not have to have duplicates. However, sets are defined by using
> operator< which is not defined here.

Right. Anyhow, using points and segments (with doubles as members) as
keys in a set / map might do you harm. In theory you can still work
out a total order. For example: compare Points lexically and store
segments with source<target and compare segments also lexically.

> So the question is, how do I create a std::set of segments? Any ideas?

Try to avoid it whenever possible.

Cheers!
SG

aaragon

11/13/2008 11:30:00 PM

0

On Nov 13, 5:24 pm, SG <s.gesem...@gmail.com> wrote:
> On 14 Nov., 00:08, aaragon <alejandro.ara...@gmail.com> wrote:
>
> > bool Segment::operator==(const Segment& s)
> > { return ((source_ == s.source_ && target_ == s.target_) || (target_
> > == s.source_ && source_ == s.target_)); }
>
> You have to be very careful with comparing doubles -- especially in
> case of many geometric algorithms. They work fine on paper but when it
> comes to the implementation rounding errors can be a pain in the a**.
>
> > Now, I would like to create a set of segments. Of course, the segment
> > does not have to have duplicates. However, sets are defined by using
> > operator< which is not defined here.
>
> Right. Anyhow, using points and segments (with doubles as members) as
> keys in a set / map might do you harm. In theory you can still work
> out a total order. For example: compare Points lexically and store
> segments with source<target and compare segments also lexically.
>
> > So the question is, how do I create a std::set of segments? Any ideas?
>
> Try to avoid it whenever possible.
>
> Cheers!
> SG

Thanks for replying to my post. This is what I came up with since atan
() returns the angle between -pi/2 and pi/2:

struct SegmentSorter {
bool operator()(const segment_type& s1, const segment_type& s2) const
{
// compute angle of first segment
double phi1 = atan((s1.t_.y() - s1.s_.y())/(s1.t_.x() - s1.s_.x()));
// compute angle of second segment
double phi2 = atan((s2.t_.y() - s2.s_.y())/(s2.t_.x() - s2.s_.x()));
return phi1 < phi2;
}
};


typedef std::set<segment_type, SegmentSorter> segment_set;

What do you think?

aa

aaa

10/13/2012 3:02:00 PM

0

Fred E Brown wrote:

>
> "aaa" <anom@aol.com> wrote in message news:dHces.251676$F52.106384@fx22.am4...
> > abelard wrote:
> >
> > >
> > >
> > >
> > > during the 'debate', obarmy rose up to 3%....then as the awful
> >> truth started to dawn...2% was wiped back off...
> > >
> > > my expectation was the biden farce will be a slow burning fuse....
> > >
> > > the last 1% gain has recently been eroded...and another 2%
> >> has now gone....
> > >
> > >
> > > that is a drop of 12%(from ~71.5) since an apparently drunken biden
> >> grimaced and shouted and smirked his way to 'victory'
> > >
> > > at least obarmy appreciates him...
> > >
> > >
> > > have you noticed that weak leaders always try to appoint
> >> the dumbest person they can find as right hand man...
> > > because that way they are less vulnerable to challenge from more
> >> intelligent rivals?
> > >
> > > note that romney appointed the strongest candidate he could find
> >> ...the sign of a confident and able leader
> > >
> > >
> > > you can follow intrade here
> > > https://data.intrade.com/graphing/jsp/timeAndSalesForm.jsp?contractId=743474&tradeURL=https://www.i...
> > >
> > > if you want to understand why this is interesting....look up
> >> 'the wisdom of crowds'
> >
> > So far, Obama has decided that invading another country every 5 minutes might not be a good idea and that there should be a medical safety net for the USA population.
> > So what is it about Obama that has you so "excercised", is it perhaps the color of his skin?
>
> Mainly it the fact that Obama wants to pay for all this with money the country
> does not have, he promotes the myth that taxing the rich to death will cure the
> multi-trillion dollar debt and leads his intellectually challenged followers to mistakenly
> believe that somehow some of the rich's wealth will find it's way into their pockets.
> Any increased tax revenue under Obama will not go towards reducing the national
> debt, it will go to new spending, driving the debt even higher.

OK, at least that is rationale view.
But if true, it seems a little strange that the like of Warren Buffett are saying that the rich pay less (as a percentage of their income) than the poor.

Buffett stated that he only paid 19% of his income for 2006 ($48.1 million) in total federal taxes (due to their being from dividends & capital gains),
while his employees paid 33% of theirs, despite making much less money.
?How can this be fair?? Buffett asked, regarding how little he pays in taxes compared to his employees. "How can this be right?"
He also added: "There's class warfare, all right, but it's my class, the rich class, that's making war, and we're winning."

Sanders Kaufman [MCSD]

10/14/2012 8:50:00 AM

0

"Fred E Brown" wrote in message
news:50797204$0$15387$bb4e3ad8@newscene.com...

> Mainly it the fact that Obama wants to pay for all this with money the
> country does not have,

Perhaps, but *everyone* still needs healthcare - even if you don't think
it's worthwhile.
What happens is that you just end up going anyway, without funding the visit
at all.

The proper solution is to pay your bills and get the healthcare you need -
rather than just cheating everybody you own out of what you owe them.