NEURON
mymath.h
Go to the documentation of this file.
1 #ifndef mymath_h
2 #define mymath_h
3 
4 #include <OS/math.h>
5 #include <InterViews/geometry.h>
6 
7 extern "C" int nrn_isdouble(double*, double, double);
8 
9 class Extension;
10 
11 class MyMath {
12 public:
13  // increase all around in screen coords
14  static void extend(Extension&, Coord);
15  static void extend(Extension&, const Extension&);
16 
17  static void minmax(Coord& min, Coord& max);
18  static bool inside(Coord x, Coord min, Coord max);
19 
20  static float min(int count, const float*);
21  static float max(int count, const float*);
22 
23  static bool inside(
24  Coord x, Coord y,
25  Coord left, Coord bottom, Coord right, Coord top
26  );
27 
28  // within epsilon distance from the infinite line
29  static bool near_line(
30  Coord x, Coord y,
31  Coord x1, Coord y1, Coord x2, Coord y2,
32  float epsilon
33  );
34 
35  // within epsilon distance from the line segment
36  static bool near_line_segment(
37  Coord x, Coord y,
38  Coord x1, Coord y1, Coord x2, Coord y2,
39  float epsilon
40  );
41 
42  // returns distance between point and line segment
43  static float distance_to_line_segment(
44  Coord x, Coord y,
45  Coord x1, Coord y1, Coord x2, Coord y2
46  );
47 
48  // returns distance between point and line
49  static float distance_to_line(
50  Coord x, Coord y,
51  Coord x1, Coord y1, Coord x2, Coord y2
52  );
53 
54 
55  // returns square norm
56  static float norm2(Coord x, Coord y) {
57  return x*x + y*y;
58  }
59 
60  // unit length vector perpindicular to vector (x, y)
61  static bool unit_normal(Coord x, Coord y, Coord* perp);
62 
63  // returns range extended to nearest 1.5 digit accuracy.
64  // ie. digit is 1, 2, or 5
65  static void round_range(Coord x1, Coord x2, double& y1, double& y2,
66  int& ntic);
67  static void round_range_down(Coord x1, Coord x2, double& y1, double& y2,
68  int& ntic);
69 
71  static double round(float& x1, float& x2, int direction, int digits);
72 
73  static void box(Requisition&, Coord& x1, Coord& y1, Coord& x2, Coord& y2);
74 
75  static double anint(double); /* round toward nearest integer */
76  static double resolution(double); // 100, 10, .1,... least significant digit
77 
78  static bool lt(double x, double y, double e) { return x < (y-e); }
79  static bool le(double x, double y, double e) { return x <= (y+e); }
80  static bool eq(double x, double y, double e) { return Math::equal(x,y,e);}
81  static bool eq2(double x, double y, double e) { return x - y <= e && y - x <= e; }
82 };
83 
84 inline void MyMath::extend(Extension& e, const Extension& x) {
85  e.set_xy(NULL, e.left() + x.left(), e.bottom() + x.bottom(),
86  e.right() + x.right(), e.top() + x.top());
87 }
88 
89 inline void MyMath::extend(Extension& e, Coord x) {
90  e.set_xy(NULL, e.left() - x, e.bottom() - x, e.right() + x, e.top() + x);
91 }
92 
93 inline void MyMath::minmax(Coord& x, Coord& y) {
94  if (y < x) {Coord z = x; x = y; y = z;}
95 }
96 
97 inline bool MyMath::inside(Coord x, Coord x1, Coord x2) {
98  return (x >= x1 && x <= x2);
99 }
100 
101 inline bool MyMath::inside(Coord x, Coord y, Coord x1, Coord y1, Coord x2, Coord y2) {
102  return inside(x, x1, x2) && inside(y, y1, y2);
103 }
104 
105 #endif
static void round_range(Coord x1, Coord x2, double &y1, double &y2, int &ntic)
Definition: mymath.cpp:281
static float distance_to_line_segment(Coord x, Coord y, Coord x1, Coord y1, Coord x2, Coord y2)
Definition: mymath.cpp:168
#define Coord
Definition: _defines.h:19
static bool equal(float x, float y, float e)
Definition: math.h:108
static bool near_line_segment(Coord x, Coord y, Coord x1, Coord y1, Coord x2, Coord y2, float epsilon)
Definition: mymath.cpp:200
static bool unit_normal(Coord x, Coord y, Coord *perp)
Definition: mymath.cpp:373
void set_xy(Canvas *, Coord left, Coord bottom, Coord right, Coord top)
Definition: math.cpp:52
static void round_range_down(Coord x1, Coord x2, double &y1, double &y2, int &ntic)
Definition: mymath.cpp:306
static double anint(double)
Definition: mymath.cpp:86
static bool inside(Coord x, Coord min, Coord max)
Definition: mymath.h:97
static double round(float &x1, float &x2, int direction, int digits)
Definition: mymath.cpp:335
#define e
Definition: passive0.cpp:24
static void minmax(Coord &min, Coord &max)
Definition: mymath.h:93
static float distance_to_line(Coord x, Coord y, Coord x1, Coord y1, Coord x2, Coord y2)
Definition: mymath.cpp:145
static float norm2(Coord x, Coord y)
Definition: mymath.h:56
static float min(int count, const float *)
Definition: mymath.cpp:93
Coord bottom() const
Definition: geometry.h:298
Coord left() const
Definition: geometry.h:297
static bool le(double x, double y, double e)
Definition: mymath.h:79
static bool eq2(double x, double y, double e)
Definition: mymath.h:81
int nrn_isdouble(double *, double, double)
Definition: isoc99.cpp:12
static void extend(Extension &, Coord)
Definition: mymath.h:89
static bool near_line(Coord x, Coord y, Coord x1, Coord y1, Coord x2, Coord y2, float epsilon)
Definition: mymath.cpp:115
#define left
Definition: rbtqueue.cpp:45
Coord right() const
Definition: geometry.h:299
static bool eq(double x, double y, double e)
Definition: mymath.h:80
#define right
Definition: rbtqueue.cpp:46
Definition: mymath.h:11
Coord top() const
Definition: geometry.h:300
static float max(int count, const float *)
Definition: mymath.cpp:103
static void box(Requisition &, Coord &x1, Coord &y1, Coord &x2, Coord &y2)
Definition: mymath.cpp:364
return NULL
Definition: cabcode.cpp:461
static double resolution(double)
static bool lt(double x, double y, double e)
Definition: mymath.h:78