1 18 package org.apache.batik.ext.awt.geom; 19 20 import java.util.Arrays ; 21 import java.awt.geom.Point2D ; 22 import java.awt.geom.Rectangle2D ; 23 import java.io.PrintStream ; 24 import java.io.FileOutputStream ; 25 26 31 public class Linear implements Segment { 32 public Point2D.Double p1, p2; 33 34 public Linear() { 35 p1 = new Point2D.Double (); 36 p2 = new Point2D.Double (); 37 } 38 39 public Linear(double x1, double y1, 40 double x2, double y2) { 41 p1 = new Point2D.Double (x1, y1); 42 p2 = new Point2D.Double (x2, y2); 43 } 44 45 public Linear(Point2D.Double p1, Point2D.Double p2) { 46 this.p1 = p1; 47 this.p2 = p2; 48 } 49 50 public Object clone() { 51 return new Linear(new Point2D.Double (p1.x, p1.y), 52 new Point2D.Double (p2.x, p2.y)); 53 } 54 55 public Segment reverse() { 56 return new Linear(new Point2D.Double (p2.x, p2.y), 57 new Point2D.Double (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 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 (x, y, w, h); 90 } 91 92 public Point2D.Double evalDt(double t) { 93 double x = (p2.x-p1.x); 94 double y = (p2.y-p1.y); 95 return new Point2D.Double (x, y); 96 } 97 public Point2D.Double 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 (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 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 np1 = eval(t0); 123 Point2D.Double 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 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 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 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 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 toString() { 221 return ("M" + p1.x + "," + p1.y + 222 "L" + p2.x + "," + p2.y); 223 } 224 225 281 } 282 | Popular Tags |