[Moving top posted response down]
>>Anyway, I have a 1.2 enables class that does the dots and thick line
>>>drawing if a 2D enables graphics object is available, but, my math is not
[quoted text clipped - 8 lines]
> great information. I will look this up and see what I can do, even if for my
> own personal achievment!
Please do not top-post.
I think a printer's devil crept into Thomas's post in the name of the
algorithm - you might find better results if you search for "Bresenham's
Algorithm" instead.
BK
SPG - 01 Mar 2005 16:01 GMT
> [Moving top posted response down]
>
[quoted text clipped - 18 lines]
>
> BK
Hi,
Yes, thanks for that, I managed to find the algorithm and coded it thus:
<SNIP>
public static void drawDottedLine(Graphics g, int x0, int y0, int x1, int
y1, Color color, int dashLen, int spaceLen) {
Color c = g.getColor();
g.setColor( color );
int dx = x1 - x0;
int dy = y1 - y0;
float t = 0.5f;
g.setColor(color);
g.drawLine(x0, y0, x0, y0);
int dashCount = 0;
int spaceCount = 0;
boolean doPlot = dashLen > 1;
if (Math.abs(dx) > Math.abs(dy)) { // slope < 1
float m = (float) dy / (float) dx; // compute slope
t += y0;
dx = (dx < 0) ? -1 : 1;
m *= dx;
while (x0 != x1) {
x0 += dx; // step to next x value
t += m;
if (doPlot) {
g.drawLine(x0, (int) t, x0, (int) t);
dashCount++;
if (dashCount >= dashLen) {
dashCount = 0;
spaceCount = 0;
doPlot = false;
}
}
else {
spaceCount++;
if (spaceCount >= spaceLen) {
spaceCount = 0;
dashCount = 0;
doPlot = true;
}
}
}
}
else if (dy != 0) { // slope >= 1
float m = (float) dx / (float) dy; // compute slope
t += x0;
dy = (dy < 0) ? -1 : 1;
m *= dy;
while (y0 != y1) {
y0 += dy; // step to next y value
t += m;
if (doPlot) {
g.drawLine( (int) t, y0, (int) t, y0);
dashCount++;
if (dashCount >= dashLen) {
dashCount = 0;
spaceCount = 0;
doPlot = false;
}
}
else {
spaceCount++;
if (spaceCount >= spaceLen) {
spaceCount = 0;
dashCount = 0;
doPlot = true;
}
}
}
}
g.setColor(c);
}
</SNIP>
Works very well!
Steve