CaptainSpaceCat

eBeam - PolyOutline

Jul 13th, 2016
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.89 KB | None | 0 0
  1. import java.util.ArrayList;
  2.  
  3. /* This support class contains information
  4.  * on the outline of a certain region
  5.  * within the formatted image.
  6.  * */
  7.  
  8. public class PolyOutline {
  9.  
  10.   public ArrayList<int[]> nodeMap;
  11.   public boolean[][] nodeGrid;
  12.   public boolean[][] edgeGrid;
  13.  
  14.   public PolyOutline(int w, int h) {
  15.     nodeMap = new ArrayList<int[]>();
  16.     nodeGrid = new boolean[w+1][h+1];
  17.     edgeGrid = new boolean[w+1][h];
  18.   }
  19.  
  20.   public void add(int x, int y) {
  21.     nodeMap.add(new int[] {x, y});
  22.     nodeGrid[x][y] = true;
  23.   }
  24.  
  25.   public void add(int[] p) {
  26.     nodeMap.add(new int[] {p[0], p[1]});
  27.     nodeGrid[p[0]][p[1]] = true;
  28.   }
  29.  
  30.   public void remove(int i) {
  31.     int[] c = nodeMap.remove(i);
  32.     nodeGrid[c[0]][c[1]] = false;
  33.   }
  34.  
  35.   public int[] getPrevPos() {
  36.     return nodeMap.get(nodeMap.size()-2);
  37.   }
  38.  
  39.   public void addEdge(int x, int y) {
  40.     edgeGrid[x][y] = true;
  41.   }
  42.  
  43.   public void addEdge(int[] p) {
  44.     edgeGrid[p[0]][p[1]] = true;
  45.   }
  46.  
  47.   public int optimize(int finalNodeNum) {
  48.     int[] temp = nodeMap.get(0);
  49.     int index = 0;
  50.     double n = 0;
  51.     int s = nodeMap.size();
  52.     double c = finalNodeNum*1d/s;
  53.     if (finalNodeNum < s) {
  54.       //System.out.println(finalNodeNum + "/" + s + " = " + c);
  55.       for (int i = 0; i < s; i++) {
  56.         n += c;
  57.         if (n < 1) {
  58.           remove(index);
  59.         } else {
  60.           n--;
  61.           index++;
  62.         }
  63.       }
  64.       if (nodeMap.size() < finalNodeNum) {
  65.         nodeMap.add(temp);
  66.       }
  67.     }
  68.     System.out.println(nodeMap.size());
  69.     return nodeMap.size();
  70.   }
  71.  
  72.   public int optimize(int finalNodeNum, int[][] crossPoints) {
  73.     int[] temp = nodeMap.get(0);
  74.     int index = 0;
  75.     double n = 0;
  76.     int s = nodeMap.size();
  77.     double c = finalNodeNum*1d/s;
  78.     boolean active = true;
  79.     if (finalNodeNum < s) {
  80.       //System.out.println(finalNodeNum + "/" + s + " = " + c);
  81.       for (int i = 0; i < s; i++) {
  82.         n += c;
  83.         if (crossCheck(index, crossPoints)) {
  84.           active = !active;
  85.           index++;
  86.         } else if (n < 1 || !active) {
  87.           remove(index);
  88.         } else {
  89.           index++;
  90.         }
  91.         if (n >= 1) {
  92.           n--;
  93.         }
  94.       }
  95.       if (nodeMap.size() < finalNodeNum) {
  96.         nodeMap.add(temp);
  97.       }
  98.     }
  99.     System.out.println(nodeMap.size());
  100.     return nodeMap.size();
  101.   }
  102.  
  103.   boolean crossCheck(int index, int[][] crossPoints) {
  104.     int[] point = nodeMap.get(index);
  105.     for (int i = 0; i < 8; i++) {
  106.       if (point[0] == crossPoints[i][0] && point[1] == crossPoints[i][1]) {
  107.         return true;
  108.       }
  109.     }
  110.     return false;
  111.   }
  112.  
  113.   boolean crossCheck(int[] point, int[][] crossPoints) {
  114.     for (int i = 0; i < 8; i++) {
  115.       if (point[0] == crossPoints[i][0] && point[1] == crossPoints[i][1]) {
  116.         return true;
  117.       }
  118.     }
  119.     return false;
  120.   }
  121.  
  122. }
Add Comment
Please, Sign In to add comment