Java Code Examples for java.awt.geom.PathIterator#SEG_CUBICTO

The following examples show how to use java.awt.geom.PathIterator#SEG_CUBICTO . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PathIteratorSegmentObjectDescription.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Creates a string representation of the given PathIterator segment type.
 *
 * @param segment
 *          the segment type
 * @return the segment type as string
 * @throws IllegalArgumentException
 *           if the segment type is none of the predefined PathIterator types.
 */
private String createSegmentType( final int segment ) throws IllegalArgumentException {
  switch ( segment ) {
    case PathIterator.SEG_CLOSE:
      return PathIteratorSegmentObjectDescription.SEG_CLOSE;
    case PathIterator.SEG_CUBICTO:
      return PathIteratorSegmentObjectDescription.SEG_CUBIC_TO;
    case PathIterator.SEG_LINETO:
      return PathIteratorSegmentObjectDescription.SEG_LINE_TO;
    case PathIterator.SEG_MOVETO:
      return PathIteratorSegmentObjectDescription.SEG_MOVE_TO;
    case PathIterator.SEG_QUADTO:
      return PathIteratorSegmentObjectDescription.SEG_QUAD_TO;
    default:
      throw new IllegalArgumentException( "The segment type is invalid." );
  }
}
 
Example 2
Source File: Order3.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
public int getSegment(double coords[]) {
    if (direction == INCREASING) {
        coords[0] = cx0;
        coords[1] = cy0;
        coords[2] = cx1;
        coords[3] = cy1;
        coords[4] = x1;
        coords[5] = y1;
    } else {
        coords[0] = cx1;
        coords[1] = cy1;
        coords[2] = cx0;
        coords[3] = cy0;
        coords[4] = x0;
        coords[5] = y0;
    }
    return PathIterator.SEG_CUBICTO;
}
 
Example 3
Source File: ShapeUtils.java    From pumpernickel with MIT License 5 votes vote down vote up
/** Return true if two shapes are equal. */
public static boolean equals(Shape shape, Shape shape2) {
	PathIterator iter1 = shape.getPathIterator(null);
	PathIterator iter2 = shape.getPathIterator(null);
	double[] coords1 = new double[6];
	double[] coords2 = new double[6];
	while ((!iter1.isDone()) && (!iter2.isDone())) {
		int k1 = iter1.currentSegment(coords1);
		int k2 = iter2.currentSegment(coords2);
		if (k1 != k2)
			return false;
		if (k1 == PathIterator.SEG_MOVETO
				|| k1 == PathIterator.SEG_LINETO) {
			if (!equals(coords1, coords2, 2))
				return false;
		} else if (k1 == PathIterator.SEG_QUADTO) {
			if (!equals(coords1, coords2, 4))
				return false;
		} else if (k1 == PathIterator.SEG_CUBICTO) {
			if (!equals(coords1, coords2, 6))
				return false;
		} else if (k1 == PathIterator.SEG_CLOSE) {
			// do nothing
		} else {
			throw new RuntimeException("unrecognized segment " + k1);
		}

		iter1.next();
		iter2.next();
	}
	return iter1.isDone() && iter2.isDone();
}
 
Example 4
Source File: Path2DCopyConstructor.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
static int getLength(int type) {
    switch(type) {
        case PathIterator.SEG_CUBICTO:
            return 6;
        case PathIterator.SEG_QUADTO:
            return 4;
        case PathIterator.SEG_LINETO:
        case PathIterator.SEG_MOVETO:
            return 2;
        case PathIterator.SEG_CLOSE:
            return 0;
        default:
            throw new IllegalStateException("Invalid type: " + type);
    }
}
 
Example 5
Source File: Path2DCopyConstructor.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
static int getLength(int type) {
    switch(type) {
        case PathIterator.SEG_CUBICTO:
            return 6;
        case PathIterator.SEG_QUADTO:
            return 4;
        case PathIterator.SEG_LINETO:
        case PathIterator.SEG_MOVETO:
            return 2;
        case PathIterator.SEG_CLOSE:
            return 0;
        default:
            throw new IllegalStateException("Invalid type: " + type);
    }
}
 
