5,最接近点对(一维和二维)
代码:
package com.zhang1;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class Point
{
int x,y;//点坐标
//构造方法
public Point(int xx,int yy)
{
x = xx;
y = yy;
}
}
//按照X坐标排序之后的点进行存放
class Point1 extends Point
{
int id;//点编号
//构造方法
public Point1(int xx,int yy, int theID)
{
super(xx,yy);
id = theID;
}
}
//按照Y坐标排序之后的点进行存放
class Point2 extends Point
{
int p;//同一点在数组X中的坐标
//构造方法
public Point2(int xx,int yy, int pp)
{
super(xx,yy);
p = pp;
}
}
public class Pair extends Frame implements ActionListener
{
Point a;//平面点a
Point b;//平面点b
double dist;//平面点a和b间的距离
int nFlag = 0;//确定要执行的操作,0为没有,1为画一维直线上的点,2为画二维平面上的点
Point1 x[] = new Point1[10];//所求最接近点对的点
Pair p;
private Button draw1Button = new Button("绘图1维");
private Button draw2Button = new Button("绘图2维");
private Button calcuButton = new Button("计算距离");
//构造方法
public Pair()
{
super("最接近点对");
Panel top = new Panel();
top.setLayout(new FlowLayout());
top.add(draw1Button);
top.add(draw2Button);
top.add(calcuButton);
Panel buttom = new Panel();
setLayout(new BorderLayout());
add("North",top);
add("South",buttom);
addWindowListener(new WindowCloser());
setSize(1024,768);
setVisible(true);
draw1Button.addActionListener(this);
draw2Button.addActionListener(this);
calcuButton.addActionListener(this);
}
//构造函数
public Pair(Point1 aa,Point1 bb,dou