1. /*
  2. * @(#)Rectangle2D.java 1.19 01/11/29
  3. *
  4. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.awt.geom;
  8. /**
  9. * The <code>Rectangle2D</code> class describes a rectangle
  10. * defined by a location (x, y) and dimension
  11. * (w x h).
  12. * <p>
  13. * This class is only the abstract superclass for all objects that
  14. * store a 2D rectangle.
  15. * The actual storage representation of the coordinates is left to
  16. * the subclass.
  17. *
  18. * @version 10 Feb 1997
  19. * @author Jim Graham
  20. */
  21. public abstract class Rectangle2D extends RectangularShape {
  22. /**
  23. * The bitmask that indicates that a point lies to the left of
  24. * this <code>Rectangle2D</code>.
  25. * @since JDK1.2
  26. */
  27. public static final int OUT_LEFT = 1;
  28. /**
  29. * The bitmask that indicates that a point lies above
  30. * this <code>Rectangle2D</code>.
  31. * @since JDK1.2
  32. */
  33. public static final int OUT_TOP = 2;
  34. /**
  35. * The bitmask that indicates that a point lies to the right of
  36. * this <code>Rectangle2D</code>.
  37. * @since JDK1.2
  38. */
  39. public static final int OUT_RIGHT = 4;
  40. /**
  41. * The bitmask that indicates that a point lies below
  42. * this <code>Rectangle2D</code>.
  43. * @since JDK1.2
  44. */
  45. public static final int OUT_BOTTOM = 8;
  46. /**
  47. * The <code>Float</code> class defines a rectangle specified in float
  48. * coordinates.
  49. * @since JDK1.2
  50. */
  51. public static class Float extends Rectangle2D {
  52. /**
  53. * The x coordinate of this <code>Rectangle2D</code>.
  54. * @since JDK1.2
  55. */
  56. public float x;
  57. /**
  58. * The y coordinate of this <code>Rectangle2D</code>.
  59. * @since JDK1.2
  60. */
  61. public float y;
  62. /**
  63. * The width of this <code>Rectangle2D</code>.
  64. * @since JDK1.2
  65. */
  66. public float width;
  67. /**
  68. * The height of this <code>Rectangle2D</code>.
  69. * @since JDK1.2
  70. */
  71. public float height;
  72. /**
  73. * Constructs a new <code>Rectangle2D</code>, initialized to
  74. * location (0.0, 0.0) and size (0.0, 0.0).
  75. * @since JDK1.2
  76. */
  77. public Float() {
  78. }
  79. /**
  80. * Constructs and initializes a <code>Rectangle2D</code>
  81. * from the specified float coordinates.
  82. * @param x, y the coordinates of the
  83. * upper left corner of the newly constructed
  84. * <code>Rectangle2D</code>
  85. * @param w the width of the newly constructed
  86. * <code>Rectangle2D</code>
  87. * @param h the height of the newly constructed
  88. * <code>Rectangle2D</code>
  89. * @since JDK1.2
  90. */
  91. public Float(float x, float y, float w, float h) {
  92. setRect(x, y, w, h);
  93. }
  94. /**
  95. * Returns the X coordinate of this <code>Rectangle2D</code>
  96. * in double precision.
  97. * @return the X coordinate of this <code>Rectangle2D</code>.
  98. * @since JDK1.2
  99. */
  100. public double getX() {
  101. return (double) x;
  102. }
  103. /**
  104. * Returns the Y coordinate of this <code>Rectangle2D</code>
  105. * in double precision.
  106. * @return the Y coordinate of this <code>Rectangle2D</code>.
  107. * @since JDK1.2
  108. */
  109. public double getY() {
  110. return (double) y;
  111. }
  112. /**
  113. * Returns the width of this <code>Rectangle2D</code>
  114. * in double precision.
  115. * @return the width of this <code>Rectangle2D</code>.
  116. * @since JDK1.2
  117. */
  118. public double getWidth() {
  119. return (double) width;
  120. }
  121. /**
  122. * Returns the height of this <code>Rectangle2D</code>
  123. * in double precision.
  124. * @return the height of this <code>Rectangle2D</code>.
  125. * @since JDK1.2
  126. */
  127. public double getHeight() {
  128. return (double) height;
  129. }
  130. /**
  131. * Determines whether or not this <code>Rectangle2D</code>
  132. * is empty.
  133. * @return <code>true</code> if this <code>Rectangle2D</code>
  134. * is empty; <code>false</code> otherwise.
  135. * @since JDK1.2
  136. */
  137. public boolean isEmpty() {
  138. return (width <= 0.0f) || (height <= 0.0f);
  139. }
  140. /**
  141. * Sets the location and size of this <code>Rectangle2D</code>
  142. * to the specified float values.
  143. * @param x, y the coordinates to which to set the
  144. * location of the upper left corner of this
  145. * <code>Rectangle2D</code>
  146. * @param w the value to use to set the width of this
  147. * <code>Rectangle2D</code>
  148. * @param h the value to use to set the height of this
  149. * <code>Rectangle2D</code>
  150. * @since JDK1.2
  151. */
  152. public void setRect(float x, float y, float w, float h) {
  153. this.x = x;
  154. this.y = y;
  155. this.width = w;
  156. this.height = h;
  157. }
  158. /**
  159. * Sets the location and size of this <code>Rectangle2D</code>
  160. * to the specified double values.
  161. * @param x, y the coordinates to which to set the
  162. * location of the upper left corner of this
  163. * <code>Rectangle2D</code>
  164. * @param w the value to use to set the width of this
  165. * <code>Rectangle2D</code>
  166. * @param h the value to use to set the height of this
  167. * <code>Rectangle2D</code>
  168. * @since JDK1.2
  169. */
  170. public void setRect(double x, double y, double w, double h) {
  171. this.x = (float) x;
  172. this.y = (float) y;
  173. this.width = (float) w;
  174. this.height = (float) h;
  175. }
  176. /**
  177. * Sets this <code>Rectangle2D</code> to be the same as the
  178. * specified <code>Rectangle2D</code>.
  179. * @param r the specified <code>Rectangle2D</code>
  180. * @since JDK1.2
  181. */
  182. public void setRect(Rectangle2D r) {
  183. this.x = (float) r.getX();
  184. this.y = (float) r.getY();
  185. this.width = (float) r.getWidth();
  186. this.height = (float) r.getHeight();
  187. }
  188. /**
  189. * Determines where the specified float coordinates lie with respect
  190. * to this <code>Rectangle2D</code>.
  191. * This method computes a binary OR of the appropriate mask values
  192. * indicating, for each side of this <code>Rectangle2D</code>,
  193. * whether or not the specified coordinates are on the same side
  194. * of the edge as the rest of this <code>Rectangle2D</code>.
  195. * @param x, y the specified coordinates
  196. * @return the logical OR of all appropriate out codes.
  197. * @see Rectangle2D#OUT_LEFT
  198. * @see Rectangle2D#OUT_TOP
  199. * @see Rectangle2D#OUT_RIGHT
  200. * @see Rectangle2D#OUT_BOTTOM
  201. * @since JDK1.2
  202. */
  203. public int outcode(double x, double y) {
  204. int out = 0;
  205. if (this.width <= 0) {
  206. out |= OUT_LEFT | OUT_RIGHT;
  207. } else if (x < this.x) {
  208. out |= OUT_LEFT;
  209. } else if (x > this.x + this.width) {
  210. out |= OUT_RIGHT;
  211. }
  212. if (this.height <= 0) {
  213. out |= OUT_TOP | OUT_BOTTOM;
  214. } else if (y < this.y) {
  215. out |= OUT_TOP;
  216. } else if (y > this.y + this.height) {
  217. out |= OUT_BOTTOM;
  218. }
  219. return out;
  220. }
  221. /**
  222. * Returns the high precision bounding box of this
  223. * <code>Rectangle2D</code>.
  224. * @return the bounding box of this <code>Rectangle2D</code>.
  225. * @since JDK1.2
  226. */
  227. public Rectangle2D getBounds2D() {
  228. return new Float(x, y, width, height);
  229. }
  230. /**
  231. * Returns a new <code>Rectangle2D</code> object
  232. * representing the intersection of
  233. * this <code>Rectangle2D</code> with the specified
  234. * <code>Rectangle2D</code>.
  235. * @param r the <code>Rectangle2D</code> that is
  236. * intersected with this <code>Rectangle2D</code>
  237. * @return the largest <code>Rectangle2D</code>
  238. * contained in both the specified
  239. * <code>Rectangle2D</code> and in this
  240. * <code>Rectangle2D</code>.
  241. * @since JDK1.2
  242. */
  243. public Rectangle2D createIntersection(Rectangle2D r) {
  244. Rectangle2D dest;
  245. if (r instanceof Float) {
  246. dest = new Rectangle2D.Float();
  247. } else {
  248. dest = new Rectangle2D.Double();
  249. }
  250. Rectangle2D.intersect(this, r, dest);
  251. return dest;
  252. }
  253. /**
  254. * Returns a new <code>Rectangle2D</code> object
  255. * representing the union of this <code>Rectangle2D</code>
  256. * with the specified <code>Rectangle2D</code>.
  257. * @param r the <code>Rectangle2D</code> to be combined with
  258. * this <code>Rectangle2D</code>
  259. * @return the smallest <code>Rectangle2D</code> containing
  260. * both the specified <code>Rectangle2D</code> and this
  261. * <code>Rectangle2D</code>.
  262. * @since JDK1.2
  263. */
  264. public Rectangle2D createUnion(Rectangle2D r) {
  265. Rectangle2D dest;
  266. if (r instanceof Float) {
  267. dest = new Rectangle2D.Float();
  268. } else {
  269. dest = new Rectangle2D.Double();
  270. }
  271. Rectangle2D.union(this, r, dest);
  272. return dest;
  273. }
  274. /**
  275. * Returns the <code>String</code> representation of this
  276. * <code>Rectangle2D</code>.
  277. * @return a <code>String</code> representing this
  278. * <code>Rectangle2D</code>.
  279. * @since JDK1.2
  280. */
  281. public String toString() {
  282. return getClass().getName()
  283. + "[x=" + x +
  284. ",y=" + y +
  285. ",w=" + width +
  286. ",h=" + height + "]";
  287. }
  288. }
  289. /**
  290. * The <code>Double</code> class defines a rectangle specified in
  291. * double coordinates.
  292. * @since JDK1.2
  293. */
  294. public static class Double extends Rectangle2D {
  295. /**
  296. * The x coordinate of this <code>Rectangle2D</code>.
  297. * @since JDK1.2
  298. */
  299. public double x;
  300. /**
  301. * The y coordinate of this <code>Rectangle2D</code>.
  302. * @since JDK1.2
  303. */
  304. public double y;
  305. /**
  306. * The width of this <code>Rectangle2D</code>.
  307. * @since JDK1.2
  308. */
  309. public double width;
  310. /**
  311. * The height of this <code>Rectangle2D</code>.
  312. * @since JDK1.2
  313. */
  314. public double height;
  315. /**
  316. * Constructs a new <code>Rectangle2D</code>, initialized to
  317. * location (0, 0) and size (0, 0).
  318. * @since JDK1.2
  319. */
  320. public Double() {
  321. }
  322. /**
  323. * Constructs and initializes a <code>Rectangle2D</code>
  324. * from the specified double coordinates.
  325. * @param x, y the coordinates of the upper left corner
  326. * of the newly constructed <code>Rectangle2D</code>
  327. * @param width the width of the
  328. * newly constructed <code>Rectangle2D</code>
  329. * @param height the height of the
  330. * newly constructed <code>Rectangle2D</code>
  331. * @since JDK1.2
  332. */
  333. public Double(double x, double y, double w, double h) {
  334. setRect(x, y, w, h);
  335. }
  336. /**
  337. * Returns the X coordinate of this <code>Rectangle2D</code> in
  338. * double precision.
  339. * @return the X coordinate of this <code>Rectangle2D</code>.
  340. * @since JDK1.2
  341. */
  342. public double getX() {
  343. return x;
  344. }
  345. /**
  346. * Returns the Y coordinate of this <code>Rectangle2D</code> in
  347. * double precision.
  348. * @return the Y coordinate of this <code>Rectangle2D</code>.
  349. * @since JDK1.2
  350. */
  351. public double getY() {
  352. return y;
  353. }
  354. /**
  355. * Returns the width of this <code>Rectangle2D</code> in
  356. * double precision.
  357. * @return the width of this <code>Rectangle2D</code>.
  358. * @since JDK1.2
  359. */
  360. public double getWidth() {
  361. return width;
  362. }
  363. /**
  364. * Returns the height of this <code>Rectangle2D</code> in
  365. * double precision.
  366. * @return the height of this <code>Rectangle2D</code>.
  367. * @since JDK1.2
  368. */
  369. public double getHeight() {
  370. return height;
  371. }
  372. /**
  373. * Determines whether or not this <code>Rectangle2D</code>
  374. * is empty.
  375. * @return <code>true</code> if this <code>Rectangle2D</code>
  376. * is empty; <code>false</code> otherwise.
  377. * @since JDK1.2
  378. */
  379. public boolean isEmpty() {
  380. return (width <= 0.0) || (height <= 0.0);
  381. }
  382. /**
  383. * Sets the location and size of this <code>Rectangle2D</code>
  384. * to the specified double values.
  385. * @param x, y the coordinates to which to set the
  386. * upper left corner of this <code>Rectangle2D</code>
  387. * @param w the value to use to set the width of this
  388. * <code>Rectangle2D</code>
  389. * @param h the value to use to set the height of this
  390. * <code>Rectangle2D</code>
  391. * @since JDK1.2
  392. */
  393. public void setRect(double x, double y, double w, double h) {
  394. this.x = x;
  395. this.y = y;
  396. this.width = w;
  397. this.height = h;
  398. }
  399. /**
  400. * Sets this <code>Rectangle2D</code> to be the same as the
  401. * specified <code>Rectangle2D</code>.
  402. * @param r the specified <code>Rectangle2D</code>
  403. * @since JDK1.2
  404. */
  405. public void setRect(Rectangle2D r) {
  406. this.x = r.getX();
  407. this.y = r.getY();
  408. this.width = r.getWidth();
  409. this.height = r.getHeight();
  410. }
  411. /**
  412. * Determines where the specified double coordinates lie with respect
  413. * to this <code>Rectangle2D</code>.
  414. * This method computes a binary OR of the appropriate mask values
  415. * indicating, for each side of this <code>Rectangle2D</code>,
  416. * whether or not the specified coordinates are on the same side
  417. * of the edge as the rest of this <code>Rectangle2D</code>.
  418. * @param x, y the specified coordinates
  419. * @return the logical OR of all appropriate out codes.
  420. * @see Rectangle2D#OUT_LEFT
  421. * @see Rectangle2D#OUT_TOP
  422. * @see Rectangle2D#OUT_RIGHT
  423. * @see Rectangle2D#OUT_BOTTOM
  424. * @since JDK1.2
  425. */
  426. public int outcode(double x, double y) {
  427. int out = 0;
  428. if (this.width <= 0) {
  429. out |= OUT_LEFT | OUT_RIGHT;
  430. } else if (x < this.x) {
  431. out |= OUT_LEFT;
  432. } else if (x > this.x + this.width) {
  433. out |= OUT_RIGHT;
  434. }
  435. if (this.height <= 0) {
  436. out |= OUT_TOP | OUT_BOTTOM;
  437. } else if (y < this.y) {
  438. out |= OUT_TOP;
  439. } else if (y > this.y + this.height) {
  440. out |= OUT_BOTTOM;
  441. }
  442. return out;
  443. }
  444. /**
  445. * Returns the high precision bounding box of this
  446. * <code>Rectangle2D</code>.
  447. * @return the bounding box of this <code>Rectangle2D</code>.
  448. * @since JDK1.2
  449. */
  450. public Rectangle2D getBounds2D() {
  451. return new Double(x, y, width, height);
  452. }
  453. /**
  454. * Returns a new <code>Rectangle2D</code> object representing
  455. * the intersection of this <code>Rectangle2D</code> with the
  456. * specified <code>Rectangle2D</code>.
  457. * @param r the <code>Rectangle2D</code> to be intersected
  458. * with this <code>Rectangle2D</code>
  459. * @return the largest <code>Rectangle2D</code> contained in
  460. * both the specified <code>Rectangle2D</code> and in this
  461. * <code>Rectangle2D</code>.
  462. * @since JDK1.2
  463. */
  464. public Rectangle2D createIntersection(Rectangle2D r) {
  465. Rectangle2D dest = new Rectangle2D.Double();
  466. Rectangle2D.intersect(this, r, dest);
  467. return dest;
  468. }
  469. /**
  470. * Returns a new <code>Rectangle2D</code> object representing
  471. * the union of this <code>Rectangle2D</code> with the
  472. * specified <code>Rectangle2D</code>.
  473. * @param r the <code>Rectangle2D</code> to be combined with
  474. * this <code>Rectangle2D</code>
  475. * @return the smallest <code>Rectangle2D</code> containing
  476. * both the specified <code>Rectangle2D</code> and this
  477. * <code>Rectangle2D</code>.
  478. * @since JDK1.2
  479. */
  480. public Rectangle2D createUnion(Rectangle2D r) {
  481. Rectangle2D dest = new Rectangle2D.Double();
  482. Rectangle2D.union(this, r, dest);
  483. return dest;
  484. }
  485. /**
  486. * Returns the <code>String</code> representation of this
  487. * <code>Rectangle2D</code>.
  488. * @return a <code>String</code> representing this
  489. * <code>Rectangle2D</code>.
  490. * @since JDK1.2
  491. */
  492. public String toString() {
  493. return getClass().getName()
  494. + "[x=" + x +
  495. ",y=" + y +
  496. ",w=" + width +
  497. ",h=" + height + "]";
  498. }
  499. }
  500. /**
  501. * This is an abstract class that cannot be instantiated directly.
  502. * Type-specific implementation subclasses are available for
  503. * instantiation and provide a number of formats for storing
  504. * the information necessary to satisfy the various accessor
  505. * methods below.
  506. *
  507. * @see java.awt.geom.Rectangle2D.Float
  508. * @see java.awt.geom.Rectangle2D.Double
  509. * @see java.awt.Rectangle
  510. */
  511. protected Rectangle2D() {
  512. }
  513. /**
  514. * Sets the location and size of this <code>Rectangle2D</code>
  515. * to the specified double values.
  516. * @param x, y the coordinates to which to set the
  517. * location of the upper left corner of this
  518. * <code>Rectangle2D</code>
  519. * @param w the value to use to set the width of this
  520. * <code>Rectangle2D</code>
  521. * @param h the value to use to set the height of this
  522. * <code>Rectangle2D</code>
  523. * @since JDK1.2
  524. */
  525. public abstract void setRect(double x, double y, double w, double h);
  526. /**
  527. * Sets this <code>Rectangle2D</code> to be the same as the specified
  528. * <code>Rectangle2D</code>.
  529. * @param r the specified <code>Rectangle2D</code>
  530. * @since JDK1.2
  531. */
  532. public void setRect(Rectangle2D r) {
  533. setRect(r.getX(), r.getY(), r.getWidth(), r.getHeight());
  534. }
  535. /**
  536. * Tests if the specified line segment intersects the interior of this
  537. * <code>Rectangle2D</code>.
  538. * @param x1, y1 the first endpoint of the specified
  539. * line segment
  540. * @param x2, y2 the second endpoint of the specified
  541. * line segment
  542. * @return <code>true</code> if the specified line segment intersects
  543. * the interior of this <code>Rectangle2D</code> <code>false</code>
  544. * otherwise.
  545. * @since JDK1.2
  546. */
  547. public boolean intersectsLine(double x1, double y1, double x2, double y2) {
  548. int out1 = outcode(x1, y1);
  549. int out2 = outcode(x2, y2);
  550. while (true) {
  551. if ((out1 & out2) != 0) {
  552. return false;
  553. }
  554. if (out1 == 0) {
  555. if (out2 == 0) {
  556. // Both are inside, line intersects
  557. return true;
  558. }
  559. double f = x1;
  560. x1 = x2;
  561. x2 = f;
  562. f = y1;
  563. y1 = y2;
  564. y2 = f;
  565. int out = out1;
  566. out1 = out2;
  567. out2 = out;
  568. }
  569. if ((out1 & (OUT_LEFT | OUT_RIGHT)) != 0) {
  570. double x = getX();
  571. if ((out1 & OUT_RIGHT) != 0) {
  572. x += getWidth();
  573. }
  574. y1 = y1 + (x - x1) * (y2 - y1) / (x2 - x1);
  575. x1 = x;
  576. } else if ((out1 & (OUT_TOP | OUT_BOTTOM)) != 0) {
  577. double y = getY();
  578. if ((out1 & OUT_BOTTOM) != 0) {
  579. y += getHeight();
  580. }
  581. x1 = x1 + (y - y1) * (x2 - x1) / (y2 - y1);
  582. y1 = y;
  583. }
  584. out1 = outcode(x1, y1);
  585. }
  586. }
  587. /**
  588. * Tests if the specified line segment intersects the interior of this
  589. * <code>Rectangle2D</code>.
  590. * @param l the specified {@link Line2D} to test for intersection
  591. * with the interior of this <code>Rectangle2D</code>
  592. * @return <code>true</code> if the specified <code>Line2D</code>
  593. * intersects the interior of this <code>Rectangle2D</code>
  594. * <code>false</code> otherwise.
  595. * @since JDK1.2
  596. */
  597. public boolean intersectsLine(Line2D l) {
  598. return intersectsLine(l.getX1(), l.getY1(), l.getX2(), l.getY2());
  599. }
  600. /**
  601. * Determines where the specified coordinates lie with respect
  602. * to this <code>Rectangle2D</code>.
  603. * This method computes a binary OR of the appropriate mask values
  604. * indicating, for each side of this <code>Rectangle2D</code>,
  605. * whether or not the specified coordinates are on the same side
  606. * of the edge as the rest of this <code>Rectangle2D</code>.
  607. * @param x, y the specified coordinates
  608. * @return the logical OR of all appropriate out codes.
  609. * @see #OUT_LEFT
  610. * @see #OUT_TOP
  611. * @see #OUT_RIGHT
  612. * @see #OUT_BOTTOM
  613. * @since JDK1.2
  614. */
  615. public abstract int outcode(double x, double y);
  616. /**
  617. * Determines where the specified {@link Point2D} lies with
  618. * respect to this <code>Rectangle2D</code>.
  619. * This method computes a binary OR of the appropriate mask values
  620. * indicating, for each side of this <code>Rectangle2D</code>,
  621. * whether or not the specified <code>Point2D</code> is on the same
  622. * side of the edge as the rest of this <code>Rectangle2D</code>.
  623. * @param p the specified <code>Point2D</code>
  624. * @return the logical OR of all appropriate out codes.
  625. * @see #OUT_LEFT
  626. * @see #OUT_TOP
  627. * @see #OUT_RIGHT
  628. * @see #OUT_BOTTOM
  629. * @since JDK1.2
  630. */
  631. public int outcode(Point2D p) {
  632. return outcode(p.getX(), p.getY());
  633. }
  634. /**
  635. * Sets the location and size of the outer bounds of this
  636. * <code>Rectangle2D</code> to the specified rectangular values.
  637. * @param x, y the coordinates to which to set the
  638. * location of the upper left corner of the outer bounds of
  639. * this <code>Rectangle2D</code>
  640. * @param w the value to use to set the width of the outer
  641. * bounds of this <code>Rectangle2D</code>
  642. * @param h the value to use to set the height of the outer
  643. * bounds of this <code>Rectangle2D</code>
  644. * @since JDK1.2
  645. */
  646. public void setFrame(double x, double y, double w, double h) {
  647. setRect(x, y, w, h);
  648. }
  649. /**
  650. * Returns the high precision bounding box of this
  651. * <code>Rectangle2D</code>.
  652. * @return the bounding box of this <code>Rectangle2D</code>.
  653. * @since JDK1.2
  654. */
  655. public Rectangle2D getBounds2D() {
  656. return (Rectangle2D) clone();
  657. }
  658. /**
  659. * Tests if a specified coordinate is inside the boundary of this
  660. * <code>Rectangle2D</code>.
  661. * @param x, y the coordinates to test
  662. * @return <code>true</code> if the specified coordinates are
  663. * inside the boundary of this <code>Rectangle2D</code>
  664. * <code>false</code> otherwise.
  665. * @since JDK1.2
  666. */
  667. public boolean contains(double x, double y) {
  668. double x0 = getX();
  669. double y0 = getY();
  670. return (x >= x0 &&
  671. y >= y0 &&
  672. x < x0 + getWidth() &&
  673. y < y0 + getHeight());
  674. }
  675. /**
  676. * Tests if the interior of this <code>Rectangle2D</code>
  677. * intersects the interior of a specified set of rectangular
  678. * coordinates.
  679. * @param x, y the coordinates of the upper left corner
  680. * of the specified set of rectangular coordinates
  681. * @param w the width of the specified set of rectangular
  682. * coordinates
  683. * @param h the height of the specified set of rectangular
  684. * coordinates
  685. * @return <code>true</code> if this <code>Rectangle2D</code>
  686. * intersects the interior of a specified set of rectangular
  687. * coordinates; <code>false</code> otherwise.
  688. * @since JDK1.2
  689. */
  690. public boolean intersects(double x, double y, double w, double h) {
  691. if (isEmpty() || w <= 0 || h <= 0) {
  692. return false;
  693. }
  694. double x0 = getX();
  695. double y0 = getY();
  696. return (x + w > x0 &&
  697. y + h > y0 &&
  698. x < x0 + getWidth() &&
  699. y < y0 + getHeight());
  700. }
  701. /**
  702. * Tests if the interior of this <code>Rectangle2D</code> entirely
  703. * contains the specified set of rectangular coordinates.
  704. * @param x, y the coordinates of the upper left corner
  705. * of the specified set of rectangular coordinates
  706. * @param w the width of the specified set of rectangular
  707. * coordinates
  708. * @param h the height of the specified set of rectangular
  709. * coordinates
  710. * @return <code>true</code> if this <code>Rectangle2D</code>
  711. * entirely contains specified set of rectangular
  712. * coordinates; <code>false</code> otherwise.
  713. * @since JDK1.2
  714. */
  715. public boolean contains(double x, double y, double w, double h) {
  716. if (isEmpty() || w <= 0 || h <= 0) {
  717. return false;
  718. }
  719. double x0 = getX();
  720. double y0 = getY();
  721. return (x >= x0 &&
  722. y >= y0 &&
  723. (x + w) <= x0 + getWidth() &&
  724. (y + h) <= y0 + getHeight());
  725. }
  726. /**
  727. * Returns a new <code>Rectangle2D</code> object representing the
  728. * intersection of this <code>Rectangle2D</code> with the specified
  729. * <code>Rectangle2D</code>.
  730. * @param r the <code>Rectangle2D</code> to be intersected with
  731. * this <code>Rectangle2D</code>
  732. * @return the largest <code>Rectangle2D</code> contained in both
  733. * the specified <code>Rectangle2D</code> and in this
  734. * <code>Rectangle2D</code>.
  735. * @since JDK1.2
  736. */
  737. public abstract Rectangle2D createIntersection(Rectangle2D r);
  738. /**
  739. * Intersects the pair of specified source <code>Rectangle2D</code>
  740. * objects and puts the result into the specified destination
  741. * <code>Rectangle2D</code> object.
  742. * @param src1 the first of a pair of <code>Rectangle2D</code>
  743. * objects to be intersected with each other
  744. * @param src2 the second of a pair of <code>Rectangle2D</code>
  745. * objects to be intersected with each other
  746. * @param dest the <code>Rectangle2D</code> that holds the
  747. * results of the intersection of <code>src1</code> and
  748. * <code>src2</code>
  749. * @since JDK1.2
  750. */
  751. public static void intersect(Rectangle2D src1,
  752. Rectangle2D src2,
  753. Rectangle2D dest) {
  754. double x1 = Math.max(src1.getMinX(), src2.getMinX());
  755. double y1 = Math.max(src1.getMinY(), src2.getMinY());
  756. double x2 = Math.min(src1.getMaxX(), src2.getMaxX());
  757. double y2 = Math.min(src1.getMaxY(), src2.getMaxY());
  758. dest.setFrameFromDiagonal(x1, y1, x2, y2);
  759. }
  760. /**
  761. * Returns a new <code>Rectangle2D</code> object representing the
  762. * union of this <code>Rectangle2D</code> with the specified
  763. * <code>Rectangle2D</code>.
  764. * @param r the <code>Rectangle2D</code> to be combined with
  765. * this <code>Rectangle2D</code>
  766. * @return the smallest <code>Rectangle2D</code> containing both
  767. * the specified <code>Rectangle2D</code> and this
  768. * <code>Rectangle2D</code>.
  769. * @since JDK1.2
  770. */
  771. public abstract Rectangle2D createUnion(Rectangle2D r);
  772. /**
  773. * Unions the pair of source <code>Rectangle2D</code> objects
  774. * and puts the result into the specified destination
  775. * <code>Rectangle2D</code> object.
  776. * @param src1 the first of a pair of <code>Rectangle2D</code>
  777. * objects to be combined with each other
  778. * @param src2 the second of a pair of <code>Rectangle2D</code>
  779. * objects to be combined with each other
  780. * @param dest the <code>Rectangle2D</code> that holds the
  781. * results of the union of <code>src1</code> and
  782. * <code>src2</code>
  783. * @since JDK1.2
  784. */
  785. public static void union(Rectangle2D src1,
  786. Rectangle2D src2,
  787. Rectangle2D dest) {
  788. double x1 = Math.min(src1.getMinX(), src2.getMinX());
  789. double y1 = Math.min(src1.getMinY(), src2.getMinY());
  790. double x2 = Math.max(src1.getMaxX(), src2.getMaxX());
  791. double y2 = Math.max(src1.getMaxY(), src2.getMaxY());
  792. dest.setFrameFromDiagonal(x1, y1, x2, y2);
  793. }
  794. /**
  795. * Adds a point, specified by the double precision arguments
  796. * <code>newx</code> and <code>newy</code>, to this
  797. * <code>Rectangle2D</code>. The resulting <code>Rectangle2D</code>
  798. * is the smallest <code>Rectangle2D</code> that
  799. * contains both the original <code>Rectangle2D</code> and the
  800. * specified point.
  801. * <p>
  802. * After adding a point, a call to <code>contains</code> with the
  803. * added point as an argument does not necessarily return
  804. * <code>true</code>. The <code>contains</code> method does not
  805. * return <code>true</code> for points on the right or bottom
  806. * edges of a rectangle. Therefore, if the added point falls on
  807. * the left or bottom edge of the enlarged rectangle,
  808. * <code>contains</code> returns <code>false</code> for that point.
  809. * @param newx, newy the coordinates of the new point
  810. * @since JDK1.0
  811. */
  812. public void add(double newx, double newy) {
  813. double x1 = Math.min(getMinX(), newx);
  814. double x2 = Math.max(getMaxX(), newx);
  815. double y1 = Math.min(getMinY(), newy);
  816. double y2 = Math.max(getMaxY(), newy);
  817. setRect(x1, y1, x2 - x1, y2 - y1);
  818. }
  819. /**
  820. * Adds the <code>Point2D</code> object <code>pt</code> to this
  821. * <code>Rectangle2D</code>.
  822. * The resulting <code>Rectangle2D</code> is the smallest
  823. * <code>Rectangle2D</code> that contains both the original
  824. * <code>Rectangle2D</code> and the specified <code>Point2D</code>.
  825. * <p>
  826. * After adding a point, a call to <code>contains</code> with the
  827. * added point as an argument does not necessarily return
  828. * <code>true</code>. The <code>contains</code>
  829. * method does not return <code>true</code> for points on the right
  830. * or bottom edges of a rectangle. Therefore, if the added point falls
  831. * on the left or bottom edge of the enlarged rectangle,
  832. * <code>contains</code> returns <code>false</code> for that point.
  833. * @param pt the new <code>Point2D</code> to add to this
  834. * <code>Rectangle2D</code>.
  835. * @since JDK1.0
  836. */
  837. public void add(Point2D pt) {
  838. add(pt.getX(), pt.getY());
  839. }
  840. /**
  841. * Adds a <code>Rectangle2D</code> object to this
  842. * <code>Rectangle2D</code>. The resulting <code>Rectangle2D</code>
  843. * is the union of the two <code>Rectangle2D</code> objects.
  844. * @param r the <code>Rectangle2D</code> to add to this
  845. * <code>Rectangle2D</code>.
  846. * @since JDK1.0
  847. */
  848. public void add(Rectangle2D r) {
  849. double x1 = Math.min(getMinX(), r.getMinX());
  850. double x2 = Math.max(getMaxX(), r.getMaxX());
  851. double y1 = Math.min(getMinY(), r.getMinY());
  852. double y2 = Math.max(getMaxY(), r.getMaxY());
  853. setRect(x1, y1, x2 - x1, y2 - y1);
  854. }
  855. /**
  856. * Returns an iteration object that defines the boundary of this
  857. * <code>Rectangle2D</code>.
  858. * The iterator for this class is multi-threaded safe, which means
  859. * that this <code>Rectangle2D</code> class guarantees that
  860. * modifications to the geometry of this <code>Rectangle2D</code>
  861. * object do not affect any iterations of that geometry that
  862. * are already in process.
  863. * @param at an optional <code>AffineTransform</code> to be applied to
  864. * the coordinates as they are returned in the iteration, or
  865. * <code>null</code> if untransformed coordinates are desired
  866. * @return the <code>PathIterator</code> object that returns the
  867. * geometry of the outline of this
  868. * <code>Rectangle2D</code>, one segment at a time.
  869. * @since JDK1.2
  870. */
  871. public PathIterator getPathIterator(AffineTransform at) {
  872. return new RectIterator(this, at);
  873. }
  874. /**
  875. * Returns an iteration object that defines the boundary of the
  876. * flattened <code>Rectangle2D</code>. Since rectangles are already
  877. * flat, the <code>flatness</code> parameter is ignored.
  878. * The iterator for this class is multi-threaded safe, which means
  879. * that this <code>Rectangle2D</code> class guarantees that
  880. * modifications to the geometry of this <code>Rectangle2D</code>
  881. * object do not affect any iterations of that geometry that
  882. * are already in process.
  883. * @param at an optional <code>AffineTransform</code> to be applied to
  884. * the coordinates as they are returned in the iteration, or
  885. * <code>null</code> if untransformed coordinates are desired
  886. * @param flatness the maximum distance that the line segments used to
  887. * approximate the curved segments are allowed to deviate from any
  888. * point on the original curve. Since rectangles are already flat,
  889. * the <code>flatness</code> parameter is ignored.
  890. * @return the <code>PathIterator</code> object that returns the
  891. * geometry of the outline of this
  892. * <code>Rectangle2D</code>, one segment at a time.
  893. * @since JDK1.2
  894. */
  895. public PathIterator getPathIterator(AffineTransform at, double flatness) {
  896. return new RectIterator(this, at);
  897. }
  898. /**
  899. * Returns the hashcode for this <code>Rectangle2D</code>.
  900. * @return the hashcode for this <code>Rectangle2D</code>.
  901. */
  902. public int hashCode() {
  903. long bits = java.lang.Double.doubleToLongBits(getX());
  904. bits += java.lang.Double.doubleToLongBits(getY()) * 37;
  905. bits += java.lang.Double.doubleToLongBits(getWidth()) * 43;
  906. bits += java.lang.Double.doubleToLongBits(getHeight()) * 47;
  907. return (((int) bits) ^ ((int) (bits >> 32)));
  908. }
  909. /**
  910. * Determines whether or not the specified <code>Object</code> is
  911. * equal to this <code>Rectangle2D</code>. The specified
  912. * <code>Object</code> is equal to this <code>Rectangle2D</code>
  913. * if it is an instance of <code>Rectangle2D</code> and if its
  914. * location and size are the same as this <code>Rectangle2D</code>.
  915. * @param obj an <code>Object</code> to be compared with this
  916. * <code>Rectangle2D</code>.
  917. * @return <code>true</code> if <code>obj</code> is an instance
  918. * of <code>Rectangle2D</code> and has
  919. * the same values; <code>false</code> otherwise.
  920. * @since JDK1.2
  921. */
  922. public boolean equals(Object obj) {
  923. if (obj == this) {
  924. return true;
  925. }
  926. if (obj instanceof Rectangle2D) {
  927. Rectangle2D r2d = (Rectangle2D) obj;
  928. return ((getX() == r2d.getX()) &&
  929. (getY() == r2d.getY()) &&
  930. (getWidth() == r2d.getWidth()) &&
  931. (getHeight() == r2d.getHeight()));
  932. }
  933. return false;
  934. }
  935. }