use of java.awt.geom.Path2D in project imagingbook-common by imagingbook.
the class RoiUtils method makePolygon.
@Deprecated
public static Roi makePolygon(Point2D[] points, double strokeWidth, Color color) {
Path2D poly = new Path2D.Double();
if (points.length > 0) {
poly.moveTo(points[0].getX(), points[0].getY());
for (int i = 1; i < points.length; i++) {
poly.lineTo(points[i].getX(), points[i].getY());
}
poly.closePath();
}
Roi shapeRoi = new ShapeRoi(poly);
shapeRoi.setStrokeWidth(strokeWidth);
shapeRoi.setStrokeColor(color);
return shapeRoi;
}
use of java.awt.geom.Path2D in project sis by apache.
the class Java2D method tryMergePolylines.
/**
* Merges a sequence of points or paths if the first instance is an implementation of this library.
*
* @throws ClassCastException if an element in the iterator is not a {@link Shape} or a {@link Point2D}.
*/
@Override
final Shape tryMergePolylines(Object next, final Iterator<?> polylines) {
if (!(next instanceof Shape || next instanceof Point2D)) {
return null;
}
final Path2D path = new Path2D.Double();
boolean lineTo = false;
for (; ; next = polylines.next()) {
if (next != null) {
if (next instanceof Point2D) {
final double x = ((Point2D) next).getX();
final double y = ((Point2D) next).getY();
if (Double.isNaN(x) || Double.isNaN(y)) {
lineTo = false;
} else if (lineTo) {
path.lineTo(x, y);
} else {
path.moveTo(x, y);
lineTo = true;
}
} else {
path.append((Shape) next, false);
lineTo = false;
}
}
if (!polylines.hasNext()) {
// to skip this condition during the first iteration.
break;
}
}
return ShapeUtilities.toPrimitive(path);
}
use of java.awt.geom.Path2D in project sis by apache.
the class Java2D method createPolyline.
/**
* Creates a path from the given ordinate values.
* Each {@link Double#NaN} ordinate value start a new path.
* The implementation returned by this method must be an instance of {@link #rootClass}.
*/
@Override
public Shape createPolyline(final int dimension, final Vector... ordinates) {
if (dimension != 2) {
throw unsupported(dimension);
}
/*
* Computes the total length of all vectors and verifies if all values
* can be casted to float without precision lost.
*/
int length = 0;
boolean isFloat = true;
for (final Vector v : ordinates) {
if (v != null) {
length = Math.addExact(length, v.size());
if (isFloat) {
for (int i = v.size(); --i >= 0; ) {
final double value = v.doubleValue(i);
if (Double.doubleToRawLongBits(value) != Double.doubleToRawLongBits((float) value)) {
isFloat = false;
break;
}
}
}
}
}
/*
* Note: Point2D is not an instance of Shape, so we can not make a special case for it.
*/
length /= 2;
if (length == 2 && ordinates.length == 1) {
final Vector v = ordinates[0];
final double x1, y1, x2, y2;
if (!Double.isNaN(x1 = v.doubleValue(0)) && !Double.isNaN(y1 = v.doubleValue(1)) && !Double.isNaN(x2 = v.doubleValue(2)) && !Double.isNaN(y2 = v.doubleValue(3))) {
final Line2D path = isFloat ? new Line2D.Float() : new Line2D.Double();
path.setLine(x1, y1, x2, y2);
return path;
}
}
final Path2D path = isFloat ? new Path2D.Float(Path2D.WIND_NON_ZERO, length) : new Path2D.Double(Path2D.WIND_NON_ZERO, length);
boolean lineTo = false;
for (final Vector v : ordinates) {
final int size = v.size();
for (int i = 0; i < size; ) {
final double x = v.doubleValue(i++);
final double y = v.doubleValue(i++);
if (Double.isNaN(x) || Double.isNaN(y)) {
lineTo = false;
} else if (lineTo) {
path.lineTo(x, y);
} else {
path.moveTo(x, y);
lineTo = true;
}
}
}
return ShapeUtilities.toPrimitive(path);
}
use of java.awt.geom.Path2D in project sis by apache.
the class LatLonPointRadius method getRectangularRegionApproximation.
/**
* Calculates the rectangular region enclosing the circular search region.
*
* @param numberOfPoints
* the number of points used to estimate the circular search region
* @return Java Rectangle2D object that bounds the circlar search region
*/
public Rectangle2D getRectangularRegionApproximation(int numberOfPoints) {
if (radius >= DistanceUtils.HALF_EARTH_CIRCUMFERENCE) {
return new Rectangle2D.Double(0.0, 0.0, 360.0, 180.0);
}
int numberOfCrossOvers = 0;
Path2D path = new Path2D.Double();
DirectPosition2D initPT = DistanceUtils.getPointOnGreatCircle(center.getOrdinate(1), center.getOrdinate(0), radius, 0);
path.moveTo(initPT.x + 180.0, initPT.y + 90.0);
DirectPosition2D currPT = initPT;
for (int i = 1; i < 360; i++) {
DirectPosition2D pt = DistanceUtils.getPointOnGreatCircle(center.getOrdinate(1), center.getOrdinate(0), radius, i);
path.lineTo(pt.x + 180.0, pt.y + 90.0);
if (dateLineCrossOver(Longitude.normalize(currPT.x), Longitude.normalize(pt.x))) {
numberOfCrossOvers++;
}
currPT = pt;
}
if (dateLineCrossOver(Longitude.normalize(initPT.x), Longitude.normalize(currPT.x))) {
numberOfCrossOvers++;
}
/**
* If the path crosses the dateline once, it's a special case, so take care
* of it differently. It will need to include areas around the pole.
*/
if (numberOfCrossOvers == 1) {
Rectangle2D r = path.getBounds2D();
Rectangle2D lowerHalf = new Rectangle2D.Double(0.0, 0.0, 360.0, r.getMaxY());
if (lowerHalf.contains(center.getOrdinate(0) + 180.0, center.getOrdinate(1) + 90.0)) {
return lowerHalf;
} else {
return new Rectangle2D.Double(0.0, r.getMinY(), 360.0, 180.0 - r.getMinY());
}
}
if (path.contains(center.getOrdinate(0) + 180.0, center.getOrdinate(1) + 90.0)) {
Rectangle2D r = path.getBounds2D();
if ((r.getMaxX() - r.getMinX()) > 359.0) {
return new Rectangle2D.Double(0.0, 0.0, 360.0, 180.0);
} else if (r.getMinX() < 0 || r.getMaxX() > 360.0) {
/**
* For circles that crosses the dateline instead of splitting in half
* and having to go down the tree twice, for first version span
* longitude 360.0 and use the exact height of the box
*/
return new Rectangle2D.Double(0.0, r.getY(), 360.0, r.getHeight());
} else {
return path.getBounds2D();
}
} else {
Area pathArea = new Area(path);
Area wholeMap = new Area(new Rectangle2D.Double(0.0, 0.0, 360.0, 180.0));
wholeMap.subtract(pathArea);
return wholeMap.getBounds2D();
}
}
use of java.awt.geom.Path2D in project flutter-intellij by flutter.
the class StackLayout method paintComponent.
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
final Rectangle bounds = getBounds();
final int height = bounds.height;
if (height <= 20) {
return;
}
final Graphics2D g2 = (Graphics2D) g;
final float msPerPixel = (2.0f * 1000000.0f / 60.0f) / height;
final float y = FlutterFramesMonitor.microsPerFrame / msPerPixel;
final Stroke oldStroke = g2.getStroke();
try {
g2.setStroke(STROKE);
final Path2D path = new Path2D.Float();
path.moveTo(0, height - y);
path.lineTo(bounds.width, height - y);
g2.draw(path);
} finally {
g2.setStroke(oldStroke);
}
}
Aggregations