How do you calculate distances between coordinates and calculate the total perimeter?

1
2
3
4
5
6
7
8
9
10
11
12
13
T getPerimeter(T x[], T y[], T p[], int numDataPoints){
  
  
  T delX, delY, dist, totalPerimeter;
  
  for(int i = 0; i <= numDataPoints; ++i){
    delX = x[i] - x[i - 1];
    delY = y[i] - y[i - 1];
    dist = sqrt(delX * delX + delY * delY);
    p[i] = dist;
    totalPerimeter += dist;
  }
  return totalPerimeter;


My program has read in a list of numbers from a .txt file and outputted them to another .txt file as 13 different arrays (with columns "index", "x[i]". "y[i]", "m[i]", and "p[i]" where m[i] is the mass at that point and p[i] is the distance from that point to the previous one.)

Now in my main function, I'm trying to output the distance between each point (which I've done successfully) but also the perimeter of this random shape (which is what I'm having a problem with.

There's no need to question numDataPoints as this is a multi-file program and it is defined elsewhere. I know this part is correct.
Last edited on
1
2
3
4
5
for(int i = 0; i <= numDataPoints; ++i){
    delX = x[i] - x[i - 1];
    delY = y[i] - y[i - 1];
...
}


What's x[i-1] when i is zero?
Last edited on
I'm trying to get x[i-1] and y[i-1] to be the last index of the set of arrays but still haven't figured out how. Here's my output and as you can see there's a NaN.


Total Mass: 
Center of Mass: 
Total Perimeter: -0
Enclosed Area: 

Point Data Columns:

x: x-coordinate at index.
y: y-coordinate at index.
m: mass at index.
p: distance from last point to current point

index  x[index]	         y[index]	 m[index]	  p[index]	
0	  0.000		  0.000		 12.340		    NaN
1	  0.000		 10.000		  7.777		 10.000
2	  0.111		 11.100		  1.110		  1.106
3	  1.889		  8.900		  1.110		  2.829
4	  2.000		 10.000		  2.340		  1.106
5	  4.000		  8.000		  3.142		  2.828
6	  6.000		  8.000		  1.111		  2.000
7	  6.000		 12.000		  2.133		  4.000
8	 10.000		 14.000		  3.212		  4.472
9	 10.000		  8.000		  4.321		  6.000
10	 14.000		  4.000		 11.230		  5.657
11	 10.000		  0.000		  2.701		  5.657
12	  6.000		  4.000		111.013		  5.657
13	  3.000		  5.000		  0.333		  3.162


I'm not sure how I'll be finding the center of mass or area of this shape either (if you have some hints, please share). I haven't gotten to that part yet so I'll just focus on asking about perimeter and fixing what x[i-1] and y[i-1] is when i is zero. I want it to be 3.000 and 5.000.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
T prevX;
T prevY;

if (i > 0)
{
    prevX = x[i-1];
    prevY = y[i-1];
}
else
{
    prevX = x[numDataPoints - 1];
    prevY = y[numDataPoints - 1];
}

delX = x[i] - prevX;
delY = y[i] - prevY;


Edit: Also, you should loop
for(int i = 0; i < numDataPoints; ++i)
Not <=.
Last edited on
Thanks. That helped get rid of the NaN but also caused p[13] to be 0.
I changed the else statement to "else if(i <=0)" and it fixed that.

Edit: I also left it as <= because < was part of the cause for the 0 value for p[13]

Do you know how I can output the perimeter in my main function? I got the p[i] column working (as you could see above), but the perimeter still shows up as -0
Last edited on
You've got weird stuff going on. Changing line 9 from else to else if(i <=0) should not make any difference at all. If it made a difference, then you typed something in wrong, or you have problems with your compiler.

What is numDataPoints? If there are 13 points, and numDataPoints==13, then your hack is absolutely wrong. The 13 points would accupy indeces 0 - 12 in your arrays, and accessing index 13 exceeds the bounds of your array. Your hack is the wrong solution. You need to change line 6 back to "<" and figure out what the real problem is.

Also, as far as the perimeter, you never initialized it to 0.
Last edited on
//
Last edited on
it's strange your compiler would let you try and access x[-1].
I'm using Quincy 2005. Not exactly my favorite.

Any ideas on how I can calculate the area of this shape?
I'm clueless and looking everywhere for reference.
Getting someone else to figure it out, or copying someone will defeat the purpose of your challenge.

Get a piece of paper out, draw some points and do some brainstorming.

Then when your ready to give up, google area of irregular polygon, and click the first link.
Last edited on
Haha. Yeah I understand. I've drawn it out and have been staring at it for a couple hours. It isn't a nice looking polygon.
Topic archived. No new replies allowed.