 Drawing Points On A Line
Certainly the idea of drawing a simple line is well supported by just about every graphical framework. What is less sure is the ability to enumerate through a set of points so as to choose just which points we are interested in.

LineDDA For Java

While the technique pre-dates Microsoft Windows, the idea of using a Digital Differential Analyzer (DDA) might seem easy enough to write. Once we account for slope management however, the algorithm can get a little more complex:

public static boolean Enumerate(int x1, int y1, int x2, int y2, int step, PointCallback result) {
if (result == null) {
return false;
}
if (step == 0) {
step = 1; // avoid "divide by zero" possibility
}
if ((x1 * y1) > x2 * y2) {
int h = x1;
x1 = x2;
x2 = h;
h = y1;
y1 = y2;
y2 = h;
}
int base = (x2 - x1);
if (base == 0) {
base = 1; // avoid "divide by zero" possibility
}
double m = (double) (y2 - y1) / base;
double y = (double) y1;

if(x1 == x2) {
// No Slope
int iy;
while (y <= y2) {
if (y % step == 0) {
iy = (int) Math.round(y);
result.onPoint(x1, iy);
}
y++;
}
return true;
}

if (x1 > x2) {
// Negative Slope
y = (double) y2;
int iy;
for (int x = x2; x <= x1; x++) {
if (x % step == 0) {
iy = (int) Math.round(y);
result.onPoint(x, iy);
}
y += m;
}
} else {
// Positive & zero Slope
int iy;
for (int x = x1; x <= x2; x++) {
if (x % step == 0) {
iy = (int) Math.round(y);
result.onPoint(x, iy);
}
y += m;
}
}

return true;
}

In the above, we have cobbled together a point enumerator. One that also has a 'step' capability: Note that testing has verified the support of positive slope, negative slope, zero slope, no slope, as well as the need for point inversion.

Graphics gr = jpDrawing.getGraphics();
gr.drawLine(300, 15, 300, 200);
boolean br = false;

LineDDA.PointPlotter plotter = new PointPlotter(gr, 5, 1);

// Slope Tesitng
gr.setColor(Color.YELLOW);
LineDDA.Enumerate(10, 100, 10, 200, 0, plotter); // No Slope & div by zero
LineDDA.Enumerate(20, 200, 20, 100, 0, plotter); // No Slope & point swap
LineDDA.Enumerate(10, 100, 300, 100, 0, plotter); // Zero Slope & div by zero

gr.setColor(Color.ORANGE);
LineDDA.Enumerate(3, 100, 300, 0, 2, plotter); // Negative Slope

// Positive Slope
gr.setColor(Color.CYAN);
LineDDA.Enumerate(15, 20, 300, 200, 7, plotter); // standard point
LineDDA.Enumerate(300, 200, 0, 0, 3, plotter); // point swap

// Picket Testing
Point p1 = new Point(20, 200);
Point p2 = new Point(300, 20);

plotter.setHeight(9);
gr.setColor(Color.RED);
LineDDA.Enumerate(p1, p2, 20, plotter);

plotter.setWidth(15);
gr.setColor(Color.CYAN);
LineDDA.Enumerate(p2, p1, 40, plotter);

Here is the source code & test. --Hope you will find it useful!