class Point{
double x,y;
Point(double x,double y){
this.x=x;
this.y=y;
}
public void show(){
System.out.println("<"+x+" "+y+"> ");
}
}
class Hexagon{
final int lenth=800,wide=800;
public static int EDGE=50;//边长
public static int char_s=8;//字符间距
public static int sum=0;//数字和
public static int RESEVERWIDE=2;//变宽
public static int UPLINE=4;//选中时线宽
public static int DEFLINE=1;//默认线宽
public static int maxmi=5;//
public static int minmi=1;//
public static Stack<Integer> stackvalue = new Stack<Integer>();
public int id;//id
public int value;//
public double x,y;//坐标
public String str;//字符串
public Polygon shape;//形状
public Text text;//文本框
public Color downColor=Color.YELLOW;//颜色
public Color upColor=Color.RED;//选中颜色
public Color color=Color.GREEN;//
public static final Color colors[]={
Color.ALICEBLUE,
Color.BLANCHEDALMOND,
Color.AQUA,
Color.CHARTREUSE,
Color.SEAGREEN,
Color.PLUM,
Color.PINK,
Color.YELLOWGREEN,
Color.BLUEVIOLET,
Color.BLUE,
Color.RED
};//颜色表
boolean isUp=false;
static Hexagon oldH=null,newH=null;//
Hexagon(double X,double Y){
id=sum;
sum++;
x=X;
y=Y;
if(id==0)
value=maxmi;
else
value=greateValues();
shape=new Polygon();
color=colors[value-minmi+1];
shape.setFill(color);
shape.setStroke(downColor);
shape.getPoints().addAll(new Double[]{
X, Y-(EDGE-RESEVERWIDE),
X+Math.sqrt(3)*(EDGE-RESEVERWIDE)/2,Y-(EDGE-RESEVERWIDE)/2,
X+Math.sqrt(3)*(EDGE-RESEVERWIDE)/2,Y+(EDGE-RESEVERWIDE)/2,
X, Y+(EDGE-RESEVERWIDE),
X-Math.sqrt(3)*(EDGE-RESEVERWIDE)/2,Y+(EDGE-RESEVERWIDE)/2,
X-Math.sqrt(3)*(EDGE-RESEVERWIDE)/2,Y-(EDGE-RESEVERWIDE)/2,
});
shape.setOnMousePressed(e->{
if(oldH==null)
{
lightUp(this);
oldH=this;
}else if(oldH==this){
lightDown(this);
oldH=null;
}else if(oldH.value==this.value&&adjacent(oldH,this)){
if(oldH.id==0)
addHexagon(oldH,this);
else
addHexagon(this,oldH);
lightDown(oldH);
oldH=null;
}else{
lightDown(oldH);
lightUp(this);
oldH=this;
}
System.out.println("id-> "+id);
});
str=""+(int)Math.pow(2,value);
text=new Text(X-char_s*str.length()/2,Y+char_s/2,""+(int)Math.pow(2,value));
//让文本也响应事件,因为文本覆盖了六边形一部分。
/* text.setOnMousePressed(e->{
//lightUp();
System.out.println("text.setOnMousePressed");});
if(oldH==null){
oldH=this;
System.out.println("id--> "+id);
}*/
}
public static int greateValues(){
Random r = new Random();
double x=3;
do{
x=Math.abs(r.nextGaussian());
}while(x>2);
return (int)(x*(maxmi-2)/2+minmi);
}
public static boolean lately(ArrayList<Hexagon> hs,Point p){//�า��
for(Hexagon i:hs){
if(Math.pow((i.x-p.x),2)+Math.pow((i.y-p.y),2)<EDGE*EDGE){
return false;
}
}
return true;
}
public static boolean lately(Hexagon h1,Hexagon h2){//�า��
if(Math.pow((h1.x-h2.x),2)+Math.pow((h1.y-h2.y),2)<EDGE*EDGE){
return true;
}
return false;
}
public static boolean lately(Point h1,Point h2){//�า��
if(Math.pow((h1.x-h2.x),2)+Math.pow((h1.y-h2.y),2)<EDGE*EDGE){
return true;
}
return false;
}
public static boolean adjacent(Hexagon h1,Hexagon h2){//������
if(Math.pow((h1.x-h2.x),2)+Math.pow((h1.y-h2.y),2)<4*EDGE*EDGE){
return true;
}
return false;
}
public void lightUp(Hexagon hexagon)
{
hexagon.shape.setStroke(hexagon.upColor);
hexagon.shape.setStrokeWidth(UPLINE);
hexagon.isUp=true;
}
public void lightDown(Hexagon hexagon)
{
hexagon.shape.setStroke(hexagon.downColor);
hexagon.shape.setStrokeWidth(DEFLINE);
hexagon.isUp=false;
}
public void addHexagon(Hexagon h1,Hexagon h2){
if(h1.value==maxmi||h2.id==0){
maxmi+=1;
if(maxmi==10)
System.out.println("666666");
}
h1.value+=1;
h2.value=greateValues();
System.out.println(">>"+h1.value+ " >"+h2.value);
h1.color=colors[h1.value-minmi+1];
h2.color=colors[h2.value-minmi+1];
h1.text.setText(""+(int)Math.pow(2,h1.value));
h2.text.setText(""+(int)Math.pow(2,h2.value));
refresh(h1);
refresh(h2);
}
public void refresh(Hexagon hexagon){
hexagon.shape.setFill(hexagon.color);
}
//利用六边形围成的图形也是六边形的特性
public static Point[] findNode(double edge,int top,Point P){//top
Point[] six=new Point[7];
Point[] poi=new Point[3*(top*top+top)+1];
int e=0;
poi[e]=P;
for(int t=1;t<=top;t++){
double a=Math.sqrt(3)*edge*t;//
double h=a*Math.sqrt(3)/2;
six[0]=new Point(P.x+a,P.y);
six[1]=new Point(P.x+a/2,P.y+h);
six[2]=new Point(P.x-a/2,P.y+h);
six[3]=new Point(P.x-a,P.y);
six[4]=new Point(P.x-a/2,P.y-h);
six[5]=new Point(P.x+a/2,P.y-h);
six[6]=new Point(P.x+a,P.y);
for(int index=0;index<6;index++){
poi[++e]=six[index];
for(int cer=1;cer<t;cer++){
int ndf=t;
double ex=six[index+1].x-six[index].x;
double ey=six[index+1].y-six[index].y;
poi[++e]=new Point(six[index].x+ex*cer/ndf,
six[index].y+ey*cer/ndf);
six[index].show();
poi[e].show();
six[index+1].show();
}
}
}
return poi;
}
//种子填充法
public static Point[] findNode2(double edge,int top,Point P){//top
int edges=3*(top*top+top);
double FERROR=0.0001;//
Point[] poi=new Point[edges+1];
poi[0]=new Point(P.x+0,P.y+0);
int e=0;
for(int t=1;t<=top;t++){
e=0;
int tedegs=3*((t-1)*(t-1)+(t-1));
double a=Math.sqrt(3)*edge*t;//��
double b=3*edge/4*(2*t);
double dt_radian=2*Math.PI/(t*6);//�Ƕ� /
for(double radian=0;radian<=2*Math.PI/3+FERROR;radian+=dt_radian){
if(radian<Math.PI/3-FERROR){
double x=a/(1+Math.tan(radian)/Math.sqrt(3));
double y=x*Math.tan(radian);
poi[++e+tedegs]=new Point(P.x+x,P.y+y);
}else{
int n=t+1;
for(int i=0;i<n;i++){
poi[++e+tedegs]=new Point(P.x+a/2-i*(a/(n-1)),P.y+b);
System.out.println("x--> "+(P.x+a/2-i*(a/(n-1)))+" y--> "+(P.y+b));
}
for(int te =tedegs +e+1,index=tedegs +e-(t+1)
;index>tedegs +1;
te++,index--){
poi[te]=new Point(2*P.x-poi[index].x,poi[index].y);
}
for(int i=tedegs+1;i<=tedegs+3*t;i++){
poi[i+3*t]=new Point(2*P.x-poi[i].x, 2*P.y-poi[i].y);
}
break;
}
}
}
return poi;
}
}
注:个人劳动成果,不提供主类。(点一下玩一年!)