📄 shapeutils.java
字号:
Point2D.Float nextPt = new Point2D.Float(); Point2D.Float pathStart = new Point2D.Float(); //Point2D.Float floatPt = new Point2D.Float(); float[] seg = new float[12]; int segType; boolean improved = false; // flags to do checking that Sun should have done in GeneralPath. // As of 1.4.1 Sun only guarantees that the path starts with a // segment of type SEG_MOVETO. boolean closed = false; while(!iter.isDone()) { segType = iter.currentSegment(seg); iter.next(); switch (segType) { case PathIterator.SEG_MOVETO : closed = false; pathStart.setLocation(seg[0], seg[1]); currPt.setLocation(seg[0], seg[1]); nextPt.setLocation(seg[0], seg[1]); newPath.moveTo(nextPt.x,nextPt.y); break; case PathIterator.SEG_CLOSE : if (closed) { throw new IllegalPathStateException("Path closed twice!"); } nextPt.x = pathStart.x; nextPt.y = pathStart.y; closed = true; if (currPt.distance(nextPt) <= precision) { newPath.closePath(); } else { improved = true; seg[0] = nextPt.x; seg[1] = nextPt.y; createIntermediateLine(currPt,seg); // seg modified newPath.lineTo(seg[0],seg[1]); newPath.closePath(); } break; case PathIterator.SEG_LINETO : if (closed) { throw new IllegalPathStateException("Path already closed!"); } nextPt.setLocation(seg[0],seg[1]); if (currPt.distance(nextPt) <= precision) { newPath.lineTo(nextPt.x,nextPt.y); } else { improved = true; createIntermediateLine(currPt,seg); // seg modified newPath.lineTo(seg[0],seg[1]); newPath.lineTo(seg[2],seg[3]); } break; case PathIterator.SEG_QUADTO : if (closed) { throw new IllegalPathStateException("Path already closed!"); } conPt1.setLocation(seg[0],seg[1]); nextPt.setLocation(seg[2],seg[3]); if (currPt.distance(nextPt) <= precision) { newPath.quadTo(conPt1.x,conPt1.y,nextPt.x,nextPt.y); } else { improved = true; createIntermediateQuad(currPt,seg); // seg modified newPath.quadTo(seg[0],seg[1],seg[2],seg[3]); newPath.quadTo(seg[4],seg[5],seg[6],seg[7]); } break; case PathIterator.SEG_CUBICTO : if (closed) { throw new IllegalPathStateException("Path already closed!"); } conPt1.setLocation(seg[0], seg[1]); conPt2.setLocation(seg[2], seg[3]); nextPt.setLocation(seg[4], seg[5]); if (currPt.distance(nextPt) <= precision) { newPath.curveTo(conPt1.x,conPt1.y,conPt2.x,conPt2.y,nextPt.x,nextPt.y); } else { improved = true; createIntermediateCurve(currPt,seg); // seg modified newPath.curveTo(seg[0],seg[1],seg[2],seg[3],seg[4],seg[5]); newPath.curveTo(seg[6],seg[7],seg[8],seg[9],seg[10],seg[11]); // add an intermediate point } break; default: // should never happen unless Sun changes GeneralPath throw new Error("Unknown segment type from Path Iterator"); } currPt.setLocation(nextPt.x,nextPt.y); } if (!improved) { return aPath; } else { return newPath; } } /** * Mutate the suppled array of float to contain an additional knot. * * @param start the start point of the Line. * @param pts An array of at least size 4 containing the end point x,y * coordinates at position 0 and 1 respectively. This array * format matches that returned by <code>PathIterator</code> */ public static void createIntermediateLine(Point2D.Float start, float[] pts) { Point2D.Float newPt = midPoint(start.x,start.y,pts[0],pts[1]); pts[2] = pts[0]; pts[3] = pts[1]; pts[0] = newPt.x; pts[1] = newPt.y; } /** * Mutate the suppled array of float to contain an additional knot and * modify the associated control points. * * @param start the start point of the quadratic Bezier spline. * @param pts An array of at least size 8 containing the end point x,y * coordinates at position 2 and 3 respectively. The control * point should be specified at positions 0 and 1. This array * format matches the format returned by * <code>PathIterator</code>. */ public static void createIntermediateQuad(Point2D.Float start, float[] pts) { Point2D.Float nearCtl = midPoint(start.x,start.y,pts[0],pts[1]); Point2D.Float farCtl = midPoint(pts[0],pts[1],pts[2],pts[3]); Point2D.Float knot = midPoint(nearCtl.x,nearCtl.y,farCtl.x,farCtl.y); // move the next point to make space for the new data pts[6] = pts[2]; pts[7] = pts[3]; // record in the new points pts[0] = nearCtl.x; pts[1] = nearCtl.y; pts[2] = knot.x; pts[3] = knot.y; pts[4] = farCtl.x; pts[5] = farCtl.y; } /** * Mutate the supplied array of float to contain an additional knot and * modify the associated control points. * * @param start the start point of the quadratic Bezier spline. * @param pts An array of at least size 8 containing the end point x,y * coordinates at position 4 and 5 respectively. The control * point for the start point should be specified at positions * 0 and 1; The control point for the end point should be * specified at positions 2 and 3 This array format matches the * format returned by <code>PathIterator</code>. */ public static void createIntermediateCurve(Point2D.Float start, float[] pts) { Point2D.Float stCtl = midPoint(start.x,start.y,pts[0],pts[1]); Point2D.Float endCtl = midPoint(pts[2],pts[3],pts[4],pts[5]); Point2D.Float btwCtls = midPoint(pts[0],pts[1],pts[2],pts[3]); Point2D.Float nearCtl = midPoint(stCtl.x,stCtl.y,btwCtls.x,btwCtls.y); Point2D.Float farCtl = midPoint(btwCtls.x,btwCtls.y,endCtl.x,endCtl.y); Point2D.Float knot = midPoint(nearCtl.x,nearCtl.y,farCtl.x,farCtl.y); // move the next point to make space for the new data pts[10] = pts[4]; pts[11] = pts[5]; // record in the new points pts[0] = stCtl.x; // the modified start control pts[1] = stCtl.y; pts[2] = nearCtl.x; // the start side control for the new knot pts[3] = nearCtl.y; pts[4] = knot.x; // the new knot pts[5] = knot.y; pts[6] = farCtl.x; // the end side control for the new knot pts[7] = farCtl.y; pts[8] = endCtl.x; // the modified end control pts[9] = endCtl.y; } /** * Find the midpoint of a specifed line segment. * * @param x1 start x coordinate * @param y1 start y coordinate * @param x2 end x coordinate * @param y2 end y coordinate * @return The calculated midpoint */ public static Point2D.Float midPoint(float x1, float y1, float x2, float y2) { return new Point2D.Float((x1+x2)/2, (y1+y2)/2); }}/* * $Log: ShapeUtils.java,v $ * Revision 1.18 2003/09/23 15:23:40 gus * javadoc fixes * * Revision 1.17 2003/06/30 23:12:21 gus * remove a slew of nasty debugging code that was all * commented out anyway. At this point, I certainly * don't remember what most of it did anyway. * * Revision 1.16 2003/06/30 23:06:53 gus * Remove old broken code comment. * * Revision 1.15 2003/06/27 15:32:58 krivard * Bugfix: * Small boxes approaching large boxes from the left were falsely declared not overlapping. * Changed upperleft and lowerleft bounding box check in isOverlapping(Shape,Shape) to Rectangle.intersects(). * * Revision 1.14 2003/03/10 16:47:24 gus * Added an optimization to isOverlapping so that points are not itterated unless the * bounding box of the two shapes overlapps. This creates a VERY noticiable * improvement in the breakout problem set. * * Revision 1.13 2003/03/10 15:53:56 gus * Commented out debugging prints * meanContainedPoint now throws NotEnoughPointsException * Fixed nearestSegment * angleToVert now returns PI/2-arcTan(tanTheta) * * Revision 1.12 2003/03/07 19:20:37 gus * Fix npe that occurs when start point is the point around which we are trying to find * a line. * * Revision 1.11 2003/03/06 23:28:30 gus * Some reflection code that might even be useful * * Revision 1.10 2003/03/06 21:19:25 gus * A get nearest point method * * Revision 1.9 2003/03/06 20:52:44 gus * get rid of a class cast exception. CreateTransformedShape always returns * a GeneralPath, so cant' cast to Line2D.Float even if we know it is a line. * * Revision 1.8 2003/03/06 19:54:09 gus * make methods static cause I forgot to * * Revision 1.7 2003/03/06 19:07:07 gus * meant != rather than == * fixed now. * * Revision 1.6 2003/03/06 18:55:21 gus * We need to advance the Pathe iterator despite what the nutshell book claims * * Revision 1.5 2003/03/06 18:24:39 gus * some partially complete bouncing routines * * Revision 1.4 2003/03/04 23:20:42 gus * bunch more methods added * * Revision 1.3 2003/03/04 16:04:45 gus * Javadoc improvements * * Revision 1.2 2003/03/04 15:32:20 gus * adding log comment * */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -