1 /* Line2D.java -- represents a line in 2-D space, plus operations on a line
2 Copyright (C) 2000, 2001, 2002 Free Software Foundation
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)
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., 59 Temple Place, Suite 330, Boston, MA
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
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. */
38 package java.awt.geom;
40 import java.awt.Rectangle;
41 import java.awt.Shape;
42 import java.util.NoSuchElementException;
45 * Represents a directed line bewteen two points in (x,y) Cartesian space.
46 * Remember, on-screen graphics have increasing x from left-to-right, and
47 * increasing y from top-to-bottom. The storage is left to subclasses.
49 * @author Tom Tromey <tromey@cygnus.com>
50 * @author Eric Blake <ebb9@email.byu.edu>
52 * @status updated to 1.4
54 public abstract class Line2D implements Shape, Cloneable
57 * The default constructor.
64 * Return the x coordinate of the first point.
66 * @return the starting x coordinate
68 public abstract double getX1();
71 * Return the y coordinate of the first point.
73 * @return the starting y coordinate
75 public abstract double getY1();
78 * Return the first point.
80 * @return the starting point
82 public abstract Point2D getP1();
85 * Return the x coordinate of the second point.
87 * @return the ending x coordinate
89 public abstract double getX2();
92 * Return the y coordinate of the second point.
94 * @return the ending y coordinate
96 public abstract double getY2();
99 * Return the second point.
101 * @return the ending point
103 public abstract Point2D getP2();
106 * Set the coordinates of the line to the given coordinates. Loss of
107 * precision may occur due to rounding issues.
109 * @param x1 the first x coordinate
110 * @param y1 the first y coordinate
111 * @param x2 the second x coordinate
112 * @param y2 the second y coordinate
114 public abstract void setLine(double x1, double y1, double x2, double y2);
117 * Set the coordinates to the given points.
119 * @param p1 the first point
120 * @param p2 the second point
121 * @throws NullPointerException if either point is null
123 public void setLine(Point2D p1, Point2D p2)
125 setLine(p1.getX(), p1.getY(), p2.getX(), p2.getY());
129 * Set the coordinates to those of the given line.
131 * @param l the line to copy
132 * @throws NullPointerException if l is null
134 public void setLine(Line2D l)
136 setLine(l.getX1(), l.getY1(), l.getX2(), l.getY2());
140 * Computes the relative rotation direction needed to pivot the line about
141 * the first point in order to have the second point colinear with point p.
142 * Because of floating point rounding, don't expect this to be a perfect
143 * measure of colinearity. The answer is 1 if the line has a shorter rotation
144 * in the direction of the positive X axis to the negative Y axis
145 * (counter-clockwise in the default Java coordinate system), or -1 if the
146 * shortest rotation is in the opposite direction (clockwise). If p
147 * is already colinear, the return value is -1 if it lies beyond the first
148 * point, 0 if it lies in the segment, or 1 if it lies beyond the second
149 * point. If the first and second point are coincident, this returns 0.
151 * @param x1 the first x coordinate
152 * @param y1 the first y coordinate
153 * @param x2 the second x coordinate
154 * @param y2 the second y coordinate
155 * @param px the reference x coordinate
156 * @param py the reference y coordinate
157 * @return the relative rotation direction
159 public static int relativeCCW(double x1, double y1, double x2, double y2,
160 double px, double py)
162 if ((x1 == x2 && y1 == y2)
163 || (x1 == px && y1 == py))
164 return 0; // Coincident points.
165 // Translate to the origin.
170 double slope2 = y2 / x2;
171 double slopep = py / px;
172 if (slope2 == slopep || (x2 == 0 && px == 0))
173 return y2 > 0 // Colinear.
174 ? (py < 0 ? -1 : py > y2 ? 1 : 0)
175 : (py > 0 ? -1 : py < y2 ? 1 : 0);
176 if (x2 >= 0 && slope2 >= 0)
177 return px >= 0 // Quadrant 1.
178 ? (slope2 > slopep ? 1 : -1)
179 : (slope2 < slopep ? 1 : -1);
181 return px < 0 // Quadrant 2.
182 ? (slope2 > slopep ? 1 : -1)
183 : (slope2 < slopep ? 1 : -1);
185 return px >= 0 // Quadrant 3.
186 ? (slope2 < slopep ? 1 : -1)
187 : (slope2 > slopep ? 1 : -1);
188 return px < 0 // Quadrant 4.
189 ? (slope2 < slopep ? 1 : -1)
190 : (slope2 > slopep ? 1 : -1);
194 * Computes the relative rotation direction needed to pivot this line about
195 * the first point in order to have the second point colinear with point p.
196 * Because of floating point rounding, don't expect this to be a perfect
197 * measure of colinearity. The answer is 1 if the line has a shorter rotation
198 * in the direction of the positive X axis to the negative Y axis
199 * (counter-clockwise in the default Java coordinate system), or -1 if the
200 * shortest rotation is in the opposite direction (clockwise). If p
201 * is already colinear, the return value is -1 if it lies beyond the first
202 * point, 0 if it lies in the segment, or 1 if it lies beyond the second
203 * point. If the first and second point are coincident, this returns 0.
205 * @param px the reference x coordinate
206 * @param py the reference y coordinate
207 * @return the relative rotation direction
208 * @see #relativeCCW(double, double, double, double, double, double)
210 public int relativeCCW(double px, double py)
212 return relativeCCW(getX1(), getY1(), getX2(), getY2(), px, py);
216 * Computes the relative rotation direction needed to pivot this line about
217 * the first point in order to have the second point colinear with point p.
218 * Because of floating point rounding, don't expect this to be a perfect
219 * measure of colinearity. The answer is 1 if the line has a shorter rotation
220 * in the direction of the positive X axis to the negative Y axis
221 * (counter-clockwise in the default Java coordinate system), or -1 if the
222 * shortest rotation is in the opposite direction (clockwise). If p
223 * is already colinear, the return value is -1 if it lies beyond the first
224 * point, 0 if it lies in the segment, or 1 if it lies beyond the second
225 * point. If the first and second point are coincident, this returns 0.
227 * @param p the reference point
228 * @return the relative rotation direction
229 * @throws NullPointerException if p is null
230 * @see #relativeCCW(double, double, double, double, double, double)
232 public int relativeCCW(Point2D p)
234 return relativeCCW(getX1(), getY1(), getX2(), getY2(), p.getX(), p.getY());
238 * Test if the line segment (x1,y1)->(x2,y2) intersects the line segment
239 * (x3,y3)->(x4,y4).
241 * @param x1 the first x coordinate of the first segment
242 * @param y1 the first y coordinate of the first segment
243 * @param x2 the second x coordinate of the first segment
244 * @param y2 the second y coordinate of the first segment
245 * @param x3 the first x coordinate of the second segment
246 * @param y3 the first y coordinate of the second segment
247 * @param x4 the second x coordinate of the second segment
248 * @param y4 the second y coordinate of the second segment
249 * @return true if the segments intersect
251 public static boolean linesIntersect(double x1, double y1,
252 double x2, double y2,
253 double x3, double y3,
254 double x4, double y4)
256 double beta = (((y1 - y3) * (x4 - x3) + (x1 - x3) * (y4 - y3))
257 / ((y2 - y1) * (x4 - x3) + (x2 - x1) * (y4 - y3)));
258 if (beta < 0.0 || beta > 1.0)
260 double alpha = (x1 + beta * (x2 - x1) - x3) / (x4 - x3);
261 return alpha >= 0.0 && alpha <= 1.0;
265 * Test if this line intersects the line given by (x1,y1)->(x2,y2).
267 * @param x1 the first x coordinate of the other segment
268 * @param y1 the first y coordinate of the other segment
269 * @param x2 the second x coordinate of the other segment
270 * @param y2 the second y coordinate of the other segment
271 * @return true if the segments intersect
272 * @see #linesIntersect(double, double, double, double,
273 * double, double, double, double)
275 public boolean intersectsLine(double x1, double y1, double x2, double y2)
277 return linesIntersect(getX1(), getY1(), getX2(), getY2(),
282 * Test if this line intersects the given line.
284 * @param l the other segment
285 * @return true if the segments intersect
286 * @throws NullPointerException if l is null
287 * @see #linesIntersect(double, double, double, double,
288 * double, double, double, double)
290 public boolean intersectsLine(Line2D l)
292 return linesIntersect(getX1(), getY1(), getX2(), getY2(),
293 l.getX1(), l.getY1(), l.getX2(), l.getY2());
297 * Measures the square of the shortest distance from the reference point
298 * to a point on the line segment. If the point is on the segment, the
301 * @param x1 the first x coordinate of the segment
302 * @param y1 the first y coordinate of the segment
303 * @param x2 the second x coordinate of the segment
304 * @param y2 the second y coordinate of the segment
305 * @param px the x coordinate of the point
306 * @param py the y coordinate of the point
307 * @return the square of the distance from the point to the segment
308 * @see #ptSegDist(double, double, double, double, double, double)
309 * @see #ptLineDistSq(double, double, double, double, double, double)
311 public static double ptSegDistSq(double x1, double y1, double x2, double y2,
312 double px, double py)
314 double pd2 = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
319 // Points are coincident.
325 double u = ((px - x1) * (x2 - x1) + (py - y1) * (y2 - y1)) / pd2;
340 x = x1 + u * (x2 - x1);
341 y = y1 + u * (y2 - y1);
345 return (x - px) * (x - px) + (y - py) * (y - py);
349 * Measures the shortest distance from the reference point to a point on
350 * the line segment. If the point is on the segment, the result will be 0.
352 * @param x1 the first x coordinate of the segment
353 * @param y1 the first y coordinate of the segment
354 * @param x2 the second x coordinate of the segment
355 * @param y2 the second y coordinate of the segment
356 * @param px the x coordinate of the point
357 * @param py the y coordinate of the point
358 * @return the distance from the point to the segment
359 * @see #ptSegDistSq(double, double, double, double, double, double)
360 * @see #ptLineDist(double, double, double, double, double, double)
362 public static double ptSegDist(double x1, double y1, double x2, double y2,
363 double px, double py)
365 return Math.sqrt(ptSegDistSq(x1, y1, x2, y2, px, py));
369 * Measures the square of the shortest distance from the reference point
370 * to a point on this line segment. If the point is on the segment, the
373 * @param px the x coordinate of the point
374 * @param py the y coordinate of the point
375 * @return the square of the distance from the point to the segment
376 * @see #ptSegDistSq(double, double, double, double, double, double)
378 public double ptSegDistSq(double px, double py)
380 return ptSegDistSq(getX1(), getY1(), getX2(), getY2(), px, py);
384 * Measures the square of the shortest distance from the reference point
385 * to a point on this line segment. If the point is on the segment, the
389 * @return the square of the distance from the point to the segment
390 * @throws NullPointerException if p is null
391 * @see #ptSegDistSq(double, double, double, double, double, double)
393 public double ptSegDistSq(Point2D p)
395 return ptSegDistSq(getX1(), getY1(), getX2(), getY2(), p.getX(), p.getY());
399 * Measures the shortest distance from the reference point to a point on
400 * this line segment. If the point is on the segment, the result will be 0.
402 * @param px the x coordinate of the point
403 * @param py the y coordinate of the point
404 * @return the distance from the point to the segment
405 * @see #ptSegDist(double, double, double, double, double, double)
407 public double ptSegDist(double px, double py)
409 return ptSegDist(getX1(), getY1(), getX2(), getY2(), px, py);
413 * Measures the shortest distance from the reference point to a point on
414 * this line segment. If the point is on the segment, the result will be 0.
417 * @return the distance from the point to the segment
418 * @throws NullPointerException if p is null
419 * @see #ptSegDist(double, double, double, double, double, double)
421 public double ptSegDist(Point2D p)
423 return ptSegDist(getX1(), getY1(), getX2(), getY2(), p.getX(), p.getY());
427 * Measures the square of the shortest distance from the reference point
428 * to a point on the infinite line extended from the segment. If the point
429 * is on the segment, the result will be 0. If the segment is length 0,
430 * the distance is to the common endpoint.
432 * @param x1 the first x coordinate of the segment
433 * @param y1 the first y coordinate of the segment
434 * @param x2 the second x coordinate of the segment
435 * @param y2 the second y coordinate of the segment
436 * @param px the x coordinate of the point
437 * @param py the y coordinate of the point
438 * @return the square of the distance from the point to the extended line
439 * @see #ptLineDist(double, double, double, double, double, double)
440 * @see #ptSegDistSq(double, double, double, double, double, double)
442 public static double ptLineDistSq(double x1, double y1, double x2, double y2,
443 double px, double py)
445 double pd2 = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
450 // Points are coincident.
456 double u = ((px - x1) * (x2 - x1) + (py - y1) * (y2 - y1)) / pd2;
457 x = x1 + u * (x2 - x1);
458 y = y1 + u * (y2 - y1);
461 return (x - px) * (x - px) + (y - py) * (y - py);
465 * Measures the shortest distance from the reference point to a point on
466 * the infinite line extended from the segment. If the point is on the
467 * segment, the result will be 0. If the segment is length 0, the distance
468 * is to the common endpoint.
470 * @param x1 the first x coordinate of the segment
471 * @param y1 the first y coordinate of the segment
472 * @param x2 the second x coordinate of the segment
473 * @param y2 the second y coordinate of the segment
474 * @param px the x coordinate of the point
475 * @param py the y coordinate of the point
476 * @return the distance from the point to the extended line
477 * @see #ptLineDistSq(double, double, double, double, double, double)
478 * @see #ptSegDist(double, double, double, double, double, double)
480 public static double ptLineDist(double x1, double y1,
481 double x2, double y2,
482 double px, double py)
484 return Math.sqrt(ptLineDistSq(x1, y1, x2, y2, px, py));
488 * Measures the square of the shortest distance from the reference point
489 * to a point on the infinite line extended from this segment. If the point
490 * is on the segment, the result will be 0. If the segment is length 0,
491 * the distance is to the common endpoint.
493 * @param px the x coordinate of the point
494 * @param py the y coordinate of the point
495 * @return the square of the distance from the point to the extended line
496 * @see #ptLineDistSq(double, double, double, double, double, double)
498 public double ptLineDistSq(double px, double py)
500 return ptLineDistSq(getX1(), getY1(), getX2(), getY2(), px, py);
504 * Measures the square of the shortest distance from the reference point
505 * to a point on the infinite line extended from this segment. If the point
506 * is on the segment, the result will be 0. If the segment is length 0,
507 * the distance is to the common endpoint.
510 * @return the square of the distance from the point to the extended line
511 * @throws NullPointerException if p is null
512 * @see #ptLineDistSq(double, double, double, double, double, double)
514 public double ptLineDistSq(Point2D p)
516 return ptLineDistSq(getX1(), getY1(), getX2(), getY2(),
521 * Measures the shortest distance from the reference point to a point on
522 * the infinite line extended from this segment. If the point is on the
523 * segment, the result will be 0. If the segment is length 0, the distance
524 * is to the common endpoint.
526 * @param px the x coordinate of the point
527 * @param py the y coordinate of the point
528 * @return the distance from the point to the extended line
529 * @see #ptLineDist(double, double, double, double, double, double)
531 public double ptLineDist(double px, double py)
533 return ptLineDist(getX1(), getY1(), getX2(), getY2(), px, py);
537 * Measures the shortest distance from the reference point to a point on
538 * the infinite line extended from this segment. If the point is on the
539 * segment, the result will be 0. If the segment is length 0, the distance
540 * is to the common endpoint.
543 * @return the distance from the point to the extended line
544 * @throws NullPointerException if p is null
545 * @see #ptLineDist(double, double, double, double, double, double)
547 public double ptLineDist(Point2D p)
549 return ptLineDist(getX1(), getY1(), getX2(), getY2(), p.getX(), p.getY());
553 * Test if a point is contained inside the line. Since a line has no area,
554 * this returns false.
556 * @param x the x coordinate
557 * @param y the y coordinate
558 * @return false; the line does not contain points
560 public boolean contains(double x, double y)
566 * Test if a point is contained inside the line. Since a line has no area,
567 * this returns false.
570 * @return false; the line does not contain points
572 public boolean contains(Point2D p)
578 * Tests if this line intersects the interior of the specified rectangle.
580 * @param x the x coordinate of the rectangle
581 * @param y the y coordinate of the rectangle
582 * @param w the width of the rectangle
583 * @param h the height of the rectangle
584 * @return true if the line intersects the rectangle
586 public boolean intersects(double x, double y, double w, double h)
588 if (w <= 0 || h <= 0)
595 if (x1 >= x && x1 <= x + w && y1 >= y && y1 <= y + h)
597 if (x2 >= x && x2 <= x + w && y2 >= y && y2 <= y + h)
603 return (linesIntersect(x1, y1, x2, y2, x, y, x, y3)
604 || linesIntersect(x1, y1, x2, y2, x, y3, x3, y3)
605 || linesIntersect(x1, y1, x2, y2, x3, y3, x3, y)
606 || linesIntersect(x1, y1, x2, y2, x3, y, x, y));
610 * Tests if this line intersects the interior of the specified rectangle.
612 * @param r the rectangle
613 * @return true if the line intersects the rectangle
614 * @throws NullPointerException if r is null
616 public boolean intersects(Rectangle2D r)
618 return intersects(r.getX(), r.getY(), r.getWidth(), r.getHeight());
622 * Tests if the line contains a rectangle. Since lines have no area, this
623 * always returns false.
625 * @param x the x coordinate of the rectangle
626 * @param y the y coordinate of the rectangle
627 * @param w the width of the rectangle
628 * @param h the height of the rectangle
629 * @return false; the line does not contain points
631 public boolean contains(double x, double y, double w, double h)
637 * Tests if the line contains a rectangle. Since lines have no area, this
638 * always returns false.
640 * @param r the rectangle
641 * @return false; the line does not contain points
643 public boolean contains(Rectangle2D r)
649 * Gets a bounding box (not necessarily minimal) for this line.
651 * @return the integer bounding box
652 * @see #getBounds2D()
654 public Rectangle getBounds()
656 return getBounds2D().getBounds();
660 * Return a path iterator, possibly applying a transform on the result. This
661 * iterator is not threadsafe.
663 * @param at the transform, or null
664 * @return a new path iterator
666 public PathIterator getPathIterator(final AffineTransform at)
668 return new PathIterator()
670 /** Current coordinate. */
673 public int getWindingRule()
675 return WIND_NON_ZERO;
678 public boolean isDone()
688 public int currentSegment(float[] coords)
694 coords[0] = (float) getX1();
695 coords[1] = (float) getY1();
699 coords[0] = (float) getX2();
700 coords[1] = (float) getY2();
704 throw new NoSuchElementException("line iterator out of bounds");
707 at.transform(coords, 0, coords, 0, 1);
711 public int currentSegment(double[] coords)
727 throw new NoSuchElementException("line iterator out of bounds");
730 at.transform(coords, 0, coords, 0, 1);
737 * Return a flat path iterator, possibly applying a transform on the result.
738 * This iterator is not threadsafe.
740 * @param at the transform, or null
741 * @param flatness ignored, since lines are already flat
742 * @return a new path iterator
743 * @see #getPathIterator(AffineTransform)
745 public PathIterator getPathIterator(AffineTransform at, double flatness)
747 return getPathIterator(at);
751 * Create a new line of the same run-time type with the same contents as
756 * @exception OutOfMemoryError If there is not enough memory available.
760 public Object clone()
764 return super.clone();
766 catch (CloneNotSupportedException e)
768 throw (Error) new InternalError().initCause(e); // Impossible
773 * This class defines a point in <code>double</code> precision.
775 * @author Eric Blake <ebb9@email.byu.edu>
777 * @status updated to 1.4
779 public static class Double extends Line2D
781 /** The x coordinate of the first point. */
784 /** The y coordinate of the first point. */
787 /** The x coordinate of the second point. */
790 /** The y coordinate of the second point. */
794 * Construct the line segment (0,0)->(0,0).
801 * Construct the line segment with the specified points.
803 * @param x1 the x coordinate of the first point
804 * @param y1 the y coordinate of the first point
805 * @param x2 the x coordinate of the second point
806 * @param y2 the y coordinate of the second point
808 public Double(double x1, double y1, double x2, double y2)
817 * Construct the line segment with the specified points.
819 * @param p1 the first point
820 * @param p2 the second point
821 * @throws NullPointerException if either point is null
823 public Double(Point2D p1, Point2D p2)
832 * Return the x coordinate of the first point.
834 * @return the value of x1
836 public double getX1()
842 * Return the y coordinate of the first point.
844 * @return the value of y1
846 public double getY1()
852 * Return the first point.
854 * @return the point (x1,y1)
856 public Point2D getP1()
858 return new Point2D.Double(x1, y1);
862 * Return the x coordinate of the second point.
864 * @return the value of x2
866 public double getX2()
872 * Return the y coordinate of the second point.
874 * @return the value of y2
876 public double getY2()
882 * Return the second point.
884 * @return the point (x2,y2)
886 public Point2D getP2()
888 return new Point2D.Double(x2, y2);
892 * Set this line to the given points.
894 * @param x1 the new x coordinate of the first point
895 * @param y1 the new y coordinate of the first point
896 * @param x2 the new x coordinate of the second point
897 * @param y2 the new y coordinate of the second point
899 public void setLine(double x1, double y1, double x2, double y2)
908 * Return the exact bounds of this line segment.
910 * @return the bounding box
912 public Rectangle2D getBounds2D()
914 double x = Math.min(x1, x2);
915 double y = Math.min(y1, y2);
916 double w = Math.abs(x1 - x2);
917 double h = Math.abs(y1 - y2);
918 return new Rectangle2D.Double(x, y, w, h);
923 * This class defines a point in <code>float</code> precision.
925 * @author Eric Blake <ebb9@email.byu.edu>
927 * @status updated to 1.4
929 public static class Float extends Line2D
931 /** The x coordinate of the first point. */
934 /** The y coordinate of the first point. */
937 /** The x coordinate of the second point. */
940 /** The y coordinate of the second point. */
944 * Construct the line segment (0,0)->(0,0).
951 * Construct the line segment with the specified points.
953 * @param x1 the x coordinate of the first point
954 * @param y1 the y coordinate of the first point
955 * @param x2 the x coordinate of the second point
956 * @param y2 the y coordinate of the second point
958 public Float(float x1, float y1, float x2, float y2)
967 * Construct the line segment with the specified points.
969 * @param p1 the first point
970 * @param p2 the second point
971 * @throws NullPointerException if either point is null
973 public Float(Point2D p1, Point2D p2)
975 x1 = (float) p1.getX();
976 y1 = (float) p1.getY();
977 x2 = (float) p2.getX();
978 y2 = (float) p2.getY();
982 * Return the x coordinate of the first point.
984 * @return the value of x1
986 public double getX1()
992 * Return the y coordinate of the first point.
994 * @return the value of y1
996 public double getY1()
1002 * Return the first point.
1004 * @return the point (x1,y1)
1006 public Point2D getP1()
1008 return new Point2D.Float(x1, y1);
1012 * Return the x coordinate of the second point.
1014 * @return the value of x2
1016 public double getX2()
1022 * Return the y coordinate of the second point.
1024 * @return the value of y2
1026 public double getY2()
1032 * Return the second point.
1034 * @return the point (x2,y2)
1036 public Point2D getP2()
1038 return new Point2D.Float(x2, y2);
1042 * Set this line to the given points.
1044 * @param x1 the new x coordinate of the first point
1045 * @param y1 the new y coordinate of the first point
1046 * @param x2 the new x coordinate of the second point
1047 * @param y2 the new y coordinate of the second point
1049 public void setLine(double x1, double y1, double x2, double y2)
1051 this.x1 = (float) x1;
1052 this.y1 = (float) y1;
1053 this.x2 = (float) x2;
1054 this.y2 = (float) y2;
1058 * Set this line to the given points.
1060 * @param x1 the new x coordinate of the first point
1061 * @param y1 the new y coordinate of the first point
1062 * @param x2 the new x coordinate of the second point
1063 * @param y2 the new y coordinate of the second point
1065 public void setLine(float x1, float y1, float x2, float y2)
1074 * Return the exact bounds of this line segment.
1076 * @return the bounding box
1078 public Rectangle2D getBounds2D()
1080 float x = Math.min(x1, x2);
1081 float y = Math.min(y1, y2);
1082 float w = Math.abs(x1 - x2);
1083 float h = Math.abs(y1 - y2);
1084 return new Rectangle2D.Float(x, y, w, h);