0N/A/*
2362N/A * Copyright (c) 1997, 2003, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.awt.geom;
0N/A
0N/Aimport java.util.*;
0N/A
0N/A/**
0N/A * A utility class to iterate over the path segments of an arc
0N/A * through the PathIterator interface.
0N/A *
0N/A * @author Jim Graham
0N/A */
0N/Aclass ArcIterator implements PathIterator {
0N/A double x, y, w, h, angStRad, increment, cv;
0N/A AffineTransform affine;
0N/A int index;
0N/A int arcSegs;
0N/A int lineSegs;
0N/A
0N/A ArcIterator(Arc2D a, AffineTransform at) {
0N/A this.w = a.getWidth() / 2;
0N/A this.h = a.getHeight() / 2;
0N/A this.x = a.getX() + w;
0N/A this.y = a.getY() + h;
0N/A this.angStRad = -Math.toRadians(a.getAngleStart());
0N/A this.affine = at;
0N/A double ext = -a.getAngleExtent();
0N/A if (ext >= 360.0 || ext <= -360) {
0N/A arcSegs = 4;
0N/A this.increment = Math.PI / 2;
0N/A // btan(Math.PI / 2);
0N/A this.cv = 0.5522847498307933;
0N/A if (ext < 0) {
0N/A increment = -increment;
0N/A cv = -cv;
0N/A }
0N/A } else {
0N/A arcSegs = (int) Math.ceil(Math.abs(ext) / 90.0);
0N/A this.increment = Math.toRadians(ext / arcSegs);
0N/A this.cv = btan(increment);
0N/A if (cv == 0) {
0N/A arcSegs = 0;
0N/A }
0N/A }
0N/A switch (a.getArcType()) {
0N/A case Arc2D.OPEN:
0N/A lineSegs = 0;
0N/A break;
0N/A case Arc2D.CHORD:
0N/A lineSegs = 1;
0N/A break;
0N/A case Arc2D.PIE:
0N/A lineSegs = 2;
0N/A break;
0N/A }
0N/A if (w < 0 || h < 0) {
0N/A arcSegs = lineSegs = -1;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Return the winding rule for determining the insideness of the
0N/A * path.
0N/A * @see #WIND_EVEN_ODD
0N/A * @see #WIND_NON_ZERO
0N/A */
0N/A public int getWindingRule() {
0N/A return WIND_NON_ZERO;
0N/A }
0N/A
0N/A /**
0N/A * Tests if there are more points to read.
0N/A * @return true if there are more points to read
0N/A */
0N/A public boolean isDone() {
0N/A return index > arcSegs + lineSegs;
0N/A }
0N/A
0N/A /**
0N/A * Moves the iterator to the next segment of the path forwards
0N/A * along the primary direction of traversal as long as there are
0N/A * more points in that direction.
0N/A */
0N/A public void next() {
0N/A index++;
0N/A }
0N/A
0N/A /*
0N/A * btan computes the length (k) of the control segments at
0N/A * the beginning and end of a cubic bezier that approximates
0N/A * a segment of an arc with extent less than or equal to
0N/A * 90 degrees. This length (k) will be used to generate the
0N/A * 2 bezier control points for such a segment.
0N/A *
0N/A * Assumptions:
0N/A * a) arc is centered on 0,0 with radius of 1.0
0N/A * b) arc extent is less than 90 degrees
0N/A * c) control points should preserve tangent
0N/A * d) control segments should have equal length
0N/A *
0N/A * Initial data:
0N/A * start angle: ang1
0N/A * end angle: ang2 = ang1 + extent
0N/A * start point: P1 = (x1, y1) = (cos(ang1), sin(ang1))
0N/A * end point: P4 = (x4, y4) = (cos(ang2), sin(ang2))
0N/A *
0N/A * Control points:
0N/A * P2 = (x2, y2)
0N/A * | x2 = x1 - k * sin(ang1) = cos(ang1) - k * sin(ang1)
0N/A * | y2 = y1 + k * cos(ang1) = sin(ang1) + k * cos(ang1)
0N/A *
0N/A * P3 = (x3, y3)
0N/A * | x3 = x4 + k * sin(ang2) = cos(ang2) + k * sin(ang2)
0N/A * | y3 = y4 - k * cos(ang2) = sin(ang2) - k * cos(ang2)
0N/A *
0N/A * The formula for this length (k) can be found using the
0N/A * following derivations:
0N/A *
0N/A * Midpoints:
0N/A * a) bezier (t = 1/2)
0N/A * bPm = P1 * (1-t)^3 +
0N/A * 3 * P2 * t * (1-t)^2 +
0N/A * 3 * P3 * t^2 * (1-t) +
0N/A * P4 * t^3 =
0N/A * = (P1 + 3P2 + 3P3 + P4)/8
0N/A *
0N/A * b) arc
0N/A * aPm = (cos((ang1 + ang2)/2), sin((ang1 + ang2)/2))
0N/A *
0N/A * Let angb = (ang2 - ang1)/2; angb is half of the angle
0N/A * between ang1 and ang2.
0N/A *
0N/A * Solve the equation bPm == aPm
0N/A *
0N/A * a) For xm coord:
0N/A * x1 + 3*x2 + 3*x3 + x4 = 8*cos((ang1 + ang2)/2)
0N/A *
0N/A * cos(ang1) + 3*cos(ang1) - 3*k*sin(ang1) +
0N/A * 3*cos(ang2) + 3*k*sin(ang2) + cos(ang2) =
0N/A * = 8*cos((ang1 + ang2)/2)
0N/A *
0N/A * 4*cos(ang1) + 4*cos(ang2) + 3*k*(sin(ang2) - sin(ang1)) =
0N/A * = 8*cos((ang1 + ang2)/2)
0N/A *
0N/A * 8*cos((ang1 + ang2)/2)*cos((ang2 - ang1)/2) +
0N/A * 6*k*sin((ang2 - ang1)/2)*cos((ang1 + ang2)/2) =
0N/A * = 8*cos((ang1 + ang2)/2)
0N/A *
0N/A * 4*cos(angb) + 3*k*sin(angb) = 4
0N/A *
0N/A * k = 4 / 3 * (1 - cos(angb)) / sin(angb)
0N/A *
0N/A * b) For ym coord we derive the same formula.
0N/A *
0N/A * Since this formula can generate "NaN" values for small
0N/A * angles, we will derive a safer form that does not involve
0N/A * dividing by very small values:
0N/A * (1 - cos(angb)) / sin(angb) =
0N/A * = (1 - cos(angb))*(1 + cos(angb)) / sin(angb)*(1 + cos(angb)) =
0N/A * = (1 - cos(angb)^2) / sin(angb)*(1 + cos(angb)) =
0N/A * = sin(angb)^2 / sin(angb)*(1 + cos(angb)) =
0N/A * = sin(angb) / (1 + cos(angb))
0N/A *
0N/A */
0N/A private static double btan(double increment) {
0N/A increment /= 2.0;
0N/A return 4.0 / 3.0 * Math.sin(increment) / (1.0 + Math.cos(increment));
0N/A }
0N/A
0N/A /**
0N/A * Returns the coordinates and type of the current path segment in
0N/A * the iteration.
0N/A * The return value is the path segment type:
0N/A * SEG_MOVETO, SEG_LINETO, SEG_QUADTO, SEG_CUBICTO, or SEG_CLOSE.
0N/A * A float array of length 6 must be passed in and may be used to
0N/A * store the coordinates of the point(s).
0N/A * Each point is stored as a pair of float x,y coordinates.
0N/A * SEG_MOVETO and SEG_LINETO types will return one point,
0N/A * SEG_QUADTO will return two points,
0N/A * SEG_CUBICTO will return 3 points
0N/A * and SEG_CLOSE will not return any points.
0N/A * @see #SEG_MOVETO
0N/A * @see #SEG_LINETO
0N/A * @see #SEG_QUADTO
0N/A * @see #SEG_CUBICTO
0N/A * @see #SEG_CLOSE
0N/A */
0N/A public int currentSegment(float[] coords) {
0N/A if (isDone()) {
0N/A throw new NoSuchElementException("arc iterator out of bounds");
0N/A }
0N/A double angle = angStRad;
0N/A if (index == 0) {
0N/A coords[0] = (float) (x + Math.cos(angle) * w);
0N/A coords[1] = (float) (y + Math.sin(angle) * h);
0N/A if (affine != null) {
0N/A affine.transform(coords, 0, coords, 0, 1);
0N/A }
0N/A return SEG_MOVETO;
0N/A }
0N/A if (index > arcSegs) {
0N/A if (index == arcSegs + lineSegs) {
0N/A return SEG_CLOSE;
0N/A }
0N/A coords[0] = (float) x;
0N/A coords[1] = (float) y;
0N/A if (affine != null) {
0N/A affine.transform(coords, 0, coords, 0, 1);
0N/A }
0N/A return SEG_LINETO;
0N/A }
0N/A angle += increment * (index - 1);
0N/A double relx = Math.cos(angle);
0N/A double rely = Math.sin(angle);
0N/A coords[0] = (float) (x + (relx - cv * rely) * w);
0N/A coords[1] = (float) (y + (rely + cv * relx) * h);
0N/A angle += increment;
0N/A relx = Math.cos(angle);
0N/A rely = Math.sin(angle);
0N/A coords[2] = (float) (x + (relx + cv * rely) * w);
0N/A coords[3] = (float) (y + (rely - cv * relx) * h);
0N/A coords[4] = (float) (x + relx * w);
0N/A coords[5] = (float) (y + rely * h);
0N/A if (affine != null) {
0N/A affine.transform(coords, 0, coords, 0, 3);
0N/A }
0N/A return SEG_CUBICTO;
0N/A }
0N/A
0N/A /**
0N/A * Returns the coordinates and type of the current path segment in
0N/A * the iteration.
0N/A * The return value is the path segment type:
0N/A * SEG_MOVETO, SEG_LINETO, SEG_QUADTO, SEG_CUBICTO, or SEG_CLOSE.
0N/A * A double array of length 6 must be passed in and may be used to
0N/A * store the coordinates of the point(s).
0N/A * Each point is stored as a pair of double x,y coordinates.
0N/A * SEG_MOVETO and SEG_LINETO types will return one point,
0N/A * SEG_QUADTO will return two points,
0N/A * SEG_CUBICTO will return 3 points
0N/A * and SEG_CLOSE will not return any points.
0N/A * @see #SEG_MOVETO
0N/A * @see #SEG_LINETO
0N/A * @see #SEG_QUADTO
0N/A * @see #SEG_CUBICTO
0N/A * @see #SEG_CLOSE
0N/A */
0N/A public int currentSegment(double[] coords) {
0N/A if (isDone()) {
0N/A throw new NoSuchElementException("arc iterator out of bounds");
0N/A }
0N/A double angle = angStRad;
0N/A if (index == 0) {
0N/A coords[0] = x + Math.cos(angle) * w;
0N/A coords[1] = y + Math.sin(angle) * h;
0N/A if (affine != null) {
0N/A affine.transform(coords, 0, coords, 0, 1);
0N/A }
0N/A return SEG_MOVETO;
0N/A }
0N/A if (index > arcSegs) {
0N/A if (index == arcSegs + lineSegs) {
0N/A return SEG_CLOSE;
0N/A }
0N/A coords[0] = x;
0N/A coords[1] = y;
0N/A if (affine != null) {
0N/A affine.transform(coords, 0, coords, 0, 1);
0N/A }
0N/A return SEG_LINETO;
0N/A }
0N/A angle += increment * (index - 1);
0N/A double relx = Math.cos(angle);
0N/A double rely = Math.sin(angle);
0N/A coords[0] = x + (relx - cv * rely) * w;
0N/A coords[1] = y + (rely + cv * relx) * h;
0N/A angle += increment;
0N/A relx = Math.cos(angle);
0N/A rely = Math.sin(angle);
0N/A coords[2] = x + (relx + cv * rely) * w;
0N/A coords[3] = y + (rely - cv * relx) * h;
0N/A coords[4] = x + relx * w;
0N/A coords[5] = y + rely * h;
0N/A if (affine != null) {
0N/A affine.transform(coords, 0, coords, 0, 3);
0N/A }
0N/A return SEG_CUBICTO;
0N/A }
0N/A}