KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > ext > awt > geom > Linear


1 /*
2
3    Copyright 2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.ext.awt.geom;
19
20 import java.util.Arrays JavaDoc;
21 import java.awt.geom.Point2D JavaDoc;
22 import java.awt.geom.Rectangle2D JavaDoc;
23 import java.io.PrintStream JavaDoc;
24 import java.io.FileOutputStream JavaDoc;
25
26 /**
27  * A class representing a linear path segment.
28  *
29  * @version $Id: Linear.java,v 1.4 2005/04/02 14:26:09 deweese Exp $
30  */

31 public class Linear implements Segment {
32     public Point2D.Double JavaDoc p1, p2;
33
34     public Linear() {
35         p1 = new Point2D.Double JavaDoc();
36         p2 = new Point2D.Double JavaDoc();
37     }
38
39     public Linear(double x1, double y1,
40                   double x2, double y2) {
41         p1 = new Point2D.Double JavaDoc(x1, y1);
42         p2 = new Point2D.Double JavaDoc(x2, y2);
43     }
44
45     public Linear(Point2D.Double JavaDoc p1, Point2D.Double JavaDoc p2) {
46         this.p1 = p1;
47         this.p2 = p2;
48     }
49
50     public Object JavaDoc clone() {
51         return new Linear(new Point2D.Double JavaDoc(p1.x, p1.y),
52                           new Point2D.Double JavaDoc(p2.x, p2.y));
53     }
54
55     public Segment reverse() {
56         return new Linear(new Point2D.Double JavaDoc(p2.x, p2.y),
57                           new Point2D.Double JavaDoc(p1.x, p1.y));
58     }
59
60     public double minX() {
61         if (p1.x < p2.x) return p1.x;
62         return p2.x;
63     }
64     public double maxX() {
65         if (p1.x > p2.x) return p1.x;
66         return p2.x;
67     }
68     public double minY() {
69         if (p1.y < p2.y) return p1.y;
70         return p2.y;
71     }
72     public double maxY() {
73         if (p1.y > p2.y) return p2.y;
74         return p1.y;
75     }
76     public Rectangle2D JavaDoc getBounds2D() {
77         double x, y, w, h;
78         if (p1.x < p2.x) {
79             x = p1.x; w = p2.x-p1.x;
80         } else {
81             x = p2.x; w = p1.x-p2.x;
82         }
83         if (p1.y < p2.y) {
84             y = p1.y; h = p2.y-p1.y;
85         } else {
86             y = p2.y; h = p1.y-p2.y;
87         }
88
89         return new Rectangle2D.Double JavaDoc(x, y, w, h);
90     }
91
92     public Point2D.Double JavaDoc evalDt(double t) {
93         double x = (p2.x-p1.x);
94         double y = (p2.y-p1.y);
95         return new Point2D.Double JavaDoc(x, y);
96     }
97     public Point2D.Double JavaDoc eval(double t) {
98         double x = p1.x + t*(p2.x-p1.x);
99         double y = p1.y + t*(p2.y-p1.y);
100         return new Point2D.Double JavaDoc(x, y);
101     }
102
103     public Segment.SplitResults split(double y) {
104         if ((y == p1.y) || (y == p2.y)) return null;
105         if ((y <= p1.y) && (y <= p2.y)) return null;
106         if ((y >= p1.y) && (y >= p2.y)) return null;
107
108     // This should be checked for numerical stability. So you
109
// need to ensure that p2.y-p1.y retains enough bits to be
110
// useful.
111
double t = (y-p1.y)/(p2.y-p1.y);
112
113         Segment [] t0 = {getSegment(0,t)};
114         Segment [] t1 = {getSegment(t,1)};
115
116         if (p2.y < y)
117             return new Segment.SplitResults(t0, t1);
118         return new Segment.SplitResults(t1, t0);
119     }
120
121     public Segment getSegment(double t0, double t1) {
122         Point2D.Double JavaDoc np1 = eval(t0);
123         Point2D.Double JavaDoc np2 = eval(t1);
124         return new Linear(np1, np2);
125     }
126     public Segment splitBefore(double t) {
127         return new Linear(p1, eval(t));
128     }
129     public Segment splitAfter(double t) {
130         return new Linear(eval(t), p2);
131     }
132
133     /**
134      * Subdivides this Linear segment into two segments at t = 0.5.
135      * can be done with getSegment but this is more efficent.
136      * @param s0 if non-null contains portion of curve from 0->.5
137      * @param s1 if non-null contains portion of curve from .5->1
138      */

139     public void subdivide(Segment s0, Segment s1) {
140         Linear l0=null, l1=null;
141         if (s0 instanceof Linear) l0 = (Linear)s0;
142         if (s1 instanceof Linear) l1 = (Linear)s1;
143         subdivide(l0, l1);
144     }
145
146     /**
147      * Subdivides this Linear segment into two segments at given t.
148      * @param s0 if non-null contains portion of curve from 0->t.
149      * @param s1 if non-null contains portion of curve from t->1.
150      */

151     public void subdivide(double t, Segment s0, Segment s1) {
152         Linear l0=null, l1=null;
153         if (s0 instanceof Linear) l0 = (Linear)s0;
154         if (s1 instanceof Linear) l1 = (Linear)s1;
155         subdivide(t, l0, l1);
156     }
157
158     /**
159      * Subdivides this Cubic curve into two curves at t = 0.5.
160      * Can be done with getSegment but this is more efficent.
161      * @param l0 if non-null contains portion of curve from 0->.5
162      * @param l1 if non-null contains portion of curve from .5->1
163      */

164     public void subdivide(Linear l0, Linear l1) {
165         if ((l0 == null) && (l1 == null)) return;
166
167         double x = (p1.x+p2.x)*.5;
168         double y = (p1.y+p2.y)*.5;
169
170         if (l0 != null) {
171             l0.p1.x = p1.x;
172             l0.p1.y = p1.y;
173             l0.p2.x = x;
174             l0.p2.y = y;
175         }
176         if (l1 != null) {
177             l1.p1.x = x;
178             l1.p1.y = y;
179             l1.p2.x = p2.x;
180             l1.p2.y = p2.y;
181         }
182     }
183
184     /**
185      * Subdivides this Cubic curve into two curves.
186      * Can be done with getSegment but this is more efficent.
187      * @param t position to split the curve
188      * @param l0 if non-null contains portion of curve from 0->t
189      * @param l1 if non-null contains portion of curve from t->1
190      */

191     public void subdivide(double t, Linear l0, Linear l1) {
192         if ((l0 == null) && (l1 == null)) return;
193
194         double x = p1.x+t*(p2.x-p1.x);
195         double y = p1.y+t*(p2.y-p1.y);
196
197         if (l0 != null) {
198             l0.p1.x = p1.x;
199             l0.p1.y = p1.y;
200             l0.p2.x = x;
201             l0.p2.y = y;
202         }
203         if (l1 != null) {
204             l1.p1.x = x;
205             l1.p1.y = y;
206             l1.p2.x = p2.x;
207             l1.p2.y = p2.y;
208         }
209     }
210
211     public double getLength() {
212         double dx = p2.x-p1.x;
213         double dy = p2.y-p1.y;
214         return Math.sqrt(dx*dx+dy*dy);
215     }
216     public double getLength(double maxErr) {
217         return getLength();
218     }
219
220     public String JavaDoc toString() {
221         return ("M" + p1.x + "," + p1.y +
222                 "L" + p2.x + "," + p2.y);
223     }
224
225     /*
226     public static boolean epsEq(double a, double b) {
227         final double eps = 0.000001;
228         return (((a + eps) > b) && ((a-eps) < b));
229     }
230
231     public static void sub(Linear orig, Linear curr,
232                            double t, double inc, int lev) {
233         Linear left=new Linear();
234         Linear right=new Linear();
235         curr.subdivide(left, right);
236         Point2D.Double ptl = left.eval(.5);
237         Point2D.Double ptr = right.eval(.5);
238         Point2D.Double pt1 = orig.eval(t-inc);
239         Point2D.Double pt2 = orig.eval(t+inc);
240         int steps = 100;
241         Point2D.Double l, r, o;
242         for (int i=0; i<=steps; i++) {
243             l = left.eval(i/(double)steps);
244             o = orig.eval(t-(2*inc)*(1-i/(double)steps));
245             if (!epsEq(l.x, o.x) || !epsEq(l.y, o.y))
246                 System.err.println("Lf Pt: [" + l.x + "," + l.y +
247                                    "] Orig: [" + o.x + "," + o.y +"]");
248             r = right.eval(i/(double)steps);
249             o = orig.eval(t+(2*inc*i/(double)steps));
250             if (!epsEq(r.x, o.x) || !epsEq(r.y, o.y))
251                 System.err.println("Rt Pt: [" + r.x + "," + r.y +
252                                    "] Orig: [" + o.x + "," + o.y +"]");
253         }
254         if (lev != 0) {
255             sub(orig, left, t-inc, inc/2, lev-1);
256             sub(orig, right, t+inc, inc/2, lev-1);
257         }
258     }
259
260     public static void eval(Linear l) {
261         System.err.println("Length : " + l.getLength());
262     }
263
264
265     public static void main(String args[]) {
266         Linear l;
267
268         l = new Linear(0,0, 30,0);
269         sub(l, l, .5, .25, 3);
270         eval(l);
271
272         l = new Linear(0,0, 0,30);
273         sub(l, l, .5, .25, 3);
274         eval(l);
275
276         l = new Linear(0,0, 20,30);
277         sub(l, l, .5, .25, 3);
278         eval(l);
279     }
280     */

281 }
282
Popular Tags