wxArt2D
hittest.cpp
Go to the documentation of this file.
1 /*! \file canvas/src/hittest.cpp
2  \brief general hittest functions
3  \author Klaas Holwerda, Michael Sögtrop
4 
5  Copyright: 2001-2004 (C) Klaas Holwerda, Michael Sögtrop
6 
7  Licence: wxWidgets licence
8 
9  RCS-ID: $Id: hittest.cpp,v 1.15 2007/07/03 22:09:47 titato Exp $
10 */
11 
12 #include "a2dprec.h"
13 
14 #ifdef __BORLANDC__
15 #pragma hdrstop
16 #endif
17 
18 #ifndef WX_PRECOMP
19 #include "wx/wx.h"
20 #endif
21 
22 #include <float.h>
23 #include <wx/artbase/drawer2d.h>
24 #include <wx/canvas/hittest.h>
25 
26 a2dHit HitTestRectangle( double xtest, double ytest, double xmin, double ymin, double xmax, double ymax, double margin )
27 {
28  if ( xmin > xmax )
29  {
30  double x = xmax;
31  xmax = xmin;
32  xmin = x;
33  }
34  if ( ymin > ymax )
35  {
36  double y = ymax;
37  ymax = ymin;
38  ymin = y;
39  }
40  wxASSERT( margin >= 0 );
41 
42  static a2dHit hittypes [16] =
43  {
44  // 0000 = inside
45  a2dHit( a2dHit::hit_fill, a2dHit::stroke1_none, a2dHit::stroke2_none, 0, 0 ),
46  // 0001 = left edge
47  a2dHit( a2dHit::hit_stroke, a2dHit::stroke1_none, a2dHit::stroke2_edgevert, 0, 0 ),
48  // 0010 = right edge
49  a2dHit( a2dHit::hit_stroke, a2dHit::stroke1_none, a2dHit::stroke2_edgevert, 1, 0 ),
50  // 0011 = degenerate (margin>width, left + right edge => use right edge)
51  a2dHit( a2dHit::hit_stroke, a2dHit::stroke1_none, a2dHit::stroke2_edgevert, 1, 0 ),
52 
53  // 0100 = bottom edge
54  a2dHit( a2dHit::hit_stroke, a2dHit::stroke1_none, a2dHit::stroke2_edgehor, 2, 0 ),
55  // 0101 = bottom left vertex
56  a2dHit( a2dHit::hit_stroke, a2dHit::stroke1_none, a2dHit::stroke2_vertex, 0, 0 ),
57  // 0110 = bottom right vertex
58  a2dHit( a2dHit::hit_stroke, a2dHit::stroke1_none, a2dHit::stroke2_vertex, 1, 0 ),
59  // 0111 = degenerate bottom right+left vertex => use bootom right vertex
60  a2dHit( a2dHit::hit_stroke, a2dHit::stroke1_none, a2dHit::stroke2_vertex, 1, 0 ),
61 
62  // 1000 = top edge
63  a2dHit( a2dHit::hit_stroke, a2dHit::stroke1_none, a2dHit::stroke2_edgehor, 3, 0 ),
64  // 1001 = top left vertex
65  a2dHit( a2dHit::hit_stroke, a2dHit::stroke1_none, a2dHit::stroke2_vertex, 2, 0 ),
66  // 1010 = top right vertex
67  a2dHit( a2dHit::hit_stroke, a2dHit::stroke1_none, a2dHit::stroke2_vertex, 3, 0 ),
68  // 1011 = degenerate top right+left vertex => use top right vertex
69  a2dHit( a2dHit::hit_stroke, a2dHit::stroke1_none, a2dHit::stroke2_vertex, 3, 0 ),
70 
71  // 1100 = degenerate top+bottom edge => use bottom edge
72  a2dHit( a2dHit::hit_stroke, a2dHit::stroke1_none, a2dHit::stroke2_edgehor, 2, 0 ),
73  // 1101 = degenerate top+bottom left vertex => us bottom left vertex
74  a2dHit( a2dHit::hit_stroke, a2dHit::stroke1_none, a2dHit::stroke2_vertex, 0, 0 ),
75  // 1110 = degenerate top+bottom right vertex => us bottom right vertex
76  a2dHit( a2dHit::hit_stroke, a2dHit::stroke1_none, a2dHit::stroke2_vertex, 1, 0 ),
77  // 1111 = degenerate top+bottom degenerate left+right vertex => us bottom right vertex
78  a2dHit( a2dHit::hit_stroke, a2dHit::stroke1_none, a2dHit::stroke2_vertex, 1, 0 )
79  };
80 
81  a2dHit result;
82 
83  // check if inside bounding box with positive margin
84  if (
85  xtest < xmax + margin &&
86  ytest < ymax + margin &&
87  xtest > xmin - margin &&
88  ytest > ymin - margin
89  )
90  {
91  // check if inside bounding box with negative margin
92  int hittype = 0;
93  if ( xtest < xmin + margin ) hittype += 1;
94  if ( xtest > xmax - margin ) hittype += 2;
95  if ( ytest < ymin + margin ) hittype += 4;
96  if ( ytest > ymax - margin ) hittype += 8;
97 
98  result = hittypes[hittype];
99  if( result.m_hit == a2dHit::hit_stroke )
100  {
101  // need inside/outside flags
102  if (
103  xtest < xmax &&
104  ytest < ymax &&
105  xtest > xmin &&
106  ytest > ymin
107  )
108  result.m_stroke1 = a2dHit::stroke1_inside;
109  else
110  result.m_stroke1 = a2dHit::stroke1_outside;
111  }
112  }
113 
114  return result;
115 }
The point is in the fill area.
Definition: polyver.h:56
general hittest functions
Contains graphical drawing context specific classes. a2dDrawer2D and derived classes are used for dra...
struct for how a single object on one layer was hit
Definition: polyver.h:38
The point is on the stroke or stroke margin.
Definition: polyver.h:54
hittest.cpp Source File -- Sun Oct 12 2014 17:04:21 -- Sun Oct 12 2014 -- 1.8.5 -- wxArt2D -- . -- Main Page Reference Documentation