code: private static Point2D computePolylineCentroid(Polyline polyline)
      {
          double xSum = 0;
          double ySum = 0;
          double weightSum = 0;
  
          Point2D startPoint = new Point2D();
          Point2D endPoint = new Point2D();
          for (int i = 0; i < polyline.getPathCount(); i++) {
              polyline.getXY(polyline.getPathStart(i), startPoint);
              polyline.getXY(polyline.getPathEnd(i) - 1, endPoint);
              double dx = endPoint.x - startPoint.x;
              double dy = endPoint.y - startPoint.y;
              double length = sqrt(dx * dx + dy * dy);
              weightSum += length;
              xSum += (startPoint.x + endPoint.x) * length / 2;
              ySum += (startPoint.y + endPoint.y) * length / 2;
          }
          return new Point2D(xSum / weightSum, ySum / weightSum);
      }
docs: Lines centroid is weighted mean of each line segment, weight in terms of line length