001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2017, by Object Refinery Limited and Contributors. 006 * 007 * Project Info: http://www.jfree.org/jfreechart/index.html 008 * 009 * This library is free software; you can redistribute it and/or modify it 010 * under the terms of the GNU Lesser General Public License as published by 011 * the Free Software Foundation; either version 2.1 of the License, or 012 * (at your option) any later version. 013 * 014 * This library is distributed in the hope that it will be useful, but 015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 017 * License for more details. 018 * 019 * You should have received a copy of the GNU Lesser General Public 020 * License along with this library; if not, write to the Free Software 021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 022 * USA. 023 * 024 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 025 * Other names may be trademarks of their respective owners.] 026 * 027 */ 028 029package org.jfree.chart.util; 030 031import java.awt.Graphics2D; 032import java.awt.Polygon; 033import java.awt.Shape; 034import java.awt.geom.AffineTransform; 035import java.awt.geom.Arc2D; 036import java.awt.geom.Ellipse2D; 037import java.awt.geom.GeneralPath; 038import java.awt.geom.Line2D; 039import java.awt.geom.PathIterator; 040import java.awt.geom.Point2D; 041import java.awt.geom.Rectangle2D; 042import java.util.Arrays; 043import org.jfree.chart.ui.RectangleAnchor; 044 045/** 046 * Utility methods for {@link Shape} objects. 047 */ 048public class ShapeUtils { 049 050 /** 051 * Prevents instantiation. 052 */ 053 private ShapeUtils() { 054 } 055 056 /** 057 * Returns a clone of the specified shape, or {@code null}. At the 058 * current time, this method supports cloning for instances of 059 * {@code Line2D}, {@code RectangularShape}, {@code Area} 060 * and {@code GeneralPath}. 061 * <p> 062 * {@code RectangularShape} includes {@code Arc2D}, 063 * {@code Ellipse2D}, {@code Rectangle2D}, 064 * {@code RoundRectangle2D}. 065 * 066 * @param shape the shape to clone ({@code null} permitted, 067 * returns {@code null}). 068 * 069 * @return A clone or {@code null}. 070 */ 071 public static Shape clone(Shape shape) { 072 if (shape instanceof Cloneable) { 073 try { 074 return (Shape) ObjectUtils.clone(shape); 075 } 076 catch (CloneNotSupportedException cnse) { 077 } 078 } 079 final Shape result = null; 080 return result; 081 } 082 083 /** 084 * Tests two shapes for equality. If both shapes are {@code null}, 085 * this method will return {@code true}. 086 * <p> 087 * In the current implementation, the following shapes are supported: 088 * {@code Ellipse2D}, {@code Line2D} and {@code Rectangle2D} 089 * (implicit). 090 * 091 * @param s1 the first shape ({@code null} permitted). 092 * @param s2 the second shape ({@code null} permitted). 093 * 094 * @return A boolean. 095 */ 096 public static boolean equal(Shape s1, Shape s2) { 097 if (s1 instanceof Line2D && s2 instanceof Line2D) { 098 return equal((Line2D) s1, (Line2D) s2); 099 } 100 else if (s1 instanceof Ellipse2D && s2 instanceof Ellipse2D) { 101 return equal((Ellipse2D) s1, (Ellipse2D) s2); 102 } 103 else if (s1 instanceof Arc2D && s2 instanceof Arc2D) { 104 return equal((Arc2D) s1, (Arc2D) s2); 105 } 106 else if (s1 instanceof Polygon && s2 instanceof Polygon) { 107 return equal((Polygon) s1, (Polygon) s2); 108 } 109 else if (s1 instanceof GeneralPath && s2 instanceof GeneralPath) { 110 return equal((GeneralPath) s1, (GeneralPath) s2); 111 } 112 else { 113 // this will handle Rectangle2D... 114 return ObjectUtils.equal(s1, s2); 115 } 116 } 117 118 /** 119 * Compares two lines are returns {@code true} if they are equal or 120 * both {@code null}. 121 * 122 * @param l1 the first line ({@code null} permitted). 123 * @param l2 the second line ({@code null} permitted). 124 * 125 * @return A boolean. 126 */ 127 public static boolean equal(Line2D l1, Line2D l2) { 128 if (l1 == null) { 129 return (l2 == null); 130 } 131 if (l2 == null) { 132 return false; 133 } 134 if (!l1.getP1().equals(l2.getP1())) { 135 return false; 136 } 137 if (!l1.getP2().equals(l2.getP2())) { 138 return false; 139 } 140 return true; 141 } 142 143 /** 144 * Compares two ellipses and returns {@code true} if they are equal or 145 * both {@code null}. 146 * 147 * @param e1 the first ellipse ({@code null} permitted). 148 * @param e2 the second ellipse ({@code null} permitted). 149 * 150 * @return A boolean. 151 */ 152 public static boolean equal(Ellipse2D e1, Ellipse2D e2) { 153 if (e1 == null) { 154 return (e2 == null); 155 } 156 if (e2 == null) { 157 return false; 158 } 159 if (!e1.getFrame().equals(e2.getFrame())) { 160 return false; 161 } 162 return true; 163 } 164 165 /** 166 * Compares two arcs and returns {@code true} if they are equal or 167 * both {@code null}. 168 * 169 * @param a1 the first arc ({@code null} permitted). 170 * @param a2 the second arc ({@code null} permitted). 171 * 172 * @return A boolean. 173 */ 174 public static boolean equal(Arc2D a1, Arc2D a2) { 175 if (a1 == null) { 176 return (a2 == null); 177 } 178 if (a2 == null) { 179 return false; 180 } 181 if (!a1.getFrame().equals(a2.getFrame())) { 182 return false; 183 } 184 if (a1.getAngleStart() != a2.getAngleStart()) { 185 return false; 186 } 187 if (a1.getAngleExtent() != a2.getAngleExtent()) { 188 return false; 189 } 190 if (a1.getArcType() != a2.getArcType()) { 191 return false; 192 } 193 return true; 194 } 195 196 /** 197 * Tests two polygons for equality. If both are {@code null} this 198 * method returns {@code true}. 199 * 200 * @param p1 polygon 1 ({@code null} permitted). 201 * @param p2 polygon 2 ({@code null} permitted). 202 * 203 * @return A boolean. 204 */ 205 public static boolean equal(Polygon p1, Polygon p2) { 206 if (p1 == null) { 207 return (p2 == null); 208 } 209 if (p2 == null) { 210 return false; 211 } 212 if (p1.npoints != p2.npoints) { 213 return false; 214 } 215 if (!Arrays.equals(p1.xpoints, p2.xpoints)) { 216 return false; 217 } 218 if (!Arrays.equals(p1.ypoints, p2.ypoints)) { 219 return false; 220 } 221 return true; 222 } 223 224 /** 225 * Tests two polygons for equality. If both are {@code null} this 226 * method returns {@code true}. 227 * 228 * @param p1 path 1 ({@code null} permitted). 229 * @param p2 path 2 ({@code null} permitted). 230 * 231 * @return A boolean. 232 */ 233 public static boolean equal(GeneralPath p1, GeneralPath p2) { 234 if (p1 == null) { 235 return (p2 == null); 236 } 237 if (p2 == null) { 238 return false; 239 } 240 if (p1.getWindingRule() != p2.getWindingRule()) { 241 return false; 242 } 243 PathIterator iterator1 = p1.getPathIterator(null); 244 PathIterator iterator2 = p2.getPathIterator(null); 245 double[] d1 = new double[6]; 246 double[] d2 = new double[6]; 247 boolean done = iterator1.isDone() && iterator2.isDone(); 248 while (!done) { 249 if (iterator1.isDone() != iterator2.isDone()) { 250 return false; 251 } 252 int seg1 = iterator1.currentSegment(d1); 253 int seg2 = iterator2.currentSegment(d2); 254 if (seg1 != seg2) { 255 return false; 256 } 257 if (!Arrays.equals(d1, d2)) { 258 return false; 259 } 260 iterator1.next(); 261 iterator2.next(); 262 done = iterator1.isDone() && iterator2.isDone(); 263 } 264 return true; 265 } 266 267 /** 268 * Creates and returns a translated shape. 269 * 270 * @param shape the shape ({@code null} not permitted). 271 * @param transX the x translation (in Java2D space). 272 * @param transY the y translation (in Java2D space). 273 * 274 * @return The translated shape. 275 */ 276 public static Shape createTranslatedShape(Shape shape, double transX, 277 double transY) { 278 if (shape == null) { 279 throw new IllegalArgumentException("Null 'shape' argument."); 280 } 281 final AffineTransform transform = AffineTransform.getTranslateInstance( 282 transX, transY); 283 return transform.createTransformedShape(shape); 284 } 285 286 /** 287 * Translates a shape to a new location such that the anchor point 288 * (relative to the rectangular bounds of the shape) aligns with the 289 * specified (x, y) coordinate in Java2D space. 290 * 291 * @param shape the shape ({@code null} not permitted). 292 * @param anchor the anchor ({@code null} not permitted). 293 * @param locationX the x-coordinate (in Java2D space). 294 * @param locationY the y-coordinate (in Java2D space). 295 * 296 * @return A new and translated shape. 297 */ 298 public static Shape createTranslatedShape(Shape shape, 299 RectangleAnchor anchor, double locationX, double locationY) { 300 if (shape == null) { 301 throw new IllegalArgumentException("Null 'shape' argument."); 302 } 303 if (anchor == null) { 304 throw new IllegalArgumentException("Null 'anchor' argument."); 305 } 306 Point2D anchorPoint = anchor.getAnchorPoint(shape.getBounds2D()); 307 final AffineTransform transform = AffineTransform.getTranslateInstance( 308 locationX - anchorPoint.getX(), locationY - anchorPoint.getY()); 309 return transform.createTransformedShape(shape); 310 } 311 312 /** 313 * Rotates a shape about the specified coordinates. 314 * 315 * @param base the shape ({@code null} permitted, returns 316 * {@code null}). 317 * @param angle the angle (in radians). 318 * @param x the x coordinate for the rotation point (in Java2D space). 319 * @param y the y coordinate for the rotation point (in Java2D space). 320 * 321 * @return the rotated shape. 322 */ 323 public static Shape rotateShape(Shape base, double angle, float x, float y) { 324 if (base == null) { 325 return null; 326 } 327 final AffineTransform rotate = AffineTransform.getRotateInstance( 328 angle, x, y); 329 final Shape result = rotate.createTransformedShape(base); 330 return result; 331 } 332 333 /** 334 * Draws a shape with the specified rotation about {@code (x, y)}. 335 * 336 * @param g2 the graphics device ({@code null} not permitted). 337 * @param shape the shape ({@code null} not permitted). 338 * @param angle the angle (in radians). 339 * @param x the x coordinate for the rotation point. 340 * @param y the y coordinate for the rotation point. 341 */ 342 public static void drawRotatedShape(Graphics2D g2, Shape shape, double angle, 343 float x, float y) { 344 345 AffineTransform saved = g2.getTransform(); 346 AffineTransform rotate = AffineTransform.getRotateInstance(angle, x, y); 347 g2.transform(rotate); 348 g2.draw(shape); 349 g2.setTransform(saved); 350 351 } 352 353 /** A useful constant used internally. */ 354 private static final float SQRT2 = (float) Math.pow(2.0, 0.5); 355 356 /** 357 * Creates a diagonal cross shape. 358 * 359 * @param l the length of each 'arm'. 360 * @param t the thickness. 361 * 362 * @return A diagonal cross shape. 363 */ 364 public static Shape createDiagonalCross(float l, float t) { 365 final GeneralPath p0 = new GeneralPath(); 366 p0.moveTo(-l - t, -l + t); 367 p0.lineTo(-l + t, -l - t); 368 p0.lineTo(0.0f, -t * SQRT2); 369 p0.lineTo(l - t, -l - t); 370 p0.lineTo(l + t, -l + t); 371 p0.lineTo(t * SQRT2, 0.0f); 372 p0.lineTo(l + t, l - t); 373 p0.lineTo(l - t, l + t); 374 p0.lineTo(0.0f, t * SQRT2); 375 p0.lineTo(-l + t, l + t); 376 p0.lineTo(-l - t, l - t); 377 p0.lineTo(-t * SQRT2, 0.0f); 378 p0.closePath(); 379 return p0; 380 } 381 382 /** 383 * Creates a diagonal cross shape. 384 * 385 * @param l the length of each 'arm'. 386 * @param t the thickness. 387 * 388 * @return A diagonal cross shape. 389 */ 390 public static Shape createRegularCross(float l, float t) { 391 final GeneralPath p0 = new GeneralPath(); 392 p0.moveTo(-l, t); 393 p0.lineTo(-t, t); 394 p0.lineTo(-t, l); 395 p0.lineTo(t, l); 396 p0.lineTo(t, t); 397 p0.lineTo(l, t); 398 p0.lineTo(l, -t); 399 p0.lineTo(t, -t); 400 p0.lineTo(t, -l); 401 p0.lineTo(-t, -l); 402 p0.lineTo(-t, -t); 403 p0.lineTo(-l, -t); 404 p0.closePath(); 405 return p0; 406 } 407 408 /** 409 * Creates a diamond shape. 410 * 411 * @param s the size factor (equal to half the height of the diamond). 412 * 413 * @return A diamond shape. 414 */ 415 public static Shape createDiamond(float s) { 416 final GeneralPath p0 = new GeneralPath(); 417 p0.moveTo(0.0f, -s); 418 p0.lineTo(s, 0.0f); 419 p0.lineTo(0.0f, s); 420 p0.lineTo(-s, 0.0f); 421 p0.closePath(); 422 return p0; 423 } 424 425 /** 426 * Creates a triangle shape that points upwards. 427 * 428 * @param s the size factor (equal to half the height of the triangle). 429 * 430 * @return A triangle shape. 431 */ 432 public static Shape createUpTriangle(float s) { 433 final GeneralPath p0 = new GeneralPath(); 434 p0.moveTo(0.0f, -s); 435 p0.lineTo(s, s); 436 p0.lineTo(-s, s); 437 p0.closePath(); 438 return p0; 439 } 440 441 /** 442 * Creates a triangle shape that points downwards. 443 * 444 * @param s the size factor (equal to half the height of the triangle). 445 * 446 * @return A triangle shape. 447 */ 448 public static Shape createDownTriangle(float s) { 449 final GeneralPath p0 = new GeneralPath(); 450 p0.moveTo(0.0f, s); 451 p0.lineTo(s, -s); 452 p0.lineTo(-s, -s); 453 p0.closePath(); 454 return p0; 455 } 456 457 /** 458 * Creates a region surrounding a line segment by 'widening' the line 459 * segment. A typical use for this method is the creation of a 460 * 'clickable' region for a line that is displayed on-screen. 461 * 462 * @param line the line ({@code null} not permitted). 463 * @param width the width of the region. 464 * 465 * @return A region that surrounds the line. 466 */ 467 public static Shape createLineRegion(Line2D line, float width) { 468 final GeneralPath result = new GeneralPath(); 469 final float x1 = (float) line.getX1(); 470 final float x2 = (float) line.getX2(); 471 final float y1 = (float) line.getY1(); 472 final float y2 = (float) line.getY2(); 473 if ((x2 - x1) != 0.0) { 474 final double theta = Math.atan((y2 - y1) / (x2 - x1)); 475 final float dx = (float) Math.sin(theta) * width; 476 final float dy = (float) Math.cos(theta) * width; 477 result.moveTo(x1 - dx, y1 + dy); 478 result.lineTo(x1 + dx, y1 - dy); 479 result.lineTo(x2 + dx, y2 - dy); 480 result.lineTo(x2 - dx, y2 + dy); 481 result.closePath(); 482 } 483 else { 484 // special case, vertical line 485 result.moveTo(x1 - width / 2.0f, y1); 486 result.lineTo(x1 + width / 2.0f, y1); 487 result.lineTo(x2 + width / 2.0f, y2); 488 result.lineTo(x2 - width / 2.0f, y2); 489 result.closePath(); 490 } 491 return result; 492 } 493 494 /** 495 * Returns a point based on (x, y) but constrained to be within the bounds 496 * of a given rectangle. 497 * 498 * @param x the x-coordinate. 499 * @param y the y-coordinate. 500 * @param area the constraining rectangle ({@code null} not 501 * permitted). 502 * 503 * @return A point within the rectangle. 504 * 505 * @throws NullPointerException if {@code area} is {@code null}. 506 */ 507 public static Point2D getPointInRectangle(double x, double y, 508 Rectangle2D area) { 509 510 x = Math.max(area.getMinX(), Math.min(x, area.getMaxX())); 511 y = Math.max(area.getMinY(), Math.min(y, area.getMaxY())); 512 return new Point2D.Double(x, y); 513 514 } 515 516 /** 517 * Checks, whether the given rectangle1 fully contains rectangle 2 518 * (even if rectangle 2 has a height or width of zero!). 519 * 520 * @param rect1 the first rectangle. 521 * @param rect2 the second rectangle. 522 * 523 * @return A boolean. 524 */ 525 public static boolean contains(Rectangle2D rect1, Rectangle2D rect2) { 526 527 final double x0 = rect1.getX(); 528 final double y0 = rect1.getY(); 529 final double x = rect2.getX(); 530 final double y = rect2.getY(); 531 final double w = rect2.getWidth(); 532 final double h = rect2.getHeight(); 533 534 return ((x >= x0) && (y >= y0) 535 && ((x + w) <= (x0 + rect1.getWidth())) 536 && ((y + h) <= (y0 + rect1.getHeight()))); 537 538 } 539 540 /** 541 * Checks, whether the given rectangle1 fully contains rectangle 2 542 * (even if rectangle 2 has a height or width of zero!). 543 * 544 * @param rect1 the first rectangle. 545 * @param rect2 the second rectangle. 546 * 547 * @return A boolean. 548 */ 549 public static boolean intersects(Rectangle2D rect1, Rectangle2D rect2) { 550 551 final double x0 = rect1.getX(); 552 final double y0 = rect1.getY(); 553 554 final double x = rect2.getX(); 555 final double width = rect2.getWidth(); 556 final double y = rect2.getY(); 557 final double height = rect2.getHeight(); 558 return (x + width >= x0 && y + height >= y0 && x <= x0 + rect1.getWidth() 559 && y <= y0 + rect1.getHeight()); 560 } 561 562 /** 563 * Returns {@code true} if the specified point (x, y) falls within or 564 * on the boundary of the specified rectangle. 565 * 566 * @param rect the rectangle ({@code null} not permitted). 567 * @param x the x-coordinate. 568 * @param y the y-coordinate. 569 * 570 * @return A boolean. 571 */ 572 public static boolean isPointInRect(Rectangle2D rect, double x, double y) { 573 return (x >= rect.getMinX() && x <= rect.getMaxX() 574 && y >= rect.getMinY() && y <= rect.getMaxY()); 575 } 576 577} 578