Interpolation of polygon edges

Interpolation of polygon edges

Algorithm description and questions:
Given a minimum length, each side of the polygon is continuously interpolated in half, so that each side after interpolation is not greater than the minimum length.

Test ChatGPT - Chinese version of VSCode plug-in.
Insert image description here

Results given by Chat GPT:

#include <iostream>
#include <vector>
#include <cmath>

struct Point {
    
    
    double x;
    double y;
};

double distance(const Point& p1, const Point& p2) {
    
    
    double dx = p2.x - p1.x;
    double dy = p2.y - p1.y;
    return std::sqrt(dx * dx + dy * dy);
}

std::vector<Point> interpolatePolygon(const std::vector<Point>& polygon, double minLength) {
    
    
    std::vector<Point> interpolatedPoints;

    for (size_t i = 0; i < polygon.size(); i++) {
    
    
        size_t j = (i + 1) % polygon.size();
        double segmentDistance = distance(polygon[i], polygon[j]);

        if (segmentDistance <= minLength) {
    
    
            interpolatedPoints.push_back(polygon[i]);  // 将原始顶点添加到插值点列表
        } else {
    
    
            int numInterpolatedPoints = static_cast<int>(segmentDistance / minLength);
            double stepSize = segmentDistance / (numInterpolatedPoints + 1);

            for (int k = 1; k <= numInterpolatedPoints; k++) {
    
    
                double factor = stepSize * k / segmentDistance;
                double newX = polygon[i].x + factor * (polygon[j].x - polygon[i].x);
                double newY = polygon[i].y + factor * (polygon[j].y - polygon[i].y);
                interpolatedPoints.push_back({
    
    newX, newY});  // 添加插值点到列表
            }
        }
    }

    return interpolatedPoints;
}

int main() {
    
    
    // 定义多边形的顶点
    std::vector<Point> polygon = {
    
    
        {
    
    0.0, 0.0}, {
    
    2.0, 0.0}, {
    
    2.0, 2.0}, {
    
    0.0, 2.0}
    };
    
    double minLength = 1.0;
    
    // 进行边均匀插值并打印结果
    std::vector<Point> interpolatedPoints = interpolatePolygon(polygon, minLength);
    
    for (const auto& point : interpolatedPoints) {
    
    
        std::cout << "Interpolated Point: (" << point.x << ", " << point.y << ")\n";
    }

    return 0;
}

Insert image description here
It's obviously wrong.
Correct result:
Insert image description here

Modified result 1

#include <iostream>
#include <vector>
#include <cmath>

struct Point {
    
    
    double x;
    double y;
};

double distance(const Point& p1, const Point& p2) {
    
    
    double dx = p2.x - p1.x;
    double dy = p2.y - p1.y;
    return std::sqrt(dx * dx + dy * dy);
}

std::vector<Point> interpolatePolygon(const std::vector<Point>& polygon, double minLength) {
    
    
    std::vector<Point> interpolatedPoints;

    for (size_t i = 0; i < polygon.size(); i++) {
    
    
        size_t j = (i + 1) % polygon.size();
        double segmentDistance = distance(polygon[i], polygon[j]);

        if (segmentDistance <= minLength) {
    
    
            interpolatedPoints.push_back(polygon[i]);  // 将原始顶点添加到插值点列表
        }
        else {
    
    
            int numInterpolatedPoints = ceil(segmentDistance / minLength);
            double stepSize = segmentDistance / numInterpolatedPoints;

            for (int k = 1; k <= numInterpolatedPoints; k++) {
    
    
                double factor = stepSize * k / segmentDistance;
                double newX = polygon[i].x + factor * (polygon[j].x - polygon[i].x);
                double newY = polygon[i].y + factor * (polygon[j].y - polygon[i].y);
                interpolatedPoints.push_back({
    
     newX, newY });  // 添加插值点到列表
            }
        }
    }

    return interpolatedPoints;
}

int main() {
    
    
    // 定义多边形的顶点
    std::vector<Point> polygon = {
    
    
        {
    
    0.0, 0.0}, {
    
    2.0, 0.0}, {
    
    2.0, 2.0}, {
    
    0.0, 2.0}
    };

    double minLength = 2;

    // 进行边均匀插值并打印结果
    std::vector<Point> interpolatedPoints = interpolatePolygon(polygon, minLength);

    for (const auto& point : interpolatedPoints) {
    
    
        std::cout << "Interpolated Point: (" << point.x << ", " << point.y << ")\n";
    }

    return 0;
}

Modified result 2

#include <iostream>
#include <vector>
#include <cmath>

struct Point {
    
    
    double x;
    double y;
};

double distance(const Point& p1, const Point& p2) {
    
    
    double dx = p2.x - p1.x;
    double dy = p2.y - p1.y;
    return std::sqrt(dx * dx + dy * dy);
}

std::vector<Point> interpolatePolygon(const std::vector<Point>& polygon, double minLength) {
    
    
    std::vector<Point> interpolatedPoints;

    int numVertices = polygon.size();
    std::vector<double> segmentLengths(numVertices);

    // 计算各边长度
    for (int i = 0; i < numVertices; i++) {
    
    
        int nextIndex = (i + 1) % numVertices;
        segmentLengths[i] = distance(polygon[i], polygon[nextIndex]);
    }


    for (int i = 0; i < numVertices; i++) {
    
    
        int nextIndex = (i + 1) % numVertices;
        double dx = polygon[nextIndex].x - polygon[i].x;
        double dy = polygon[nextIndex].y - polygon[i].y;
        double segmentDistance = distance(polygon[i], polygon[nextIndex]);

        interpolatedPoints.push_back(polygon[i]);

        if (segmentDistance > minLength) {
    
    
            
            int n = ceil(segmentDistance / minLength);
            int p = ceil(n / 2);
            for (int j = 0; j < p*2-1; j++) {
    
    
                double newX = polygon[i].x + pow(0.5,p) * dx*(1+j);
                double newY = polygon[i].y + pow(0.5,p) * dy*(1+j);
                interpolatedPoints.push_back({
    
     newX, newY });
            }
        }
    }
    return interpolatedPoints;
}

int main() {
    
    
    // 定义多边形的顶点
    std::vector<Point> polygon = {
    
    
        {
    
    0.0, 0.0}, {
    
    2.0, 0.0}, {
    
    2.0, 2.0}, {
    
    0.0, 2.0}
    };
    double minLength = 1.5;
    // 进行边均匀插值并打印结果
    std::vector<Point> interpolatedPoints = interpolatePolygon(polygon, minLength);
    for (const auto& point : interpolatedPoints) {
    
    
        std::cout << "Interpolated Point: (" << point.x << ", " << point.y << ")\n";
    }
    return 0;
}

Summarize

Using ChatGPT-Chinese version of VSCode, you can basically write a simple algorithm, but whether it is correct or not requires personal debugging and modification, which can save some time and energy.

reference

1、https://blog.csdn.net/mrbaolong/article/details/131585575?csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId %22%3A%22131585575%22%2C%22source%22%3A%22mrbaolong%22%7D

Guess you like

Origin blog.csdn.net/mrbaolong/article/details/131585575