001/* 002 * To change this template, choose Tools | Templates 003 * and open the template in the editor. 004 */ 005package armyc2.c2sd.graphics2d; 006/* 007 * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved. 008 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 009 * 010 * This code is free software; you can redistribute it and/or modify it 011 * under the terms of the GNU General Public License version 2 only, as 012 * published by the Free Software Foundation. Sun designates this 013 * particular file as subject to the "Classpath" exception as provided 014 * by Sun in the LICENSE file that accompanied this code. 015 * 016 * This code is distributed in the hope that it will be useful, but WITHOUT 017 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 018 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 019 * version 2 for more details (a copy is included in the LICENSE file that 020 * accompanied this code). 021 * 022 * You should have received a copy of the GNU General Public License version 023 * 2 along with this work; if not, write to the Free Software Foundation, 024 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 025 * 026 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 027 * CA 95054 USA or visit www.sun.com if you need additional information or 028 * have any questions. 029 */ 030 031import java.io.Serializable; 032import armyc2.c2sd.JavaLineArray.lineutility; 033/** 034 * This <code>Line2D</code> represents a line segment in {@code (x,y)} 035 * coordinate space. This class, like all of the Java 2D API, uses a default 036 * coordinate system called <i>user space</i> in which the y-axis values 037 * increase downward and x-axis values increase to the right. For more 038 * information on the user space coordinate system, see the <a href= 039 * "http://java.sun.com/j2se/1.3/docs/guide/2d/spec/j2d-intro.fm2.html#61857"> 040 * Coordinate Systems</a> section of the Java 2D Programmer's Guide. 041 * <p> 042 * This class is only the abstract superclass for all objects that store a 2D 043 * line segment. The actual storage representation of the coordinates is left to 044 * the subclass. 045 * 046 * @author Jim Graham 047 * @since 1.2 048 */ 049public abstract class Line2D { 050 public Rectangle2D getBounds2D() 051 { 052 double x1=getX1(); 053 double y1=getY1(); 054 double x2=getX1(); 055 double y2=getY1(); 056 double x=x1; 057 double y=y1; 058 if(x2<x1) 059 x=x2; 060 if(y2<y1) 061 y=y2; 062 double width=Math.abs(x1-x2); 063 double height=Math.abs(y1-y2); 064 Rectangle2D rect=new Rectangle2D.Double(x,y,width,height); 065 return rect; 066 } 067 068 public boolean intersectsLine(Line2D edge) 069 { 070 double x1=getX1(); 071 double y1=getY1(); 072 double x2=getX2(); 073 double y2=getY2(); 074 double edgex1=edge.getX1(); 075 double edgey1=edge.getY1(); 076 double edgex2=edge.getX2(); 077 double edgey2=edge.getY2(); 078 079 //handle vertical lines 080 if(x2==x1 && edgex2==edgex1) 081 { 082 if(x1 != edgex1) 083 return false; 084 085 if(y1<y2) 086 { 087 if(y1<=edgey1 && edgey1<=y2) 088 return true; 089 else if(y1<=edgey2 && edgey2<=y2) 090 return true; 091 else 092 return false; 093 } 094 else if(y2<y1) 095 { 096 if(y2<=edgey1 && edgey1<=y1) 097 return true; 098 else if(y2<=edgey2 && edgey2<=y1) 099 return true; 100 else 101 return false; 102 } 103 } 104 //do the mbr's intersect? 105 if(x1<x2) //was x1<=x2 106 { 107 if(edgex1<x1 && edgex2<x1) 108 return false; 109 if(edgex1>x2 && edgex2>x2) 110 return false; 111 } 112 else if(x2<x1) 113 { 114 if(edgex1<x2 && edgex2<x2) 115 return false; 116 if(edgex1>x1 && edgex2>x1) 117 return false; 118 } 119 if(y1<y2) //was y1<=y2 120 { 121 if(edgey1<y1 && edgey2<x1) 122 return false; 123 if(edgey1>y2 && edgey2>y2) 124 return false; 125 } 126 else if(y2<y1) 127 { 128 if(edgey1<y2 && edgey2<y2) 129 return false; 130 if(edgey1>y1 && edgey2>y1) 131 return false; 132 } 133 if(x1==x2) 134 { 135 if(x1<edgex1 && x1<edgex2) 136 return false; 137 if(x1>edgex1 && x1>edgex2) 138 return false; 139 } 140 if(y1==y2) 141 { 142 if(y1<edgey1 && y1<edgey2) 143 return false; 144 if(y1>edgey1 && y1>edgey2) 145 return false; 146 } 147 148 //if we reach this point we have nonvertical lines with intersecting mbr's 149 double slope=(y2-y1)/(x2-x1); 150 double b1=y2-slope*x2; 151 double edgeSlope=(edgey2-edgey1)/(edgex2-edgex1); 152 double b2=edgey2-edgeSlope*edgex2; 153 Rectangle2D rect=new Rectangle2D.Double(x1,y1,x2,y2); 154 Rectangle2D rect2=new Rectangle2D.Double(edgex1,edgey1,Math.abs(edgex1-edgex2),Math.abs(edgey1-edgey2)); 155 if(slope==edgeSlope) 156 { 157 if(b1==b2 && rect.intersects(rect2)==true) 158 return true; 159 else 160 return false; 161 } 162 else 163 { 164 //non-vertical lines 165 //calculate the theoretical intersection point x,y from the two line equations 166 double x=(b2-b1)/(slope-edgeSlope); 167 double y=(slope*x+b1); 168 //the first rect 169 if(x1<x2) 170 { 171 if(x<x1) 172 return false; 173 if(x>x2) 174 return false; 175 } 176 else if(x2<x1) 177 { 178 if(x<x2) 179 return false; 180 if(x>x1) 181 return false; 182 } 183 if(y1<y2) 184 { 185 if(y<y1) 186 return false; 187 if(y>y2) 188 return false; 189 } 190 else if(y2<y1) 191 { 192 if(y<y2) 193 return false; 194 if(y>y1) 195 return false; 196 } 197 //the edge rect 198 if(edgex1<edgex2) 199 { 200 if(x<edgex1) 201 return false; 202 if(x>edgex2) 203 return false; 204 } 205 else if(edgex2<edgex1) 206 { 207 if(x<edgex2) 208 return false; 209 if(x>edgex1) 210 return false; 211 } 212 if(edgey1<edgey2) 213 { 214 if(y<edgey1) 215 return false; 216 if(y>edgey2) 217 return false; 218 } 219 else if(edgey2<edgey1) 220 { 221 if(y<edgey2) 222 return false; 223 if(y>edgey1) 224 return false; 225 } 226 } 227 return true; 228 } 229 /** 230 * A line segment specified with float coordinates. 231 * 232 * @since 1.2 233 */ 234 public static class Float extends Line2D implements Serializable { 235 236 /** 237 * The X coordinate of the start point of the line segment. 238 * 239 * @since 1.2 240 * @serial 241 */ 242 public float x1; 243 /** 244 * The Y coordinate of the start point of the line segment. 245 * 246 * @since 1.2 247 * @serial 248 */ 249 public float y1; 250 /** 251 * The X coordinate of the end point of the line segment. 252 * 253 * @since 1.2 254 * @serial 255 */ 256 public float x2; 257 /** 258 * The Y coordinate of the end point of the line segment. 259 * 260 * @since 1.2 261 Line2D.java : » App » android-app-examples » org » loon » framework » android » game ...Page 1 of 14 262 http://www.java2s.com/Open-Source/Android/App/android-app-examples/org/loon/frame... 2/25/2013 263 * @serial 264 */ 265 public float y2; 266 267 /** 268 * Constructs and initializes a Line with coordinates (0, 0) -> (0, 0). 269 * 270 * @since 1.2 271 */ 272 public Float() { 273 } 274 275 /** 276 * Constructs and initializes a Line from the specified coordinates. 277 * 278 * @param x1 279 * the X coordinate of the start point 280 * @param y1 281 * the Y coordinate of the start point 282 * @param x2 283 * the X coordinate of the end point 284 * @param y2 285 * the Y coordinate of the end point 286 * @since 1.2 287 */ 288 public Float(float x1, float y1, float x2, float y2) { 289 setLine(x1, y1, x2, y2); 290 } 291 292 /** 293 * Constructs and initializes a <code>Line2D</code> from the specified 294 * <code>Point2D</code> objects. 295 * 296 * @param p1 297 * the start <code>Point2D</code> of this line segment 298 * @param p2 299 * the end <code>Point2D</code> of this line segment 300 * @since 1.2 301 */ 302 public Float(Point2D p1, Point2D p2) { 303 setLine(p1, p2); 304 } 305 306 /** 307 * {@inheritDoc} 308 * 309 * @since 1.2 310 */ 311 public double getX1() { 312 return (double) x1; 313 } 314 315 /** 316 * {@inheritDoc} 317 * 318 * @since 1.2 319 */ 320 public double getY1() { 321 return (double) y1; 322 } 323 324 /** 325 * {@inheritDoc} 326 * 327 * @since 1.2 328 */ 329 public Point2D getP1() { 330 return new Point2D.Float(x1, y1); 331 } 332 333 /** 334 * {@inheritDoc} 335 * 336 * @since 1.2 337 */ 338 public double getX2() { 339 return (double) x2; 340 } 341 342 /** 343 * {@inheritDoc} 344 * 345 * @since 1.2 346 */ 347 public double getY2() { 348 return (double) y2; 349 } 350 351 /** 352 * {@inheritDoc} 353 * 354 Line2D.java : » App » android-app-examples » org » loon » framework » android » game ...Page 2 of 14 355 http://www.java2s.com/Open-Source/Android/App/android-app-examples/org/loon/frame... 2/25/2013 356 * @since 1.2 357 */ 358 public Point2D getP2() { 359 return new Point2D.Float(x2, y2); 360 } 361 362 /** 363 * {@inheritDoc} 364 * 365 * @since 1.2 366 */ 367 public void setLine(double x1, double y1, double x2, double y2) { 368 this.x1 = (float) x1; 369 this.y1 = (float) y1; 370 this.x2 = (float) x2; 371 this.y2 = (float) y2; 372 } 373 374 /** 375 * Sets the location of the end points of this <code>Line2D</code> to 376 * the specified float coordinates. 377 * 378 * @param x1 379 * the X coordinate of the start point 380 * @param y1 381 * the Y coordinate of the start point 382 * @param x2 383 * the X coordinate of the end point 384 * @param y2 385 * the Y coordinate of the end point 386 * @since 1.2 387 */ 388 public void setLine(float x1, float y1, float x2, float y2) { 389 this.x1 = x1; 390 this.y1 = y1; 391 this.x2 = x2; 392 this.y2 = y2; 393 } 394 395 /** 396 * {@inheritDoc} 397 * 398 * @since 1.2 399 */ 400 public Rectangle2D getBounds2D() { 401 float x, y, w, h; 402 if (x1 < x2) { 403 x = x1; 404 w = x2 - x1; 405 } else { 406 x = x2; 407 w = x1 - x2; 408 } 409 if (y1 < y2) { 410 y = y1; 411 h = y2 - y1; 412 } else { 413 y = y2; 414 h = y1 - y2; 415 } 416 return new Rectangle2D.Double(x, y, w, h); 417 } 418 /* 419 * JDK 1.6 serialVersionUID 420 */ 421 private static final long serialVersionUID = 6161772511649436349L; 422 } 423 424 /** 425 * A line segment specified with double coordinates. 426 * 427 * @since 1.2 428 */ 429 public static class Double extends Line2D implements Serializable { 430 431 /** 432 * The X coordinate of the start point of the line segment. 433 * 434 * @since 1.2 435 * @serial 436 */ 437 public double x1; 438 /** 439 * The Y coordinate of the start point of the line segment. 440 * 441 * @since 1.2 442 * @serial 443 */ 444 public double y1; 445 /** 446 * The X coordinate of the end point of the line segment. 447 * 448 * @since 1.2 449 * @serial 450 */ 451 public double x2; 452 /** 453 * The Y coordinate of the end point of the line segment. 454 * 455 * @since 1.2 456 * @serial 457 */ 458 public double y2; 459 460 /** 461 * Constructs and initializes a Line with coordinates (0, 0) -> (0, 0). 462 * 463 * @since 1.2 464 */ 465 public Double() { 466 } 467 468 /** 469 * Constructs and initializes a <code>Line2D</code> from the specified 470 * coordinates. 471 * 472 * @param x1 473 * the X coordinate of the start point 474 * @param y1 475 * the Y coordinate of the start point 476 * @param x2 477 * the X coordinate of the end point 478 * @param y2 479 * the Y coordinate of the end point 480 * @since 1.2 481 */ 482 public Double(double x1, double y1, double x2, double y2) { 483 setLine(x1, y1, x2, y2); 484 } 485 486 /** 487 * Constructs and initializes a <code>Line2D</code> from the specified 488 * <code>Point2D</code> objects. 489 * 490 * @param p1 491 * the start <code>Point2D</code> of this line segment 492 * @param p2 493 * the end <code>Point2D</code> of this line segment 494 * @since 1.2 495 */ 496 public Double(Point2D p1, Point2D p2) { 497 setLine(p1, p2); 498 } 499 500 /** 501 * {@inheritDoc} 502 * 503 * @since 1.2 504 */ 505 public double getX1() { 506 return x1; 507 } 508 509 /** 510 * {@inheritDoc} 511 * 512 * @since 1.2 513 */ 514 public double getY1() { 515 return y1; 516 } 517 518 /** 519 * {@inheritDoc} 520 * 521 * @since 1.2 522 */ 523 public Point2D getP1() { 524 return new Point2D.Double(x1, y1); 525 } 526 527 /** 528 * {@inheritDoc} 529 * 530 * @since 1.2 531 */ 532 public double getX2() { 533 return x2; 534 } 535 536 /** 537 * {@inheritDoc} 538 * 539 * @since 1.2 540 */ 541 public double getY2() { 542 return y2; 543 } 544 545 /** 546 * {@inheritDoc} 547 * 548 * @since 1.2 549 */ 550 public Point2D getP2() { 551 return new Point2D.Double(x2, y2); 552 } 553 554 /** 555 * {@inheritDoc} 556 * 557 * @since 1.2 558 */ 559 public void setLine(double x1, double y1, double x2, double y2) { 560 this.x1 = x1; 561 this.y1 = y1; 562 this.x2 = x2; 563 this.y2 = y2; 564 } 565 566 /** 567 * {@inheritDoc} 568 * 569 * @since 1.2 570 */ 571 public Rectangle2D getBounds2D() { 572 double x, y, w, h; 573 if (x1 < x2) { 574 x = x1; 575 w = x2 - x1; 576 } else { 577 x = x2; 578 w = x1 - x2; 579 } 580 if (y1 < y2) { 581 y = y1; 582 h = y2 - y1; 583 } else { 584 y = y2; 585 h = y1 - y2; 586 } 587 return new Rectangle2D.Double(x, y, w, h); 588 } 589 /* 590 * JDK 1.6 serialVersionUID 591 */ 592 private static final long serialVersionUID = 7979627399746467499L; 593 } 594 595 /** 596 * This is an abstract class that cannot be instantiated directly. 597 * Type-specific implementation subclasses are available for instantiation 598 * and provide a number of formats for storing the information necessary to 599 * satisfy the various accessory methods below. 600 * 601 * @see and.awt.geom.Line2D.Float 602 * @see and.awt.geom.Line2D.Double 603 * @since 1.2 604 */ 605 protected Line2D() { 606 } 607 608 /** 609 * Returns the X coordinate of the start point in double precision. 610 * 611 * @return the X coordinate of the start point of this {@code Line2D} 612 * object. 613 * @since 1.2 614 */ 615 public abstract double getX1(); 616 617 /** 618 * Returns the Y coordinate of the start point in double precision. 619 * 620 * @return the Y coordinate of the start point of this {@code Line2D} 621 * object. 622 * @since 1.2 623 */ 624 public abstract double getY1(); 625 626 /** 627 * Returns the start <code>Point2D</code> of this <code>Line2D</code>. 628 * 629 * @return the start <code>Point2D</code> of this <code>Line2D</code>. 630 * @since 1.2 631 */ 632 public abstract Point2D getP1(); 633 634 /** 635 * Returns the X coordinate of the end point in double precision. 636 * 637 * @return the X coordinate of the end point of this {@code Line2D} object. 638 * @since 1.2 639 */ 640 public abstract double getX2(); 641 642 /** 643 * Returns the Y coordinate of the end point in double precision. 644 * 645 * @return the Y coordinate of the end point of this {@code Line2D} object. 646 * @since 1.2 647 */ 648 public abstract double getY2(); 649 650 /** 651 * Returns the end <code>Point2D</code> of this <code>Line2D</code>. 652 * 653 * @return the end <code>Point2D</code> of this <code>Line2D</code>. 654 * @since 1.2 655 */ 656 public abstract Point2D getP2(); 657 658 /** 659 * Sets the location of the end points of this <code>Line2D</code> to the 660 * specified double coordinates. 661 * 662 * @param x1 663 * the X coordinate of the start point 664 * @param y1 665 * the Y coordinate of the start point 666 * @param x2 667 * the X coordinate of the end point 668 * @param y2 669 * the Y coordinate of the end point 670 * @since 1.2 671 */ 672 public abstract void setLine(double x1, double y1, double x2, double y2); 673 674 /** 675 * Sets the location of the end points of this <code>Line2D</code> to the 676 * specified <code>Point2D</code> coordinates. 677 * 678 * @param p1 679 * the start <code>Point2D</code> of the line segment 680 * @param p2 681 * the end <code>Point2D</code> of the line segment 682 * @since 1.2 683 */ 684 public void setLine(Point2D p1, Point2D p2) { 685 setLine(p1.getX(), p1.getY(), p2.getX(), p2.getY()); 686 } 687 688 /** 689 * Sets the location of the end points of this <code>Line2D</code> to the 690 * same as those end points of the specified <code>Line2D</code>. 691 * 692 * @param l 693 * the specified <code>Line2D</code> 694 * @since 1.2 695 */ 696 public void setLine(Line2D l) { 697 setLine(l.getX1(), l.getY1(), l.getX2(), l.getY2()); 698 } 699 700 /** 701 * Returns an indicator of where the specified point {@code (px,py)} lies 702 * with respect to the line segment from {@code (x1,y1)} to {@code (x2,y2)}. 703 * The return value can be either 1, -1, or 0 and indicates in which 704 * direction the specified line must pivot around its first end point, 705 * {@code (x1,y1)}, in order to point at the specified point {@code (px,py)} 706 * . 707 * <p> 708 * A return value of 1 indicates that the line segment must turn in the 709 * direction that takes the positive X axis towards the negative Y axis. In 710 * the default coordinate system used by Java 2D, this direction is 711 * counterclockwise. 712 * <p> 713 * A return value of -1 indicates that the line segment must turn in the 714 * direction that takes the positive X axis towards the positive Y axis. In 715 * the default coordinate system, this direction is clockwise. 716 Line2D.java : » App » android-app-examples » org » loon » framework » android » game ...Page 6 of 14 717 http://www.java2s.com/Open-Source/Android/App/android-app-examples/org/loon/frame... 2/25/2013 718 * <p> 719 * A return value of 0 indicates that the point lies exactly on the line 720 * segment. Note that an indicator value of 0 is rare and not useful for 721 * determining colinearity because of floating point rounding issues. 722 * <p> 723 * If the point is colinear with the line segment, but not between the end 724 * points, then the value will be -1 if the point lies "beyond {@code 725 * (x1,y1)}" or 1 if the point lies "beyond {@code (x2,y2)}". 726 * 727 * @param x1 728 * the X coordinate of the start point of the specified line 729 * segment 730 * @param y1 731 * the Y coordinate of the start point of the specified line 732 * segment 733 * @param x2 734 * the X coordinate of the end point of the specified line 735 * segment 736 * @param y2 737 * the Y coordinate of the end point of the specified line 738 * segment 739 * @param px 740 * the X coordinate of the specified point to be compared with 741 * the specified line segment 742 * @param py 743 * the Y coordinate of the specified point to be compared with 744 * the specified line segment 745 * @return an integer that indicates the position of the third specified 746 * coordinates with respect to the line segment formed by the first 747 * two specified coordinates. 748 * @since 1.2 749 */ 750 public static int relativeCCW(double x1, double y1, double x2, double y2, 751 double px, double py) { 752 x2 -= x1; 753 y2 -= y1; 754 px -= x1; 755 py -= y1; 756 double ccw = px * y2 - py * x2; 757 if (ccw == 0.0) { 758// The point is colinear, classify based on which side of 759// the segment the point falls on. We can calculate a 760// relative value using the projection of px,py onto the 761// segment - a negative value indicates the point projects 762// outside of the segment in the direction of the particular 763// endpoint used as the origin for the projection. 764 ccw = px * x2 + py * y2; 765 if (ccw > 0.0) { 766// Reverse the projection to be relative to the original x2,y2 767// x2 and y2 are simply negated. 768// px and py need to have (x2 - x1) or (y2 - y1) subtracted 769// from them (based on the original values) 770// Since we really want to get a positive answer when the 771// point is "beyond (x2,y2)", then we want to calculate 772// the inverse anyway - thus we leave x2 & y2 negated. 773 px -= x2; 774 py -= y2; 775 ccw = px * x2 + py * y2; 776 if (ccw < 0.0) { 777 ccw = 0.0; 778 } 779 } 780 } 781 return (ccw < 0.0) ? -1 : ((ccw > 0.0) ? 1 : 0); 782 } 783 784 /** 785 * Returns an indicator of where the specified point {@code (px,py)} lies 786 * with respect to this line segment. See the method comments of 787 * {@link #relativeCCW(double, double, double, double, double, double)} to 788 * interpret the return value. 789 * 790 * @param px 791 * the X coordinate of the specified point to be compared with 792 * this <code>Line2D</code> 793 * @param py 794 * the Y coordinate of the specified point to be compared with 795 * this <code>Line2D</code> 796 * @return an integer that indicates the position of the specified 797 * coordinates with respect to this <code>Line2D</code> 798 * @see #relativeCCW(double, double, double, double, double, double) 799 * @since 1.2 800 */ 801 public int relativeCCW(double px, double py) { 802 return relativeCCW(getX1(), getY1(), getX2(), getY2(), px, py); 803 } 804 805 /** 806 * Returns the square of the distance from a point to a line. The distance 807 * measured is the distance between the specified point and the closest 808 * point on the infinitely-extended line defined by the specified 809 * coordinates. If the specified point intersects the line, this method 810 * returns 0.0. 811 * 812 * @param x1 813 * the X coordinate of the start point of the specified line 814 * @param y1 815 * the Y coordinate of the start point of the specified line 816 * @param x2 817 * the X coordinate of the end point of the specified line 818 * @param y2 819 * the Y coordinate of the end point of the specified line 820 * @param px 821 * the X coordinate of the specified point being measured against 822 * the specified line 823 * @param py 824 * the Y coordinate of the specified point being measured against 825 * the specified line 826 * @return a double value that is the square of the distance from the 827 * specified point to the specified line. 828 * @see #ptSegDistSq(double, double, double, double, double, double) 829 * @since 1.2 830 */ 831 public static double ptLineDistSq(double x1, double y1, double x2, 832 double y2, double px, double py) { 833// Adjust vectors relative to x1,y1 834// x2,y2 becomes relative vector from x1,y1 to end of segment 835 x2 -= x1; 836 y2 -= y1; 837// px,py becomes relative vector from x1,y1 to test point 838 px -= x1; 839 py -= y1; 840 double dotprod = px * x2 + py * y2; 841// dotprod is the length of the px,py vector 842// projected on the x1,y1=>x2,y2 vector times the 843// length of the x1,y1=>x2,y2 vector 844 double projlenSq = dotprod * dotprod / (x2 * x2 + y2 * y2); 845// Distance to line is now the length of the relative point 846// vector minus the length of its projection onto the line 847 double lenSq = px * px + py * py - projlenSq; 848 if (lenSq < 0) { 849 lenSq = 0; 850 } 851 return lenSq; 852 } 853 854 /** 855 * Returns the distance from a point to a line. The distance measured is the 856 * distance between the specified point and the closest point on the 857 * infinitely-extended line defined by the specified coordinates. If the 858 * specified point intersects the line, this method returns 0.0. 859 * 860 * @param x1 861 * the X coordinate of the start point of the specified line 862 * @param y1 863 * the Y coordinate of the start point of the specified line 864 * @param x2 865 * the X coordinate of the end point of the specified line 866 * @param y2 867 * the Y coordinate of the end point of the specified line 868 * @param px 869 * the X coordinate of the specified point being measured against 870 * the specified line 871 * @param py 872 * the Y coordinate of the specified point being measured against 873 * the specified line 874 * @return a double value that is the distance from the specified point to 875 * the specified line. 876 * @see #ptSegDist(double, double, double, double, double, double) 877 Line2D.java : » App » android-app-examples » org » loon » framework » android » ga... Page 11 of 14 878 http://www.java2s.com/Open-Source/Android/App/android-app-examples/org/loon/frame... 2/25/2013 879 * @since 1.2 880 */ 881 public static double ptLineDist(double x1, double y1, double x2, double y2, 882 double px, double py) { 883 return Math.sqrt(ptLineDistSq(x1, y1, x2, y2, px, py)); 884 } 885 886 /** 887 * Returns the square of the distance from a point to this line. The 888 * distance measured is the distance between the specified point and the 889 * closest point on the infinitely-extended line defined by this 890 * <code>Line2D</code>. If the specified point intersects the line, this 891 * method returns 0.0. 892 * 893 * @param px 894 * the X coordinate of the specified point being measured against 895 * this line 896 * @param py 897 * the Y coordinate of the specified point being measured against 898 * this line 899 * @return a double value that is the square of the distance from a 900 * specified point to the current line. 901 * @see #ptSegDistSq(double, double) 902 * @since 1.2 903 */ 904 public double ptLineDistSq(double px, double py) { 905 return ptLineDistSq(getX1(), getY1(), getX2(), getY2(), px, py); 906 } 907 908 /** 909 * Returns the square of the distance from a specified <code>Point2D</code> 910 * to this line. The distance measured is the distance between the specified 911 * point and the closest point on the infinitely-extended line defined by 912 * this <code>Line2D</code>. If the specified point intersects the line, 913 * this method returns 0.0. 914 * 915 * @param pt 916 * the specified <code>Point2D</code> being measured against this 917 * line 918 * @return a double value that is the square of the distance from a 919 * specified <code>Point2D</code> to the current line. 920 * @see #ptSegDistSq(Point2D) 921 * @since 1.2 922 */ 923 public double ptLineDistSq(Point2D pt) { 924 return ptLineDistSq(getX1(), getY1(), getX2(), getY2(), pt.getX(), pt.getY()); 925 } 926 927 /** 928 * Returns the distance from a point to this line. The distance measured is 929 * the distance between the specified point and the closest point on the 930 * infinitely-extended line defined by this <code>Line2D</code>. If the 931 * specified point intersects the line, this method returns 0.0. 932 * 933 * @param px 934 * the X coordinate of the specified point being measured against 935 * this line 936 * @param py 937 * the Y coordinate of the specified point being measured against 938 * this line 939 * @return a double value that is the distance from a specified point to the 940 * current line. 941 * @see #ptSegDist(double, double) 942 * @since 1.2 943 */ 944 public double ptLineDist(double px, double py) { 945 return ptLineDist(getX1(), getY1(), getX2(), getY2(), px, py); 946 } 947 948 /** 949 * Returns the distance from a <code>Point2D</code> to this line. The 950 * distance measured is the distance between the specified point and the 951 * closest point on the infinitely-extended line defined by this 952 * <code>Line2D</code>. If the specified point intersects the line, this 953 * method returns 0.0. 954 * 955 * @param pt 956 * the specified <code>Point2D</code> being measured 957 * @return a double value that is the distance from a specified 958 * <code>Point2D</code> to the current line. 959 * @see #ptSegDist(Point2D) 960 * @since 1.2 961 */ 962 public double ptLineDist(Point2D pt) { 963 return ptLineDist(getX1(), getY1(), getX2(), getY2(), pt.getX(), pt.getY()); 964 } 965 966 /** 967 * Tests if a specified coordinate is inside the boundary of this 968 Line2D.java : » App » android-app-examples » org » loon » framework » android » ga... Page 12 of 14 969 http://www.java2s.com/Open-Source/Android/App/android-app-examples/org/loon/frame... 2/25/2013 970 * <code>Line2D</code>. This method is required to implement the 971 * {@link Shape} interface, but in the case of <code>Line2D</code> objects 972 * it always returns <code>false</code> since a line contains no area. 973 * 974 * @param x 975 * the X coordinate of the specified point to be tested 976 * @param y 977 * the Y coordinate of the specified point to be tested 978 * @return <code>false</code> because a <code>Line2D</code> contains no 979 * area. 980 * @since 1.2 981 */ 982 public boolean contains(double x, double y) { 983 return false; 984 } 985 986 /** 987 * Tests if a given <code>Point2D</code> is inside the boundary of this 988 * <code>Line2D</code>. This method is required to implement the 989 * {@link Shape} interface, but in the case of <code>Line2D</code> objects 990 * it always returns <code>false</code> since a line contains no area. 991 * 992 * @param p 993 * the specified <code>Point2D</code> to be tested 994 * @return <code>false</code> because a <code>Line2D</code> contains no 995 * area. 996 * @since 1.2 997 */ 998 public boolean contains(Point2D p) { 999 return false; 1000 } 1001 1002 /** 1003 * Tests if the interior of this <code>Line2D</code> entirely contains the 1004 * specified set of rectangular coordinates. This method is required to 1005 * implement the <code>Shape</code> interface, but in the case of 1006 * <code>Line2D</code> objects it always returns false since a line contains 1007 * no area. 1008 * 1009 * @param x 1010 * the X coordinate of the upper-left corner of the specified 1011 * rectangular area 1012 * @param y 1013 * the Y coordinate of the upper-left corner of the specified 1014 * rectangular area 1015 * @param w 1016 * the width of the specified rectangular area 1017 * @param h 1018 * the height of the specified rectangular area 1019 * @return <code>false</code> because a <code>Line2D</code> contains no 1020 * area. 1021 * @since 1.2 1022 */ 1023 public boolean contains(double x, double y, double w, double h) { 1024 return false; 1025 } 1026 1027 /** 1028 * Tests if the interior of this <code>Line2D</code> entirely contains the 1029 * specified <code>Rectangle2D</code>. This method is required to implement 1030 * the <code>Shape</code> interface, but in the case of <code>Line2D</code> 1031 * objects it always returns <code>false</code> since a line contains no 1032 * area. 1033 * 1034 * @param r 1035 * the specified <code>Rectangle2D</code> to be tested 1036 * @return <code>false</code> because a <code>Line2D</code> contains no 1037 * area. 1038 * @since 1.2 1039 */ 1040 public boolean contains(Rectangle2D r) { 1041 return false; 1042 } 1043 /** 1044 * Creates a new object of the same class as this object. 1045 * 1046 * @return a clone of this instance. 1047 * @exception OutOfMemoryError 1048 * if there is not enough memory. 1049 * @see java.lang.Cloneable 1050 * @since 1.2 1051 */ 1052 public Object clone() { 1053 try { 1054 return super.clone(); 1055 } catch (CloneNotSupportedException e) { 1056// this shouldn't happen, since we are Cloneable 1057 throw new InternalError(); 1058 } 1059 } 1060}