部分集合の束をSVGでお絵かき



public class PowerSet {
	int[][][] element;
	double[][][] location;
	int[][][] relation;
	int maxcomb;
	
	public PowerSet(int k){
		element = Combination.PowerSet(k);
		int half=k/2;
		System.out.println("k/2 " + half);
		maxcomb = Combination.NComb2(k,half);
		System.out.println("maxcomb " + maxcomb);
		location = new double[element.length][0][2];
		for(int i=0;i<element.length;i++){
			location[i] = new double[element[i].length][2];
			double dif = (double)(element[half].length-element[i].length)/2;
			for(int j=0;j<element[i].length;j++){
				location[i][j][0]=dif+j;
				location[i][j][1]=i;
			}
		}
		relation = new int[element.length-1][0][0];
		for(int i=0;i<element.length-1;i++){
			relation[i]=new int[element[i].length][element[i+1].length];
			for(int j=0;j<location[i].length;j++){
				
				
				for(int t=0;t<element[i+1].length;t++){
					relation[i][j][t]=1;
					for(int s=0;s<element[i][j].length;s++){
						
						
						if(element[i][j][s]!=element[i+1][t][s] && 
							element[i][j][s]!=element[i+1][t][s+1]){
							relation[i][j][t]=0;
								//flag =false;
							break;
						}
						
						
					}
					
				}
				
			}
		}
		
	} 
	public static void main(String[] args){
		int k=8;
		PowerSet ps = new PowerSet(k);
		//PowerSet.PrintPowerSetElement(ps,"\t","\n");
		//PowerSet.PrintPowerSetLocation(ps,"\t","\n");
		//PowerSet.PrintPowerSetRelation(ps,"\t","\n");
		String outfile ="out.svg";
		String outfileInfo ="info.txt";
		BufferedWriter[] bw = new BufferedWriter[2];
		bw[0]= null;
		bw[1]= null;
		
		Print2FileSVGPowerSet(ps,bw[0],outfile);
		Print2FileInfoPowerSet(ps,"\t","\n",bw[1],outfileInfo);
		/*
		String svg = StringSVGPowerSet(ps);
		System.out.println(svg);
		*/
	}
	public static String StringSVGPowerSet(PowerSet ps){
		double[] size ={500,500};
		double[] viewBoxZero ={-1,-1};
		double[] viewBoxLength = {ps.maxcomb-viewBoxZero[0],ps.maxcomb-viewBoxZero[1]};
		if(ps.maxcomb<ps.element.length){
			viewBoxLength[0]+=(ps.element.length-ps.maxcomb);
			viewBoxLength[1]+=(ps.element.length-ps.maxcomb);
		}
		double r=0.2;
		double width = 0.01;
		String ret ="";
		String objP ="";
		String objM ="";
		String header = "<?xml version=\"1.0\" standalone=\"yes\" ?>\n";
		header += "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 20010904//EN\" \"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd\">\n";
		header += "<svg width=\""+ size[0] +"\" height=\"" + size[1] +"\" viewBox=\"" + viewBoxZero[0] + " " + viewBoxZero[1] +" " + viewBoxLength[0] + " " + viewBoxLength[1] +"\" xmlns=\"http://www.w3.org/2000/svg\">\n";
		String footer = "</svg>\n";
		for(int i=0;i<ps.location.length;i++){
			for(int j=0;j<ps.location[i].length;j++){
				
				
				String stP = StringCircle(ps.location[i][j],r);
				
				objP += stP;
			}
			 
		}
		for(int i=0;i<ps.relation.length;i++){
			for(int j=0;j<ps.relation[i].length;j++){
				for(int k=0;k<ps.relation[i][j].length;k++){
					if(ps.relation[i][j][k]==1){
						double[] tmp = new double[4];
						tmp[0]= ps.location[i][j][0];
						tmp[1]= ps.location[i][j][1];
						tmp[2]= ps.location[i+1][k][0];
						tmp[3]= ps.location[i+1][k][1];
						String stM = StringLine(tmp,width);
						objM+=stM;
					}
				}
			}
		}
		
		ret += header + objM + objP  + footer;
		
		return ret;
	}
	
	public static void Print2FileSVGPowerSet(PowerSet ps,BufferedWriter bw,String file){
		try{
			bw = new BufferedWriter(new FileWriter(file));
			double[] size ={500,500};
			double[] viewBoxZero ={-1,-1};
			double[] viewBoxLength = {ps.maxcomb-viewBoxZero[0],ps.maxcomb-viewBoxZero[1]};
			if(ps.maxcomb<ps.element.length){
				viewBoxLength[0]+=(ps.element.length-ps.maxcomb);
				viewBoxLength[1]+=(ps.element.length-ps.maxcomb);
			}
			double r=0.2;
			double width = 0.01;
			String ret ="";
			String objP ="";
			String objM ="";
			String header = "<?xml version=\"1.0\" standalone=\"yes\" ?>\n";
			header += "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 20010904//EN\" \"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd\">\n";
			header += "<svg width=\""+ size[0] +"\" height=\"" + size[1] +"\" viewBox=\"" + viewBoxZero[0] + " " + viewBoxZero[1] +" " + viewBoxLength[0] + " " + viewBoxLength[1] +"\" xmlns=\"http://www.w3.org/2000/svg\">\n";
			
			InOut.out3File(bw,header);
			
			
			String footer = "</svg>\n";
			for(int i=0;i<ps.location.length;i++){
				for(int j=0;j<ps.location[i].length;j++){
					
					
					String stP = StringCircle(ps.location[i][j],r);
					InOut.out3File(bw,stP);
					//objP += stP;
				}
				 
			}
			for(int i=0;i<ps.relation.length;i++){
				for(int j=0;j<ps.relation[i].length;j++){
					for(int k=0;k<ps.relation[i][j].length;k++){
						if(ps.relation[i][j][k]==1){
							double[] tmp = new double[4];
							tmp[0]= ps.location[i][j][0];
							tmp[1]= ps.location[i][j][1];
							tmp[2]= ps.location[i+1][k][0];
							tmp[3]= ps.location[i+1][k][1];
							String stM = StringLine(tmp,width);
							InOut.out3File(bw,stM);
							//objM+=stM;
						}
					}
				}
			}
			
			//ret += header + objM + objP  + footer;
			
			//return ret;
			InOut.out3File(bw,header);
			bw.close();
		}catch(IOException e){
			System.out.println(e);
		}
		
		
	}
	
	public static void Print2FileInfoPowerSet(PowerSet ps,String sep1,String sep2,BufferedWriter bw,String file){
		try{
//			PowerSet.PrintPowerSetElement(ps,"\t","\n");
			//PowerSet.PrintPowerSetLocation(ps,"\t","\n");
			//PowerSet.PrintPowerSetRelation(ps,"\t","\n");
			bw = new BufferedWriter(new FileWriter(file));
			String pst="Powerset_Element"+sep2;
			InOut.out3File(bw,pst);
			for(int i=0;i<ps.element.length;i++){
				String pst_i="";
				for(int j=0;j<ps.element[i].length;j++){
					String pst_j="";
					for(int t=0;t<ps.element[i][j].length;t++){
						pst_j += ps.element[i][j][t] + ",";
					}
					pst_j += sep1;
					InOut.out3File(bw,pst_j);
				}
				pst_i += sep2;
				InOut.out3File(bw,pst_i);
			}
			
			String pst2="Powerset_Location"+sep2;
			InOut.out3File(bw,pst2);
			for(int i=0;i<ps.location.length;i++){
				String pst_i="";
				for(int j=0;j<ps.location[i].length;j++){
					String pst_j="";
					for(int t=0;t<ps.location[i][j].length;t++){
						pst_j += ps.location[i][j][t] + ",";
					}
					pst_j += sep1;
					InOut.out3File(bw,pst_j);
				}
				pst_i += sep2;
				InOut.out3File(bw,pst_i);
			}
			
			String pst3="Powerset_Relation"+sep2;
			InOut.out3File(bw,pst2);
			for(int i=0;i<ps.relation.length;i++){
				String pst_i="";
				for(int j=0;j<ps.relation[i].length;j++){
					String pst_j="";
					for(int t=0;t<ps.relation[i][j].length;t++){
						pst_j += ps.relation[i][j][t] + ",";
					}
					pst_j += sep1;
					InOut.out3File(bw,pst_j);
				}
				pst_i += sep2;
				InOut.out3File(bw,pst_i);
			}
			
			bw.close();
		}catch(IOException e){
			System.out.println(e);
		}
		
		
	}
	
	
	public static String StringLine(double[] d,double width){
		/*
		 * <line x1="100" y1="300" x2="300" y2="100"
            stroke-width="5"  />

		 */
		String ret="<line x1=\"";
		ret += d[0]+"\" y1=\""+d[1]+"\" x2=\"" +d[2] +"\" y2=\"" +d[3] + "\"";
		ret += " stroke=\"black\" stroke-width=\"" + width + "\" ";
		//ret += "black";
		ret += "/>\n";
		
		return ret;
	}
	public static String StringCircle(double[] d,double r){
		/*
		 * <circle cx="600" cy="200" r="100"
        fill="red" stroke="blue" stroke-width="10"  />

		 */
		String ret="<circle cx=\"";
		ret += d[0]+"\" cy=\""+d[1]+"\" r=\"" +r +"\"";
		ret += " stroke=\"none\" stroke-width=\"0\" fill=\"";
		ret += "black";
		ret += "\"/>\n";

		return ret;
	}
	
	public static void PrintPowerSetRelation(PowerSet ps, String sep1, String sep2){
		String ret = StringPowerSetRelation(ps,sep1,sep2);
		
		System.out.println(ret);
	}
	public static String StringPowerSetRelation(PowerSet ps, String sep1, String sep2){
		
		String pst="Powerset_Relation"+sep2;
		for(int i=0;i<ps.relation.length;i++){
			for(int j=0;j<ps.relation[i].length;j++){
				for(int t=0;t<ps.relation[i][j].length;t++){
					pst += ps.relation[i][j][t] + ",";
				}
				pst += sep1;
			}
			pst += sep2;
		}
		return pst;
		
	}
	
	public static void PrintPowerSetLocation(PowerSet ps, String sep1, String sep2){
		String ret = StringPowerSetLocation(ps,sep1,sep2);
		
		System.out.println(ret);
	}
	public static String StringPowerSetLocation(PowerSet ps, String sep1, String sep2){
		
		String pst="Powerset_Location"+sep2;
		for(int i=0;i<ps.location.length;i++){
			for(int j=0;j<ps.location[i].length;j++){
				for(int t=0;t<ps.location[i][j].length;t++){
					pst += ps.location[i][j][t] + ",";
				}
				pst += sep1;
			}
			pst += sep2;
		}
		return pst;
		
	}
	public static void PrintPowerSetElement(PowerSet ps, String sep1, String sep2){
		String ret = StringPowerSetElement(ps,sep1,sep2);
		
		System.out.println(ret);
	}
	public static String StringPowerSetElement(PowerSet ps, String sep1, String sep2){
		
		String pst="Powerset_Element"+sep2;
		for(int i=0;i<ps.element.length;i++){
			for(int j=0;j<ps.element[i].length;j++){
				for(int t=0;t<ps.element[i][j].length;t++){
					pst += ps.element[i][j][t] + ",";
				}
				pst += sep1;
			}
			pst += sep2;
		}
		return pst;
		
	}
}