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 Tesitnggr.setColor(Color.YELLOW);LineDDA.Enumerate(10, 100, 10, 200, 0, plotter); // No Slope & div by zeroLineDDA.Enumerate(20, 200, 20, 100, 0, plotter); // No Slope & point swapLineDDA.Enumerate(10, 100, 300, 100, 0, plotter); // Zero Slope & div by zerogr.setColor(Color.ORANGE);LineDDA.Enumerate(3, 100, 300, 0, 2, plotter); // Negative Slope// Positive Slopegr.setColor(Color.CYAN);LineDDA.Enumerate(15, 20, 300, 200, 7, plotter); // standard pointLineDDA.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!

[ view entry ] ( 3151 views )   |  permalink  |  related link
Backup & Restore Dates & Times under Linux
Here are two single-line scripts to save + restore the date & times of files + directories under Linux:

Save:

`find / -mount -print0 | perl -ne 'INIT{ \$/ = "\0"; use File::stat;} chomp; my \$s = stat(\$_); next unless \$s; print \$s->ctime . "/" . \$s->mtime . "/" . \$s->atime ."/\$_\0"; ' > dates.dat`

Restore:

`cat dates.dat |  perl -ne 'INIT{ \$/ = "\0";} chomp; m!^([0-9]+)/([0-9]+)/([0-9]+)/(.*)!s or next; my (\$ct, \$mt, \$at, \$f) = (\$1, \$2, \$3, \$4); utime \$at, \$mt, \$f;'`

I did not write them - got them from somewhere else - but I used them today. They worked great: We use them to save + restore the dates & times from a backup over to a server where a few gig of files were intermixed.

Wanted to keep it here for future use!

[ view entry ] ( 3107 views )   |  permalink  |  related link
"Doctor Quote" Scores Another Award
Doctor Quote scores another 5-Star award ... and this is for the old 1.0 Version.

I think the official tally is up to about 50 awards so far - even got one from PC Week & Simtel a few years ago.

Techy Note: Wrote it, as well as the installer (EzInstall) for Microsoft Windows in Borland C++ Builder. These days, it all works just fine under Linux & OS X via WIN32 Emulation, as well.

[ view entry ] ( 2035 views )   |  permalink  |  related link

<<First <Back | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | Next> Last>>