Results 1 to 11 of 11
  1. #1
    nidhirastogi is offline Member
    Join Date
    Jul 2008
    Posts
    16
    Rep Power
    0

    Default 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.
    Java Code:
    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.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default

    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?

    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?

    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 05:27 PM.

  3. #3
    nidhirastogi is offline Member
    Join Date
    Jul 2008
    Posts
    16
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    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

    Quote Originally Posted by Norm View Post
    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.

    Quote Originally Posted by Norm View Post
    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.

  4. #4
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    my outputs:
    Java Code:
    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)
    Java Code:
    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.
    Java Code:
    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.
    Last edited by Supamagier; 09-09-2008 at 06:52 PM.
    I die a little on the inside...
    Every time I get shot.

  5. #5
    nidhirastogi is offline Member
    Join Date
    Jul 2008
    Posts
    16
    Rep Power
    0

    Default

    Quote Originally Posted by Supamagier View Post
    my outputs:
    Java Code:
    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.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default

    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?

  7. #7
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    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...
    I die a little on the inside...
    Every time I get shot.

  8. #8
    nidhirastogi is offline Member
    Join Date
    Jul 2008
    Posts
    16
    Rep Power
    0

    Default

    Quote Originally Posted by Supamagier View Post
    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.

  9. #9
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    Than why use
    Java Code:
    for(int j=i-1;j>i-2;j--)
    ?

    j = i-1;
    while j > i-2 you compare, than you decrease j by one. This obviously always happens only one time...

    use
    Java Code:
    for(int j=i-1;j>0;j--)
    should work, I think.
    I die a little on the inside...
    Every time I get shot.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default

    To debug how your loop is working add a println() to show the values of i and j.

  11. #11
    nidhirastogi is offline Member
    Join Date
    Jul 2008
    Posts
    16
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    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.

    Java Code:
    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.

Similar Threads

  1. how to compare the elements of the two arraylists al1,al2
    By raj reddy in forum Web Frameworks
    Replies: 33
    Last Post: 11-25-2009, 05:48 PM
  2. how to compare the elements of these two arraylists
    By raj reddy in forum Web Frameworks
    Replies: 1
    Last Post: 03-25-2009, 10:55 PM
  3. Finding elements in a vector
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 08:37 PM
  4. Adding a double element to a vector
    By peachyco in forum New To Java
    Replies: 5
    Last Post: 11-25-2007, 06:07 PM
  5. how to compare 2 vector lists?
    By oregon in forum New To Java
    Replies: 2
    Last Post: 07-25-2007, 08:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •