|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

09-09-2008, 06:42 PM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 14
|
|
|
compare newly added Vector Element with previous elements
Hi all
I am adding Point2D elements to a vector "Numbers" in such a way that every newly randomly created element that is at a distance of 0.3 from all the previous points is added else not.
I have written a code that only checks from the last element but not all the previous elements.
Can someone check what wrong am I doing.
The code is runnable.
import java.awt.geom.Point2D;
import java.util.*;
public class Distance
{
Vector<Point2D> numbers = new Vector<Point2D>();
Random rnd = new Random();
void getList()
{
double xCoord,yCoord;
for(int i=0;i<5;i++)// is is the no. of coordinates stored later only "b" to be used
{
xCoord = rnd.nextDouble(); // X-coord of AP
yCoord = rnd.nextDouble(); // Y coord of AP
if(i<2)
{
numbers.addElement(new Point2D.Double(xCoord,yCoord));
System.out.println("0th n 1st element added : "+i+ " "+numbers.get(i));
}
else
{
for(int j=i-1;j>i-2;j--)
{
if((numbers.get(j).distance(xCoord,yCoord))>0.3)
{
numbers.addElement(new Point2D.Double(xCoord,yCoord));
System.out.println("next element added : "+i+ " "+numbers.get(j)+" with distance: "
+(numbers.get(j).distance(xCoord,yCoord))+"from "+ xCoord + " "+ yCoord);
}
}
}
}
Iterator it = numbers.iterator ();
while (it.hasNext ())
{
System.out.println(it.next());
}
}
public static void main(String[] argv)
{
Distance d = new Distance();
d.getList();
}
}
Thanks a lot.
|
|

09-09-2008, 07:22 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,225
|
|
Does the program give correct results? If not, what is wrong? Can you post the program's output and describe what is wrong with it?
only checks from the last element but not all the previous elements.
Why only the last element?
What are you trying to do in the above loop?
Can you explain the logic of your loops and if statements?
Another design idea:
I'd embed the Point2D object within your own class with a method: boolean isCloseTo(Point2D) that returns true if the new point is close to the other.
Then when adding a point, you can scan thru all the elements in the Vector testing if the new object isCloseTo any ones in the Vector.
Last edited by Norm : 09-09-2008 at 07:27 PM.
|
|

09-09-2008, 08:33 PM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 14
|
|
Originally Posted by Norm
Does the program give correct results? If not, what is wrong? Can you post the program's output and describe what is wrong with it? Why only the last element?
--> The program compares the newly created element only with the last element and not all the last elements.So the output is wrong
Originally Posted by Norm
for(int j=i-1;j>i-2;j--)
What are you trying to do in the above loop?
Can you explain the logic of your loops and if statements?
--> I am running the loop from the last element created to the first so that each element is compared with the current element.This will help in checking that the new element is at a minimum distance from all of the Point2D elements.
Originally Posted by Norm
Another design idea:
I'd embed the Point2D object within your own class with a method: boolean isCloseTo(Point2D) that returns true if the new point is close to the other.
Then when adding a point, you can scan thru all the elements in the Vector testing if the new object isCloseTo any ones in the Vector.
--> I am very much going to try this now.
Thankyou very much Norm.
|
|

09-09-2008, 08:41 PM
|
|
Senior Member
|
|
Join Date: Aug 2008
Posts: 186
|
|
my outputs:
0th n 1st element added : 0 Point2D.Double[0.6094098019020386, 0.9838941381683657]
0th n 1st element added : 1 Point2D.Double[0.9106562806196608, 0.7847578243124705]
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 2
at java.util.Vector.get(Vector.java:694)
at Distance.getList(Distance.java:27)
at Distance.main(Distance.java:48)
0th n 1st element added : 0 Point2D.Double[0.896698639364791, 0.9499883975395484]
0th n 1st element added : 1 Point2D.Double[0.2738432301781768, 0.20839170127748607]
next element added : 2 Point2D.Double[0.2738432301781768, 0.20839170127748607] with distance: 0.35512275998569814 from 0.365903118208346 0.5513744292328927
next element added : 3 Point2D.Double[0.365903118208346, 0.5513744292328927] with distance: 0.6504405536372115 from 0.012782587243660215 0.005133818854004213
next element added : 4 Point2D.Double[0.012782587243660215, 0.005133818854004213] with distance: 0.9490164105252665 from 0.6874161106112764 0.6725929985757736
Point2D.Double[0.896698639364791, 0.9499883975395484]
Point2D.Double[0.2738432301781768, 0.20839170127748607]
Point2D.Double[0.365903118208346, 0.5513744292328927]
Point2D.Double[0.012782587243660215, 0.005133818854004213]
Point2D.Double[0.6874161106112764, 0.6725929985757736]
As you see, it throws the error if one of the points has a distance greater than 0.3.
if((numbers.get(j).distance(xCoord,yCoord))>0.3)
{
numbers.addElement(new Point2D.Double(xCoord,yCoord));
System.out.println("next element added : "+i+ " "+numbers.get(j)+" with distance: "
+(numbers.get(j).distance(xCoord,yCoord))+" from "+ xCoord + " "+ yCoord);
}
I reckon you fix this, because, obviously there's the error.
Sorry, can't be asked to fix it for you, you gotta do it yourself.
__________________
check out To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. , 100% made by me. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Last edited by Supamagier : 09-09-2008 at 08:52 PM.
|
|

