#includeint main() { QPointF p1(1,1); QPointF p2(4,5); qreal distance = p1.manhattanLength(p2); qDebug() << "Manhattan distance:" << distance; // Output: Manhattan distance: 7 return 0; }
#includeIn this example, we have a list of three points, and we want to calculate the total Manhattan distance between them. We create a variable called totalDistance, and loop through the points using their indices. At each step, we calculate the Manhattan distance between the current point and the next point in the list, and add it to the totalDistance variable. The output is 14. Package Library: QPointF is a part of the QtCore module in the Qt library.int main() { QList points; points.append(QPointF(0,0)); points.append(QPointF(3,3)); points.append(QPointF(7,2)); qreal totalDistance = 0; for (int i = 0; i < points.size() - 1; i++) { totalDistance += points[i].manhattanLength(points[i+1]); } qDebug() << "Total Manhattan distance:" << totalDistance; // Output: Total Manhattan distance: 14 return 0; }