Example 6
Source File: PathSegment.java    From pumpernickel with MIT License 5 votes vote down vote up
public Float cubicTo(float cx0, float cy0, float cx1, float cy1,
		float x1, float y1) {
	Float newSegment = newSegment();
	newSegment.type = PathIterator.SEG_CUBICTO;
	newSegment.data = new float[] { cx0, cy0, cx1, cy1, x1, y1 };
	append(newSegment);
	return newSegment;
}
 
Example 7
Source File: RenderingEngine.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Utility method to feed a {@link PathConsumer2D} object from a
 * given {@link PathIterator}.
 * This method deals with the details of running the iterator and
 * feeding the consumer a segment at a time.
 */
public static void feedConsumer(PathIterator pi, PathConsumer2D consumer) {
    float coords[] = new float[6];
    while (!pi.isDone()) {
        switch (pi.currentSegment(coords)) {
        case PathIterator.SEG_MOVETO:
            consumer.moveTo(coords[0], coords[1]);
            break;
        case PathIterator.SEG_LINETO:
            consumer.lineTo(coords[0], coords[1]);
            break;
        case PathIterator.SEG_QUADTO:
            consumer.quadTo(coords[0], coords[1],
                            coords[2], coords[3]);
            break;
        case PathIterator.SEG_CUBICTO:
            consumer.curveTo(coords[0], coords[1],
                             coords[2], coords[3],
                             coords[4], coords[5]);
            break;
        case PathIterator.SEG_CLOSE:
            consumer.closePath();
            break;
        }
        pi.next();
    }
}
 
Example 8
Source File: RenderingEngine.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Utility method to feed a {@link PathConsumer2D} object from a
 * given {@link PathIterator}.
 * This method deals with the details of running the iterator and
 * feeding the consumer a segment at a time.
 */
public static void feedConsumer(PathIterator pi, PathConsumer2D consumer) {
    float coords[] = new float[6];
    while (!pi.isDone()) {
        switch (pi.currentSegment(coords)) {
        case PathIterator.SEG_MOVETO:
            consumer.moveTo(coords[0], coords[1]);
            break;
        case PathIterator.SEG_LINETO:
            consumer.lineTo(coords[0], coords[1]);
            break;
        case PathIterator.SEG_QUADTO:
            consumer.quadTo(coords[0], coords[1],
                            coords[2], coords[3]);
            break;
        case PathIterator.SEG_CUBICTO:
            consumer.curveTo(coords[0], coords[1],
                             coords[2], coords[3],
                             coords[4], coords[5]);
            break;
        case PathIterator.SEG_CLOSE:
            consumer.closePath();
            break;
        }
        pi.next();
    }
}
 
Example 9
Source File: PiscesRenderingEngine.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
public int currentSegment(float[] coords) {
    int type = src.currentSegment(coords);

    int lastCoord;
    switch(type) {
    case PathIterator.SEG_CUBICTO:
        lastCoord = 4;
        break;
    case PathIterator.SEG_QUADTO:
        lastCoord = 2;
        break;
    case PathIterator.SEG_LINETO:
    case PathIterator.SEG_MOVETO:
        lastCoord = 0;
        break;
    case PathIterator.SEG_CLOSE:
        // we don't want to deal with this case later. We just exit now
        curx_adjust = movx_adjust;
        cury_adjust = movy_adjust;
        return type;
    default:
        throw new InternalError("Unrecognized curve type");
    }

    // normalize endpoint
    float x_adjust = (float)Math.floor(coords[lastCoord] + lval) +
                 rval - coords[lastCoord];
    float y_adjust = (float)Math.floor(coords[lastCoord+1] + lval) +
                 rval - coords[lastCoord + 1];

    coords[lastCoord    ] += x_adjust;
    coords[lastCoord + 1] += y_adjust;

    // now that the end points are done, normalize the control points
    switch(type) {
    case PathIterator.SEG_CUBICTO:
        coords[0] += curx_adjust;
        coords[1] += cury_adjust;
        coords[2] += x_adjust;
        coords[3] += y_adjust;
        break;
    case PathIterator.SEG_QUADTO:
        coords[0] += (curx_adjust + x_adjust) / 2;
        coords[1] += (cury_adjust + y_adjust) / 2;
        break;
    case PathIterator.SEG_LINETO:
        break;
    case PathIterator.SEG_MOVETO:
        movx_adjust = x_adjust;
        movy_adjust = y_adjust;
        break;
    case PathIterator.SEG_CLOSE:
        throw new InternalError("This should be handled earlier.");
    }
    curx_adjust = x_adjust;
    cury_adjust = y_adjust;
    return type;
}
 
