use of java.awt.geom.PathIterator in project openblocks by mikaelhg.
the class BlockShapeUtil method appendPath.
/**
* Appends path gp2 to gp1.
* Taken from pre-redesign code.
* @param reversed is true if the segments are added in reverse order
*/
public static void appendPath(GeneralPath gp1, GeneralPath gp2, boolean reversed) {
// Each element is an array consisting of one Integer and six Floats
ArrayList<Number[]> points = new ArrayList<Number[]>();
PathIterator i = gp2.getPathIterator(new AffineTransform());
float[] segment = new float[] { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f };
float leftmost = Float.POSITIVE_INFINITY;
while (!i.isDone()) {
int type = i.currentSegment(segment);
i.next();
points.add(new Number[] { new Integer(type), new Float(segment[0]), new Float(segment[1]), new Float(segment[2]), new Float(segment[3]), new Float(segment[4]), new Float(segment[5]) });
}
if (reversed) {
float deltaX = (float) gp1.getCurrentPoint().getX();
float deltaY = (float) gp1.getCurrentPoint().getY();
Object[] typeAndPoints = points.get(points.size() - 1);
int type = ((Integer) typeAndPoints[0]).intValue();
if (type == PathIterator.SEG_LINETO) {
deltaX -= ((Float) typeAndPoints[1]).floatValue();
deltaY -= ((Float) typeAndPoints[2]).floatValue();
} else if (type == PathIterator.SEG_QUADTO) {
deltaX -= ((Float) typeAndPoints[3]).floatValue();
deltaY -= ((Float) typeAndPoints[4]).floatValue();
} else if (type == PathIterator.SEG_CUBICTO) {
deltaX -= ((Float) typeAndPoints[5]).floatValue();
deltaY -= ((Float) typeAndPoints[6]).floatValue();
} else {
assert false : type;
}
for (int j = points.size() - 1; j >= 1; j--) {
typeAndPoints = points.get(j);
type = ((Integer) typeAndPoints[0]).intValue();
float x1 = ((Float) typeAndPoints[1]).floatValue();
float y1 = ((Float) typeAndPoints[2]).floatValue();
float x2 = ((Float) typeAndPoints[3]).floatValue();
float y2 = ((Float) typeAndPoints[4]).floatValue();
float prevX = 0.0f, prevY = 0.0f;
int prevType = ((Integer) points.get(j - 1)[0]).intValue();
if ((prevType == PathIterator.SEG_MOVETO) || (prevType == PathIterator.SEG_LINETO)) {
prevX = ((Float) points.get(j - 1)[1]).floatValue();
prevY = ((Float) points.get(j - 1)[2]).floatValue();
} else if (prevType == PathIterator.SEG_QUADTO) {
prevX = ((Float) points.get(j - 1)[3]).floatValue();
prevY = ((Float) points.get(j - 1)[4]).floatValue();
} else if (prevType == PathIterator.SEG_CUBICTO) {
prevX = ((Float) points.get(j - 1)[5]).floatValue();
prevY = ((Float) points.get(j - 1)[6]).floatValue();
} else {
assert false : prevType;
}
leftmost = Math.min(leftmost, prevX + deltaX);
if ((type == PathIterator.SEG_MOVETO) || (type == PathIterator.SEG_LINETO)) {
gp1.lineTo(prevX + deltaX, prevY + deltaY);
} else if (type == PathIterator.SEG_QUADTO) {
gp1.quadTo(x1 + deltaX, y1 + deltaY, prevX + deltaX, prevY + deltaY);
} else if (type == PathIterator.SEG_CUBICTO) {
gp1.curveTo(x2 + deltaX, y2 + deltaY, x1 + deltaX, y1 + deltaY, prevX + deltaX, prevY + deltaY);
} else {
assert false : type;
}
}
} else {
// Not reversed
float deltaX = (float) gp1.getCurrentPoint().getX() - ((Float) points.get(0)[1]).floatValue();
float deltaY = (float) gp1.getCurrentPoint().getY() - ((Float) points.get(0)[2]).floatValue();
for (int j = 1; j < points.size(); j++) {
Object[] typeAndPoints = points.get(j);
int type = ((Integer) typeAndPoints[0]).intValue();
float x1 = ((Float) typeAndPoints[1]).floatValue();
float y1 = ((Float) typeAndPoints[2]).floatValue();
float x2 = ((Float) typeAndPoints[3]).floatValue();
float y2 = ((Float) typeAndPoints[4]).floatValue();
float x3 = ((Float) typeAndPoints[5]).floatValue();
float y3 = ((Float) typeAndPoints[6]).floatValue();
if (type == PathIterator.SEG_MOVETO) {
} else if (type == PathIterator.SEG_LINETO) {
gp1.lineTo(x1 + deltaX, y1 + deltaY);
leftmost = Math.min(leftmost, x1 + deltaX);
} else if (type == PathIterator.SEG_QUADTO) {
gp1.quadTo(x1 + deltaX, y1 + deltaY, x2 + deltaX, y2 + deltaY);
leftmost = Math.min(leftmost, x2 + deltaX);
} else if (type == PathIterator.SEG_CUBICTO) {
gp1.curveTo(x1 + deltaX, y1 + deltaY, x2 + deltaX, y2 + deltaY, x3 + deltaX, y3 + deltaY);
leftmost = Math.min(leftmost, x3 + deltaX);
} else {
assert false : type;
}
}
}
}
use of java.awt.geom.PathIterator in project limelight by slagyr.
the class BorderTest method testGettingShapeInsideBorderWithNoCorners.
public void testGettingShapeInsideBorderWithNoCorners() throws Exception {
style.setBorderWidth("1");
style.setRoundedCornerRadius("0");
border = new Border(style, insideMargin);
double[] coords = new double[6];
Shape inside = border.getShapeInsideBorder();
AffineTransform transform = new AffineTransform();
PathIterator iterator = inside.getPathIterator(transform);
assertEquals(PathIterator.SEG_MOVETO, iterator.currentSegment(coords));
assertEquals(1, coords[0], 0.1);
assertEquals(1, coords[1], 0.1);
iterator.next();
checkLineSegment(coords, iterator, 99, 1);
checkLineSegment(coords, iterator, 99, 199);
checkLineSegment(coords, iterator, 99, 199);
checkLineSegment(coords, iterator, 1, 199);
checkLineSegment(coords, iterator, 1, 199);
checkLineSegment(coords, iterator, 1, 1);
checkLineSegment(coords, iterator, 1, 1);
iterator.next();
assertEquals(true, iterator.isDone());
}
use of java.awt.geom.PathIterator in project android by JetBrains.
the class LineChartReducerTest method convertToArray.
private static float[][] convertToArray(Path2D path) {
PathIterator pathIterator = path.getPathIterator(null);
float[] coords = new float[6];
ArrayList<Point2D.Float> points = new ArrayList<>();
while (!pathIterator.isDone()) {
int type = pathIterator.currentSegment(coords);
assert type == PathIterator.SEG_MOVETO || type == PathIterator.SEG_LINETO;
points.add(new Point2D.Float(coords[0], coords[1]));
pathIterator.next();
}
float[][] result = new float[points.size()][2];
for (int i = 0; i < points.size(); ++i) {
result[i][0] = points.get(i).x;
result[i][1] = points.get(i).y;
}
return result;
}
use of java.awt.geom.PathIterator in project android by JetBrains.
the class LineChartReducer method reduce.
@Override
public Path2D reduce(@NotNull Path2D path, @NotNull LineConfig config) {
if (path.getCurrentPoint() == null) {
return path;
}
Path2D resultPath = new Path2D.Float();
float[] coords = new float[PATH_ITERATOR_COORDS_COUNT];
float pixel = -1;
float minX = -1, minY = -1;
float maxX = -1, maxY = -1;
float curX = -1, curY = -1;
int minIndex = -1, maxIndex = -1;
int curIndex = 0;
PathIterator iterator = path.getPathIterator(null);
while (!iterator.isDone()) {
int segType = iterator.currentSegment(coords);
assert segType == PathIterator.SEG_MOVETO || segType == PathIterator.SEG_LINETO;
float lastX = curX;
float lastY = curY;
curX = coords[0];
curY = coords[1];
if (curIndex > 0 && curX < lastX) {
// The second last point must be with maximum Y
assert equals(maxX, lastX) && equals(maxY, lastY);
break;
}
if (curIndex == 0 || curX >= pixel) {
if (curIndex > 0) {
// Add min and max points from the previous pixel
addMinMaxPoints(resultPath, config, minIndex, minX, minY, maxIndex, maxX, maxY);
// Add the last point from the previous pixel
addToResultPath(resultPath, config, lastX, lastY);
}
pixel = (float) Math.floor(curX) + 1;
minX = maxX = curX;
minY = maxY = curY;
minIndex = maxIndex = curIndex;
// Add the first point from the current pixel
addToResultPath(resultPath, config, curX, curY);
} else {
if (minY > curY) {
minIndex = curIndex;
minX = curX;
minY = curY;
}
if (maxY <= curY) {
maxIndex = curIndex;
maxX = curX;
maxY = curY;
}
}
iterator.next();
curIndex++;
}
addMinMaxPoints(resultPath, config, minIndex, minX, minY, maxIndex, maxX, maxY);
addToResultPath(resultPath, config, curX, curY);
if (config.isStepped()) {
// The last point won't be added if Y value is the same with previous point, so let's add it
if (resultPath.getCurrentPoint() == null || equals((float) resultPath.getCurrentPoint().getY(), curY)) {
addToPath(resultPath, curX, curY);
}
}
return resultPath;
}
use of java.awt.geom.PathIterator in project android_frameworks_base by DirtyUnicorns.
the class Path_Delegate method offset.
/**
* Offset the path by (dx,dy), returning true on success
*
* @param dx The amount in the X direction to offset the entire path
* @param dy The amount in the Y direction to offset the entire path
*/
public void offset(float dx, float dy) {
GeneralPath newPath = new GeneralPath();
PathIterator iterator = mPath.getPathIterator(new AffineTransform(0, 0, dx, 0, 0, dy));
newPath.append(iterator, false);
mPath = newPath;
}
Aggregations