473,387 Members | 1,606 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

API for intersection.

I want to create a program that has an "element" class, the element is
a graphic object that has a finite shape. It goes some what like
this :

class Element
{
public :
// Returns the minimum radius that encompass the holle element
double radius() = 0;

void center(double &x, double &y) = 0;

double distance(Element &e) = 0;

// naive implementation
bool intersect(Element &e)
{
return distance(*this) < radius() + e.radius();
}
};

Now suppose I have a decedent class that holds a "circle element" and
another that holds a "square element", how do I make a smarter
intersect that compares both of them? If I simply create a "bool
intersect(CircleElement &e)" in the SquareElement class it will not be
called if I use this code :

Element *a = new CircleElement(...);
Element *b = new SquareElement(...);

if (b->intersect(a)) {...}

Because C++ does not make the dynamic binding on the parameters, only
on the class. So the best I can do is to compare a SquareElement to a
generic element. Is there a way out? How this is done usually?

Apr 24 '07 #1
3 1689
Victor Bogado wrote:
I want to create a program that has an "element" class, the element is
a graphic object that has a finite shape. It goes some what like
this :

class Element
{
[..]
// naive implementation
bool intersect(Element &e)
{
return distance(*this) < radius() + e.radius();
}
};

Now suppose I have a decedent class that holds a "circle element" and
another that holds a "square element", how do I make a smarter
intersect that compares both of them? [..] How this is done usually?
Read about "double dispatch". You will most likely have to make your
'Element' aware of all descendants, and then implement No-Op functions
that would 'intersect' the 'Element' with any of those. And then make
the descendants implement those they know about in a funny redirected
way:

class Element { // Circle Square Triangle will derive
public:
virtual bool intersectCircle(Element&) { return false; }
virtual bool intersectSquare(Element&) { return false; }
virtual bool intersectCircle(Element&) { return false; }
virtual bool intersect(Element&) = 0;
};
class Circle : public Element {
public:
virtual bool intersectCircle(Element&);
virtual bool intersectSquare(Element&);
virtual bool intersectCircle(Element&);
virtual bool intersect(Element& e) {
return e.intersectCircle(*this);
}
};

class Square : public Element {
public:
virtual bool intersectCircle(Element&);
virtual bool intersectSquare(Element&);
virtual bool intersectCircle(Element&);
virtual bool intersect(Element& e) {
return e.intersectSquare(*this);
}
};

class Triangle : public Element {
public:
virtual bool intersectCircle(Element&);
virtual bool intersectSquare(Element&);
virtual bool intersectCircle(Element&);
virtual bool intersect(Element& e) {
return e.intersectTriangle(*this);
}
};

The other functions implement as intended/required.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 24 '07 #2
Victor Bogado wrote:
I want to create a program that has an "element" class, the element is
a graphic object that has a finite shape. It goes some what like
this :

class Element
{
public :
// Returns the minimum radius that encompass the holle element
double radius() = 0;

void center(double &x, double &y) = 0;

double distance(Element &e) = 0;

// naive implementation
bool intersect(Element &e)
{
return distance(*this) < radius() + e.radius();
}
};

Now suppose I have a decedent class that holds a "circle element" and
another that holds a "square element", how do I make a smarter
intersect that compares both of them? If I simply create a "bool
intersect(CircleElement &e)" in the SquareElement class it will not be
called if I use this code :

Element *a = new CircleElement(...);
Element *b = new SquareElement(...);

if (b->intersect(a)) {...}

Because C++ does not make the dynamic binding on the parameters, only
on the class. So the best I can do is to compare a SquareElement to a
generic element. Is there a way out? How this is done usually?
Victor described one way. The static method of doing does not extend
easily.

The way I've done these things in the past is to have a dynamic system
where you register all the types of algorithms (Square/Square,
Circle/Square etc) and select the algorithm based on the type of
intersection. It still breaks down when you have 2 different people
extend the class types and you're left with missing methods, but you can
implement a rudimentary generic geometry like spline or nurb and
degenerate your interestions into these types as an approximation.

Apr 24 '07 #3
Victor Bazarov wrote:
Victor Bogado wrote:
>I want to create a program that has an "element" class, the element is
a graphic object that has a finite shape. It goes some what like
this :

class Element
{
[..]
// naive implementation
bool intersect(Element &e)
{
return distance(*this) < radius() + e.radius();
}
};

Now suppose I have a decedent class that holds a "circle element" and
another that holds a "square element", how do I make a smarter
intersect that compares both of them? [..] How this is done usually?

Read about "double dispatch". You will most likely have to make your
'Element' aware of all descendants, and then implement No-Op functions
that would 'intersect' the 'Element' with any of those. And then make
the descendants implement those they know about in a funny redirected
way
To the OP, Scott Meyers discusses this topic at length (~30 pages, IIRC)
in "More Effective C++". Well worth checking out.
Apr 24 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mickel Grönroos | last post by:
Hi! Are there any standard list methods for getting the intersection and difference of two lists? (The union is easy ("list1.extend(list2)"), unless you want it to contain unique values.) ...
5
by: Antoine Logean | last post by:
Hi, What is the easiest way to get the intersection of two strings in python (a kind a "and" operator) ? ex: string_1 =...
17
by: Gordon Williams | last post by:
Hi, I have to lists that I need to find the common numbers (2nd rounded to nearest integral) and I am wondering if there is a more efficient way of doing it. >>> a= >>> b= >>> ...
3
by: Alain Frisch | last post by:
Hello, The following rule in the XML Schema spec, section "Schema Component Constraint: Attribute Wildcard Intersection" seems strange to me: ======================================= 3 If...
2
by: James Stroud | last post by:
Hello All, I find myself in this situation from time to time: I want to compare two lists of arbitrary objects and (1) find those unique to the first list, (2) find those unique to the second...
4
by: Sebastian Cohen S | last post by:
Hello, I am new to SQL and currently using Access 2003 and need a little help on the following. I have two queries each contains one column formed with a string that is very similar (only...
6
by: kimos | last post by:
hi all, how to calculate the intersection of 2 rectangle a rectangle is the following: Rectangle makeRectangle (Point lowerLeft, Point upperRight) { Rectangle r;
3
by: cai_rongxi | last post by:
Hi, Can some body share the code to find the intersection of two rectangles? Thanks in advance
2
by: mkppk | last post by:
I have kind of strange change I'd like to make to the sets.Set() intersection() method.. Normally, intersection would return items in both s1 and s2 like with something like this: ...
11
by: Prateek | last post by:
I have 3 variable length lists of sets. I need to find the common elements in each list (across sets) really really quickly. Here is some sample code: # Doesn't make sense to union the sets -...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.