compute next point on a line with known slope

自闭症网瘾萝莉.ら 提交于 2020-01-25 12:17:05

问题


according to the line equation y = (m * x ) + c. if I know the slope(m) and I know that the line pass through a point(cx, cy). I want to know the next points on the same line before and after cx, cy, how do I go about to compute them.


回答1:


In C++:

Compute the other points by doing

extrapolate line(m, cx, cy);
double y_before  = line.y(cx - 1);  // for example
double y_after   = line.y(cx + 1);

See it live on http://ideone.com/BELNc (two examples)

struct extrapolate
{
     extrapolate(double slope, double x1, double y1) 
         : _slope(slope), _x1(x1), _y1(y1) 
     {
     }

     double y(double x) const // return y for given x
     {
          return _y1 + (x-_x1)*_slope;                  
     }

  private:
     double _slope, _x1, _y1;
};



回答2:


Are you trying to draw a line on a pixel based display? If so, a slight modification of Bresenham's algorithm might be what you need. Note that the original algorithm needs two points as input and draws the line between them and takes advantage of this in order to give a really elegant solution that does not use floating point arithmetic. In your case, you will likely need to use floating point operations, since I imagine your slope m is not necessarily an integer.

The gist of it, in short is that, depending on the quadrant you are in, you always have a choice between two points that you could draw (see the Algorithm section on the wiki page). What you do is choose the one that is closer to the actual line. That's pretty much it.



来源:https://stackoverflow.com/questions/7948809/compute-next-point-on-a-line-with-known-slope

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!