Example 10
Source File: CurvedPolylineCreationUI.java    From pumpernickel with MIT License 4 votes vote down vote up
@Override
protected void paintControls(Graphics2D g0, ShapeCreationPanel scp) {
	AffineTransform tx = scp.getTransform();
	Graphics2D g = (Graphics2D) g0.create();
	g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
			RenderingHints.VALUE_ANTIALIAS_ON);
	g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL,
			RenderingHints.VALUE_STROKE_PURE);
	g.setStroke(new BasicStroke(1, BasicStroke.CAP_ROUND,
			BasicStroke.JOIN_ROUND));
	float r = (float) (scp.getHandleSize()) / 2f;
	Selection selection = scp.getSelectionModel().getSelection();
	Selection indication = scp.getSelectionModel().getIndication();
	Shape[] shapes = scp.getDataModel().getShapes();
	float[] coords = new float[6];
	Ellipse2D ellipse = new Ellipse2D.Float();
	for (int shapeIndex = 0; shapeIndex < shapes.length; shapeIndex++) {
		if (scp.getHandlesActive().supports(scp, shapeIndex)) {
			PathIterator iter = shapes[shapeIndex].getPathIterator(tx);
			int nodeCtr = -1;
			while (!iter.isDone()) {
				int k = iter.currentSegment(coords);
				float x, y;
				if (k == PathIterator.SEG_MOVETO
						|| k == PathIterator.SEG_LINETO) {
					x = coords[0];
					y = coords[1];
					nodeCtr++;
				} else if (k == PathIterator.SEG_QUADTO) {
					x = coords[2];
					y = coords[3];
					nodeCtr++;
				} else if (k == PathIterator.SEG_CUBICTO) {
					x = coords[4];
					y = coords[5];
					nodeCtr++;
				} else if (k == PathIterator.SEG_CLOSE) {
					x = -1;
					y = -1;
				} else {
					throw new RuntimeException("unexpected segment type: "
							+ k);
				}
				g.setColor(Color.white);
				if (selection != null
						&& selection.getShapeIndex() == shapeIndex
						&& selection.getNodeIndex() == nodeCtr) {
					g.setColor(Color.darkGray);
				} else if (indication != null
						&& indication.getShapeIndex() == shapeIndex
						&& indication.getNodeIndex() == nodeCtr) {
					g.setColor(Color.gray);
				}
				ellipse.setFrame(x - r, y - r, 2 * r, 2 * r);
				g.fill(ellipse);
				g.setColor(Color.black);
				g.draw(ellipse);

				iter.next();
			}
		}
	}
	g.dispose();
}
 
Example 11
Source File: WPathGraphics.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Given a Java2D <code>PathIterator</code> instance,
 * this method translates that into a Window's path
 * in the printer device context.
 */
