http://java-sl.com/tip_flatteningpathiterator_moving_shape.html
Trying to study this code in the link above but I am having trouble understanding the following code:
float[] coords=new float[6];
while (!iter.isDone()) {
iter.currentSegment(coords);
int x=(int)coords[0];
int y=(int)coords[1];
points.add(new Point(x,y));
iter.next();
}
What is the purpose of the float array, and why is there 6, and how does this increments in the while loop to move an object along a shape's path?
A Path is defined by multiple segments, which can be:
SEG_CLOSE (0 coordinates)SEG_MOVETO (2 coordinates: x, y)SEG_LINETO (2 coordinates: x, y)SEG_QUADTO (4 coordinates: x1, y1, x2, y2)SEG_CUBICTO (6 coordinates: x1, y1, x2, y2, x3, y3)(SEG_LINETO, SEG_QUADTO and SEG_CUBICTO segments also have an implicit starting point: the end of the previous segment)
For example, a triangle might be made from the the following:
GeneralPath path = new GeneralPath();
path.moveTo(10, 10);
path.lineTo(15, 20);
path.lineTo(20, 8);
path.close();
The while(!iter.done()) { } loop processes each segment of the path in turn, starting with the SEG_MOVETO, followed by a SEG_LINETO, a second SEG_LINETO and lastly the SEG_CLOSE.
The coords[] array is used as temporary storage, for the output of the currentSegment() call. Instead of allocating storage for each segment as it is returned, the temporary storage array is overwritten. Since a "cubic" segments need 3 points (6 coordinates), the array size must be at least 6.
The code you posted includes an implicit assumption - that only SEG_MOVETO / SEG_LINETO type segments are present, so only coord[0] and coord[1] will ever be valid. This seems like a safe assumption, since the FlatteningPathIterator (shown in the linked text, not the question text) should never return a curved segment. But, it also includes a more dangerous assumption that a SEG_CLOSE segment will never appear, which has 0 coordinates and could result in garbage corrupting the resulting points list.
Better code might read more like:
float[] coords = new float[6];
while(!iter.done()) {
int type = iter.currentSegment(coords);
if (type == SEG_MOVETO || type == SEG_LINETO) {
int x = (int) coords[0];
int y = (int) coords[1];
points.add(new Point(x,y));
} else if (type == SEG_CLOSE) {
// Handling sub-path closing here
}
iter.next();
}
(for context) iter is an instance of FlatteningPathIterator (that's important to explain all the iter calls!)
float[] coords=new float[6]; Initializes a new float array, with 6 indexes, with default value (0.0f)
while (!iter.isDone()) {
...
iter.next();
}
Here iter is a PathIterator instance. So iter.next() moves the iterator pointer to the next item in the iterator, and iter.isDone() checks if there is anything left in the iterator.
And lastly, iter.currentSegment(coords); loads the contents of the current iterator segment to the coords array.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With