Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- /* This support class contains information
- * on the outline of a certain region
- * within the formatted image.
- * */
- public class PolyOutline {
- public ArrayList<int[]> nodeMap;
- public boolean[][] nodeGrid;
- public boolean[][] edgeGrid;
- public PolyOutline(int w, int h) {
- nodeMap = new ArrayList<int[]>();
- nodeGrid = new boolean[w+1][h+1];
- edgeGrid = new boolean[w+1][h];
- }
- public void add(int x, int y) {
- nodeMap.add(new int[] {x, y});
- nodeGrid[x][y] = true;
- }
- public void add(int[] p) {
- nodeMap.add(new int[] {p[0], p[1]});
- nodeGrid[p[0]][p[1]] = true;
- }
- public void remove(int i) {
- int[] c = nodeMap.remove(i);
- nodeGrid[c[0]][c[1]] = false;
- }
- public int[] getPrevPos() {
- return nodeMap.get(nodeMap.size()-2);
- }
- public void addEdge(int x, int y) {
- edgeGrid[x][y] = true;
- }
- public void addEdge(int[] p) {
- edgeGrid[p[0]][p[1]] = true;
- }
- public int optimize(int finalNodeNum) {
- int[] temp = nodeMap.get(0);
- int index = 0;
- double n = 0;
- int s = nodeMap.size();
- double c = finalNodeNum*1d/s;
- if (finalNodeNum < s) {
- //System.out.println(finalNodeNum + "/" + s + " = " + c);
- for (int i = 0; i < s; i++) {
- n += c;
- if (n < 1) {
- remove(index);
- } else {
- n--;
- index++;
- }
- }
- if (nodeMap.size() < finalNodeNum) {
- nodeMap.add(temp);
- }
- }
- System.out.println(nodeMap.size());
- return nodeMap.size();
- }
- public int optimize(int finalNodeNum, int[][] crossPoints) {
- int[] temp = nodeMap.get(0);
- int index = 0;
- double n = 0;
- int s = nodeMap.size();
- double c = finalNodeNum*1d/s;
- boolean active = true;
- if (finalNodeNum < s) {
- //System.out.println(finalNodeNum + "/" + s + " = " + c);
- for (int i = 0; i < s; i++) {
- n += c;
- if (crossCheck(index, crossPoints)) {
- active = !active;
- index++;
- } else if (n < 1 || !active) {
- remove(index);
- } else {
- index++;
- }
- if (n >= 1) {
- n--;
- }
- }
- if (nodeMap.size() < finalNodeNum) {
- nodeMap.add(temp);
- }
- }
- System.out.println(nodeMap.size());
- return nodeMap.size();
- }
- boolean crossCheck(int index, int[][] crossPoints) {
- int[] point = nodeMap.get(index);
- for (int i = 0; i < 8; i++) {
- if (point[0] == crossPoints[i][0] && point[1] == crossPoints[i][1]) {
- return true;
- }
- }
- return false;
- }
- boolean crossCheck(int[] point, int[][] crossPoints) {
- for (int i = 0; i < 8; i++) {
- if (point[0] == crossPoints[i][0] && point[1] == crossPoints[i][1]) {
- return true;
- }
- }
- return false;
- }
- }
Add Comment
Please, Sign In to add comment