OSDN Git Service

first commit.
[gintenlib/gintenlib.git] / gintenlib / plane / rect.hpp
1 #ifndef GINTENLIB_PLANE_INCLUDED_RECT_HPP_
2 #define GINTENLIB_PLANE_INCLUDED_RECT_HPP_
3
4 /*
5
6       <gintenlib/plane/rect.hpp>
7
8   rect ¡§ Æ󼡸µ¶õ´Ö¾å¤Î¶ë·Á
9
10   Àë¸À¡§
11     // rect : ¶ë·Á
12     template<typename Real>
13     struct basic_rect
14     {
15       typedef Real real_type;
16       typedef basic_point<Real> pos_type;
17       typedef basic_vector<Real> vec_type;
18       
19       real_type x1, y1, x2, y2;
20       
21       basic_rect();
22       basic_rect( real_type x1_, real_type y1_, real_type x2_, real_type y2_ );
23         
24       template<typename T>
25       basic_rect( const basic_rect<T>& src );
26       
27       // operator[]
28       // ÄºÅÀ¤ÎºÂɸ¤òÊÖ¤·¤Þ¤¹¡£
29       pos_type operator[]( std::size_t index ) const;
30       
31       pos_type at( std::size_t index ) const;
32       static std::size_t size()
33       {
34         return 4;
35       }
36       
37       // °ÌÃ֤Υ·¥Õ¥È
38       // ¤³¤Î¶ë·Á¼«¿È¤ò¥·¥Õ¥È¤µ¤»¤Þ¤¹¡£
39       basic_rect& shift( const vec_type& d );
40       // friend ÈǤϥ³¥Ô¡¼¤·¤Æ¤«¤é¥·¥Õ¥È¤·¤Þ¤¹¡£
41       friend basic_rect shift( const basic_rect& target, const vec_type& d );
42       
43       // ²óž
44       // ¤³¤Î¶ë·Á¼«¿È¤ò²óž¤µ¤»¤¿¤¤¤¬¡¢¤½¤¦¤¹¤ë¤È¶ë·Á¤Ç¤Ê¤¯¤Ê¤Ã¤Æ¤·¤Þ¤¦¤Î¤Ç¡¢
45       // ¤ä¤à¤Ê¤¯»Í³Ñ·Á¤È¤·¤Æ¥³¥Ô¡¼¤·¤Æ¤«¤é²óž¤µ¤»¤Þ¤¹¡£
46       // Angle ÈǤϳÑÅÙ¤«¤é¥µ¥¤¥ó¥³¥µ¥¤¥ó¤òµá¤á¤Æ²óž¡¢
47       // s, c ÈǤϥµ¥¤¥ó¥³¥µ¥¤¥óÃͤ«¤éľÀܲ󞤵¤»¤Þ¤¹¡£
48       template<typename Angle>
49       basic_quadrangle<Real> rotate( const Angle& theta ) const;
50       basic_quadrangle<Real> rotate( real_type s, real_type c ) const;
51       template<typename Angle>
52       friend basic_quadrangle<Real> rotate( const basic_rect& x, const Angle& theta );
53       basic_quadrangle<Real> rotate( const basic_rect& x, real_type s, real_type c );
54       
55     };
56     typedef basic_rect<double> rect;
57
58   µ¡Ç½¡§
59     Æ󼡸µ¶õ´Ö¾å¤Î¡¢°ÌÃ֤γÎÄꤷ¤¿¶ë·Á¤òɽ¤¹¥¯¥é¥¹¡£
60     ²óž¤µ¤»¤ë¤È»Í³Ñ·Á¤ËÊѲ½¤¹¤ë°Ê³°¤ÏÉáÄ̤«¤È¡£
61
62 */
63
64 #include <cstddef>
65 #include <cassert>
66 #include <stdexcept>
67
68 #include "fwd.hpp"
69 #include "vector.hpp"
70 #include "point.hpp"
71 #include "quadrangle.hpp"
72
73 namespace gintenlib
74 {
75  namespace plane
76  {
77   // rect : ¶ë·Á
78   template<typename Real>
79   struct basic_rect
80   {
81     typedef Real real_type;
82     typedef basic_point<Real> pos_type;
83     typedef basic_vector<Real> vec_type;
84     
85     real_type x1, y1, x2, y2;
86     
87     basic_rect()
88       : x1(), y1(), x2(), y2() {}
89     
90     basic_rect( real_type x1_, real_type y1_, real_type x2_, real_type y2_ )
91       : x1(x1_), y1(y1_), x2(x2_), y2(y2_) {}
92       
93     template<typename T>
94     basic_rect( const basic_rect<T>& src )
95       : x1(src.x1), y1(src.y1), x2(src.x2), y2(src.y2) {}
96     
97     // operator[]
98     pos_type operator[]( std::size_t index ) const
99     {
100       using namespace std;
101       
102       switch( index )
103       {
104        case 0:
105         return pos_type( x1, y1 );
106        
107        case 1:
108         return pos_type( x2, y1 );
109       
110        case 2:
111         return pos_type( x2, y2 );
112       
113        case 3:
114         return pos_type( x1, y2 );
115       
116        default:
117         assert( !"should not get here." );
118       
119       }
120     }
121     pos_type at( std::size_t index ) const
122     {
123       if( index > size() )
124       {
125         throw std::out_of_range();
126       }
127       
128       return operator[](index);
129     }
130     static std::size_t size()
131     {
132       return 4;
133     }
134     
135     // °ÌÃ֤Υ·¥Õ¥È
136     basic_rect& shift( const vec_type& d )
137     {
138       x1 += d.x;
139       y1 += d.y;
140       x2 += d.x;
141       y2 += d.y;
142       
143       return *this;
144     }
145     friend basic_rect shift( const basic_rect& target, const vec_type& d )
146     {
147       basic_rect temp = target;
148       return temp.shift(d);
149     }
150     
151     // ²óž
152     template<typename Angle>
153     basic_quadrangle<Real> rotate( const Angle& theta ) const
154     {
155       using std::sin; using std::cos;
156       return rotate( sin(theta), cos(theta) );
157     }
158     basic_quadrangle<Real> rotate( real_type s, real_type c ) const
159     {
160       pos_type center( (x1 + x2) / 2, (y1 + y2) / 2 );
161       real_type w = (x2 - x1) / 2, h = (x2 - x1) / 2;
162       real_type sw = s * w, sh = s * h, cw = c * w, ch = c * h;
163       
164       vec_type p1( -cw - sh, -ch + sw ), p2( cw - sh, -ch - sw );
165       
166       return basic_quadrangle<Real>( center, p1, p2, -p1, -p2 );
167     }
168     template<typename Angle>
169     friend basic_quadrangle<Real> rotate( const basic_rect& x, const Angle& theta )
170     {
171       return x.rotate(theta);
172     }
173     basic_quadrangle<Real> rotate( const basic_rect& x, real_type s, real_type c )
174     {
175       return x.rotate( s, c );
176     }
177     
178   };
179   typedef basic_rect<double> rect;
180   
181  }  // namespace plane
182 }   // namespace gintenlib
183
184 #endif  // #ifndef GINTENLIB_PLANE_INCLUDED_RECT_HPP_