I am using a for loop to calculate the distance between the vertices of a polygon
using

for (i=0; i<N, i++)
{
j= (i-1)
result = sqrt (sqr(X-X[j])+sqr(y-y[j]));
}
return result;

This work fine however it doesn't and I cant calculate the distance between the last and first vertice e.g for N=4, calculates N0 to N1, N1 to N2, etc, but doesnt enclose the polygon and calculate N3 to N0
Is there any alteration to this code that will enable this, any help is much appreciated

Dani AI

Generated

The loop never closed the polygon: the edge from the last vertex back to the first must be included explicitly. ’s original snippet also shows a common typo in the loop header (commas instead of semicolons), which changes loop behavior; and using j = (i-1) without handling i==0 produces an out‑of‑bounds index. ’s special‑case solution is valid, but an index‑wrap technique keeps the body uniform and avoids a separate step.

A compact, robust pattern is to iterate every vertex and add the distance to the "next" vertex computed with modulo. This handles the wrap automatically and works with either raw arrays or std::vector. Example (different from the posted answers):

#include <vector>
#include <cmath>

double polygonPerimeter(const std::vector<double>& x,
                        const std::vector<double>& y) {
    size_t N = x.size();
    if (N < 2) return 0.0;
    double sum = 0.0;
    for (size_t i = 0; i < N; ++i) {
        size_t j = (i + 1) % N;   // next vertex, wraps to 0 after N-1
        sum += std::hypot(x[j] - x[i], y[j] - y[i]);
    }
    return sum;
}

Practical notes: ensure x.size() == y.size() before calling, and check N (perimeter needs N>=2; polygon area needs N>=3). Prefer std::hypot for numeric stability instead of sqrt(dx*dx + dy*dy). If signed indices are used, avoid unsigned underflow—either use (i==0 ? N-1 : i-1) for the previous index or keep indices unsigned and compute the next index with modulo. Performance differences between the modulo approach and ’s special-case first/last addition are negligible for ordinary polygon sizes; choose the one that makes the code clearer and safer.

Recommended Answers

All 2 Replies

There's no way in a for loop, since the (first,last) is a special case. Also, watch your index bounds and maintain your result's aggregate value:

double ClosedPolyPerimeter( double x[], double y[], int N ) {
  int i;
  double result = sqrt( sqr( x[N-1] -x[0] ) +sqr( y[N-1] -y[0] ) );
  for (i = 1; i < N; i++)
    result += sqrt( sqr( x[i] -x[i-1] ) +sqr( y[i] -y[i-1] ) );
  return result;
  }

This code first calculates the special case of distance(N-1,0), then uses the loop to calculate the remaining distance( 1..N-1, 0..N-2 ) and add them each to the result.

Hope this helps.

Now I get it.
Thanks for the quick reply. Your help is very much appreciated!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.