Algorithm for smoothing

前端 未结 6 516
遥遥无期
遥遥无期 2021-01-01 02:06

I wrote this code for smoothing of a curve . It takes 5 points next to a point and adds them and averages it .

/* Smoothing */
void smoothing(vector

        
6条回答
  •  被撕碎了的回忆
    2021-01-01 02:53

    The current-value of the point is used twice: once because you use += and once if y==0. So you are building the sum of eg 6 points but only dividing by 5. This problem is in both the IF and ELSE case. Also: you should check that the vector is long enough otherwise your ELSE-case will read at negative indices.

    Following is not a problem in itself but just a thought: Have you considered to use an algorithm that only touches every point twice?: You can store a temporary x-y-value (initialized to be identical to the first point), then as you visit each point you just add the new point in and subtract the very-oldest point if it is further than your NEIGHBOURS back. You keep this "running sum" updated for every point and store this value divided by the NEIGHBOURS-number into the new point.

提交回复
热议问题