Geometry-Based Edge Clustering
 All Classes Functions
GridRegion.java
1 import java.awt.geom.Area;
2 import java.util.ArrayList;
3 
10 public class GridRegion {
11 
12  private double primaryAngle;
13  private int minX;
14  private int minY;
15  private int maxX;
16  private int maxY;
17  private Area area;
18  private ArrayList<GridCell> cells;
19 
24  public GridRegion(double primaryAngle){
25  cells = new ArrayList<GridCell>();
26  this.primaryAngle = primaryAngle;
27  minX = Integer.MAX_VALUE;
28  minY = Integer.MAX_VALUE;
29  maxX = Integer.MIN_VALUE;
30  maxY = Integer.MIN_VALUE;
31  }
32 
37  public ArrayList<GridCell> getCells(){
38  return cells;
39  }
40 
45  public void addCell(GridCell cell){
46  area = null;
47  cells.add(cell);
48  if(cell.getX() < minX)
49  minX = cell.getX();
50  if(cell.getY() < minY)
51  minY = cell.getY();
52  if(cell.getX()+cell.getWidth() > maxX)
53  maxX = cell.getX()+cell.getWidth();
54  if(cell.getY()+cell.getHeight() > maxY)
55  maxY = cell.getY()+cell.getHeight();
56  }
57 
62  public double getPrimaryAngle(){
63  return primaryAngle;
64  }
65 
70  public int getMinX(){
71  return minX;
72  }
73 
78  public int getMinY(){
79  return minY;
80  }
81 
86  public int getMaxX(){
87  return maxX;
88  }
89 
94  public int getMaxY(){
95  return maxY;
96  }
97 
103  public boolean contains(Node node){
104  if(area == null){
105  area = new Area();
106  for(GridCell c : cells){
107  area.add(new Area(c.bounds()));
108  }
109  }
110  return area.contains(node.getX(), node.getY());
111  }
112 
117  public Node center(){
118  return new Node((minX+maxX)/2, (minY+maxY)/2);
119  }
120 
125  public void updatePrimaryAngle(){
126  double diffSum = 0;
127  for(GridCell c : cells){
128  double diff = c.getPrimaryAngle() - primaryAngle;
129  if(diff > 90)
130  diffSum = diffSum + 180-diff;
131  else if(diff < -90)
132  diffSum = diffSum - 180-diff;
133  else
134  diffSum = diffSum + diff;
135  }
136  primaryAngle = primaryAngle + (diffSum/cells.size());
137  }
138 }