private void convertToWPath(PathIterator pathIter) {

    float[] segment = new float[6];
    int segmentType;

    WPrinterJob wPrinterJob = (WPrinterJob) getPrinterJob();

    /* Map the PathIterator's fill rule into the Window's
     * polygon fill rule.
     */
    int polyFillRule;
    if (pathIter.getWindingRule() == PathIterator.WIND_EVEN_ODD) {
        polyFillRule = WPrinterJob.POLYFILL_ALTERNATE;
    } else {
        polyFillRule = WPrinterJob.POLYFILL_WINDING;
    }
    wPrinterJob.setPolyFillMode(polyFillRule);

    wPrinterJob.beginPath();

    while (pathIter.isDone() == false) {
        segmentType = pathIter.currentSegment(segment);

        switch (segmentType) {
         case PathIterator.SEG_MOVETO:
            wPrinterJob.moveTo(segment[0], segment[1]);
            break;

         case PathIterator.SEG_LINETO:
            wPrinterJob.lineTo(segment[0], segment[1]);
            break;

        /* Convert the quad path to a bezier.
         */
         case PathIterator.SEG_QUADTO:
            int lastX = wPrinterJob.getPenX();
            int lastY = wPrinterJob.getPenY();
            float c1x = lastX + (segment[0] - lastX) * 2 / 3;
            float c1y = lastY + (segment[1] - lastY) * 2 / 3;
            float c2x = segment[2] - (segment[2] - segment[0]) * 2/ 3;
            float c2y = segment[3] - (segment[3] - segment[1]) * 2/ 3;
            wPrinterJob.polyBezierTo(c1x, c1y,
                                     c2x, c2y,
                                     segment[2], segment[3]);
            break;

         case PathIterator.SEG_CUBICTO:
            wPrinterJob.polyBezierTo(segment[0], segment[1],
                                     segment[2], segment[3],
                                     segment[4], segment[5]);
            break;

         case PathIterator.SEG_CLOSE:
            wPrinterJob.closeFigure();
            break;
        }


        pathIter.next();
    }

    wPrinterJob.endPath();

}
 
Example 12
Source File: Curve.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Calculates the number of times the given path
 * crosses the ray extending to the right from (px,py).
 * If the point lies on a part of the path,
 * then no crossings are counted for that intersection.
 * +1 is added for each crossing where the Y coordinate is increasing
 * -1 is added for each crossing where the Y coordinate is decreasing
 * The return value is the sum of all crossings for every segment in
 * the path.
 * The path must start with a SEG_MOVETO, otherwise an exception is
 * thrown.
 * The caller must check p[xy] for NaN values.
 * The caller may also reject infinite p[xy] values as well.
 */
public static int pointCrossingsForPath(PathIterator pi,
                                        double px, double py)
{
    if (pi.isDone()) {
        return 0;
    }
    double coords[] = new double[6];
    if (pi.currentSegment(coords) != PathIterator.SEG_MOVETO) {
        throw new IllegalPathStateException("missing initial moveto "+
                                            "in path definition");
    }
    pi.next();
    double movx = coords[0];
    double movy = coords[1];
    double curx = movx;
    double cury = movy;
    double endx, endy;
    int crossings = 0;
    while (!pi.isDone()) {
        switch (pi.currentSegment(coords)) {
        case PathIterator.SEG_MOVETO:
            if (cury != movy) {
                crossings += pointCrossingsForLine(px, py,
                                                   curx, cury,
                                                   movx, movy);
            }
            movx = curx = coords[0];
            movy = cury = coords[1];
            break;
        case PathIterator.SEG_LINETO:
            endx = coords[0];
            endy = coords[1];
            crossings += pointCrossingsForLine(px, py,
                                               curx, cury,
                                               endx, endy);
            curx = endx;
            cury = endy;
            break;
        case PathIterator.SEG_QUADTO:
            endx = coords[2];
            endy = coords[3];
            crossings += pointCrossingsForQuad(px, py,
                                               curx, cury,
                                               coords[0], coords[1],
                                               endx, endy, 0);
            curx = endx;
            cury = endy;
            break;
        case PathIterator.SEG_CUBICTO:
            endx = coords[4];
            endy = coords[5];
            crossings += pointCrossingsForCubic(px, py,
                                                curx, cury,
                                                coords[0], coords[1],
                                                coords[2], coords[3],
                                                endx, endy, 0);
            curx = endx;
            cury = endy;
            break;
        case PathIterator.SEG_CLOSE:
            if (cury != movy) {
                crossings += pointCrossingsForLine(px, py,
                                                   curx, cury,
                                                   movx, movy);
            }
            curx = movx;
            cury = movy;
            break;
        }
        pi.next();
    }
    if (cury != movy) {
        crossings += pointCrossingsForLine(px, py,
                                           curx, cury,
                                           movx, movy);
    }
    return crossings;
}
 
Example 13
Source File: ImageMapEmitter.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Convert AWT shape to image map coordinates.
 * 
 * @param shape
 * @return
 */
