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