I am using a space filling curve (Hilbert Curve) to try to approximate the fastest way to go through a set of cities in a matrix). My last step in the project is to turns all the numbers into 0s and 1s
I use a matrix of values that goes from 0 to 1023 (32x32) and every time there is a city in a number, it draws a line between them.
My question is: how do I turn these numbers from 0 to 1023, to all 0s (no cities) and 1s (cities) and connect the 1s?
The matrix code and how I fill the matrix is in the SimpleGraphics class in the lineRel function. In my example, there are cities in the points 10, 123, 193, 430, and 934.
Class HilbertCurve:
import java.awt.*;
import java.applet.*;
public class HilbertCurve extends Applet{
private SimpleGraphics sg = null;
private final int dist0=512;
private int dist=dist0;
public static int ix =0;
public static int iy =0;
@Override
public void init(){
resize(dist0, dist0);
sg = new SimpleGraphics(getGraphics());
}
@Override
public void paint(Graphics g){
int level = 5;
dist = dist0;
for(int i=level; i>0; i--) dist/=2;
sg.goToXY (dist/2, dist/2);
HilbertU(level);
}
private void HilbertU(int level){
if(level>0){
HilbertD(level-1); sg.lineRel(0,dist); iy++;
HilbertU(level-1); sg.lineRel(dist,0);ix++;
HilbertU(level-1); sg.lineRel(0,-dist);iy--;
HilbertC(level-1);
}
}
private void HilbertD(int level){
if(level>0){
HilbertU(level-1); sg.lineRel(dist,0); ix++;
HilbertD(level-1); sg.lineRel(0,dist); iy++;
HilbertD(level-1); sg.lineRel(-dist,0); ix--;
HilbertA(level-1);
}
}
private void HilbertC(int level){
if(level>0){
HilbertA(level-1); sg.lineRel(-dist,0); ix--;
HilbertC(level-1); sg.lineRel(0,-dist); iy++;
HilbertC(level-1); sg.lineRel(dist,0); ix++;
HilbertU(level-1);
}
}
private void HilbertA(int level){
if(level>0){
HilbertC(level-1); sg.lineRel(0,-dist); iy++;
HilbertA(level-1); sg.lineRel(-dist,0); ix++;
HilbertA(level-1); sg.lineRel(0,dist); iy--;
HilbertD(level-1);
}
}
}
Class SimpleGraphics:
import java.awt.*;
class SimpleGraphics{
private Graphics g = null;
private int x =0, y = 0;
public int a;
public static int[][] matrix = new int[200][200];
int c = 0;
int matrixpoint = -1;
int tempx = 0;
int tempy = 0;
int k=0;
public SimpleGraphics(Graphics g) {
this.a = 1;
this.g = g;}
public void goToXY(int x, int y){
this.x =x;
this.y= y;
}
public void lineRel(int deltaX, int deltaY){
if(c ==0) {matrix[HilbertCurve.ix][HilbertCurve.iy]=matrixpoint;
c++;
matrixpoint++;
}
matrix[HilbertCurve.ix][HilbertCurve.iy]=matrixpoint;
matrixpoint++;
if(k==0 && matrix[HilbertCurve.ix][HilbertCurve.iy]==10){
tempx = x;
tempy = y;
}
if(matrix[HilbertCurve.ix][HilbertCurve.iy]==123 || matrix[HilbertCurve.ix][HilbertCurve.iy]== 193 ||
matrix[HilbertCurve.ix][HilbertCurve.iy]==430 || matrix[HilbertCurve.ix][HilbertCurve.iy]== 934 ){
g.drawLine(tempx, tempy, x, y);
tempx = x;
tempy = y;
}
x+=deltaX;
y+=deltaY;
}
}