private String shape2polyCoords( Shape shape )
{
	if ( shape == null )
	{
		return null;
	}

	ArrayList<Double> al = new ArrayList<Double>( );

	FlatteningPathIterator pitr = new FlatteningPathIterator( shape.getPathIterator( null ),
			1 );
	double[] data = new double[6];

	while ( !pitr.isDone( ) )
	{
		int type = pitr.currentSegment( data );

		switch ( type )
		{
			case PathIterator.SEG_MOVETO :
				al.add( new Double( data[0] ) );
				al.add( new Double( data[1] ) );
				break;
			case PathIterator.SEG_LINETO :
				al.add( new Double( data[0] ) );
				al.add( new Double( data[1] ) );
				break;
			case PathIterator.SEG_QUADTO :
				al.add( new Double( data[0] ) );
				al.add( new Double( data[1] ) );
				al.add( new Double( data[2] ) );
				al.add( new Double( data[3] ) );
				break;
			case PathIterator.SEG_CUBICTO :
				al.add( new Double( data[0] ) );
				al.add( new Double( data[1] ) );
				al.add( new Double( data[2] ) );
				al.add( new Double( data[3] ) );
				al.add( new Double( data[4] ) );
				al.add( new Double( data[5] ) );
				break;
			case PathIterator.SEG_CLOSE :
				break;
		}

		pitr.next( );
	}

	if ( al.size( ) == 0 )
	{
		return null;
	}

	StringBuffer sb = new StringBuffer( );

	for ( int i = 0; i < al.size( ); i++ )
	{
		Double db = al.get( i );
		if ( i > 0 )
		{
			sb.append( "," ); //$NON-NLS-1$
		}
		sb.append( (int) translateCoor( db.doubleValue( ) ) );
	}

	return sb.toString( );
}
 
Example 14
Source File: GeneralPathObjectDescription.java    From pentaho-reporting with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Creates an object based on this description.
 *
 * @return The object.
 */
public Object createObject() {
  final int wRule = parseWindingRule();
  if ( wRule == -1 ) {
    return null;
  }

  final PathIteratorSegment[] segments =
      (PathIteratorSegment[]) getParameter( GeneralPathObjectDescription.SEGMENTS_NAME );
  if ( segments == null ) {
    return null;
  }

  final GeneralPath path = new GeneralPath();
  path.setWindingRule( wRule );
  for ( int i = 0; i < segments.length; i++ ) {
    final int segmentType = segments[i].getSegmentType();
    switch ( segmentType ) {
      case PathIterator.SEG_CLOSE: {
        path.closePath();
        break;
      }
      case PathIterator.SEG_CUBICTO: {
        path.curveTo( segments[i].getX1(), segments[i].getY1(), segments[i].getX2(), segments[i].getY2(), segments[i]
            .getX3(), segments[i].getY3() );
        break;
      }
      case PathIterator.SEG_LINETO: {
        path.lineTo( segments[i].getX1(), segments[i].getY1() );
        break;
      }
      case PathIterator.SEG_MOVETO: {
        path.moveTo( segments[i].getX1(), segments[i].getY1() );
        break;
      }
      case PathIterator.SEG_QUADTO: {
        path.quadTo( segments[i].getX1(), segments[i].getY1(), segments[i].getX2(), segments[i].getY2() );
        break;
      }
      default:
        throw new IllegalStateException( "Unexpected result from path iterator." );
    }
  }
  return path;
}
 
