0

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;
}

}

The output of the numbers is: Output Numbers

The output of the path is:Output Path

  • Your code is filling the matrix with a consecutive sequence of integers during the recursive call of `HilberCurve.paint` or `HilbertCurve.HilbertU`. When you reach predefined numbers in the sequence, you connect the respective locations (and remember them for the next location to connect). Rather than filling the matrix, you could use a static matrix where every non-city cell is `0` and every city cell is `1`. Then you connect the locations marked as `1`, i.e. you ask for the `1` rather than for the specific number in the sequence. – Axel Kemper Mar 29 '18 at 20:15
  • Where do I fill the matrix? in the SimpleGraphics or the HilbertCurve class – Ian Tupiara Mar 29 '18 at 20:23
  • The matrix is created in `SimpleGraphics`. It should be filled right after creation (= before usage). You could pass the list of city locations from `HilbertCurve.init()` to the extended version of the `SimpleGraphics` constructor. – Axel Kemper Mar 29 '18 at 20:27
  • I did that, and I am filling it with 1s just to check if it is working, and it is not working. It is filling 1s in only some of the matrix. I wish I could post it here, but it fills the first 8x16 values (I think)and then it fills some of the values below the 9th row and 9th column. It is weird, I don't know what is happening – Ian Tupiara Mar 29 '18 at 20:47
  • Yes, that's the fate of debugging. Use your `Eclipse Debugger` (or whatever Java IDE Debugger you are using) to find out what is going on. That will help you to correct your code. – Axel Kemper Mar 29 '18 at 20:58
  • Thank you for the help, but there is something weird going on. I will see what I can do – Ian Tupiara Mar 29 '18 at 21:35
  • I am posting the question in a different way now to see if someone else know what to do about it, but I can send you the link through here later if you want to take a look – Ian Tupiara Mar 29 '18 at 21:37

0 Answers0