adonthell-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Adonthell-commits] CVS: adonthell/src/tools/dlgedit dlg_arrow.cc,1.4,1.


From: Kai Sterker <address@hidden>
Subject: [Adonthell-commits] CVS: adonthell/src/tools/dlgedit dlg_arrow.cc,1.4,1.5 dlg_arrow.h,1.4,1.5
Date: Sat, 26 Oct 2002 13:43:31 -0400

Update of /cvsroot/adonthell/adonthell/src/tools/dlgedit
In directory subversions:/tmp/cvs-serv12310

Modified Files:
        dlg_arrow.cc dlg_arrow.h 
Log Message:
ADDED correct calculation of arrow/node intersection


Index: dlg_arrow.cc
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/tools/dlgedit/dlg_arrow.cc,v
retrieving revision 1.4
retrieving revision 1.5
diff -C2 -r1.4 -r1.5
*** dlg_arrow.cc        24 Oct 2002 20:12:56 -0000      1.4
--- dlg_arrow.cc        26 Oct 2002 17:43:29 -0000      1.5
***************
*** 79,85 ****
      float b = y / s;
      
!     DlgPoint line0 = start.offset ((int)(-10.0f * a), (int)(-10.0f * b));
!     DlgPoint line1 = end.offset ((int)(10.0f * a), (int)(10.0f * b));
! 
      // line from start- to end-circle
      line[0] = (GdkPoint) line0;
--- 79,85 ----
      float b = y / s;
      
!     DlgPoint line0 = getIntersection (start, end, *prev_.front ());
!     DlgPoint line1 = getIntersection (end, start, *next_.front ());
!     
      // line from start- to end-circle
      line[0] = (GdkPoint) line0;
***************
*** 88,97 ****
      // arrow's tip
      tip[0] = line[1];
!     tip[1] = (GdkPoint) line1.offset ((int)(a * 10.0f + b * 5.0f), (int)(b * 
10.0f - a * 5.0f));
!     tip[2] = (GdkPoint) line1.offset ((int)(a * 10.0f - b * 5.0f), (int)(b * 
10.0f + a * 5.0f));
      
      // calculate arrow's new dimension
      init (line0, line1);
      grow (2, 2);
  }
  
--- 88,190 ----
      // arrow's tip
      tip[0] = line[1];
!     tip[1] = (GdkPoint) line1.offset ((int)(a * 8.0f + b * 4.0f), (int)(b * 
8.0f - a * 4.0f));
!     tip[2] = (GdkPoint) line1.offset ((int)(a * 8.0f - b * 4.0f), (int)(b * 
8.0f + a * 4.0f));
      
      // calculate arrow's new dimension
      init (line0, line1);
      grow (2, 2);
+ }
+ 
+ // calculate intersection of arrow and node shape
+ DlgPoint DlgArrow::getIntersection (DlgPoint &start, DlgPoint &end, DlgRect 
&shape)
+ {
+     DlgPoint tl = shape.topLeft ();
+     DlgPoint br = shape.bottomRight ();
+     
+     int direction;
+     double a, b;
+     DlgPoint p;    
+     
+     // gradient of line
+     double x = end.x () - start.x ();
+     double y = end.y () - start.y ();
+ 
+     // tangens of angle between line(start, end) and x-axis
+     double m = x == 0 ? 1.0 : y / x;
+     
+     // direction where line(start, end) intersects with border of start
+     enum { NORTH, EAST, SOUTH, WEST };
+ 
+     // calculate where line(start, end) intersects with border of start
+     if (y < 0)
+     {
+         a = tl.y () - start.y ();
+         
+         if (x > 0)
+         {
+             // 1. quadrant
+             b = br.x () - start.x ();
+             
+             if (m > a / b) direction = EAST;
+             else direction = NORTH;
+         }
+         else 
+         {
+             // 2. quadrant
+             b = tl.x () - start.x ();
+             
+             if (m >= a / b) direction = NORTH;
+             else direction = WEST;
+         }
+     }
+     else
+     {
+         a = br.y () - start.y ();
+         
+         if (x < 0)
+         {
+             // 3. quadrant
+             b = tl.x () - start.x ();
+ 
+             if (m > a / b) direction = WEST;
+             else direction = SOUTH;
+         }
+         else
+         {
+             // 4. quadrant
+             b = br.x () - start.x ();
+ 
+             if (m >= a / b) direction = SOUTH;
+             else direction = EAST;
+         }
+     }
+     
+     // now we have one coordinate of the intersection and can
+     // calculate the second
+     switch (direction)
+     {
+         case NORTH:
+         {
+             p = DlgPoint ((tl.y () - start.y ()) * x / y + start.x (), tl.y 
());
+             break;
+         }
+         case EAST:
+         {
+             p = DlgPoint (br.x (), start.y () + y / x * (br.x () - start.x 
()));
+             break;
+         }
+         case SOUTH: 
+         {
+             p = DlgPoint ((br.y () - start.y ()) * x / y + start.x (), br.y 
());
+             break;
+         }
+         case WEST:
+         {
+             p = DlgPoint (tl.x (), start.y () + y / x * (tl.x () - start.x 
()));
+             break;
+         }
+     }
+     
+     return p;
  }
  

Index: dlg_arrow.h
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/tools/dlgedit/dlg_arrow.h,v
retrieving revision 1.4
retrieving revision 1.5
diff -C2 -r1.4 -r1.5
*** dlg_arrow.h 13 Oct 2002 20:37:45 -0000      1.4
--- dlg_arrow.h 26 Oct 2002 17:43:29 -0000      1.5
***************
*** 96,99 ****
--- 96,128 ----
      
  private:
+     /**
+      * Calculate intersection of arrow line and node shape. This works
+      * as follows.
+      *
+      * We have a rectangle that represents the shape of the start node.
+      * The start of the arrow's line lies in the center of this rectangle,
+      * and it will obviously pass through exactly one of its four sides.
+      *
+      * To find out which side, we split the rectangle into four quadrants,
+      * with the start point as origin. Then we check through which of the 
+      * four quadrants from start to end runs.
+      *
+      * That way, only two sides will remain for the intersection. To figure
+      * out the right one, we compare the line with the angle bisection of
+      * its quadrant.
+      *
+      * Now that we know the side, we also know one coordinate of the
+      * intersection. Now we simply insert this into the equation of the 
+      * line and calculate the second coordinate. Voila: we have the point
+      * where the arrow intersects with the node.
+      *
+      * @param start Point where the arrow starts
+      * @param end Point where the arrow ends
+      * @param shape Shape of the start node
+      *
+      * @return The intersection of arrow and start node.
+      */
+     DlgPoint getIntersection (DlgPoint &start, DlgPoint &end, DlgRect &shape);
+ 
      GdkPoint line[2];       // the arrow's line
      GdkPoint tip[3];        // the arrow's tip





reply via email to

[Prev in Thread] Current Thread [Next in Thread]