I am working on a 2D TwoColorImage class, might be useful to someone out there thus I provide the following classes freely, MIT License
package graphics;
import data.BooleanMatrix;
public class TwoColorImage {
BooleanMatrix pixels;
public int getWidth() {
return pixels.getWidth();
}
public int getHeight() {
return pixels.getHeight();
}
public TwoColorImage(int width, int height) {
pixels = new BooleanMatrix(width, height, false);
}
public TwoColorImage(BooleanMatrix pixels) {
this.pixels = pixels;
}
public BooleanMatrix getPixels() {
return pixels;
}
public void turnPixelOn(int x, int y) {
pixels.setElement(x, y, true);
}
public void turnPixelOff(int x, int y) {
pixels.setElement(x, y, false);
}
public void drawLine(int x1, int y1, int x2, int y2) {
int yDifference = y2-y1;
int xDifference = x2-x1;
if (xDifference<0) {
// Switch points, draw left to right
int x1Temp=x1;
int x2Temp=x2;
int y1Temp=y1;
int y2Temp=y2;
x1=x2Temp;
y1=y2Temp;
x2=x1Temp;
y2=y1Temp;
xDifference = x2-x1;
}
double riseOverRun = (double) yDifference/xDifference;
double b = y1-(riseOverRun*x1);
turnPixelOn(x1, y1); // Draw start point
if (y1==y2) {
for (int x=x1+1; x<x2-1; x++) {
turnPixelOn(x, y1);
}
} else if (x1==x2) {
for (int y=y1+1; y<y2-1; y++) {
turnPixelOn(x1, y);
}
} else if (y1<y2){
double computedY;
int integerY;
int usedXs=x1;
int usedYs=y1;
for (int x=x1; x<x2; x++) {
computedY = (riseOverRun*x)+b;
integerY = (int) computedY;
if (computedY>usedYs && x>usedXs) {
turnPixelOn(x, integerY);
usedXs=x;
usedYs=integerY;
}
}
} else if (y2<y1) {
double computedY;
int integerY;
int usedXs=x1;
int usedYs=y1;
for (int x=x1; x<x2; x++) {
computedY = (riseOverRun*x)+b;
computedY = Math.abs(computedY);
integerY = (int) computedY;
integerY = Math.abs(integerY);
if (computedY<usedYs && x>usedXs) {
turnPixelOn(x, integerY);
usedXs=x;
usedYs=integerY;
}
}
}
turnPixelOn(x2, y2); // Draw end point
}
public void drawRectangeCoordinates(int x1, int y1, int x2, int y2) {
for (int x=x1; x<=x2; x++) {
for (int y=y1; y<=y2; y++) {
if (x==x1 || x==x2 || y==y1 || y==y2) {
turnPixelOn(x, y);
}
}
}
}
public void drawRectangeFilledCoordinates(int x1, int y1, int x2, int y2) {
for (int x=x1; x<=x2; x++) {
for (int y=y1; y<=y2; y++) {
turnPixelOn(x, y);
}
}
}
}
I switched it to an object for BooleanMatrix to test the Vector API
package data;
public class BooleanMatrix {
boolean matrix[][];
public int getWidth() {
return matrix.length;
}
public int getHeight() {
return matrix[0].length;
}
public BooleanMatrix(int width, int height, boolean defaultValue) {
matrix = new boolean[width][height];
}
public boolean getElement(int i, int j) {
return matrix[i][j];
}
public void setElement(int i, int j, boolean element) {
matrix[i][j]=element;
}
}
Did not seem to see performance increases via vector API over boolean primitive matrices
package data;
import java.util.Vector;
public class VectorMatrix<T> {
private Vector<Vector<T>> matrix;
public int getWidth() {
return matrix.size();
}
public int getHeight() {
return matrix.get(0).size();
}
public VectorMatrix(int width, int height, T defaultValue) {
matrix = new Vector<>(width);
for (int i=0; i<width; i++) {
matrix.add(i,new Vector<T>(height));
for (int j=0; j<height; j++) {
matrix.get(i).add(defaultValue);
}
}
}
public T getElement(int i, int j) {
return matrix.get(i).get(j);
}
public void setElement(int i, int j, T element) {
matrix.get(i).set(j, element);
}
}
Might be a trick to it and with extra performance data and options for testing likely can tailor hardware for performance gains in Java too
TwoColorImages have the potential to be very cheap from a Throughput standpoint, thoughts of Nintendo’s Virtual Boy
1 bit vs 32 bit or 64 bit colors per pixel? Potential for very fast throughput