Develop and Download Open Source Software

Browse Subversion Repository

Contents of /common/CalculateGeometry.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 279 - (show annotations) (download) (as text)
Wed Mar 12 05:28:19 2008 UTC (16 years, 1 month ago) by satofumi
File MIME type: text/x-chdr
File size: 2206 byte(s)
adjust tab index
1 #ifndef CALCULATE_GEOMETRY_H
2 #define CALCULATE_GEOMETRY_H
3
4 /*!
5 \file
6 \brief ‹——ŁŠÖŒW‚ĚŒvŽZ
7
8 \author Satofumi KAMIMURA
9
10 $Id$
11 */
12
13 #include "GridTypes.h"
14 #include "MathUtils.h"
15
16
17 namespace beego {
18 /*!
19 \brief ˆĘ’u‚Ƃ̋——Ł‚đŒvŽZ
20 */
21 template<class T> T calculate_getLength_toPosition(const Position<T>& pos,
22 const Position<T>& line) {
23
24 double diff_x = pos.x - line.x;
25 double diff_y = pos.y - line.y;
26 double radian = line.angle.to_rad();
27 return static_cast<T>((diff_x * cos(radian) + diff_y * sin(radian)));
28 }
29
30
31 /*!
32 \brief “_‚Ƃ̋——Ł‚đŒvŽZ
33 */
34 template<class T> T calculate_getLength_toPoint(const Position<T>& pos,
35 const Grid<T>& point) {
36
37 double x_diff = pos.x - point.x;
38 double y_diff = pos.y - point.y;
39 return static_cast<T>(sqrt((x_diff * x_diff) + (y_diff * y_diff)));
40 }
41
42
43 /*!
44 \brief ’źü‚Ƃ̋——Ł‚đŒvŽZ
45 */
46 template<class T> T calculate_getLength_toLine(const Position<T>& pos,
47 const Grid<T> p0,
48 const Grid<T> p1) {
49
50 Position<T> line(p0.x, p0.y, rad(atan2(p1.y - p0.y, p1.x - p0.x)));
51 T length0 = calculate_getLength_toPosition<T>(pos, line);
52
53 set_Position(&line, p1.x, p1.y, rad(atan2(p0.y - p1.y, p0.x - p1.x)));
54 T length1 = calculate_getLength_toPosition<T>(pos, line);
55
56 if (length0 * length1 < 0) {
57 // ’[“_‚Ƃ̋——Ł‚đ•Ô‚ˇ
58 double x_diff = p0.x - pos.x;
59 double y_diff = p0.y - pos.y;
60 double length_p0 = (x_diff * x_diff) + (y_diff * y_diff);
61
62 x_diff = p1.x - pos.x;
63 y_diff = p1.y - pos.y;
64 double length_p1 = (x_diff * x_diff) + (y_diff * y_diff);
65
66 double min_length = (length_p0 > length_p1) ? length_p1 : length_p0;
67 return static_cast<T>(sqrt(min_length));
68
69 } else {
70 // ’źü‚Ƃ̋——Ł‚đ•Ô‚ˇ
71 set_Position(&line, p0.x, p0.y,
72 rad(atan2(p1.y - p0.y, p1.x - p0.x) + M_PI/2));
73 T length = calculate_getLength_toPosition<T>(pos, line);
74 return (length < 0) ? -length : length;
75 }
76 }
77 };
78
79 #endif /* !CALCULATE_GEOMETRY_H */

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26