1. /*
  2. * @(#)Arc2D.java 1.18 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. * <CODE>Arc2D</CODE> is the abstract superclass for all objects that
  10. * store a 2D arc defined by a bounding rectangle,
  11. * start angle, angular extent (length of the arc), and a closure type
  12. * (<CODE>OPEN</CODE>, <CODE>CHORD</CODE>, or <CODE>PIE</CODE>).
  13. * <p>
  14. * The bounding rectangle defines the outer boundary of the full ellipse
  15. * of which this arc is a partial section.
  16. * The angles are specified relative to the non-square extents of the
  17. * bounding rectangle such that 45 degrees always falls on the line from
  18. * the center of the ellipse to the upper right corner of the bounding
  19. * rectangle.
  20. * As a result, if the bounding rectangle is noticeably longer along one
  21. * axis than the other, the angles to the start and end of the arc segment
  22. * will be skewed farther along the longer axis of the bounds.
  23. * <p>
  24. * The actual storage representation of the coordinates is left to
  25. * the subclass.
  26. *
  27. * @version 10 Feb 1997
  28. * @author Jim Graham
  29. */
  30. public abstract class Arc2D extends RectangularShape {
  31. /**
  32. * The closure type for an open arc with no path segments
  33. * connecting the two ends of the arc segment.
  34. */
  35. public final static int OPEN = 0;
  36. /**
  37. * The closure type for an arc closed by drawing a straight
  38. * line segment from the start of the arc segment to the end of the
  39. * arc segment.
  40. */
  41. public final static int CHORD = 1;
  42. /**
  43. * The closure type for an arc closed by drawing straight line
  44. * segments from the start of the arc segment to the center
  45. * of the full ellipse and from that point to the end of the arc segment.
  46. */
  47. public final static int PIE = 2;
  48. /**
  49. * An arc specified in float precision,
  50. */
  51. public static class Float extends Arc2D {
  52. /**
  53. * The x coordinate of the upper left corner of the arc.
  54. */
  55. public float x;
  56. /**
  57. * The y coordinate of the upper left corner of the arc.
  58. */
  59. public float y;
  60. /**
  61. * The overall width of the full ellipse of which this arc is
  62. * a partial section (not considering the
  63. * angular extents).
  64. */
  65. public float width;
  66. /**
  67. * The overall height of the full ellipse of which this arc is
  68. * a partial section (not considering the
  69. * angular extents).
  70. */
  71. public float height;
  72. /**
  73. * The starting angle of the arc in degrees.
  74. */
  75. public float start;
  76. /**
  77. * The angular extent of the arc in degrees.
  78. */
  79. public float extent;
  80. /**
  81. * Constructs a new OPEN arc, initialized to location (0, 0),
  82. * size (0, 0), angular extents (start = 0, extent = 0).
  83. */
  84. public Float() {
  85. super(OPEN);
  86. }
  87. /**
  88. * Constructs a new arc, initialized to location (0, 0),
  89. * size (0, 0), angular extents (start = 0, extent = 0), and
  90. * the specified closure type.
  91. *
  92. * @param type The closure type for the arc:
  93. * {@link #OPEN OPEN}, {@link #CHORD CHORD}, or {@link #PIE PIE}.
  94. */
  95. public Float(int type) {
  96. super(type);
  97. }
  98. /**
  99. * Constructs a new arc, initialized to the specified location,
  100. * size, angular extents, and closure type.
  101. *
  102. * @param x, y The coordinates of the upper left corner of
  103. * the arc. (Specified in float precision.)
  104. * @param w The overall width of the full ellipse of which
  105. * this arc is a partial section. (Specified in float precision.)
  106. * @param h The overall height of the full ellipse of which this
  107. * arc is a partial section. (Specified in float precision.)
  108. * @param start The starting angle of the arc in degrees.
  109. * (Specified in float precision.)
  110. * @param extent The angular extent of the arc in degrees.
  111. * (Specified in float precision.)
  112. * @param type The closure type for the arc:
  113. * {@link #OPEN OPEN}, {@link #CHORD CHORD}, or {@link #PIE PIE}.
  114. */
  115. public Float(float x, float y, float w, float h,
  116. float start, float extent, int type) {
  117. super(type);
  118. this.x = x;
  119. this.y = y;
  120. this.width = w;
  121. this.height = h;
  122. this.start = start;
  123. this.extent = extent;
  124. }
  125. /**
  126. * Constructs a new arc, initialized to the specified location,
  127. * size, angular extents, and closure type.
  128. *
  129. * @param ellipseBounds The bounding rectangle that defines the
  130. * outer boundary of the full ellipse of which this arc is a
  131. * partial section.
  132. * @param start The starting angle of the arc in degrees.
  133. * (Specified in float precision.)
  134. * @param extent The angular extent of the arc in degrees.
  135. * (Specified in float precision.)
  136. * @param type The closure type for the arc:
  137. * {@link #OPEN OPEN}, {@link #CHORD CHORD}, or {@link #PIE PIE}.
  138. */
  139. public Float(Rectangle2D ellipseBounds,
  140. float start, float extent, int type) {
  141. super(type);
  142. this.x = (float) ellipseBounds.getX();
  143. this.y = (float) ellipseBounds.getY();
  144. this.width = (float) ellipseBounds.getWidth();
  145. this.height = (float) ellipseBounds.getHeight();
  146. this.start = start;
  147. this.extent = extent;
  148. }
  149. /**
  150. * Returns the x coordinate of the upper left corner of the arc.
  151. *
  152. * @return The x coordinate of arc's upper left coordinate in
  153. * double precision.
  154. */
  155. public double getX() {
  156. return (double) x;
  157. }
  158. /**
  159. * Returns the y coordinate of the upper left corner of the arc.
  160. *
  161. * @return The y coordinate of arc's upper left coordinate in
  162. * double precision.
  163. */
  164. public double getY() {
  165. return (double) y;
  166. }
  167. /**
  168. * Returns the width of the ellipse of which this arc is
  169. * a partial section
  170. *
  171. * @return A double value that represents the width of the full
  172. * ellipse of which this arc is a partial section.
  173. */
  174. public double getWidth() {
  175. return (double) width;
  176. }
  177. /**
  178. * Returns the height of the ellipse of which this arc is
  179. * a partial section
  180. *
  181. * @return A double value that represents the height of the full
  182. * ellipse of which this arc is a partial section.
  183. */
  184. public double getHeight() {
  185. return (double) height;
  186. }
  187. /**
  188. * Returns the starting angle of the arc.
  189. *
  190. * @return A double value that represents the starting angle of
  191. * the arc in degrees.
  192. */
  193. public double getAngleStart() {
  194. return (double) start;
  195. }
  196. /**
  197. * Returns the angular extent of the arc.
  198. *
  199. * @return A double value that represents the angular extent of
  200. * the arc in degrees.
  201. */
  202. public double getAngleExtent() {
  203. return (double) extent;
  204. }
  205. /**
  206. * Determines whether the arc is empty.
  207. *
  208. * @return <CODE>true</CODE> if the arc is empty, <CODE>false</CODE>
  209. * if it is not.
  210. */
  211. public boolean isEmpty() {
  212. return (width <= 0.0 || height <= 0.0);
  213. }
  214. /**
  215. * Sets the location, size, angular extents, and closure type of
  216. * this arc to the specified double values.
  217. *
  218. * @param x, y The coordinates of the upper left corner of
  219. * the arc.
  220. * @param w The overall width of the full ellipse of which this
  221. * arc is a partial section.
  222. * @param h The overall height of the full ellipse of which this
  223. * arc is a partial section.
  224. * @param angSt The starting angle of the arc in degrees.
  225. * @param angExt The angular extent of the arc in degrees.
  226. * @param closure The closure type for the arc:
  227. * {@link #OPEN OPEN}, {@link #CHORD CHORD}, or {@link #PIE PIE}.
  228. */
  229. public void setArc(double x, double y, double w, double h,
  230. double angSt, double angExt, int closure) {
  231. this.setArcType(closure);
  232. this.x = (float) x;
  233. this.y = (float) y;
  234. this.width = (float) w;
  235. this.height = (float) h;
  236. this.start = (float) angSt;
  237. this.extent = (float) angExt;
  238. }
  239. /**
  240. * Sets the starting angle of this arc to the specified double
  241. * value.
  242. *
  243. * @param angSt The starting angle of the arc in degrees.
  244. */
  245. public void setAngleStart(double angSt) {
  246. this.start = (float) angSt;
  247. }
  248. /**
  249. * Sets the angular extent of this arc to the specified double
  250. * value.
  251. *
  252. * @param angExt The angular extent of the arc in degrees.
  253. */
  254. public void setAngleExtent(double angExt) {
  255. this.extent = (float) angExt;
  256. }
  257. /**
  258. * Return the high-precision bounding box of the arc.
  259. *
  260. * @param x, y The coordinates of the upper left corner
  261. * of the arc.
  262. * @param w The overall width of the full ellipse of which
  263. * this arc is a partial section.
  264. * @param h The overall height of the full ellipse of which
  265. * this arc is a partial section.
  266. *
  267. * @return The bounding box as a <CODE>Rectangle2D</CODE> object.
  268. */
  269. protected Rectangle2D makeBounds(double x, double y,
  270. double w, double h) {
  271. return new Rectangle2D.Float((float) x, (float) y,
  272. (float) w, (float) h);
  273. }
  274. }
  275. /**
  276. * An arc specified in double precision,
  277. */
  278. public static class Double extends Arc2D {
  279. /**
  280. * The x coordinate of the upper left corner of the arc.
  281. */
  282. public double x;
  283. /**
  284. * The y coordinate of the upper left corner of the arc.
  285. */
  286. public double y;
  287. /**
  288. * The overall width of the full ellipse (not considering the
  289. * angular extents).
  290. */
  291. public double width;
  292. /**
  293. * The overall height of the full ellipse (not considering the
  294. * angular extents).
  295. */
  296. public double height;
  297. /**
  298. * The starting angle of the arc in degrees.
  299. */
  300. public double start;
  301. /**
  302. * The angular extent of the arc in degrees.
  303. */
  304. public double extent;
  305. /**
  306. * Constructs a new OPEN arc, initialized to location (0, 0),
  307. * size (0, 0), angular extents (start = 0, extent = 0).
  308. */
  309. public Double() {
  310. super(OPEN);
  311. }
  312. /**
  313. * Constructs a new arc, initialized to location (0, 0),
  314. * size (0, 0), angular extents (start = 0, extent = 0), and
  315. * the specified closure type.
  316. *
  317. * @param type The closure type for the arc:
  318. * {@link #OPEN OPEN}, {@link #CHORD CHORD}, or {@link #PIE PIE}.
  319. */
  320. public Double(int type) {
  321. super(type);
  322. }
  323. /**
  324. * Constructs a new arc, initialized to the specified location,
  325. * size, angular extents, and closure type.
  326. *
  327. * @param x, y The coordinates of the upper left corner
  328. * of the arc. (Specified in double precision.)
  329. * @param w The overall width of the full ellipse of which this
  330. * arc is a partial section. (Specified in double precision.)
  331. * @param h The overall height of the full ellipse of which this
  332. * arc is a partial section. (Specified in double precision.)
  333. * @param angSt The starting angle of the arc in degrees.
  334. * (Specified in double precision.)
  335. * @param angExt The angular extent of the arc in degrees.
  336. * (Specified in double precision.)
  337. * @param closure The closure type for the arc:
  338. * {@link #OPEN OPEN}, {@link #CHORD CHORD}, or {@link #PIE PIE}.
  339. */
  340. public Double(double x, double y, double w, double h,
  341. double start, double extent, int type) {
  342. super(type);
  343. this.x = x;
  344. this.y = y;
  345. this.width = w;
  346. this.height = h;
  347. this.start = start;
  348. this.extent = extent;
  349. }
  350. /**
  351. * Constructs a new arc, initialized to the specified location,
  352. * size, angular extents, and closure type.
  353. *
  354. * @param ellipseBounds The bounding rectangle that defines the
  355. * outer boundary of the full ellipse of which this arc is a
  356. * partial section.
  357. * @param angSt The starting angle of the arc in degrees.
  358. * (Specified in double precision.)
  359. * @param angExt The angular extent of the arc in degrees.
  360. * (Specified in double precision.)
  361. * @param closure The closure type for the arc:
  362. * {@link #OPEN OPEN}, {@link #CHORD CHORD}, or {@link #PIE PIE}.
  363. */
  364. public Double(Rectangle2D ellipseBounds,
  365. double start, double extent, int type) {
  366. super(type);
  367. this.x = ellipseBounds.getX();
  368. this.y = ellipseBounds.getY();
  369. this.width = ellipseBounds.getWidth();
  370. this.height = ellipseBounds.getHeight();
  371. this.start = start;
  372. this.extent = extent;
  373. }
  374. /**
  375. * Returns the x coordinate of the upper left corner of the arc.
  376. *
  377. * @return The x coordinate of arc's upper left coordinate in
  378. * double precision.
  379. */
  380. public double getX() {
  381. return x;
  382. }
  383. /**
  384. * Returns the y coordinate of the upper left corner of the arc.
  385. *
  386. * @return The y coordinate of arc's upper left coordinate in
  387. * double precision.
  388. */
  389. public double getY() {
  390. return y;
  391. }
  392. /**
  393. * Returns the width of the ellipse of which this arc is
  394. * a partial section
  395. *
  396. * @return A double value that represents the width of the full
  397. * ellipse of which this arc is a partial section.
  398. */
  399. public double getWidth() {
  400. return width;
  401. }
  402. /**
  403. * Returns the height of the ellipse of which this arc is
  404. * a partial section
  405. *
  406. * @return A double value that represents the height of the full
  407. * ellipse of which this arc is a partial section.
  408. */
  409. public double getHeight() {
  410. return height;
  411. }
  412. /**
  413. * Returns the starting angle of the arc.
  414. *
  415. * @return A double value that represents the starting angle
  416. * of the arc in degrees.
  417. */
  418. public double getAngleStart() {
  419. return start;
  420. }
  421. /**
  422. * Returns the angular extent of the arc.
  423. *
  424. * @return A double value that represents the angular extent of
  425. * the arc in degrees.
  426. */
  427. public double getAngleExtent() {
  428. return extent;
  429. }
  430. /**
  431. * Determines whether the arc is empty.
  432. *
  433. * @return <CODE>true</CODE> if the arc is empty, <CODE>false</CODE>
  434. * if it not.
  435. */
  436. public boolean isEmpty() {
  437. return (width <= 0.0 || height <= 0.0);
  438. }
  439. /**
  440. * Sets the location, size, angular extents, and closure type of
  441. * this arc to the specified double values.
  442. *
  443. * @param x, y The coordinates of the upper left corner
  444. * of the arc.
  445. * @param w The overall width of the full ellipse of which
  446. * this arc is a partial section.
  447. * @param h The overall height of the full ellipse of which
  448. * this arc is a partial section.
  449. * @param angSt The starting angle of the arc in degrees.
  450. * @param angExt The angular extent of the arc in degrees.
  451. * @param closure The closure type for the arc:
  452. * {@link #OPEN OPEN}, {@link #CHORD CHORD}, or {@link #PIE PIE}.
  453. */
  454. public void setArc(double x, double y, double w, double h,
  455. double angSt, double angExt, int closure) {
  456. this.setArcType(closure);
  457. this.x = x;
  458. this.y = y;
  459. this.width = w;
  460. this.height = h;
  461. this.start = angSt;
  462. this.extent = angExt;
  463. }
  464. /**
  465. * Sets the starting angle of this arc to the specified double
  466. * value.
  467. *
  468. * @param angSt The starting angle of the arc in degrees.
  469. */
  470. public void setAngleStart(double angSt) {
  471. this.start = angSt;
  472. }
  473. /**
  474. * Sets the angular extent of this arc to the specified double
  475. * value.
  476. *
  477. * @param angExt The angular extent of the arc in degrees.
  478. */
  479. public void setAngleExtent(double angExt) {
  480. this.extent = angExt;
  481. }
  482. /**
  483. * Returns the high-precision bounding box of the arc.
  484. *
  485. * @param x, y The coordinates of the upper left corner
  486. * of the arc.
  487. * @param w The overall width of the full ellipse of which
  488. * this arc is a partial section.
  489. * @param h The overall height of the full ellipse of which
  490. * this arc is a partial section.
  491. *
  492. * @return The bounding box as a <CODE>Rectangle2D</CODE> object.
  493. */
  494. protected Rectangle2D makeBounds(double x, double y,
  495. double w, double h) {
  496. return new Rectangle2D.Double(x, y, w, h);
  497. }
  498. }
  499. private int type;
  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. * @param type The closure type of this arc:
  508. * {@link #OPEN OPEN}, {@link #CHORD CHORD}, or {@link #PIE PIE}.
  509. * @see java.awt.geom.Arc2D.Float
  510. * @see java.awt.geom.Arc2D.Double
  511. */
  512. protected Arc2D(int type) {
  513. setArcType(type);
  514. }
  515. /**
  516. * Returns the starting angle of the arc.
  517. *
  518. * @return A double value that represents the starting angle
  519. * of the arc in degrees.
  520. */
  521. public abstract double getAngleStart();
  522. /**
  523. * Returns the angular extent of the arc.
  524. *
  525. * @return A double value that represents the angular extent
  526. * of the arc in degrees.
  527. */
  528. public abstract double getAngleExtent();
  529. /**
  530. * Returns the arc closure type of the arc: {@link #OPEN OPEN},
  531. * {@link #CHORD CHORD}, or {@link #PIE PIE}.
  532. * @return One of the integer constant closure types defined
  533. * in this class.
  534. */
  535. public int getArcType() {
  536. return type;
  537. }
  538. /**
  539. * Returns the starting point of the arc. This point is the
  540. * intersection of the ray from the center defined by the
  541. * starting angle and the elliptical boundary of the arc.
  542. *
  543. * @return A <CODE>Point2D</CODE> object representing the
  544. * x,y coordinates of the starting point of the arc.
  545. */
  546. public Point2D getStartPoint() {
  547. double angle = Math.toRadians(-getAngleStart());
  548. double x = getX() + (Math.cos(angle) * 0.5 + 0.5) * getWidth();
  549. double y = getY() + (Math.sin(angle) * 0.5 + 0.5) * getHeight();
  550. return new Point2D.Double(x, y);
  551. }
  552. /**
  553. * Returns the ending point of the arc. This point is the
  554. * intersection of the ray from the center defined by the
  555. * starting angle plus the angular extent of the arc and the
  556. * elliptical boundary of the arc.
  557. *
  558. * @return A <CODE>Point2D</CODE> object representing the
  559. * x,y coordinates of the ending point of the arc.
  560. */
  561. public Point2D getEndPoint() {
  562. double angle = Math.toRadians(-getAngleStart() - getAngleExtent());
  563. double x = getX() + (Math.cos(angle) * 0.5 + 0.5) * getWidth();
  564. double y = getY() + (Math.sin(angle) * 0.5 + 0.5) * getHeight();
  565. return new Point2D.Double(x, y);
  566. }
  567. /**
  568. * Sets the location, size, angular extents, and closure type of
  569. * this arc to the specified double values.
  570. *
  571. * @param x, y The coordinates of the upper left corner of
  572. * the arc.
  573. * @param w The overall width of the full ellipse of which
  574. * this arc is a partial section.
  575. * @param h The overall height of the full ellipse of which
  576. * this arc is a partial section.
  577. * @param angSt The starting angle of the arc in degrees.
  578. * @param angExt The angular extent of the arc in degrees.
  579. * @param closure The closure type for the arc:
  580. * {@link #OPEN OPEN}, {@link #CHORD CHORD}, or {@link #PIE PIE}.
  581. */
  582. public abstract void setArc(double x, double y, double w, double h,
  583. double angSt, double angExt, int closure);
  584. /**
  585. * Sets the location, size, angular extents, and closure type of
  586. * this arc to the specified values.
  587. *
  588. * @param loc The <CODE>Point2D</CODE> representing the coordinates of
  589. * the upper left corner of the arc.
  590. * @param size The <CODE>Dimension2D</CODE> representing the width
  591. * and height of the full ellipse of which this arc is
  592. * a partial section.
  593. * @param angSt The starting angle of the arc in degrees.
  594. * (Specified in double precision.)
  595. * @param angExt The angular extent of the arc in degrees.
  596. * (Specified in double precision.)
  597. * @param closure The closure type for the arc:
  598. * {@link #OPEN OPEN}, {@link #CHORD CHORD}, or {@link #PIE PIE}.
  599. */
  600. public void setArc(Point2D loc, Dimension2D size,
  601. double angSt, double angExt, int closure) {
  602. setArc(loc.getX(), loc.getY(), size.getWidth(), size.getHeight(),
  603. angSt, angExt, closure);
  604. }
  605. /**
  606. * Sets the location, size, angular extents, and closure type of
  607. * this arc to the specified values.
  608. *
  609. * @param rect The bounding rectangle that defines the
  610. * outer boundary of the full ellipse of which this arc is a
  611. * partial section.
  612. * @param angSt The starting angle of the arc in degrees.
  613. * (Specified in double precision.)
  614. * @param angExt The angular extent of the arc in degrees.
  615. * (Specified in double precision.)
  616. * @param closure The closure type for the arc:
  617. * {@link #OPEN OPEN}, {@link #CHORD CHORD}, or {@link #PIE PIE}.
  618. */
  619. public void setArc(Rectangle2D rect, double angSt, double angExt,
  620. int closure) {
  621. setArc(rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight(),
  622. angSt, angExt, closure);
  623. }
  624. /**
  625. * Sets this arc to be the same as the specified arc.
  626. *
  627. * @param a The <CODE>Arc2D</CODE> to use to set the arc's values.
  628. */
  629. public void setArc(Arc2D a) {
  630. setArc(a.getX(), a.getY(), a.getWidth(), a.getHeight(),
  631. a.getAngleStart(), a.getAngleExtent(), a.type);
  632. }
  633. /**
  634. * Sets the position, bounds, angular extents, and closure type of
  635. * this arc to the specified values. The arc is defined by a center
  636. * point and a radius rather than a bounding box for the full ellipse.
  637. *
  638. * @param x, y The coordinates of the center of the arc.
  639. * (Specified in double precision.)
  640. * @param r The radius of the arc. (Specified in double precision.)
  641. * @param angSt The starting angle of the arc in degrees.
  642. * (Specified in double precision.)
  643. * @param angExt The angular extent of the arc in degrees.
  644. * (Specified in double precision.)
  645. * @param closure The closure type for the arc:
  646. * {@link #OPEN OPEN}, {@link #CHORD CHORD}, or {@link #PIE PIE}.
  647. */
  648. public void setArcByCenter(double x, double y, double radius,
  649. double angSt, double angExt, int closure) {
  650. setArc(x - radius, y - radius, radius * 2.0, radius * 2.0,
  651. angSt, angExt, closure);
  652. }
  653. /**
  654. * Sets the position, bounds, and angular extents of this arc to the
  655. * specified value. The starting angle of the arc is tangent to the
  656. * line specified by points (p1, p2), the ending angle is tangent to
  657. * the line specified by points (p2, p3), and the arc has the
  658. * specified radius.
  659. *
  660. * @param p1 The first point that defines the arc. The starting
  661. * angle of the arc is tangent to the line specified by points (p1, p2).
  662. * @param p2 The second point that defines the arc. The starting
  663. * angle of the arc is tangent to the line specified by points (p1, p2).
  664. * The ending angle of the arc is tangent to the line specified by
  665. * points (p2, p3).
  666. * @param p3 The third point that defines the arc. The ending angle
  667. * of the arc is tangent to the line specified by points (p2, p3).
  668. * @param radius The radius of the arc. (Specified in double precision.)
  669. */
  670. public void setArcByTangent(Point2D p1, Point2D p2, Point2D p3,
  671. double radius) {
  672. double ang1 = Math.atan2(p1.getY() - p2.getY(),
  673. p1.getX() - p2.getX());
  674. double ang2 = Math.atan2(p3.getY() - p2.getY(),
  675. p3.getX() - p2.getX());
  676. double diff = ang2 - ang1;
  677. if (diff > Math.PI) {
  678. ang2 -= Math.PI * 2.0;
  679. } else if (diff < -Math.PI) {
  680. ang2 += Math.PI * 2.0;
  681. }
  682. double bisect = (ang1 + ang2) / 2.0;
  683. double theta = Math.abs(ang2 - bisect);
  684. double dist = radius / Math.sin(theta);
  685. double x = p2.getX() + dist * Math.cos(bisect);
  686. double y = p2.getY() + dist * Math.sin(bisect);
  687. // REMIND: This needs some work...
  688. if (ang1 < ang2) {
  689. ang1 -= Math.PI / 2.0;
  690. ang2 += Math.PI / 2.0;
  691. } else {
  692. ang1 += Math.PI / 2.0;
  693. ang2 -= Math.PI / 2.0;
  694. }
  695. ang1 = Math.toDegrees(-ang1);
  696. ang2 = Math.toDegrees(-ang2);
  697. diff = ang2 - ang1;
  698. if (diff < 0) {
  699. diff += 360;
  700. } else {
  701. diff -= 360;
  702. }
  703. setArcByCenter(x, y, radius, ang1, diff, type);
  704. }
  705. /**
  706. * Sets the starting angle of this arc to the specified double
  707. * value.
  708. *
  709. * @param angSt The starting angle of the arc in degrees.
  710. */
  711. public abstract void setAngleStart(double angSt);
  712. /**
  713. * Sets the angular extent of this arc to the specified double
  714. * value.
  715. *
  716. * @param angExt The angular extent of the arc in degrees.
  717. */
  718. public abstract void setAngleExtent(double angExt);
  719. /**
  720. * Sets the starting angle of this arc to the angle that the
  721. * specified point defines relative to the center of this arc.
  722. * The angular extent of the arc will remain the same.
  723. *
  724. * @param p The <CODE>Point2D</CODE> that defines the starting angle.
  725. */
  726. public void setAngleStart(Point2D p) {
  727. setAngleStart(-Math.toDegrees(Math.atan2(p.getY() - getCenterY(),
  728. p.getX() - getCenterX())));
  729. }
  730. /**
  731. * Sets the starting angle and angular extent of this arc using two
  732. * sets of coordinates. The first set of coordinates is used to
  733. * determine the angle of the starting point relative to the arc's
  734. * center. The second set of coordinates is used to determine the
  735. * angle of the end point relative to the arc's center.
  736. * The arc will always be non-empty and extend counterclockwise
  737. * from the first point around to the second point.
  738. *
  739. * @param x1, y1 The coordinates of the arc's starting point.
  740. * @param x2, y2 The coordinates of the arc's ending point.
  741. */
  742. public void setAngles(double x1, double y1, double x2, double y2) {
  743. double x = getCenterX();
  744. double y = getCenterY();
  745. // Note: reversing the Y equations negates the angle to adjust
  746. // for the upside down coordinate system.
  747. double ang1 = Math.atan2(y - y1, x1 - x);
  748. double ang2 = Math.atan2(y - y2, x2 - x);
  749. ang2 -= ang1;
  750. if (ang2 <= 0.0) {
  751. ang2 += Math.PI * 2.0;
  752. }
  753. setAngleStart(Math.toDegrees(ang1));
  754. setAngleExtent(Math.toDegrees(ang2));
  755. }
  756. /**
  757. * Sets the starting angle and angular extent of this arc using
  758. * two points. The first point is used to determine the angle of
  759. * the starting point relative to the arc's center.
  760. * The second point is used to determine the angle of the end point
  761. * relative to the arc's center.
  762. * The arc will always be non-empty and extend counterclockwise
  763. * from the first point around to the second point.
  764. *
  765. * @param p1 The <CODE>Point2D</CODE> that defines the arc's
  766. * starting point.
  767. * @param p2 The <CODE>Point2D</CODE> that defines the arc's
  768. * ending point.
  769. */
  770. public void setAngles(Point2D p1, Point2D p2) {
  771. setAngles(p1.getX(), p1.getY(), p2.getX(), p2.getY());
  772. }
  773. /**
  774. * Sets the closure type of this arc to the specified value:
  775. * <CODE>OPEN</CODE>, <CODE>CHORD</CODE>, or <CODE>PIE</CODE>.
  776. *
  777. * @param type The integer constant that represents the closure
  778. * type of this arc: {@link #OPEN}, {@link #CHORD}, or
  779. * {@link #PIE}.
  780. *
  781. * @throws IllegalArgumentException if <code>type</code> is not
  782. * 0, 1, or 2.+
  783. */
  784. public void setArcType(int type) {
  785. if (type < OPEN || type > PIE) {
  786. throw new IllegalArgumentException("invalid type for Arc: "+type);
  787. }
  788. this.type = type;
  789. }
  790. /**
  791. * Sets the location and size of the outer bounds of this arc
  792. * to the specified values.
  793. *
  794. * @param x, y The coordinates of the upper left corner of the
  795. * arc's bounding box. (Specified in double precision.)
  796. * @param w The width of the arc's bounding box. (Specified in
  797. * double precision.)
  798. * @param h The height of the arc's bounding box. (Specified in
  799. * double precision.)
  800. */
  801. public void setFrame(double x, double y, double w, double h) {
  802. setArc(x, y, w, h, getAngleStart(), getAngleExtent(), type);
  803. }
  804. /**
  805. * Returns the high-precision bounding box of the arc. The bounding
  806. * box contains only the part of this <code>Arc2D</code> that is
  807. * in between the starting and ending angles and contains the pie
  808. * wedge, if this <code>Arc2D</code> has a <code>PIE</code> closure type.
  809. * <p>
  810. * This method differs from the
  811. * {@link RectangularShape#getBounds() getBounds} in that the
  812. * <code>getBounds</code> method only returns the bounds of the
  813. * enclosing ellipse of this <code>Arc2D</code> without considering
  814. * the starting and ending angles of this <code>Arc2D</code>.
  815. *
  816. * @return the <CODE>Rectangle2D</CODE> that represents the arc's
  817. * bounding box.
  818. */
  819. public Rectangle2D getBounds2D() {
  820. if (isEmpty()) {
  821. return makeBounds(getX(), getY(), getWidth(), getHeight());
  822. }
  823. double x1, y1, x2, y2;
  824. if (getArcType() == PIE) {
  825. x1 = y1 = x2 = y2 = 0.0;
  826. } else {
  827. x1 = y1 = 1.0;
  828. x2 = y2 = -1.0;
  829. }
  830. double angle = 0.0;
  831. for (int i = 0; i < 6; i++) {
  832. if (i < 4) {
  833. // 0-3 are the four quadrants
  834. angle += 90.0;
  835. if (!containsAngle(angle)) {
  836. continue;
  837. }
  838. } else if (i == 4) {
  839. // 4 is start angle
  840. angle = getAngleStart();
  841. } else {
  842. // 5 is end angle
  843. angle += getAngleExtent();
  844. }
  845. double rads = Math.toRadians(-angle);
  846. double xe = Math.cos(rads);
  847. double ye = Math.sin(rads);
  848. x1 = Math.min(x1, xe);
  849. y1 = Math.min(y1, ye);
  850. x2 = Math.max(x2, xe);
  851. y2 = Math.max(y2, ye);
  852. }
  853. double w = getWidth();
  854. double h = getHeight();
  855. x2 = (x2 - x1) * 0.5 * w;
  856. y2 = (y2 - y1) * 0.5 * h;
  857. x1 = getX() + (x1 * 0.5 + 0.5) * w;
  858. y1 = getY() + (y1 * 0.5 + 0.5) * h;
  859. return makeBounds(x1, y1, x2, y2);
  860. }
  861. /**
  862. * Constructs a Rectangle2D of the appropriate precision
  863. * to hold the parameters calculated to be the bounding box
  864. * of this arc.
  865. *
  866. * @param x, y The coordinates of the upper left corner of the
  867. * bounding box. (Specified in double precision.)
  868. * @param w The width of the bounding box. (Specified in
  869. * double precision.)
  870. * @param h The height of the bounding box. (Specified in
  871. * double precision.)
  872. */
  873. protected abstract Rectangle2D makeBounds(double x, double y,
  874. double w, double h);
  875. /*
  876. * Normalizes the specified angle into the range -180 to 180.
  877. */
  878. static double normalizeDegrees(double angle) {
  879. if (angle > 180.0) {
  880. if (angle <= (180.0 + 360.0)) {
  881. angle = angle - 360.0;
  882. } else {
  883. angle = Math.IEEEremainder(angle, 360.0);
  884. // IEEEremainder can return -180 here for some input values...
  885. if (angle == -180.0) {
  886. angle = 180.0;
  887. }
  888. }
  889. } else if (angle <= -180.0) {
  890. if (angle > (-180.0 - 360.0)) {
  891. angle = angle + 360.0;
  892. } else {
  893. angle = Math.IEEEremainder(angle, 360.0);
  894. // IEEEremainder can return -180 here for some input values...
  895. if (angle == -180.0) {
  896. angle = 180.0;
  897. }
  898. }
  899. }
  900. return angle;
  901. }
  902. /**
  903. * Determines whether or not the specified angle is within the
  904. * angular extents of the arc.
  905. *
  906. * @param angle The angle to test. (Specified in double precision.)
  907. *
  908. * @return <CODE>true</CODE> if the arc contains the angle,
  909. * <CODE>false</CODE> if the arc doesn't contain the angle.
  910. */
  911. public boolean containsAngle(double angle) {
  912. double angExt = getAngleExtent();
  913. boolean backwards = (angExt < 0.0);
  914. if (backwards) {
  915. angExt = -angExt;
  916. }
  917. if (angExt >= 360.0) {
  918. return true;
  919. }
  920. angle = normalizeDegrees(angle) - normalizeDegrees(getAngleStart());
  921. if (backwards) {
  922. angle = -angle;
  923. }
  924. if (angle < 0.0) {
  925. angle += 360.0;
  926. }
  927. return (angle >= 0.0) && (angle < angExt);
  928. }
  929. /**
  930. * Determines whether or not the specified point is inside the boundary
  931. * of the arc.
  932. *
  933. * @param x, y The coordinates of the point to test. (Specified in
  934. * double precision.)
  935. *
  936. * @return <CODE>true</CODE> if the point lies within the bound of
  937. * the arc, <CODE>false</CODE> if the point lies outside of the
  938. * arc's bounds.
  939. */
  940. public boolean contains(double x, double y) {
  941. // Normalize the coordinates compared to the ellipse
  942. // having a center at 0,0 and a radius of 0.5.
  943. double ellw = getWidth();
  944. if (ellw <= 0.0) {
  945. return false;
  946. }
  947. double normx = (x - getX()) / ellw - 0.5;
  948. double ellh = getHeight();
  949. if (ellh <= 0.0) {
  950. return false;
  951. }
  952. double normy = (y - getY()) / ellh - 0.5;
  953. double distSq = (normx * normx + normy * normy);
  954. if (distSq >= 0.25) {
  955. return false;
  956. }
  957. double angExt = Math.abs(getAngleExtent());
  958. if (angExt >= 360.0) {
  959. return true;
  960. }
  961. boolean inarc = containsAngle(-Math.toDegrees(Math.atan2(normy,
  962. normx)));
  963. if (type == PIE) {
  964. return inarc;
  965. }
  966. // CHORD and OPEN behave the same way
  967. if (inarc) {
  968. if (angExt >= 180.0) {
  969. return true;
  970. }
  971. // point must be outside the "pie triangle"
  972. } else {
  973. if (angExt <= 180.0) {
  974. return false;
  975. }
  976. // point must be inside the "pie triangle"
  977. }
  978. // The point is inside the pie triangle iff it is on the same
  979. // side of the line connecting the ends of the arc as the center.
  980. double angle = Math.toRadians(-getAngleStart());
  981. double x1 = Math.cos(angle);
  982. double y1 = Math.sin(angle);
  983. angle += Math.toRadians(-getAngleExtent());
  984. double x2 = Math.cos(angle);
  985. double y2 = Math.sin(angle);
  986. boolean inside = (Line2D.relativeCCW(x1, y1, x2, y2, x, y) *
  987. Line2D.relativeCCW(x1, y1, x2, y2, 0, 0) >= 0);
  988. return inarc ? !inside : inside;
  989. }
  990. /**
  991. * Determines whether or not the interior of the arc intersects
  992. * the interior of the specified rectangle.
  993. *
  994. * @param x, y The coordinates of the rectangle's upper left corner.
  995. * (Specified in double precision.)
  996. * @param w The width of the rectangle. (Specified in double precision.)
  997. * @param h The height of the rectangle. (Specified in double precision.)
  998. *
  999. * @return <CODE>true</CODE> if the arc intersects the rectangle,
  1000. * <CODE>false</CODE> if the arc doesn't intersect the rectangle.
  1001. */
  1002. public boolean intersects(double x, double y, double w, double h) {
  1003. /*
  1004. * REMIND: Verify correct handling of "upside down angles".
  1005. */
  1006. /*
  1007. Change around so that is really works this time
  1008. We check each of the four line segments for intersection with an
  1009. ellipse of the same radius as the imaginary arc.
  1010. We then compute the angle of the intersections and call included
  1011. angle to find out if the intersection is within the arc itself
  1012. - Aaron Muderick
  1013. */
  1014. double intersect_angle;
  1015. double yint, xint;
  1016. //if there are any points of the rect in the arc then return true;
  1017. if (contains(x, y) || contains(x + w, y) ||
  1018. contains(x, y + h) || contains(x + w, y + h)) return true;
  1019. //we need to translate the arc and the rect so that we can do
  1020. //quadrant checking
  1021. x = x - (getX() + (getWidth()/2));
  1022. y = (y - (getY() + (getHeight()/2))) * (-1);
  1023. //find out the squash ratio
  1024. double squash = getWidth()/getHeight();
  1025. if (((x*x)/((getWidth()/2)*(getWidth()/2)) < 1)) {
  1026. if ((x == 0) && ((((getHeight()/2) >= (y-h)) && ((getHeight()/2) <= y))
  1027. || ((((-1) * (getHeight()/2)) >= (y-h)) && (((-1)
  1028. * (getHeight()/2) <= y))))) {
  1029. if (containsAngle(Math.PI2)) {
  1030. return true;
  1031. }
  1032. if (containsAngle(Math.PI*(3/2))) {
  1033. return true;
  1034. }
  1035. }
  1036. yint = Math.abs(Math.sqrt((1-((x*x)/((getWidth()/2)*(getWidth()/2))))
  1037. *((getHeight()/2)*(getHeight()/2))));
  1038. intersect_angle = Math.abs(Math.atan((yint*squash)/x));
  1039. if ((x > 0) && (((yint >= (y-h)) && (yint <= y))
  1040. || ((((-1) * yint) >= (y-h))
  1041. && (((-1) * yint) <= y)))) {
  1042. if (containsAngle(intersect_angleMath.PI * 180)) {
  1043. return true;
  1044. }
  1045. if (containsAngle((((2*Math.PI) - intersect_angle)/Math.PI)
  1046. * 180)) {
  1047. return true;
  1048. }
  1049. }
  1050. if ((x < 0) && (((yint >= (y-h)) && (yint <= y))
  1051. || ((((-1) * yint) >= (y-h))
  1052. && (((-1) * yint) <= y)))){
  1053. if (containsAngle((((Math.PI) - intersect_angle)/Math.PI) * 180)) {
  1054. return true;
  1055. }
  1056. if (containsAngle(((Math.PI + intersect_angle)/Math.PI) * 180)) {
  1057. return true;
  1058. }
  1059. }
  1060. }
  1061. if ((((x+w)*(x+w))/((getWidth()/2)*(getWidth()/2)) < 1)) {
  1062. if (((x+w) == 0) && ((((getHeight()/2) >= (y-h))
  1063. && ((getHeight()/2) <= y))
  1064. || ((((-1) * (getHeight()/2)) >= (y-h))
  1065. && (((-1) * (getHeight()/2) <= y))))) {
  1066. if (containsAngle(Math.PI2)) {
  1067. return true;
  1068. }
  1069. if (containsAngle(Math.PI*(3/2))) {
  1070. return true;
  1071. }
  1072. }
  1073. yint = Math.abs(Math.sqrt((1-(((x+w)*(x+w))
  1074. /((getWidth()/2)*(getWidth()/2))))
  1075. *((getHeight()/2)*(getHeight()/2))));
  1076. intersect_angle = Math.abs(Math.atan((yint*squash)/(x+w)));
  1077. if (((x+w) > 0) && (((yint >= (y-h)) && (yint <= y))
  1078. || ((((-1) * yint) >= (y-h))
  1079. && (((-1) * yint) <= y)))) {
  1080. if (containsAngle((intersect_angleMath.PI) * 180)) {
  1081. return true;
  1082. }
  1083. if (containsAngle((((2*Math.PI) - intersect_angle)/Math.PI) * 180)) {
  1084. return true;
  1085. }
  1086. }
  1087. if (((x+w) < 0) && (((yint >= (y-h)) && (yint <= y))
  1088. || ((((-1) * yint) >= (y-h))
  1089. && (((-1) * yint) <= y)))) {
  1090. if (containsAngle((((Math.PI) - intersect_angle)/Math.PI) * 180)) {
  1091. return true;
  1092. }
  1093. if (containsAngle(((Math.PI + intersect_angle)/Math.PI) * 180)) {
  1094. return true;
  1095. }
  1096. }
  1097. }
  1098. if (((y*y)/((getHeight()/2)*(getHeight()/2)) < 1)) {
  1099. if ((y == 0) && ((((getWidth()/2) >= x) && ((getWidth()/2) <= (x+w)))
  1100. || ((((-1) * (getWidth()/2)) >= x)
  1101. && (((-1) * (getWidth()/2)) <= (x+w))))) {
  1102. if (containsAngle(Math.PI)) {
  1103. return true;
  1104. }
  1105. if (containsAngle(Math.PI*2)) {
  1106. return true;
  1107. }
  1108. }
  1109. xint = Math.abs(Math.sqrt((1-((y*y)
  1110. /((getHeight()/2)
  1111. *(getHeight()/2))))
  1112. *((getWidth()/2)*(getWidth()/2))));
  1113. intersect_angle = Math.abs(Math.atan(y(xintsquash)));
  1114. if ((y > 0) && (((xint >= x) && (xint <= (x+w)))
  1115. || ((((-1) * xint) >= x)
  1116. && (((-1) * xint) <= (x+w))))) {
  1117. if (containsAngle((intersect_angle)/Math.PI * 180)) {
  1118. return true;
  1119. }
  1120. if (containsAngle(((Math.PI) - intersect_angle)/Math.PI * 180)) {
  1121. return true;
  1122. }
  1123. }
  1124. if ((y < 0) && (((xint >= x) && (xint <= (x+w)))
  1125. || ((((-1) * xint) >= x)
  1126. && (((-1) * xint) <= (x+w))))) {
  1127. if (containsAngle(((Math.PI*2) - intersect_angle)/Math.PI * 180)) {
  1128. return true;
  1129. }
  1130. if (containsAngle((Math.PI + intersect_angle)/Math.PI * 180)) {
  1131. return true;
  1132. }
  1133. }
  1134. }
  1135. if ((((y-h)*(y-h))/((getHeight()/2)*(getHeight()/2)) < 1)) {
  1136. if (((y-h) == 0) && ((((getWidth()/2) >= x)
  1137. && ((getWidth()/2) <= (x+w)))
  1138. || ((((-1) * (getWidth()/2)) >= x)
  1139. && (((-1) * (getWidth()/2)) <= (x+w))))) {
  1140. if (containsAngle(Math.PI)) {
  1141. return true;
  1142. }
  1143. if (containsAngle(Math.PI*2)) {
  1144. return true;
  1145. }
  1146. }
  1147. xint = Math.abs(Math.sqrt((1-(((y-h)*(y-h))
  1148. /((getHeight()/2)*(getHeight()/2))))
  1149. *((getWidth()/2)*(getWidth()/2))));
  1150. intersect_angle = Math.abs(Math.atan((y-h)/(xintsquash)));
  1151. if (((y-h) > 0) && (((xint >= x) && (xint <= (x+w)))
  1152. || ((((-1) * xint) >= x)
  1153. && (((-1) * xint) <= (x+w))))) {
  1154. if (containsAngle(intersect_angleMath.PI * 180)) {
  1155. return true;
  1156. }
  1157. if (containsAngle(((Math.PI) - intersect_angle)/Math.PI * 180)) {
  1158. return true;
  1159. }
  1160. }
  1161. if (((y-h) < 0) && (((xint >= x) && (xint <= (x+w)))
  1162. || ((((-1) * xint) >= x)
  1163. && (((-1) * xint) <= (x+w))))) {
  1164. if (containsAngle(((Math.PI*2) - intersect_angle)/Math.PI * 180)) {
  1165. return true;
  1166. }
  1167. if (containsAngle((Math.PI + intersect_angle)/Math.PI * 180)) {
  1168. return true;
  1169. }
  1170. }
  1171. }
  1172. return false;
  1173. }
  1174. /**
  1175. * Determine whether or not the interior of the arc entirely contains
  1176. * the specified rectangle.
  1177. *
  1178. * @param x, y The coordinates of the rectangle's upper left corner.
  1179. * (Specified in double precision.)
  1180. * @param w The width of the rectangle. (Specified in double precision.)
  1181. * @param h The height of the rectangle. (Specified in double precision.)
  1182. *
  1183. * @return <CODE>true</CODE> if the arc contains the rectangle,
  1184. * <CODE>false</CODE> if the arc doesn't contain the rectangle.
  1185. */
  1186. public boolean contains(double x, double y, double w, double h) {
  1187. return contains(x, y, w, h, null);
  1188. }
  1189. /**
  1190. * Determine whether or not the interior of the arc entirely contains
  1191. * the specified rectangle.
  1192. *
  1193. * @param r The <CODE>Rectangle2D</CODE> to test.
  1194. *
  1195. * @return <CODE>true</CODE> if the arc contains the rectangle,
  1196. * <CODE>false</CODE> if the arc doesn't contain the rectangle.
  1197. */
  1198. public boolean contains(Rectangle2D r) {
  1199. return contains(r.getX(), r.getY(), r.getWidth(), r.getHeight(), r);
  1200. }
  1201. private boolean contains(double x, double y, double w, double h,
  1202. Rectangle2D origrect) {
  1203. if (!(contains(x, y) &&
  1204. contains(x + w, y) &&
  1205. contains(x, y + h) &&
  1206. contains(x + w, y + h))) {
  1207. return false;
  1208. }
  1209. // If the shape is convex then we have done all the testing
  1210. // we need. Only PIE arcs can be concave and then only if
  1211. // the angular extents are greater than 180 degrees.
  1212. if (type != PIE || getAngleExtent() <= 180.0) {
  1213. return true;
  1214. }
  1215. // For a PIE shape we have an additional test for the case where
  1216. // the angular extents are greater than 180 degrees and all four
  1217. // rectangular corners are inside the shape but one of the
  1218. // rectangle edges spans across the "missing wedge" of the arc.
  1219. // We can test for this case by checking if the rectangle intersects
  1220. // either of the pie angle segments.
  1221. if (origrect == null) {
  1222. origrect = new Rectangle2D.Double(x, y, w, h);
  1223. }
  1224. double halfW = getWidth() / 2.0;
  1225. double halfH = getHeight() / 2.0;
  1226. double xc = getX() + halfW;
  1227. double yc = getY() + halfH;
  1228. double angle = Math.toRadians(-getAngleStart());
  1229. double xe = xc + halfW * Math.cos(angle);
  1230. double ye = yc + halfH * Math.sin(angle);
  1231. if (origrect.intersectsLine(xc, yc, xe, ye)) {
  1232. return false;
  1233. }
  1234. angle += Math.toRadians(-getAngleExtent());
  1235. xe = xc + halfW * Math.cos(angle);
  1236. ye = yc + halfH * Math.sin(angle);
  1237. return !origrect.intersectsLine(xc, yc, xe, ye);
  1238. }
  1239. /**
  1240. * Returns an iteration object that defines the boundary of the
  1241. * arc.
  1242. * This iterator is multithread safe.
  1243. * <code>Arc2D</code> guarantees that
  1244. * modifications to the geometry of the arc
  1245. * do not affect any iterations of that geometry that
  1246. * are already in process.
  1247. *
  1248. * @param at an optional <CODE>AffineTransform</CODE> to be applied
  1249. * to the coordinates as they are returned in the iteration, or null
  1250. * if the untransformed coordinates are desired.
  1251. *
  1252. * @return A <CODE>PathIterator</CODE> that defines the arc's boundary.
  1253. */
  1254. public PathIterator getPathIterator(AffineTransform at) {
  1255. return new ArcIterator(this, at);
  1256. }
  1257. }