Results 1 to 4 of 4

Thread: Loop help!

  1. #1
    huuduy216 is offline Member
    Join Date
    Sep 2013
    Posts
    4
    Rep Power
    0

    Default Loop help!

    Hi, I'm quite new to programming and Java. I need some help with my homework. The problem is in the image below:
    Loop help!-untitled.jpg
    And here is my code so far:
    Java Code:
    // Duy Nguyen (ID:02104267)
    
    // Duy Nguyen (ID:02104267)
    
    public class Points {
    	public static void main( String[] args ){
    
    		// Input check 
    		if( args.length != 7 ){
    		    System.err.println( "ERROR: invalid number of command line " + 
    		            "inputs." );
    		    System.err.println( "Usage: points X1 Y1 X2 Y2 X3 Y3 N" );
    		    System.exit( 1 );}
    		// Command line input
    		double x1= Double.parseDouble(args[0]);
    		double y1= Double.parseDouble(args[1]);
    		double x2= Double.parseDouble(args[2]);
    		double y2= Double.parseDouble(args[3]);
    		double x3= Double.parseDouble(args[4]);
    		double y3= Double.parseDouble(args[5]);
    		double n= Double.parseDouble(args[6]);
    		
    // Distance+ line
    double distance= Math.sqrt(Math.pow(x1- x2, 2) + Math.pow(y1- y2, 2));
    double slope= (y2-y1)/(x2-x1);
    
    // Points
    int i= 1;
    System.out.printf("The distance from(%.2f, %.2f) to (%.2f, %.2f) is: %f ", x1, y1, x2, y2, distance);
    double xi, yi;
    for(i=1; i<= n; i++)
    {
    xi= (x2/n)+ x1;
    yi= (y2/n)+ y1;
    System.out.printf("%.2f, %.2f", xi, yi)
    }
    }
    }
    }
    I stucked at the loop. I know how to calculate the cordinate, but how can I make my loop indicate which cordinate is the closest one to point p3 ? Please help
    Attached Thumbnails Attached Thumbnails Loop help!-untitled.jpg  
    Last edited by huuduy216; 09-21-2013 at 12:59 AM.

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: Loop help!

    You see that float you're using to display the %f placeholder value? Declare another variable to store the smallest of those, as well as another variable to store the index of the args array corresponding to that value, then simply use that index variable to display the point directly out of the args array.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,431
    Blog Entries
    7
    Rep Power
    20

    Default Re: Loop help!

    Also, don't try to calculate the slope; it isn't defined if P1 and P2 are the endpoints of a vertical line segment (x2 == x1). Note that if you walk over the points of the line, if the distance of a point is larger than the distance of the previous point, the previous point has the minimal distance to that fixed third point.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    huuduy216 is offline Member
    Join Date
    Sep 2013
    Posts
    4
    Rep Power
    0

    Default Re: Loop help!

    This is what I came up with.
    Java Code:
    // Duy Nguyen (ID:02104267)
     
    // Duy Nguyen (ID:02104267)
     
    public class Points {
        public static void main( String[] args ){
     
            // Input check
            if( args.length != 7 ){
                System.err.println( "ERROR: invalid number of command line " +
                        "inputs." );
                System.err.println( "Usage: points X1 Y1 X2 Y2 X3 Y3 N" );
                System.exit( 1 );}
            // Command line input
            double x1= Double.parseDouble(args[0]);
            double y1= Double.parseDouble(args[1]);
            double x2= Double.parseDouble(args[2]);
            double y2= Double.parseDouble(args[3]);
            double x3= Double.parseDouble(args[4]);
            double y3= Double.parseDouble(args[5]);
            double n= Double.parseDouble(args[6]);
             
    		// Distance+ line
    		double distance= Math.sqrt(Math.pow(x1- x2, 2) + Math.pow(y1- y2, 2));
    	 
    		// Points
    		int i1= 1;
    		int i2= 1;
    		System.out.printf("The distance from(%.2f, %.2f) to (%.2f, %.2f) is: %f \n", x1, y1, x2, y2, distance);
    		double xi= x1;
    		double yi= y1;
    		double closestDistance= Math.sqrt(Math.pow(xi- x3, 2) + Math.pow(yi- y3, 2));
    		int closesti= 0; //hold the number i2 that has shortest distance
    		for(i1=1; i1<=n;i1++)
    		{
    			if (i1== closesti)
    			{
    				System.out.printf("(%.2f, %.2f)     <--- Closest point to (%.2f,%.2f)\n", xi, yi, x3, y3);
    			}
    			else
    			{
    				System.out.printf("(%.2f, %.2f)\n", xi, yi);
    				
    			}
    			for(i2=1; i2<= n; i2++)
    				{
    				xi+= (x2-x1)/10;
    				yi+= (y2-y1)/10;
    
    				if(Math.sqrt(Math.pow(xi- x3, 2) + Math.pow(yi- y3, 2)) <= closestDistance)
    					{
    						closestDistance= Math.sqrt(Math.pow(xi- x3, 2) + Math.pow(yi- y3, 2));
    						closesti= i2;
    					}
    				}
    		}
    	}
    }
    However, for line 47 and 48 it seems to me that it won't divide by n,but it just add x2,y2 to xi,yi instead :( I also tried to replace n with 10 or 10.0, but still I recived the same result. Why is that happenning ? And how can I fix that ?

Similar Threads

  1. Converting a while loop to a for loop
    By danboy4 in forum New To Java
    Replies: 7
    Last Post: 08-08-2012, 03:35 PM
  2. Replies: 2
    Last Post: 11-07-2011, 02:25 AM
  3. Replies: 1
    Last Post: 10-01-2011, 02:12 AM
  4. JTextField loop 2x for-loop WEIRD!
    By Streetproject in forum AWT / Swing
    Replies: 2
    Last Post: 02-16-2011, 05:46 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
  •