09-09-2008, 08:48 PM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 14
|
|
Originally Posted by Supamagier
my outputs:
0th n 1st element added : 0 Point2D.Double[0.896698639364791, 0.9499883975395484]
0th n 1st element added : 1 Point2D.Double[0.2738432301781768, 0.20839170127748607]
next element added : 2 Point2D.Double[0.2738432301781768, 0.20839170127748607] with distance: 0.35512275998569814 from 0.365903118208346 0.5513744292328927
next element added : 3 Point2D.Double[0.365903118208346, 0.5513744292328927] with distance: 0.6504405536372115 from 0.012782587243660215 0.005133818854004213
That's exactly my output is.
However, what is required ,ALL previous elements are compared with the newly added element and not just the last element.
|
|

09-09-2008, 08:55 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,225
|
|
If you are not comparing against all the elements in the Vector, then the indexes for the for loop must not be correct.
running the loop from the last element created to the first
Why go backwards? Why not start at the beginning and go to the end?
|
|

09-09-2008, 08:56 PM
|
|
Senior Member
|
|
Join Date: Aug 2008
Posts: 186
|
|
|
Because he wants to compare the new element with the last element, I think. Because he just adds it, he needs to go backwards. Though, I don't really see the use/problem(s) of that...
__________________
check out To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. , 100% made by me. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

09-09-2008, 09:04 PM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 14
|
|
Originally Posted by Supamagier
Because he wants to compare the new element with the last element, I think. Because he just adds it, he needs to go backwards. Though, I don't really see the use/problem(s) of that...
ALL the previous and not just the last one.
Although, it shouldnt' matter wherether we go last to first or first to last when comparing.
|
|

09-09-2008, 09:16 PM
|
|
Senior Member
|
|
Join Date: Aug 2008
Posts: 186
|
|
Than why use
?
j = i-1;
while j > i-2 you compare, than you decrease j by one. This obviously always happens only one time...
use
should work, I think.
__________________
check out To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. , 100% made by me. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

09-09-2008, 09:32 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,225
|
|
|
To debug how your loop is working add a println() to show the values of i and j.
|
|

09-10-2008, 03:32 AM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 14
|
|
Originally Posted by Norm
To debug how your loop is working add a println() to show the values of i and j.
yeah did that.
Thanks for the debugging tip.
got the code running in the way it should.
for(int i=0;i<5;i++)// is is the no. of coordinates stored later only "b" to be used
{
xCoord = rnd.nextDouble(); // X-coord of AP
yCoord = rnd.nextDouble(); // Y coord of AP
if(i==0)
{
numbers.addElement(new Point2D.Double(xCoord,yCoord));
System.out.println("Element added : "+i+ " "+numbers.get(i));
System.out.println("i: "+i);
}
else
{
for(int j=0;j<i;j++)//(int j=i-1;j>i-2;j--)
{
if((numbers.get(j).distance(xCoord,yCoord))>0.5)
{
numbers.addElement(new Point2D.Double(xCoord,yCoord));
System.out.println("next element added : "+xCoord + " "+ yCoord+ " "+" with distance: "
+(numbers.get(j).distance(xCoord,yCoord))+"from "+ numbers.get(j));
System.out.println("i: "+i+", j: "+j);
}
}
}
}
Many many thanks Norm and Supamagier.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|