问题
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