use of aima.core.util.math.geom.shapes.Point2D in project aima-java by aimacode.
the class ExtendedMapAgentView method paintMap.
/**
* Represents roads by lines and locations by name-labeled points.
*/
protected void paintMap(java.awt.Graphics2D g2) {
Map envMap = getMapEnv().getMap();
Map aMap = (agentMap != null) ? agentMap : envMap;
List<Roadblock> roadblocks = new ArrayList<Roadblock>();
for (String l1 : envMap.getLocations()) {
Point2D pt1 = envMap.getPosition(l1);
List<String> linkedLocs = envMap.getPossibleNextLocations(l1);
for (String l2 : aMap.getPossibleNextLocations(l1)) if (!linkedLocs.contains(l2))
linkedLocs.add(l2);
for (String l2 : linkedLocs) {
Point2D pt2 = envMap.getPosition(l2);
g2.setColor(Color.lightGray);
g2.drawLine(x(pt1), y(pt1), x(pt2), y(pt2));
boolean blockedInEnv = !envMap.getPossibleNextLocations(l2).contains(l1);
boolean blockedInAgent = !aMap.getPossibleNextLocations(l2).contains(l1);
roadblocks.add(new Roadblock(pt1, pt2, blockedInEnv, blockedInAgent));
if (blockedInEnv && blockedInAgent) {
boolean blockedInEnvOtherDir = !envMap.getPossibleNextLocations(l1).contains(l2);
boolean blockedInAgentOtherDir = !aMap.getPossibleNextLocations(l1).contains(l2);
roadblocks.add(new Roadblock(pt2, pt1, blockedInEnvOtherDir, blockedInAgentOtherDir));
}
}
}
for (Roadblock block : roadblocks) paintRoadblock(g2, block);
}
use of aima.core.util.math.geom.shapes.Point2D in project aima-java by aimacode.
the class MapAgentView method adjustTransformation.
/**
* Adjusts offsets and scale so that the whole map fits on the view without
* scrolling.
*/
private void adjustTransformation() {
Map map = getMapEnv().getMap();
List<String> locs = map.getLocations();
// adjust coordinates relative to the left upper corner of the graph
// area
double minX = Double.POSITIVE_INFINITY;
double minY = Double.POSITIVE_INFINITY;
double maxX = Double.NEGATIVE_INFINITY;
double maxY = Double.NEGATIVE_INFINITY;
for (String loc : locs) {
Point2D xy = map.getPosition(loc);
if (xy.getX() < minX)
minX = xy.getX();
if (xy.getY() < minY)
minY = xy.getY();
if (xy.getX() > maxX)
maxX = xy.getX();
if (xy.getY() > maxY)
maxY = xy.getY();
}
this.setBorder(20, 20, 20, 100);
adjustTransformation(minX, minY, maxX, maxY);
}
use of aima.core.util.math.geom.shapes.Point2D in project aima-java by aimacode.
the class MapAgentView method paintLoc.
protected void paintLoc(java.awt.Graphics2D g2, String loc) {
Map map = getMapEnv().getMap();
Point2D pt = map.getPosition(loc);
if (pt != null) {
int x = x(pt);
int y = y(pt);
String info = "";
List<String> track = new ArrayList<String>();
if (!env.getAgents().isEmpty())
// show details only for track of first agent...
track = getTrack(env.getAgents().get(0));
ArrayList<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < track.size(); i++) if (track.get(i).equals(loc))
list.add(i + 1);
if (!list.isEmpty())
info = list.toString();
if (getAgentLocs().contains(loc)) {
g2.setColor(Color.red);
g2.fillOval(x - 4, y - 4, 8, 8);
}
if (track.contains(loc))
g2.setColor(Color.black);
else
g2.setColor(Color.gray);
g2.drawString(loc + info, x, y);
}
}
use of aima.core.util.math.geom.shapes.Point2D in project aima-java by aimacode.
the class CspViewCtrl method getPosition.
/**
* Provides a 2D-position for each variable. If no mapping is given, a simple grid position is computed.
*/
protected Point2D getPosition(VAR var) {
int[] pos = positionMapping.get(var);
if (pos != null)
return new Point2D(pos[0], pos[1]);
else {
int vIndex = csp.indexOf(var);
int rows = Math.max((int) (pane.getHeight() / 100), 1);
int x = (vIndex / rows) * 160 + 40;
int y = (vIndex % rows) * 100 + 40;
return new Point2D(x, y);
}
}
use of aima.core.util.math.geom.shapes.Point2D in project aima-java by aimacode.
the class MapEnvironmentViewCtrl method adjustTransform.
/**
* Computes transforms (translations and scaling) and applies them to the
* environment state view. Those transforms map location positions in the
* map to screen positions in the viewer pane.
* @return The scale value.
*/
private double adjustTransform() {
double xMin = Double.POSITIVE_INFINITY;
double xMax = Double.NEGATIVE_INFINITY;
double yMin = Double.POSITIVE_INFINITY;
double yMax = Double.NEGATIVE_INFINITY;
for (String loc : env.getMap().getLocations()) {
Point2D point = env.getMap().getPosition(loc);
xMin = Math.min(xMin, point.getX());
xMax = Math.max(xMax, point.getX());
yMin = Math.min(yMin, point.getY());
yMax = Math.max(yMax, point.getY());
}
double scale = Math.min((envStateView.getWidth() - 150) / (xMax - xMin), (envStateView.getHeight() - 60) / (yMax - yMin));
envStateView.setTranslateX((scale * (envStateView.getWidth() - xMin - xMax) / 2.0 - 30));
envStateView.setTranslateY((scale * (envStateView.getHeight() - yMin - yMax) / 2.0 - 10));
envStateView.setScaleX(scale);
envStateView.setScaleY(scale);
return scale;
}
Aggregations