public void render(Graphics2D g2, int xOffset, int yOffset, AdminModel model) { g2.setStroke(new BasicStroke(3)); g2.setColor(model.getGroupColor(hullName)); if (hull.size() > 2) { Polygon temp = new Polygon(); for (Point p : hull) temp.addPoint(p.x - xOffset, p.y - yOffset); g2.draw(temp); } else { if (hull.size() == 2) { Line2D line = new Line2D.Double(hull.get(0), hull.get(1)); g2.draw(line); } for (OrganismNode o : nodes) g2.draw( new Rectangle2D.Double( o.getX() - (1.5 + xOffset), o.getY() - (1.5 + yOffset), o.getWidth() + 3, o.getHeight() + 3)); } g2.setStroke(new BasicStroke()); }
/** Constructor for child hull (has parent) */ public ConvexHull(List<OrganismNode> nodes, String hullName, ConvexHull parent) { this.level = parent == null ? 1 : parent.getLevel() + 1; this.nodes = nodes; // Construct points as well as any children hull that exist this.points = new LinkedList<Point>(); Map<String, List<OrganismNode>> childrenGroups = new HashMap<String, List<OrganismNode>>(); for (OrganismNode o : nodes) { this.points.add(o.getCenter()); if (o.getTypes().containsKey(level + 1)) { if (childrenGroups.containsKey(o.getTypes().get(level + 1))) childrenGroups.get(o.getTypes().get(level + 1)).add(o); else { List<OrganismNode> temp = new LinkedList<OrganismNode>(); temp.add(o); childrenGroups.put(o.getTypes().get(level + 1), temp); } } } this.hullName = hullName; this.parent = parent; hull = new ArrayList<Point>(); hullShape = new Polygon(); if (points.size() < 3) { hull.addAll(points); for (Point p : hull) hullShape.addPoint(p.x, p.y); } else GrahamScan(); children = new LinkedList<ConvexHull>(); if (childrenGroups.size() > 0) { for (Map.Entry<String, List<OrganismNode>> e : childrenGroups.entrySet()) children.add(new ConvexHull(e.getValue(), e.getKey(), this)); } childCollisions = TBSUtils.hullCollisions(level, children); }