资源简介
自己写的解决凸包问题的代码,还具备界面。
代码片段和文件信息
package convexHull;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.util.linkedList;
import java.util.PriorityQueue;
public class ConvexHull {
private frame f = new frame(“CovexHull“);
private final int num = 60;
private MyPoint[] p = new MyPoint[num];
private PriorityQueue p1Queue = new PriorityQueue();
private PriorityQueue p2Queue = new PriorityQueue();
BufferedImage image = new BufferedImage(800 600 BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
private Color foreColor = new Color(25500);
private MyCanvas drawArea = new MyCanvas();
public void init(){
g.fillRect(0 0 800 600);
g.setColor(foreColor);
drawArea.setPreferredSize(new Dimension(800600));
f.addWindowListener(new MyListener());
f.add(drawArea);
f.pack();
f.setVisible(true);
}
public void createPoint(){
int i;
p[0]=new MyPoint();
p[0].x=50;
p[0].y=300;
g.fillOval(p[0].x-3 p[0].y-3 6 6);
for(i=1;i p[i]=new MyPoint();
p[i].x=100+600/num*i;
p[i].y=50+(int)(Math.random()*500);
g.fillOval(p[i].x-3 p[i].y-3 6 6);
}
//i--;
p[i]=new MyPoint();
p[i].x=750;
p[i].y=300;
g.fillOval(p[i].x-3 p[i].y-3 6 6);
drawArea.repaint();
g.drawLine(p[0].x p[0].y p[num-1].x p[num-1].y);
//for(i=0;i // g.drawLine(p[i].x p[i].y p[i+1].x p[i+1].y);
//}
}
public void upHull(linkedList sint maxPriorityQueue queue){
linkedList s1 = new linkedList()s2 = new linkedList();
int i=0divtemp1=0;
MyPoint temp;
if(s.get(max).x>0){
g.drawLine(s.getFirst().x s.getFirst().y s.get(max).x s.get(max).y);
g.drawLine(s.get(max).x s.get(max).y s.getLast().x s.getLast().y);
}
else{
g.drawLine(-s.getFirst().x -s.getFirst().y -s.get(max).x -s.get(max).y);
g.drawLine(-s.get(max).x -s.get(max).y -s.getLast().x -s.getLast().y);
}
try {
Thread.sleep(1000);
drawArea.repaint();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
queue.add(s.get(max));
System.out.println(s.get(max));
//g.fillOval(s.get(max).x s.get(max).y 10 10);
if(max>1){
while(i<=max){
s1.offerLast(s.get(i++));
}
temp=s1.getFirst();
for(i=1;i
div=s1.getFirst().x*(s1.getLast().y-s1.get(i).y)+s1.getLast().x*(s1.get(i).y-s1.getFirst().y)
+s1.get(i).x*(s1.getFirst().y-s1.getLast().y);
div*=-1;
if(div>0){
if(div>temp1){
temp1=div;
temp=s1.get(i);
}
i++;
}
else{
s1.remove(i);
//System.out.println(i+“ “+s1.size());
}
}
upHull(s1s1.indexOf(temp)queue);
}
if(max i=max;
while(i s2.offer(new MyPoint(
评论
共有 条评论