简体   繁体   English

如何在C ++中的两点之间插入“ k”个点?

[英]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. 我想将曲线上的对应点与等间距的“ k”点连接以形成一条直线。

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: 我尝试使用以下公式来计算x和y都位于点之间的路径上:

    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 . 曲线的数据点存储在2D数组xz的第一列和最后一列中。 num_k is the number of intervals I want. num_k是我想要的间隔数。 num_points is the number of points on both the curves. num_points是两条曲线上的点数。

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. (1. - j/num_k)几乎总是求值为1,因为j/num_k是使用整数数学完成的,这意味着除最后一次迭代外,它将为零。

Use (1. - double(j)/num_k) instead. 请改用(1. - double(j)/num_k)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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