Merge from mainline.
[official-gcc.git] / libjava / classpath / gnu / java / awt / java2d / QuadSegment.java
blob9c15a8cfda0218d6d3dd9d50a59b01bd8a59a7b5
1 /* QuadSegment.java -- QuadCurve segment used for BasicStroke
2 Copyright (C) 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package gnu.java.awt.java2d;
42 import java.awt.geom.Point2D;
43 import java.awt.geom.QuadCurve2D;
45 /**
46 * Quadratic Bezier curve segment
48 * Note: Most peers don't support quadratics directly, so it might make
49 * sense to represent them as cubics internally and just be done with it.
50 * I think we should be peer-agnostic, however, and stay faithful to the
51 * input geometry types as far as possible.
53 public class QuadSegment extends Segment
55 public Point2D cp; // control point
57 /**
58 * Constructor, takes the coordinates of the start, control,
59 * and end point, respectively.
61 public QuadSegment(double x1, double y1, double cx, double cy, double x2,
62 double y2)
64 super();
65 P1 = new Point2D.Double(x1, y1);
66 P2 = new Point2D.Double(x2, y2);
67 cp = new Point2D.Double(cx, cy);
70 public QuadSegment(Point2D p1, Point2D cp, Point2D p2)
72 super();
73 P1 = p1;
74 P2 = p2;
75 this.cp = cp;
78 public QuadSegment(QuadCurve2D curve)
80 super();
81 P1 = curve.getP1();
82 P2 = curve.getP2();
83 this.cp = curve.getCtrlPt();
86 /**
87 * Clones this segment
89 public Object clone()
91 return new QuadSegment(P1.getX(), P1.getY(), cp.getX(), cp.getY(),
92 P2.getX(), P2.getY());
95 /**
96 * Get the "top" and "bottom" segments of a given segment.
97 * First array element is p0 + normal, second is p0 - normal.
99 public Segment[] getDisplacedSegments(double radius)
101 this.radius = radius;
102 double x0 = P1.getX();
103 double y0 = P1.getY();
104 double x1 = cp.getX();
105 double y1 = cp.getY();
106 double x2 = P2.getX();
107 double y2 = P2.getY();
109 QuadCurve2D left = new QuadCurve2D.Double();
110 QuadCurve2D right = new QuadCurve2D.Double();
111 QuadCurve2D orig = new QuadCurve2D.Double(x0, y0, x1, y1, x2, y2);
112 orig.subdivide(left, right);
114 QuadSegment s1 = offsetSubdivided(left, true);
115 QuadSegment s2 = offsetSubdivided(left, false);
117 s1.add( offsetSubdivided(right, true) );
118 s2.add( offsetSubdivided(right, false) );
120 return new Segment[]{s1, s2};
123 private QuadSegment offsetSubdivided(QuadCurve2D curve, boolean plus)
125 double[] n1 = normal(curve.getX1(), curve.getY1(),
126 curve.getCtrlX(), curve.getCtrlY());
127 double[] n2 = normal(curve.getCtrlX(), curve.getCtrlY(),
128 curve.getX2(), curve.getY2());
130 Point2D cp;
131 QuadSegment s;
132 if( plus )
134 cp = lineIntersection(curve.getX1() + n1[0],
135 curve.getY1() + n1[1],
136 curve.getCtrlX() + n1[0],
137 curve.getCtrlY() + n1[1],
138 curve.getCtrlX() + n2[0],
139 curve.getCtrlY() + n2[1],
140 curve.getX2() + n2[0],
141 curve.getY2() + n2[1], true);
142 s = new QuadSegment(curve.getX1() + n1[0], curve.getY1() + n1[1],
143 cp.getX(), cp.getY(),
144 curve.getX2() + n2[0], curve.getY2() + n2[1]);
146 else
148 cp = lineIntersection(curve.getX1() - n1[0],
149 curve.getY1() - n1[1],
150 curve.getCtrlX() - n1[0],
151 curve.getCtrlY() - n1[1],
152 curve.getCtrlX() - n2[0],
153 curve.getCtrlY() - n2[1],
154 curve.getX2() - n2[0],
155 curve.getY2() - n2[1], true);
157 s = new QuadSegment(curve.getX1() - n1[0], curve.getY1() - n1[1],
158 cp.getX(), cp.getY(),
159 curve.getX2() - n2[0], curve.getY2() - n2[1]);
162 return s;
165 private Point2D lineIntersection(double X1, double Y1,
166 double X2, double Y2,
167 double X3, double Y3,
168 double X4, double Y4,
169 boolean infinite)
171 double x1 = X1;
172 double y1 = Y1;
173 double rx = X2 - x1;
174 double ry = Y2 - y1;
176 double x2 = X3;
177 double y2 = Y3;
178 double sx = X4 - x2;
179 double sy = Y4 - y2;
181 double determinant = sx * ry - sy * rx;
182 double nom = (sx * (y2 - y1) + sy * (x1 - x2));
184 // lines can be considered parallel.
185 if (Math.abs(determinant) < 1E-6)
186 return null;
188 nom = nom / determinant;
190 // check if lines are within the bounds
191 if(!infinite && (nom > 1.0 || nom < 0.0))
192 return null;
194 return new Point2D.Double(x1 + nom * rx, y1 + nom * ry);
197 public void reverse()
199 Point2D p = P1;
200 P1 = P2;
201 P2 = p;
204 public double[] first()
206 return new double[]{cp.getX(), cp.getY()};
209 public double[] last()
211 return new double[]{cp.getX(), cp.getY()};