Example 15
Source File: PSPrinterJob.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
* Given a Java2D <code>PathIterator</code> instance,
* this method translates that into a PostScript path..
*/
void convertToPSPath(PathIterator pathIter) {

    float[] segment = new float[6];
    int segmentType;

    /* Map the PathIterator's fill rule into the PostScript
     * fill rule.
     */
    int fillRule;
    if (pathIter.getWindingRule() == PathIterator.WIND_EVEN_ODD) {
        fillRule = FILL_EVEN_ODD;
    } else {
        fillRule = FILL_WINDING;
    }

    beginPath();

    setFillMode(fillRule);

    while (pathIter.isDone() == false) {
        segmentType = pathIter.currentSegment(segment);

        switch (segmentType) {
         case PathIterator.SEG_MOVETO:
            moveTo(segment[0], segment[1]);
            break;

         case PathIterator.SEG_LINETO:
            lineTo(segment[0], segment[1]);
            break;

        /* Convert the quad path to a bezier.
         */
         case PathIterator.SEG_QUADTO:
            float lastX = getPenX();
            float lastY = getPenY();
            float c1x = lastX + (segment[0] - lastX) * 2 / 3;
            float c1y = lastY + (segment[1] - lastY) * 2 / 3;
            float c2x = segment[2] - (segment[2] - segment[0]) * 2/ 3;
            float c2y = segment[3] - (segment[3] - segment[1]) * 2/ 3;
            bezierTo(c1x, c1y,
                     c2x, c2y,
                     segment[2], segment[3]);
            break;

         case PathIterator.SEG_CUBICTO:
            bezierTo(segment[0], segment[1],
                     segment[2], segment[3],
                     segment[4], segment[5]);
            break;

         case PathIterator.SEG_CLOSE:
            closeSubpath();
            break;
        }


        pathIter.next();
    }
}
 
Example 16
Source File: PiscesRenderingEngine.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
public int currentSegment(float[] coords) {
    int type = src.currentSegment(coords);

    int lastCoord;
    switch(type) {
    case PathIterator.SEG_CUBICTO:
        lastCoord = 4;
        break;
    case PathIterator.SEG_QUADTO:
        lastCoord = 2;
        break;
    case PathIterator.SEG_LINETO:
    case PathIterator.SEG_MOVETO:
        lastCoord = 0;
        break;
    case PathIterator.SEG_CLOSE:
        // we don't want to deal with this case later. We just exit now
        curx_adjust = movx_adjust;
        cury_adjust = movy_adjust;
        return type;
    default:
        throw new InternalError("Unrecognized curve type");
    }

    // normalize endpoint
    float x_adjust = (float)Math.floor(coords[lastCoord] + lval) +
                 rval - coords[lastCoord];
    float y_adjust = (float)Math.floor(coords[lastCoord+1] + lval) +
                 rval - coords[lastCoord + 1];

    coords[lastCoord    ] += x_adjust;
    coords[lastCoord + 1] += y_adjust;

    // now that the end points are done, normalize the control points
    switch(type) {
    case PathIterator.SEG_CUBICTO:
        coords[0] += curx_adjust;
        coords[1] += cury_adjust;
        coords[2] += x_adjust;
        coords[3] += y_adjust;
        break;
    case PathIterator.SEG_QUADTO:
        coords[0] += (curx_adjust + x_adjust) / 2;
        coords[1] += (cury_adjust + y_adjust) / 2;
        break;
    case PathIterator.SEG_LINETO:
        break;
    case PathIterator.SEG_MOVETO:
        movx_adjust = x_adjust;
        movy_adjust = y_adjust;
        break;
    case PathIterator.SEG_CLOSE:
        throw new InternalError("This should be handled earlier.");
    }
    curx_adjust = x_adjust;
    cury_adjust = y_adjust;
    return type;
}
 
