owner = (Polygon) getOwner().getChildren().get(0);
} else {
throw new NullPointerException();
}
Point center = getReferencePoint();
if (reference.x == center.x && reference.y == center.y) {
return center;
}
// The line run
float run = (reference.x - center.x);
PointList pointList = owner.getPoints();
for (int i = 0; i < pointList.size() - 1; i++) {
Point start = pointList.getPoint(i);
Point end = pointList.getPoint(i + 1);
// Translate from relative to absolute coordinates
owner.translateToAbsolute(start);
owner.translateToAbsolute(end);
// Check intersection
if (Geometry.linesIntersect(center.x, center.y, reference.x,
reference.y, start.x, start.y, end.x, end.y)) {
float p = ((float) (start.y - end.y))
/ ((float) (start.x - end.x));
float d = start.y - p * start.x;
// Compute xAnchor
int xAnchor;
if (run == 0) {
// Line equation: x = center.x
xAnchor = center.x;
} else {
// Line equation: y = ax + b = px + d =>
// x = (d - b) / (a - p)
float rise = (reference.y - center.y);
float a = rise / run;
float b = center.y - a * center.x;
xAnchor = (int) ((d - b) / (a - p));
}
// yAnchor is just y = px + d
int yAnchor = (int) (p * xAnchor + d);
return new Point(xAnchor, yAnchor);
}
}
// Should never happen
return center;