]> Creatis software - clitk.git/blob - utilities/CxImage/ximath.cpp
017518445dbf7aed601fd444299eaf769a9f5440
[clitk.git] / utilities / CxImage / ximath.cpp
1 #include "ximage.h"\r
2 #include "ximath.h"\r
3 #include <math.h>\r
4 \r
5 //this module should contain some classes for geometrical transformations\r
6 //usable with selections, etc... once it's done, that is. :)\r
7 \r
8 CxPoint2::CxPoint2()\r
9 {\r
10   x=y=0.0f;\r
11 }\r
12 \r
13 CxPoint2::CxPoint2(float const x_, float const y_)\r
14 {\r
15   x=x_;\r
16   y=y_;\r
17 }\r
18 \r
19 CxPoint2::CxPoint2(CxPoint2 const &p)\r
20 {\r
21   x=p.x;\r
22   y=p.y;\r
23 }\r
24 \r
25 float CxPoint2::Distance(CxPoint2 const p2)\r
26 {\r
27   return (float)sqrt((x-p2.x)*(x-p2.x)+(y-p2.y)*(y-p2.y));\r
28 }\r
29 \r
30 float CxPoint2::Distance(float const x_, float const y_)\r
31 {\r
32   return (float)sqrt((x-x_)*(x-x_)+(y-y_)*(y-y_));\r
33 }\r
34 \r
35 CxRect2::CxRect2()\r
36 {\r
37 }\r
38 \r
39 CxRect2::CxRect2(float const x1_, float const y1_, float const x2_, float const y2_)\r
40 {\r
41   botLeft.x=x1_;\r
42   botLeft.y=y1_;\r
43   topRight.x=x2_;\r
44   topRight.y=y2_;\r
45 }\r
46 \r
47 CxRect2::CxRect2(CxRect2 const &p)\r
48 {\r
49   botLeft=p.botLeft;\r
50   topRight=p.topRight;\r
51 }\r
52 \r
53 float CxRect2::Surface() const\r
54 /*\r
55  * Returns the surface of rectangle.\r
56  */\r
57 {\r
58   return (topRight.x-botLeft.x)*(topRight.y-botLeft.y);\r
59 }\r
60 \r
61 CxRect2 CxRect2::CrossSection(CxRect2 const &r2) const\r
62 /*\r
63  * Returns crossection with another rectangle.\r
64  */\r
65 {\r
66   CxRect2 cs;\r
67   cs.botLeft.x=max(botLeft.x, r2.botLeft.x);\r
68   cs.botLeft.y=max(botLeft.y, r2.botLeft.y);\r
69   cs.topRight.x=min(topRight.x, r2.topRight.x);\r
70   cs.topRight.y=min(topRight.y, r2.topRight.y);\r
71   if (cs.botLeft.x<=cs.topRight.x && cs.botLeft.y<=cs.topRight.y) {\r
72     return cs;\r
73   } else {\r
74     return CxRect2(0,0,0,0);\r
75   }//if\r
76 }\r
77 \r
78 CxPoint2 CxRect2::Center() const\r
79 /*\r
80  * Returns the center point of rectangle.\r
81  */\r
82 {\r
83   return CxPoint2((topRight.x+botLeft.x)/2.0f, (topRight.y+botLeft.y)/2.0f);\r
84 }\r
85 \r
86 float CxRect2::Width() const\r
87 //returns rectangle width\r
88 {\r
89   return topRight.x-botLeft.x;\r
90 }\r
91 \r
92 float CxRect2::Height() const\r
93 //returns rectangle height\r
94 {\r
95   return topRight.y-botLeft.y;\r
96 }\r
97 \r