*
* @param points
* @return normalized point list
*/
protected PointList getNormalizedPointList(PointList points) {
PointList ps = new PointList(points.size());
int[] rawPoints = points.toIntArray();
if (rawPoints.length == 0)
return ps;
ps.addPoint(rawPoints[0], rawPoints[1]);
if (rawPoints.length == 2)
return ps;
ps.addPoint(rawPoints[2], rawPoints[3]);
if (rawPoints.length == 4) {
return ps;
}
int p1 = 0, p2 = 2, p3 = 4;
while (true) {
if (!collinear(rawPoints, p1, p2, p3, getPixelTolerance())) {
ps.addPoint(rawPoints[p1], rawPoints[p1 + 1]);
p1 = p2;
} else {
// even if collinear we have to check whether p1 and p3 are on
// different sides
if (rawPoints[p1] < rawPoints[p2]
&& rawPoints[p3] < rawPoints[p2]
|| rawPoints[p1] > rawPoints[p2]
&& rawPoints[p3] > rawPoints[p2]
|| rawPoints[p1 + 1] < rawPoints[p2 + 1]
&& rawPoints[p3 + 1] < rawPoints[p2 + 1]
|| rawPoints[p1 + 1] > rawPoints[p2 + 1]
&& rawPoints[p3 + 1] > rawPoints[p2 + 1]) {
// if so, add the middle point and increment a first time
ps.addPoint(rawPoints[p2], rawPoints[p2 + 1]);
p1 = p3;
p2 = p3;
p3 += 2;
if (p3 >= rawPoints.length)
break;
}
}
p2 = p3;
p3 += 2;
if (p3 >= rawPoints.length)
break;
}
// If the last check was collinear (if and only if p2-p1>4) we still
// have to add p1
if (p2 - p1 > 4) {
ps.addPoint(rawPoints[p1], rawPoints[p1 + 1]);
}
ps.addPoint(rawPoints[p2], rawPoints[p2 + 1]);
return ps;
}