Raster Line
// Created by plusminus on 13:24:05 - 21.09.2008
//package org.andnav2.osm.util;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
class Util {
public static final String DEBUGTAG = "OPENSTREETMAP";
public static final String BASEPATH_ON_EXTERNAL_MEDIA = "andnav2/";
public static final String SDCARD_SAVEDROUTES_PATH = "routes/";
public static final String SDCARD_SAVEDTRACES_PATH = "traces/";
public static final String SDCARD_SKYHOOKCACHE_PATH = "skyhookcache/";
public static final String SDCARD_TILE_PATH = "tiles/";
public static final int NOT_SET = Integer.MIN_VALUE;
public static final int Y = 0;
public static final int X = 1;
/**
* @see http://www.cs.unc.edu/~mcmillan/comp136/Lecture6/Lines.html
*/
public static void rasterLine(int x0, int y0, int x1, int y1, final PixelSetter raster) {
int dy = y1 - y0;
int dx = x1 - x0;
int stepx, stepy;
if (dy < 0) {
dy = -dy;
stepy = -1;
} else {
stepy = 1;
}
if (dx < 0) {
dx = -dx;
stepx = -1;
} else {
stepx = 1;
}
raster.setPixel(x0, y0);
raster.setPixel(x1, y1);
if (dx > dy) {
final int length = (dx - 1) >> 2;
final int extras = (dx - 1) & 3;
final int incr2 = (dy << 2) - (dx << 1);
if (incr2 < 0) {
final int c = dy << 1;
final int incr1 = c << 1;
int d = incr1 - dx;
for (int i = 0; i < length; i++) {
x0 += stepx;
x1 -= stepx;
if (d < 0) { // Pattern:
raster.setPixel(x0, y0); //
raster.setPixel(x0 += stepx, y0); // x o o
raster.setPixel(x1, y1); //
raster.setPixel(x1 -= stepx, y1);
d += incr1;
} else {
if (d < c) { // Pattern:
raster.setPixel(x0, y0); // o
raster.setPixel(x0 += stepx, y0 += stepy); // x o
raster.setPixel(x1, y1); //
raster.setPixel(x1 -= stepx, y1 -= stepy);
} else {
raster.setPixel(x0, y0 += stepy); // Pattern:
raster.setPixel(x0 += stepx, y0); // o o
raster.setPixel(x1, y1 -= stepy); // x
raster.setPixel(x1 -= stepx, y1); //
}
d += incr2;
}
}
if (extras > 0) {
if (d < 0) {
raster.setPixel(x0 += stepx, y0);
if (extras > 1) {
raster.setPixel(x0 += stepx, y0);
}
if (extras > 2) {
raster.setPixel(x1 -= stepx, y1);
}
} else if (d < c) {
raster.setPixel(x0 += stepx, y0);
if (extras > 1) {
raster.setPixel(x0 += stepx, y0 += stepy);
}
if (extras > 2) {
raster.setPixel(x1 -= stepx, y1);
}
} else {
raster.setPixel(x0 += stepx, y0 += stepy);
if (extras > 1) {
raster.setPixel(x0 += stepx, y0);
}
if (extras > 2) {
raster.setPixel(x1 -= stepx, y1 -= stepy);
}
}
}
} else {
final int c = (dy - dx) << 1;
final int incr1 = c << 1;
int d = incr1 + dx;
for (int i = 0; i < length; i++) {
x0 += stepx;
x1 -= stepx;
if (d > 0) {
raster.setPixel(x0, y0 += stepy); // Pattern:
raster.setPixel(x0 += stepx, y0 += stepy); // o
raster.setPixel(x1, y1 -= stepy); // o
raster.setPixel(x1 -= stepx, y1 -= stepy); // x
d += incr1;
} else {
if (d < c) {
raster.setPixel(x0, y0); // Pattern:
raster.setPixel(x0 += stepx, y0 += stepy); // o
raster.setPixel(x1, y1); // x o
raster.setPixel(x1 -= stepx, y1 -= stepy); //
} else {
raster.setPixel(x0, y0 += stepy); // Pattern:
raster.setPixel(x0 += stepx, y0); // o o
raster.setPixel(x1, y1 -= stepy); // x
raster.setPixel(x1 -= stepx, y1); //
}
d += incr2;
}
}
if (extras > 0) {
if (d > 0) {
raster.setPixel(x0 += stepx, y0 += stepy);
if (extras > 1) {
raster.setPixel(x0 += stepx, y0 += stepy);
}
if (extras > 2) {
raster.setPixel(x1 -= stepx, y1 -= stepy);
}
} else if (d < c) {
raster.setPixel(x0 += stepx, y0);
if (extras > 1) {
raster.setPixel(x0 += stepx, y0 += stepy);
}
if (extras > 2) {
raster.setPixel(x1 -= stepx, y1);
}
} else {
raster.setPixel(x0 += stepx, y0 += stepy);
if (extras > 1) {
raster.setPixel(x0 += stepx, y0);
}
if (extras > 2) {
if (d > c) {
raster.setPixel(x1 -= stepx, y1 -= stepy);
} else {
raster.setPixel(x1 -= stepx, y1);
}
}
}
}
}
} else {
final int length = (dy - 1) >> 2;
final int extras = (dy - 1) & 3;
final int incr2 = (dx << 2) - (dy << 1);
if (incr2 < 0) {
final int c = dx << 1;
final int incr1 = c << 1;
int d = incr1 - dy;
for (int i = 0; i < length; i++) {
y0 += stepy;
y1 -= stepy;
if (d < 0) {
raster.setPixel(x0, y0);
raster.setPixel(x0, y0 += stepy);
raster.setPixel(x1, y1);
raster.setPixel(x1, y1 -= stepy);
d += incr1;
} else {
if (d < c) {
raster.setPixel(x0, y0);
raster.setPixel(x0 += stepx, y0 += stepy);
raster.setPixel(x1, y1);
raster.setPixel(x1 -= stepx, y1 -= stepy);
} else {
raster.setPixel(x0 += stepx, y0);
raster.setPixel(x0, y0 += stepy);
raster.setPixel(x1 -= stepx, y1);
raster.setPixel(x1, y1 -= stepy);
}
d += incr2;
}
}
if (extras > 0) {
if (d < 0) {
raster.setPixel(x0, y0 += stepy);
if (extras > 1) {
raster.setPixel(x0, y0 += stepy);
}
if (extras > 2) {
raster.setPixel(x1, y1 -= stepy);
}
} else if (d < c) {
raster.setPixel(stepx, y0 += stepy);
if (extras > 1) {
raster.setPixel(x0 += stepx, y0 += stepy);
}
if (extras > 2) {
raster.setPixel(x1, y1 -= stepy);
}
} else {
raster.setPixel(x0 += stepx, y0 += stepy);
if (extras > 1) {
raster.setPixel(x0, y0 += stepy);
}
if (extras > 2) {
raster.setPixel(x1 -= stepx, y1 -= stepy);
}
}
}
} else {
final int c = (dx - dy) << 1;
final int incr1 = c << 1;
int d = incr1 + dy;
for (int i = 0; i < length; i++) {
y0 += stepy;
y1 -= stepy;
if (d > 0) {
raster.setPixel(x0 += stepx, y0);
raster.setPixel(x0 += stepx, y0 += stepy);
raster.setPixel(x1 -= stepy, y1);
raster.setPixel(x1 -= stepx, y1 -= stepy);
d += incr1;
} else {
if (d < c) {
raster.setPixel(x0, y0);
raster.setPixel(x0 += stepx, y0 += stepy);
raster.setPixel(x1, y1);
raster.setPixel(x1 -= stepx, y1 -= stepy);
} else {
raster.setPixel(x0 += stepx, y0);
raster.setPixel(x0, y0 += stepy);
raster.setPixel(x1 -= stepx, y1);
raster.setPixel(x1, y1 -= stepy);
}
d += incr2;
}
}
if (extras > 0) {
if (d > 0) {
raster.setPixel(x0 += stepx, y0 += stepy);
if (extras > 1) {
raster.setPixel(x0 += stepx, y0 += stepy);
}
if (extras > 2) {
raster.setPixel(x1 -= stepx, y1 -= stepy);
}
} else if (d < c) {
raster.setPixel(x0, y0 += stepy);
if (extras > 1) {
raster.setPixel(x0 += stepx, y0 += stepy);
}
if (extras > 2) {
raster.setPixel(x1, y1 -= stepy);
}
} else {
raster.setPixel(x0 += stepx, y0 += stepy);
if (extras > 1) {
raster.setPixel(x0, y0 += stepy);
}
if (extras > 2) {
if (d > c) {
raster.setPixel(x1 -= stepx, y1 -= stepy);
} else {
raster.setPixel(x1, y1 -= stepy);
}
}
}
}
}
}
}
//
// Inner and Anonymous Classes
//
public static interface PixelSetter {
public void setPixel(final int x, final int y);
}
}
Related examples in the same category