简体   繁体   中英

How to insert “k” number of points between two points in C++?

I have two curves with an equal number of data points. I want to connect the corresponding points on the curves with "k" equally spaced points to form a straight line.

How it should look

I have tried to use the following formula to calculate both x's and y's lying on the path between the points:

    for(int j = 1; j<=num_k; j++) {
        for (int i = 2; i <= (num_points-1); i++) {
            x[i][j] = x[i][1] * (1. - j/num_k) +  x[i][num_points] * j/num_k;
            y[i][j] = y[i][1] * (1. - j/num_k) +  y[i][num_points] * j/num_k;
        }
    }

The data points of the curve are stored in the first and last columns of 2D arrays x and z . num_k is the number of intervals I want. num_points is the number of points on both the curves.

But this is not giving me the result that I need - this gives me the points, but they are not between the two input points as given. Am I using the right technique or is there something else I should be using? Also, are there any special cases?

Thanks!!

(1. - j/num_k) will almost always evaluate to 1, because j/num_k is done using integer math, which will mean it will be zero except on the last iteration.

Use (1. - double(j)/num_k) instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM