NEURON
mymath.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <InterViews/geometry.h>
4 
5 class Extension;
6 
7 class MyMath {
8  public:
9  // increase all around in screen coords
10  static void extend(Extension&, Coord);
11  static void extend(Extension&, const Extension&);
12 
13  static void minmax(Coord& min, Coord& max);
14  static bool inside(Coord x, Coord min, Coord max);
15 
16  static float min(int count, const float*);
17  static float max(int count, const float*);
18 
19  static bool inside(Coord x, Coord y, Coord left, Coord bottom, Coord right, Coord top);
20 
21  // within epsilon distance from the infinite line
22  static bool near_line(Coord x, Coord y, Coord x1, Coord y1, Coord x2, Coord y2, float epsilon);
23 
24  // within epsilon distance from the line segment
25  static bool
26  near_line_segment(Coord x, Coord y, Coord x1, Coord y1, Coord x2, Coord y2, float epsilon);
27 
28  // returns distance between point and line segment
29  static float distance_to_line_segment(Coord x, Coord y, Coord x1, Coord y1, Coord x2, Coord y2);
30 
31  // returns distance between point and line
32  static float distance_to_line(Coord x, Coord y, Coord x1, Coord y1, Coord x2, Coord y2);
33 
34 
35  // returns square norm
36  static float norm2(Coord x, Coord y) {
37  return x * x + y * y;
38  }
39 
40  // unit length vector perpindicular to vector (x, y)
41  static bool unit_normal(Coord x, Coord y, Coord* perp);
42 
43  // returns range extended to nearest 1.5 digit accuracy.
44  // ie. digit is 1, 2, or 5
45  static void round_range(Coord x1, Coord x2, double& y1, double& y2, int& ntic);
46  static void round_range_down(Coord x1, Coord x2, double& y1, double& y2, int& ntic);
47 
48  enum { Expand, Contract, Higher, Lower };
49  static double round(float& x1, float& x2, int direction, int digits);
50 
51  static void box(Requisition&, Coord& x1, Coord& y1, Coord& x2, Coord& y2);
52 
53  static double anint(double); /* round toward nearest integer */
54  static double resolution(double); // 100, 10, .1,... least significant digit
55 
56  static bool lt(double x, double y, double e) {
57  return x < (y - e);
58  }
59  static bool le(double x, double y, double e) {
60  return x <= (y + e);
61  }
62  template <typename T>
63  static bool eq(T x, T y, T e) {
64  return x - y < e && y - x < e;
65  }
66  static bool eq2(double x, double y, double e) {
67  return x - y <= e && y - x <= e;
68  }
69 };
70 
71 inline void MyMath::extend(Extension& e, const Extension& x) {
72  e.set_xy(nullptr,
73  e.left() + x.left(),
74  e.bottom() + x.bottom(),
75  e.right() + x.right(),
76  e.top() + x.top());
77 }
78 
79 inline void MyMath::extend(Extension& e, Coord x) {
80  e.set_xy(nullptr, e.left() - x, e.bottom() - x, e.right() + x, e.top() + x);
81 }
82 
83 inline void MyMath::minmax(Coord& x, Coord& y) {
84  if (y < x) {
85  Coord z = x;
86  x = y;
87  y = z;
88  }
89 }
90 
91 inline bool MyMath::inside(Coord x, Coord x1, Coord x2) {
92  return (x >= x1 && x <= x2);
93 }
94 
95 inline bool MyMath::inside(Coord x, Coord y, Coord x1, Coord y1, Coord x2, Coord y2) {
96  return inside(x, x1, x2) && inside(y, y1, y2);
97 }
#define Coord
Definition: _defines.h:17
Coord left() const
Definition: geometry.h:293
void set_xy(Canvas *, Coord left, Coord bottom, Coord right, Coord top)
Coord top() const
Definition: geometry.h:296
Coord bottom() const
Definition: geometry.h:294
Coord right() const
Definition: geometry.h:295
Definition: mymath.h:7
static bool near_line(Coord x, Coord y, Coord x1, Coord y1, Coord x2, Coord y2, float epsilon)
Definition: mymath.cpp:110
static void round_range_down(Coord x1, Coord x2, double &y1, double &y2, int &ntic)
Definition: mymath.cpp:224
static bool near_line_segment(Coord x, Coord y, Coord x1, Coord y1, Coord x2, Coord y2, float epsilon)
Definition: mymath.cpp:184
static float distance_to_line_segment(Coord x, Coord y, Coord x1, Coord y1, Coord x2, Coord y2)
Definition: mymath.cpp:155
static bool le(double x, double y, double e)
Definition: mymath.h:59
static double anint(double)
Definition: mymath.cpp:81
static void minmax(Coord &min, Coord &max)
Definition: mymath.h:83
static bool eq2(double x, double y, double e)
Definition: mymath.h:66
static bool unit_normal(Coord x, Coord y, Coord *perp)
Definition: mymath.cpp:291
static bool eq(T x, T y, T e)
Definition: mymath.h:63
static bool lt(double x, double y, double e)
Definition: mymath.h:56
static void round_range(Coord x1, Coord x2, double &y1, double &y2, int &ntic)
Definition: mymath.cpp:200
static double resolution(double)
static float max(int count, const float *)
Definition: mymath.cpp:98
static void extend(Extension &, Coord)
Definition: mymath.h:79
static bool inside(Coord x, Coord min, Coord max)
Definition: mymath.h:91
static void box(Requisition &, Coord &x1, Coord &y1, Coord &x2, Coord &y2)
Definition: mymath.cpp:282
static float min(int count, const float *)
Definition: mymath.cpp:88
static double round(float &x1, float &x2, int direction, int digits)
Definition: mymath.cpp:253
static float norm2(Coord x, Coord y)
Definition: mymath.h:36
static float distance_to_line(Coord x, Coord y, Coord x1, Coord y1, Coord x2, Coord y2)
Definition: mymath.cpp:133
@ Expand
Definition: mymath.h:48
@ Lower
Definition: mymath.h:48
@ Contract
Definition: mymath.h:48
@ Higher
Definition: mymath.h:48