1. Member Join Date
Sep 2013
Posts
4
Rep Power
0

## 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: 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);
double y1= Double.parseDouble(args);
double x2= Double.parseDouble(args);
double y2= Double.parseDouble(args);
double x3= Double.parseDouble(args);
double y3= Double.parseDouble(args);
double n= Double.parseDouble(args);

// 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
Last edited by huuduy216; 09-21-2013 at 01:59 AM.  Reply With Quote

2. Senior Member Join Date
Jun 2008
Posts
2,568
Rep Power
15

## 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.  Reply With Quote

3. ## 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  Reply With Quote

4. Member Join Date
Sep 2013
Posts
4
Rep Power
0

## 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);
double y1= Double.parseDouble(args);
double x2= Double.parseDouble(args);
double y2= Double.parseDouble(args);
double x3= Double.parseDouble(args);
double y3= Double.parseDouble(args);
double n= Double.parseDouble(args);

// 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 ?  Reply With Quote

#### Posting Permissions

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