{
double totalLength = edgeState.getLength();
double[] segments = edgeState.getSegments();
// Works which line segment the point of the label is closest to
mxPoint p0 = edgeState.getAbsolutePoint(0);
mxPoint pe = edgeState.getAbsolutePoint(1);
Line2D line = new Line2D.Double(p0.getPoint(), pe.getPoint());
double minDist = line.ptSegDistSq(x, y);
int index = 0;
double tmp = 0;
double length = 0;
for (int i = 2; i < pointCount; i++)
{
tmp += segments[i - 2];
pe = edgeState.getAbsolutePoint(i);
line = new Line2D.Double(p0.getPoint(), pe.getPoint());
double dist = line.ptSegDistSq(x, y);
if (dist < minDist)
{
minDist = dist;
index = i - 1;
length = tmp;
}
p0 = pe;
}
double seg = segments[index];
p0 = edgeState.getAbsolutePoint(index);
pe = edgeState.getAbsolutePoint(index + 1);
double x2 = p0.getX();
double y2 = p0.getY();
double x1 = pe.getX();
double y1 = pe.getY();
double px = x;
double py = y;
double xSegment = x2 - x1;
double ySegment = y2 - y1;
px -= x1;
py -= y1;
double projlenSq = 0;
px = xSegment - px;
py = ySegment - py;
double dotprod = px * xSegment + py * ySegment;
if (dotprod <= 0.0)
{
projlenSq = 0;
}
else
{
projlenSq = dotprod * dotprod
/ (xSegment * xSegment + ySegment * ySegment);
}
double projlen = Math.sqrt(projlenSq);
if (projlen > seg)
{
projlen = seg;
}
double yDistance = Line2D.ptLineDist(p0.getX(), p0.getY(),
pe.getX(), pe.getY(), x, y);
int direction = Line2D.relativeCCW(p0.getX(), p0.getY(),
pe.getX(), pe.getY(), x, y);
if (direction == -1)
{
yDistance = -yDistance;
}
// Constructs the relative point for the label
return new mxPoint(
Math.round(((totalLength / 2 - length - projlen) / totalLength)
* -2), Math.round(yDistance / scale));
}
}
return new mxPoint();
}