1 /*
2 * MySeg.h
3 * nlivarot
4 *
5 * Created by fred on Wed Nov 12 2003.
6 *
7 */
9 #ifndef my_math_seg
10 #define my_math_seg
12 #include "MyMath.h"
14 // codes for the intersections computations
15 // pt = point
16 // seg = segment
17 // dmd = half line
18 // dr = infinite line
19 enum
20 {
21 inters_seg_seg, // intersection between 2 segments
22 inters_seg_dmd, // intersection between one segment (parameter no 1) and one half-line (parameter no 2)
23 inters_seg_dr, // ....
24 inters_dmd_dmd,
25 inters_dmd_dr,
26 inters_dr_dr,
28 inters_seg_pt, // "intersection" between segment and point= "does the segment contain the point?"
29 inters_dmd_pt,
30 inters_dr_pt,
32 inters_orseg_pt // don't use
33 };
35 // return codes for the intersection computations; build as a concatenation of
36 // _a = first parameter
37 // _b = second parameter
38 // _st = start of the segment/half-line (lines don't have starts)
39 // _en = end of thz segment (half-lines and lines don't have ends)
40 // _mi = inside of the segment/half-line/line
41 // _colinear = this flag is set if the intersection of the 2 parameter is more than a point
42 // the first 2 bits of the return code contain the position of the intersection on the first parameter (_st, _mi or _en)
43 // the next 2 bits of the return code contain the position of the intersection on the second parameter (_st, _mi or _en)
44 // the 5th bit is set if the parameters are colinear
45 enum
46 {
47 inters_a_st = 1,
48 inters_a_mi = 2,
49 inters_a_en = 3,
50 inters_b_st = 4,
51 inters_b_mi = 8,
52 inters_b_en = 12,
53 inters_colinear = 16
54 };
57 //
58 // a class to describe a segment: defined by its startpoint p and its direction d
59 // if the object is considered as a segment: p+xd, where x ranges from 0 to 1
60 // if the object is considered as an half-line, the length of the direction vector doesn't matter:
61 // p+xd, where x ranges from 0 to +infinity
62 // if the object is considered as a line: p+xd, where x ranges from -infinity to +infinity
63 //
64 class L_SEG
65 {
66 public:
67 vec2d p, d;
69 // constructors
70 L_SEG (vec2d & st, vec2d & dir):p (st), d (dir)
71 {
72 }; // by default, you give one startpoint and one direction
73 L_SEG (void)
74 {
75 };
76 ~L_SEG (void)
77 {
78 };
80 // assignations
81 void Set (L_SEG * s)
82 {
83 p = s->p;
84 d = s->d;
85 };
86 void Set (L_SEG & s)
87 {
88 p = s.p;
89 d = s.d;
90 };
91 // 2 specific assignations:
92 // assignation where you give the startpoint and the direction (like in the constructor):
93 void SetSD (vec2d & st, vec2d & dir)
94 {
95 p = st;
96 d = dir;
97 };
98 // assignation where you give the startpoint and the endpoint:
99 void SetSE (vec2d & st, vec2d & en)
100 {
101 p = st;
102 d.x = en.x - st.x;
103 d.y = en.y - st.y;
104 };
106 // reverses the segment
107 void Rev (void)
108 {
109 p.x += d.x;
110 p.y += d.y;
111 d.x = -d.x;
112 d.y = -d.y;
113 };
114 // transitibve version: the reversed segment is stored in s
115 void Rev (L_SEG & s)
116 {
117 s.p.x = p.x + d.x;
118 s.p.y = p.y + d.y;
119 s.d.x = -d.x;
120 s.d.y = -d.y;
121 };
123 // distance of the point iv to the segment/half-line/line
124 // the mode parameter specifies how the caller instance should be handled:
125 // inters_seg_pt : segment
126 // inters_dmd_pt : half-line
127 // inters_dr_pt : line
128 void Distance (vec2d & iv, double &d, int mode = inters_dr_pt);
129 // distance between 2 segments
130 // mode parameter specifies how the segments have to be treated (just like above)
131 void Distance (L_SEG & is, double &d, int mode = inters_seg_seg);
133 // tests if the segment contains the point pos
134 // mode is as in the Distance function
135 int Contains (vec2d & pos, int mode);
137 // intersection between 2 lines/half-lines/segments
138 // mode specifies how the L_SEG instances have to be considered; codes at the beginning of this file
139 // the "at" parameter stores the intersection point, if it exists and is unique
140 static int Intersect (L_SEG & iu, L_SEG & iv, int mode);
141 static int Intersect (L_SEG & iu, L_SEG & iv, vec2d & at, int mode);
142 // specific version, when you can garantuee the colinearity case won't occur
143 static int IntersectGeneral (L_SEG & iu, L_SEG & iv, vec2d & at, int mode);
144 };
147 #endif