Login | Register
My pages Projects Community openCollabNet

Discussions > commits > svn commit: r1333 - trunk/src/org/tigris/gef/base/Geometry.java

gef
Discussion topic

Back to topic list

svn commit: r1333 - trunk/src/org/tigris/gef/base/Geometry.java

Author bobtarling
Full name Bob Tarling
Date 2011-05-22 03:27:25 PDT
Message Author: bobtarling
Date: 2011-05-22 03:27:24-0700
New Revision: 1333

Modified:
   trunk/src/org/tigris​/gef/base/Geometry.j​ava

Log:
Change algorithm for ptGetClosestTo for a point to rectangle

This now finds the closest point on the rectangle boundary rather than any point within the rectangle,

Modified: trunk/src/org/tigris​/gef/base/Geometry.j​ava
Url: http://gef.tigris.or​g/source/browse/gef/​trunk/src/org/tigris​/gef/base/Geometry.j​ava?view=diff&pa​threv=1333&r1=13​32&r2=1333
====================​====================​====================​==================
--- trunk/src/org/tigris​/gef/base/Geometry.j​ava (original)
+++ trunk/src/org/tigris​/gef/base/Geometry.j​ava 2011-05-22 03:27:24-0700
@@ -44,61 +44,92 @@
      * Rectangle that is closest to the given point.
      */
     public static void ptClosestTo(Rectangle r, Point p, Point res) {
- int x1 = Math.min(r.x, r.x + r.width);
- int y1 = Math.min(r.y, r.y + r.height);
- int x2 = Math.max(r.x, r.x + r.width);
- int y2 = Math.max(r.y, r.y + r.height);
- int c = 0;
+
+ final int NORTHWEST = 0;
+ final int NORTH = 1;
+ final int NORTHEAST = 2;
+ final int WEST = 3;
+ final int CENTER = 4;
+ final int EAST = 5;
+ final int SOUTHWEST = 6;
+ final int SOUTH = 7;
+ final int SOUTHEAST = 8;
+
+ int x1 = Math.min(r.x, r.x + (r.width - 1));
+ int y1 = Math.min(r.y, r.y + (r.height - 1));
+ int x2 = Math.max(r.x, r.x + (r.width - 1));
+ int y2 = Math.max(r.y, r.y + (r.height - 1));
+ int c;
         if (p.x < x1) {
- c = 0;
+ c = NORTHWEST;
         } else if (p.x > x2) {
- c = 2;
+ c = NORTHEAST;
         } else {
- c = 1;
+ c = NORTH;
         }
 
- if (p.y < y1) {
- c += 0;
- } else if (p.y > y2) {
+ if (p.y > y2) {
             c += 6;
- } else {
+ } else if (p.y > y1) {
             c += 3;
+ if (c == CENTER) {
+ int westDist = p.x - x1;
+ int eastDist = x2 - p.x;
+ int northDist = p.y - y1;
+ int southDist = y2 - p.y;
+ int shortDist;
+ if (westDist < eastDist) {
+ shortDist = westDist;
+ c = WEST;
+ } else {
+ shortDist = eastDist;
+ c = EAST;
+ }
+ if (northDist < shortDist) {
+ shortDist = northDist;
+ c = NORTH;
+ }
+ if (southDist < shortDist) {
+ shortDist = southDist;
+ c = SOUTH;
+ }
+ }
         }
 
         switch (c) {
- case 0:
+ case NORTHWEST:
             res.x = x1;
             res.y = y1;
             return; // above, left
- case 1:
+ case NORTH:
             res.x = p.x;
             res.y = y1;
             return; // above
- case 2:
+ case NORTHEAST:
             res.x = x2;
             res.y = y1;
             return; // above, right
- case 3:
+ case WEST:
             res.x = x1;
             res.y = p.y;
             return; // left
- case 4:
+ case CENTER:
             res.x = p.x;
             res.y = p.y;
             return; // inside rect
- case 5:
+ case EAST:
             res.x = x2;
             res.y = p.y;
             return; // right
- case 6:
+ case SOUTHWEST:
             res.x = x1;
             res.y = y2;
             return; // below, left
- case 7:
+ case SOUTH:
             res.x = p.x;
             res.y = y2;
             return; // below
- case 8:
+ case SOUTHEAST:
             res.x = x2;
             res.y = y2;
             return; // below right

« Previous message in topic | 1 of 1 | Next message in topic »

Messages

Show all messages in topic

svn commit: r1333 - trunk/src/org/tigris/gef/base/Geometry.java bobtarling Bob Tarling 2011-05-22 03:27:25 PDT
Messages per page: