简体   繁体   中英

finding the length given the coordinates for a polygon

I'm working on a question where i need to find the length of the side given the coordinates of a polygon. N is the number of rows and 2 is number of columns ( x and y coordinates) and i've collected the coordinates in a multi-dimensional array.

what my idea was to collect the x coordinates in a array say x1 and collect y coordinates in a array say y1. now find the difference between the numbers in the array and perform the distance formula operation. but im not able to proceed any further. im not able to find the length using it as the answer is always short of the actual number. kindly help on how can i find the length of the sides of a given polygon. please find my code below:

 import java.util.Scanner; public class Rope { public static void main(String[] args) { int N = 1, R=1; double AoN=1; float d=0 , e=0, f=0, h=0, s=0, length=0, g=0;; Scanner in = new Scanner(System.in); int[] arr = new int[2]; System.out.println("Enter number of Nails (N) and Radius of Nail (R) seperated by space: "); for (int i=0;i<arr.length;i++) { arr[i]=in.nextInt(); } if (arr[0]>=1 && arr[0]<=100) { N=arr[0]; // N is the number of rows of the multi-dimensional array and rows is fixed to 2 as coordinates are fixed to x and y so 2. } R=arr[1]; // kindly ignore R as it is used for other purpose. float[ ][ ] arr1 = new float[N][2]; System.out.println("Enter Coordinates separated by spaces: "); for(int i=0; i<N;i++) { for (int j=0;j<2;j++) { arr1[i][j]=in.nextFloat(); //System.out.println(arr1[i][j]); } } float[] x = new float[N]; float[] y = new float[N]; for(int i=0; i<N;i++) { x[i] = arr1[i][0]; } for (int j=0;j<N;j++) { y[j] = arr1[j][1]; } for (int i=0; i<x.length-1;i++) { d = (float) (d + (Math.pow((x[i+1] - x[i]),2))); } for (int i=0; i<y.length-1;i++) { e = (float) (e + (Math.pow((y[i+1] - y[i]),2))); } g = d+e; s = (float) Math.sqrt(g); sysout(s); in.close(); } } 

because you have a logical glitch in your code. Here if you notice in the following section :

for (int i=0; i<x.length-1;i++) {
            d = (float) (d + (Math.pow((x[i+1] - x[i]),2)));
        }

        for (int i=0; i<y.length-1;i++) {
            e = (float) (e + (Math.pow((y[i+1] - y[i]),2)));
        }

Lets say,

x1-x2 = X1
x2-x3 = X2 

and so on

similarly,

y1-y2 = Y1
y2-y3 = Y2 

and so on

now what your code does is it calculates

    sqrt(X1*X1 + X2*X2.... +Y1*Y1 + Y2*Y2....)

But what actually it is supposed to do is,

sqrt(Math.pow((X1-X2),2) + Math.pow(Y1-Y2),2)) + sqrt (Math.pow((X2-X3),2) + Math.pow((Y2-Y3), 2) + ...

thus your code generates wrong values.

You should try the following :

for (int i=0; i<x.length-1;i++) {
            float temp;

            temp = (Math.pow((x[i+1] - x[i]),2)) + (Math.pow((y[i+1] - y[i]),2));

            d = (float) (d + sqrt(temp));
        }

// for first and last values /  coordinates w.r.t distance formula
for (int i=x.length-1; i<x.length;i++) {
        float temp;

        temp = (float) ((Math.pow((x[i] - x[0]),2)) + (Math.pow((y[i] - y[0]),2)));

        d = (float) (d + Math.sqrt(temp));
    }   

Instead of that above mentioned two lines.

Hope this helps !!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM