package laladin.angleTest; import java.util.ArrayList; import java.util.List; public class AngleTest { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub String state="1234"; int key; List<Integer> list=new ArrayList<Integer>(); StringBuffer sb=new StringBuffer(); for(int i=0;i<100;i++){ key=(int)(Math.random()*5); switch (key) { case 0: //转90 list.add(90); state=rotate(state,90); sb.append("转90,"); break; case 1: //转180 list.add(180); sb.append("转180,"); state=rotate(state,180); break; case 2: //转270 list.add(270); sb.append("转270,"); state=rotate(state,270); break; case 3: //水平 list.add(-1); sb.append("水平,"); state=hFlip(state); break; case 4: //垂直 list.add(-2); sb.append("垂直,"); state=vFlip(state); break; } } System.out.println(sb.toString()); System.out.println(state); state="1234"; int aa=rotateAndFlip(list); if(aa<0) state=hFlip(state); state=rotate(state,Math.abs(aa)); System.out.println(state); } public static String rotate(String string,int degree){ switch (degree) { case 90: string=string.substring(3)+string.substring(0, 3); break; case 180: string=string.substring(2)+string.substring(0,2); break; case 270: string=string.substring(1)+string.substring(0,1); break; } return string; } public static String hFlip(String string){ string=string.substring(1,2)+string.substring(0,1)+string.substring(3)+string.substring(2,3); return string; } public static String vFlip(String string){ string=string.substring(3)+string.substring(2,3)+string.substring(1,2)+string.substring(0,1); return string; } /** * 旋转和翻转过后与原图的比较 * @param list 得到的步骤集合,-1表示水平翻转,-2表示垂直翻转,0到360表示旋转度数. * @return 返回结果,数值表示需要顺时针旋转的度数,符号位表示是否需要水平翻转.<br /> * 如果得到的是一个正数,那么原图只需做相应度数的顺时针旋转<br /> * 如果得到的是一个负数,那么原图需要先做一次水平翻转,再做相应度数的顺时针旋转.<br /> */ public static int rotateAndFlip(List<Integer> list){ /**是否需要翻转**/ boolean needRotate=false; /**需要旋转的度数**/ int degree=0; for (Integer integer : list) { switch (integer) { case -1://水平翻转 if(needRotate=!needRotate) degree=360-degree; else degree=-degree; break; case -2://垂直翻转 degree=180-degree; needRotate=!needRotate; break; default: degree=degree+integer; break; } if (degree<0) degree=degree+360; if (degree>=360) degree=degree-360; } if (needRotate)degree=-degree; if(needRotate && degree==0)return -360; return degree; } }