Example 17
Source File: Crossings.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
public static Crossings findCrossings(PathIterator pi,
                                      double xlo, double ylo,
                                      double xhi, double yhi)
{
    Crossings cross;
    if (pi.getWindingRule() == PathIterator.WIND_EVEN_ODD) {
        cross = new EvenOdd(xlo, ylo, xhi, yhi);
    } else {
        cross = new NonZero(xlo, ylo, xhi, yhi);
    }
    // coords array is big enough for holding:
    //     coordinates returned from currentSegment (6)
    //     OR
    //         two subdivided quadratic curves (2+4+4=10)
    //         AND
    //             0-1 horizontal splitting parameters
    //             OR
    //             2 parametric equation derivative coefficients
    //     OR
    //         three subdivided cubic curves (2+6+6+6=20)
    //         AND
    //             0-2 horizontal splitting parameters
    //             OR
    //             3 parametric equation derivative coefficients
    double[] coords = new double[23];
    double movx = 0;
    double movy = 0;
    double curx = 0;
    double cury = 0;
    double newx, newy;
    while (!pi.isDone()) {
        int type = pi.currentSegment(coords);
        switch (type) {
        case PathIterator.SEG_MOVETO:
            if (movy != cury &&
                cross.accumulateLine(curx, cury, movx, movy))
            {
                return null;
            }
            movx = curx = coords[0];
            movy = cury = coords[1];
            break;
        case PathIterator.SEG_LINETO:
            newx = coords[0];
            newy = coords[1];
            if (cross.accumulateLine(curx, cury, newx, newy)) {
                return null;
            }
            curx = newx;
            cury = newy;
            break;
        case PathIterator.SEG_QUADTO:
            newx = coords[2];
            newy = coords[3];
            if (cross.accumulateQuad(curx, cury, coords)) {
                return null;
            }
            curx = newx;
            cury = newy;
            break;
        case PathIterator.SEG_CUBICTO:
            newx = coords[4];
            newy = coords[5];
            if (cross.accumulateCubic(curx, cury, coords)) {
                return null;
            }
            curx = newx;
            cury = newy;
            break;
        case PathIterator.SEG_CLOSE:
            if (movy != cury &&
                cross.accumulateLine(curx, cury, movx, movy))
            {
                return null;
            }
            curx = movx;
            cury = movy;
            break;
        }
        pi.next();
    }
    if (movy != cury) {
        if (cross.accumulateLine(curx, cury, movx, movy)) {
            return null;
        }
    }
    if (debug) {
        cross.print();
    }
    return cross;
}
 
Example 18
Source File: WPathGraphics.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Given a Java2D <code>PathIterator</code> instance,
 * this method translates that into a Window's path
 * in the printer device context.
 */
private void convertToWPath(PathIterator pathIter) {

    float[] segment = new float[6];
    int segmentType;

    WPrinterJob wPrinterJob = (WPrinterJob) getPrinterJob();

    /* Map the PathIterator's fill rule into the Window's
     * polygon fill rule.
     */
    int polyFillRule;
    if (pathIter.getWindingRule() == PathIterator.WIND_EVEN_ODD) {
        polyFillRule = WPrinterJob.POLYFILL_ALTERNATE;
    } else {
        polyFillRule = WPrinterJob.POLYFILL_WINDING;
    }
    wPrinterJob.setPolyFillMode(polyFillRule);

    wPrinterJob.beginPath();

    while (pathIter.isDone() == false) {
        segmentType = pathIter.currentSegment(segment);

        switch (segmentType) {
         case PathIterator.SEG_MOVETO:
            wPrinterJob.moveTo(segment[0], segment[1]);
            break;

         case PathIterator.SEG_LINETO:
            wPrinterJob.lineTo(segment[0], segment[1]);
            break;

        /* Convert the quad path to a bezier.
         */
         case PathIterator.SEG_QUADTO:
            int lastX = wPrinterJob.getPenX();
            int lastY = wPrinterJob.getPenY();
            float c1x = lastX + (segment[0] - lastX) * 2 / 3;
            float c1y = lastY + (segment[1] - lastY) * 2 / 3;
            float c2x = segment[2] - (segment[2] - segment[0]) * 2/ 3;
            float c2y = segment[3] - (segment[3] - segment[1]) * 2/ 3;
            wPrinterJob.polyBezierTo(c1x, c1y,
                                     c2x, c2y,
                                     segment[2], segment[3]);
            break;

         case PathIterator.SEG_CUBICTO:
            wPrinterJob.polyBezierTo(segment[0], segment[1],
                                     segment[2], segment[3],
                                     segment[4], segment[5]);
            break;

         case PathIterator.SEG_CLOSE:
            wPrinterJob.closeFigure();
            break;
        }


        pathIter.next();
    }

    wPrinterJob.endPath();

}
 
