Hilbert Curve in Applet with Matrix of coordinates

余生颓废 提交于 2019-12-23 04:28:25

问题


I am trying to approximate a solution of the Traveling Salesman Problem with a Hilbert Curve program. I have to do it by using an applet. How would I add the Matrix in my code, and how would I display the coordinates in the Applet. I don't need to have more than one frame.

The code is below:

import java.awt.*;
import java.applet.*;   

// Background images (put a map in the back of the applet)

public class HilbertCurve extends Applet{
private SimpleGraphics sg = null;
private final int dist0=512;
private int dist=dist0;


@Override
public void init(){
    resize(dist0, dist0 +300);
    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);
        HilbertU(level-1); sg.lineRel(dist,0);
        HilbertU(level-1); sg.lineRel(0,-dist);
        HilbertC(level-1);
    }
}

private void HilbertD(int level){
    if(level>0){
        HilbertU(level-1); sg.lineRel(dist,0);
        HilbertD(level-1); sg.lineRel(0,dist);
        HilbertD(level-1); sg.lineRel(-dist,0);
        HilbertA(level-1);
    }
}
private void HilbertC(int level){
    if(level>0){
        HilbertA(level-1); sg.lineRel(-dist,0);
        HilbertC(level-1); sg.lineRel(0,-dist);
        HilbertC(level-1); sg.lineRel(dist,0);
        HilbertU(level-1);
    }
}
private void HilbertA(int level){
    if(level>0){
        HilbertC(level-1); sg.lineRel(0,-dist);
        HilbertA(level-1); sg.lineRel(-dist,0);
        HilbertA(level-1); sg.lineRel(0,dist);
        HilbertD(level-1);
    }
}
}

I need to set cities in the matrix, and then when I reach a city to draw a line between the cities, in order to find the shortest path to reach it.

来源:https://stackoverflow.com/questions/48979775/hilbert-curve-in-applet-with-matrix-of-coordinates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!