# How to check itersection of objects in on collection

• July 10th, 2012, 12:11 AM
Eldarmk2
How to check itersection of objects in on collection
Hi to all !
Im looking for a solution to my problem.
Dont know how to check itersection of rects from objects in one List.
Example: We have a Object.class;The Object has a simple method
Code :

Rectangle getRect(){return new Rectangle(x,y,10,10)}

We create a new List; Adding some Objects to it.
Now the problem, how to check does the Rectangles in one list itersecting each other or not ?
I try to create an iterator the a loop the an if statement
Code :

Iterator<Object> i = list.iterator();
while (i.hasNext()){
Object o = i.next();
if (o.getRect().itersects())
{
//Some code
}
}

Well i dont know what to put in the intersects() method;
• July 10th, 2012, 08:42 AM
copeg
Re: How to check itersection of objects in on collection
Quote:

Well i dont know what to put in the intersects() method;
Read the API for this method - it can accept a variety of parameters, one of which is another Rectangle object. So pass the other Rectangle you wish to compare.
• July 10th, 2012, 09:09 AM
Eldarmk2
Re: How to check itersection of objects in on collection
Look at my example code , ive allready the itersects and of course i know that i need an other object.
Ive asked about objects in one list. According to my example and using you solution it mean that ive to this if (o.getRect().itersects(o.getRect())) and this is not right because it checks itself.
All objects are in one collection, how to chekc them ? what should be in the intersects()?
• July 10th, 2012, 09:25 AM
KevinWorkman
Re: How to check itersection of objects in on collection

The intersects() method takes an argument. You need to figure out what that argument should be. What it should be depends on your end goal, which only you know.
• July 10th, 2012, 09:32 AM
Eldarmk2
Re: How to check itersection of objects in on collection
No thats not the help, becasue ive asked not about the functionality of the itersect() method.
Okay and here is a a simple solution of my problem i found.
Ive to use 2 itertators
Code :

Iterator<Object> i = list.iterator();
Iterator<Object> i = list.iterator();
while (i.hasNext()){
while(j.hasNext()){
Object o1 = i.next();
Object o2 = j.next();
if (o1.getRect().itersects(o2.getRect()))
{
//Some code
}
}
}

And it works! Hope this will some else.
And some help from others
this is more right becaouse it cheks that objects are not itersectin them self
Code :

for( int i = 0; list.length; i++)
{
Rectangle current = list[i].getRect();
for( int j = 0; list.length; j++ )
{
Rectangle compare = list[j];
if( !current.equals(compare)) //you could also check to see if i!=j
if( current.intersects(compare) )
System.out.println( "They intersect!");
}
}

• July 10th, 2012, 09:35 AM
KevinWorkman
Re: How to check itersection of objects in on collection
That was exactly what copeg told you, to take a closer look at the intersection() method, which you were using incorrectly. Actually, I predict you'll have quite a few problems with that code.
• July 10th, 2012, 09:44 AM
Eldarmk2
Re: How to check itersection of objects in on collection
i know how the intersects90 work since my first java book
The problem was that i dindt think about 2 iterators in one list.
This code should now be correct
Code :

Iterator<Object> i = list.iterator();
Iterator<Object> j = list.iterator();
while (i.hasNext()){
Object o1 = i.hasNext();
while(j.hasNext()){
Object o2 = j.next();
if (o1.getRect().itersects(o2.getRect()))
{
//Some code
}
}
}

• July 10th, 2012, 10:28 AM
KevinWorkman
Re: How to check itersection of objects in on collection
That still is not correct. Aren't you testing this out as you go?

Also, did you really use Object as a class name?
• July 10th, 2012, 10:36 AM
copeg
Re: How to check itersection of objects in on collection
Quote:

Originally Posted by Eldarmk2

I beg to differ...

Quote:

Originally Posted by Eldarmk2
Well i dont know what to put in the intersects() method

• July 10th, 2012, 11:26 AM
Eldarmk2
Re: How to check itersection of objects in on collection
No my class is caled shape
• July 10th, 2012, 11:26 AM
Eldarmk2
Re: How to check itersection of objects in on collection
Quote:

Originally Posted by copeg
I beg to differ...