Example 19
Source File: PiscesRenderingEngine.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
public int currentSegment(float[] coords) {
    int type = src.currentSegment(coords);

    int lastCoord;
    switch(type) {
    case PathIterator.SEG_CUBICTO:
        lastCoord = 4;
        break;
    case PathIterator.SEG_QUADTO:
        lastCoord = 2;
        break;
    case PathIterator.SEG_LINETO:
    case PathIterator.SEG_MOVETO:
        lastCoord = 0;
        break;
    case PathIterator.SEG_CLOSE:
        // we don't want to deal with this case later. We just exit now
        curx_adjust = movx_adjust;
        cury_adjust = movy_adjust;
        return type;
    default:
        throw new InternalError("Unrecognized curve type");
    }

    // normalize endpoint
    float x_adjust = (float)Math.floor(coords[lastCoord] + lval) +
                 rval - coords[lastCoord];
    float y_adjust = (float)Math.floor(coords[lastCoord+1] + lval) +
                 rval - coords[lastCoord + 1];

    coords[lastCoord    ] += x_adjust;
    coords[lastCoord + 1] += y_adjust;

    // now that the end points are done, normalize the control points
    switch(type) {
    case PathIterator.SEG_CUBICTO:
        coords[0] += curx_adjust;
        coords[1] += cury_adjust;
        coords[2] += x_adjust;
        coords[3] += y_adjust;
        break;
    case PathIterator.SEG_QUADTO:
        coords[0] += (curx_adjust + x_adjust) / 2;
        coords[1] += (cury_adjust + y_adjust) / 2;
        break;
    case PathIterator.SEG_LINETO:
        break;
    case PathIterator.SEG_MOVETO:
        movx_adjust = x_adjust;
        movy_adjust = y_adjust;
        break;
    case PathIterator.SEG_CLOSE:
        throw new InternalError("This should be handled earlier.");
    }
    curx_adjust = x_adjust;
    cury_adjust = y_adjust;
    return type;
}
 
Example 20
Source File: Curve.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
/**
 * Calculates the number of times the given path
 * crosses the ray extending to the right from (px,py).
 * If the point lies on a part of the path,
 * then no crossings are counted for that intersection.
 * +1 is added for each crossing where the Y coordinate is increasing
 * -1 is added for each crossing where the Y coordinate is decreasing
 * The return value is the sum of all crossings for every segment in
 * the path.
 * The path must start with a SEG_MOVETO, otherwise an exception is
 * thrown.
 * The caller must check p[xy] for NaN values.
 * The caller may also reject infinite p[xy] values as well.
 */
public static int pointCrossingsForPath(PathIterator pi,
                                        double px, double py)
{
    if (pi.isDone()) {
        return 0;
    }
    double[] coords = new double[6];
    if (pi.currentSegment(coords) != PathIterator.SEG_MOVETO) {
        throw new IllegalPathStateException("missing initial moveto "+
                                            "in path definition");
    }
    pi.next();
    double movx = coords[0];
    double movy = coords[1];
    double curx = movx;
    double cury = movy;
    double endx, endy;
    int crossings = 0;
    while (!pi.isDone()) {
        switch (pi.currentSegment(coords)) {
        case PathIterator.SEG_MOVETO:
            if (cury != movy) {
                crossings += pointCrossingsForLine(px, py,
                                                   curx, cury,
                                                   movx, movy);
            }
            movx = curx = coords[0];
            movy = cury = coords[1];
            break;
        case PathIterator.SEG_LINETO:
            endx = coords[0];
            endy = coords[1];
            crossings += pointCrossingsForLine(px, py,
                                               curx, cury,
                                               endx, endy);
            curx = endx;
            cury = endy;
            break;
        case PathIterator.SEG_QUADTO:
            endx = coords[2];
            endy = coords[3];
            crossings += pointCrossingsForQuad(px, py,
                                               curx, cury,
                                               coords[0], coords[1],
                                               endx, endy, 0);
            curx = endx;
            cury = endy;
            break;
        case PathIterator.SEG_CUBICTO:
            endx = coords[4];
            endy = coords[5];
            crossings += pointCrossingsForCubic(px, py,
                                                curx, cury,
                                                coords[0], coords[1],
                                                coords[2], coords[3],
                                                endx, endy, 0);
            curx = endx;
            cury = endy;
            break;
        case PathIterator.SEG_CLOSE:
            if (cury != movy) {
                crossings += pointCrossingsForLine(px, py,
                                                   curx, cury,
                                                   movx, movy);
            }
            curx = movx;
            cury = movy;
            break;
        }
        pi.next();
    }
    if (cury != movy) {
        crossings += pointCrossingsForLine(px, py,
                                           curx, cury,
                                           movx, movy);
    }
    return crossings;
}