00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 #ifndef MLN_WIN_OCTAGON2D_HH
00027 # define MLN_WIN_OCTAGON2D_HH
00028 
00032 
00033 # include <mln/core/internal/classical_window_base.hh>
00034 # include <mln/core/alias/dpoint2d.hh>
00035 
00036 
00037 namespace mln
00038 {
00039 
00040   mln_internal_add_classical_window_trait(octagon2d);
00041 
00042 
00043   namespace win
00044   {
00045 
00067     struct octagon2d : public internal::classical_window_base< dpoint2d, octagon2d >
00068     {
00075       octagon2d(unsigned length);
00076 
00079       unsigned length() const;
00080 
00084       unsigned delta_() const;
00085 
00088       unsigned area() const;
00089 
00090       void print_(std::ostream& ostr) const;
00091 
00092     protected:
00093       unsigned length_;
00094     };
00095 
00096 
00097 
00098 # ifndef MLN_INCLUDE_ONLY
00099 
00100     inline
00101     octagon2d::octagon2d(unsigned length)
00102       : length_(length)
00103     {
00104       mln_precondition(length % 6 == 1);
00105       const def::coord
00106         y = static_cast<def::coord>(length / 6),
00107         x = static_cast<def::coord>(y * 2),
00108         z = static_cast<def::coord>(y + x);
00109       insert(dpoint2d(0, 0));
00110       for (def::coord a = 1; a <= x; ++a)
00111         for (def::coord b = 0; b <= x; ++b)
00112         {
00113           def::coord 
00114             _a = static_cast<def::coord>(-a),
00115             _b = static_cast<def::coord>(-b);
00116           insert(dpoint2d(a, b));
00117           insert(dpoint2d(_b, a));
00118           insert(dpoint2d(b, _a));
00119           insert(dpoint2d(_a, _b));
00120         }
00121       const def::coord a_min = static_cast<def::coord>(x + 1);
00122       for (def::coord a = a_min; a <= z; ++a)
00123         {
00124           const def::coord
00125             b_min = static_cast<def::coord>(-2 * x + a),
00126             b_max = static_cast<def::coord>(2 * x - a);
00127           for (def::coord b = b_min; b <= b_max; ++b)
00128             {
00129               def::coord 
00130                 _a = static_cast<def::coord>(-a),
00131                 _b = static_cast<def::coord>(-b);
00132               insert(dpoint2d(a, b));
00133               insert(dpoint2d(a, _b));
00134               insert(dpoint2d(_a, b));
00135               insert(dpoint2d(_a, _b));
00136               insert(dpoint2d(b, a));
00137               insert(dpoint2d(b, _a));
00138               insert(dpoint2d(_b, a));
00139               insert(dpoint2d(_b, _a));
00140             }
00141         }
00142     }
00143 
00144     inline
00145     unsigned octagon2d::length() const
00146     {
00147       return length_;
00148     }
00149 
00150     inline
00151     unsigned octagon2d::delta_() const
00152     {
00153       return length_ / 2;
00154     }
00155 
00156     inline
00157     unsigned octagon2d::area() const
00158     {
00159       unsigned l = (length_ - 1) / 6;
00160       return l * (28 * l + 8) + 4;
00161     }
00162 
00163     inline
00164     void octagon2d::print_(std::ostream& ostr) const
00165     {
00166       ostr << "[octagon2d: length=" << length_ << ']';
00167     }
00168 
00169 # endif // ! MLN_INCLUDE_ONLY
00170 
00171   } 
00172 
00173 } 
00174 
00175 
00176 
00177 
00178 # include <mln/win/hline2d.hh>
00179 # include <mln/win/vline2d.hh>
00180 # include <mln/win/diag2d.hh>
00181 # include <mln/win/backdiag2d.hh>
00182 
00183 #endif // ! MLN_WIN_OCTAGON2D_HH