OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libjava/] [classpath/] [gnu/] [java/] [awt/] [java2d/] [QuadSegment.java] - Blame information for rev 769

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 769 jeremybenn
/* QuadSegment.java -- QuadCurve segment used for BasicStroke
2
   Copyright (C) 2006 Free Software Foundation, Inc.
3
 
4
This file is part of GNU Classpath.
5
 
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.
10
 
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.
15
 
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.
20
 
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.
25
 
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. */
37
 
38
 
39
package gnu.java.awt.java2d;
40
 
41
 
42
import java.awt.geom.Point2D;
43
import java.awt.geom.QuadCurve2D;
44
 
45
/**
46
 * Quadratic Bezier curve segment
47
 *
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.
52
 */
53
public class QuadSegment extends Segment
54
{
55
  public Point2D cp; // control point
56
 
57
  /**
58
   * Constructor, takes the coordinates of the start, control,
59
   * and end point, respectively.
60
   */
61
  public QuadSegment(double x1, double y1, double cx, double cy, double x2,
62
                     double y2)
63
  {
64
    super();
65
    P1 = new Point2D.Double(x1, y1);
66
    P2 = new Point2D.Double(x2, y2);
67
    cp = new Point2D.Double(cx, cy);
68
  }
69
 
70
  public QuadSegment(Point2D p1, Point2D cp, Point2D p2)
71
  {
72
    super();
73
    P1 = p1;
74
    P2 = p2;
75
    this.cp = cp;
76
  }
77
 
78
  public QuadSegment(QuadCurve2D curve)
79
  {
80
    super();
81
    P1 = curve.getP1();
82
    P2 = curve.getP2();
83
    this.cp = curve.getCtrlPt();
84
  }
85
 
86
  /**
87
   * Clones this segment
88
   */
89
  public Object clone()
90
  {
91
    QuadSegment segment = null;
92
 
93
    try
94
      {
95
        segment = (QuadSegment) super.clone();
96
 
97
        segment.P1 = (Point2D) P1.clone();
98
        segment.P2 = (Point2D) P2.clone();
99
        segment.cp = (Point2D) cp.clone();
100
      }
101
    catch (CloneNotSupportedException cnse)
102
      {
103
        InternalError ie = new InternalError();
104
        ie.initCause(cnse);
105
        throw ie;
106
      }
107
 
108
    return segment;
109
  }
110
 
111
  /**
112
   * Get the "top" and "bottom" segments of a given segment.
113
   * First array element is p0 + normal, second is p0 - normal.
114
   */
115
  public Segment[] getDisplacedSegments(double radius)
116
  {
117
    this.radius = radius;
118
    double x0 = P1.getX();
119
    double y0 = P1.getY();
120
    double x1 = cp.getX();
121
    double y1 = cp.getY();
122
    double x2 = P2.getX();
123
    double y2 = P2.getY();
124
 
125
    QuadCurve2D left = new QuadCurve2D.Double();
126
    QuadCurve2D right = new QuadCurve2D.Double();
127
    QuadCurve2D orig = new QuadCurve2D.Double(x0, y0, x1, y1, x2, y2);
128
    orig.subdivide(left, right);
129
 
130
    QuadSegment s1 = offsetSubdivided(left, true);
131
    QuadSegment s2 = offsetSubdivided(left, false);
132
 
133
    s1.add( offsetSubdivided(right, true) );
134
    s2.add( offsetSubdivided(right, false) );
135
 
136
    return new Segment[]{s1, s2};
137
  }
138
 
139
  private QuadSegment offsetSubdivided(QuadCurve2D curve, boolean plus)
140
  {
141
    double[] n1 = normal(curve.getX1(), curve.getY1(),
142
                         curve.getCtrlX(), curve.getCtrlY());
143
    double[] n2 = normal(curve.getCtrlX(), curve.getCtrlY(),
144
                         curve.getX2(), curve.getY2());
145
 
146
    Point2D cp;
147
    QuadSegment s;
148
    if(!plus)
149
      {
150
        n1[0] = -n1[0];
151
        n1[1] = -n1[1];
152
        n2[0] = -n2[0];
153
        n2[1] = -n2[1];
154
      }
155
 
156
    // Handle special cases where the control point is equal to an end point
157
    // or end points are equal (ie, straight lines)
158
    if (curve.getP1().equals(curve.getCtrlPt()))
159
      {
160
        cp = curve.getCtrlPt();
161
        cp.setLocation(cp.getX() + n2[0], cp.getY() + n2[1]);
162
        n1[0] = n2[0];
163
        n1[1] = n2[1];
164
      }
165
    else if (curve.getP2().equals(curve.getCtrlPt()))
166
      {
167
        cp = curve.getCtrlPt();
168
        cp.setLocation(cp.getX() + n1[0], cp.getY() + n1[1]);
169
        n2[0] = n1[0];
170
        n2[1] = n1[1];
171
      }
172
    else if (curve.getP1().equals(curve.getP2()))
173
      {
174
        cp = curve.getCtrlPt();
175
 
176
        double deltaX = curve.getX1() - curve.getCtrlX();
177
        double deltaY = curve.getY1() - curve.getCtrlY();
178
        double length = Math.sqrt((deltaX * deltaX) + (deltaY * deltaY));
179
        double ratio = radius / length;
180
        deltaX *= ratio;
181
        deltaY *= ratio;
182
 
183
        if (plus)
184
          cp.setLocation(cp.getX() + deltaX, cp.getY() + deltaY);
185
        else
186
          cp.setLocation(cp.getX() - deltaX, cp.getY() - deltaY);
187
      }
188
    else if (n1[0] == n2[0] && n1[1] == n2[1])
189
      {
190
        cp = curve.getCtrlPt();
191
        cp.setLocation(cp.getX() + n1[0], cp.getY() + n1[1]);
192
      }
193
    else
194
      {
195
        cp = lineIntersection(curve.getX1() + n1[0],
196
                              curve.getY1() + n1[1],
197
                              curve.getCtrlX() + n1[0],
198
                              curve.getCtrlY() + n1[1],
199
                              curve.getCtrlX() + n2[0],
200
                              curve.getCtrlY() + n2[1],
201
                              curve.getX2() + n2[0],
202
                              curve.getY2() + n2[1], true);
203
      }
204
 
205
    s = new QuadSegment(curve.getX1() + n1[0], curve.getY1() + n1[1],
206
                        cp.getX(), cp.getY(),
207
                        curve.getX2() + n2[0], curve.getY2() + n2[1]);
208
 
209
    return s;
210
  }
211
 
212
  private Point2D lineIntersection(double X1, double Y1,
213
                                   double X2, double Y2,
214
                                   double X3, double Y3,
215
                                   double X4, double Y4,
216
                                   boolean infinite)
217
  {
218
    double x1 = X1;
219
    double y1 = Y1;
220
    double rx = X2 - x1;
221
    double ry = Y2 - y1;
222
 
223
    double x2 = X3;
224
    double y2 = Y3;
225
    double sx = X4 - x2;
226
    double sy = Y4 - y2;
227
 
228
    double determinant = sx * ry - sy * rx;
229
    double nom = (sx * (y2 - y1) + sy * (x1 - x2));
230
 
231
    // lines can be considered parallel.
232
    if (Math.abs(determinant) < 1E-6)
233
      return null;
234
 
235
    nom = nom / determinant;
236
 
237
    // check if lines are within the bounds
238
    if(!infinite && (nom > 1.0 || nom < 0.0))
239
      return null;
240
 
241
    return new Point2D.Double(x1 + nom * rx, y1 + nom * ry);
242
  }
243
 
244
  public void reverse()
245
  {
246
    Point2D p = P1;
247
    P1 = P2;
248
    P2 = p;
249
  }
250
 
251
  public double[] cp1()
252
  {
253
    return new double[]{cp.getX(), cp.getY()};
254
  }
255
 
256
  public double[] cp2()
257
  {
258
    return new double[]{cp.getX(), cp.getY()};
259
  }
260
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.