proj home

Files   Classes   Functions   Hierarchy  

spiralindex< T > Class Template Reference

Spiral about a rectangular matrix, travelling to every point. More...

#include <spiralindex.h>

Collaboration diagram for spiralindex< T >:

List of all members.

Public Member Functions

 spiralindex (T const _rows, T const _columns, boolc anticlockwise=true)
 Define a 2D matrix.
void reset ()
 Reset the iterator.
boolc operator! () const
 Is the iterator valid?
void operator++ ()
 Increment the iterator.
void pos (T &row, T &col) const
 The current position as a 2D indexed point.
void pos (T &k2) const
 The current position as an index from the start.
boolc valid (point2< T > const &p) const
 Is the point within the matrix?
void indexsequence (T *seq)
 Write the 2D matrix in one go.

Public Attributes

T const rows
 The number of rows.
T const columns
 The number of columns.
point2< Tdi [4]
 The four ordered directions.
uint direction
 Index to the current direction.


Detailed Description

template<typename T = int>
class spiralindex< T >

Spiral about a rectangular matrix, travelling to every point.

The start is top left. Choose to traverse in either an anticlockwise or clockwise direction. The matrix coordinate system's meaning is preserved so increasing terms go down in the negative y-axis direction.

To reverse the spiral so it travels from the inside out, generate the iteration sequence using indexsequence(T*) and then reverse it.

Definition at line 22 of file spiralindex.h.


Constructor & Destructor Documentation

template<typename T >
spiralindex< T >::spiralindex ( T const   _rows,
T const   _columns,
boolc  anticlockwise = true 
) [inline]

Define a 2D matrix.

Definition at line 261 of file spiralindex.h.

00266   : count(0), countend(_rows*_columns), current(0,0), 
00267     rows(_rows), columns(_columns)
00268 {
00269   assert(rows>0);
00270   assert(columns>0);
00271 
00272   if (anticlockwise)
00273     directionanticlockwise();
00274   else
00275     directionclockwise();
00276 }


Member Function Documentation

template<typename T >
void spiralindex< T >::indexsequence ( T seq  )  [inline]

Write the 2D matrix in one go.

seq is a M by N matrix.

Definition at line 231 of file spiralindex.h.

References spiralindex< T >::operator!(), spiralindex< T >::operator++(), spiralindex< T >::pos(), and spiralindex< T >::reset().

Referenced by spiralindex3D< T >::spiralindex3D().

00232 {
00233   //T k2;
00234   for ( reset(); operator !(); operator++() )
00235   {
00236     pos(*seq);
00237     ++seq;
00238   }
00239 }

template<typename T = int>
boolc spiralindex< T >::operator! (  )  const [inline]

Is the iterator valid?

Definition at line 64 of file spiralindex.h.

Referenced by spiralindex< T >::indexsequence().

00065     { return count < countend; }

template<typename T >
void spiralindex< T >::operator++ (  )  [inline]

Increment the iterator.

Definition at line 321 of file spiralindex.h.

References spiralindex< T >::di, spiralindex< T >::direction, and spiralindex< T >::valid().

Referenced by spiralindex< T >::indexsequence().

00322 {
00323   ++count;
00324 
00325   point2<T> c2 = current + di[direction];
00326   if (valid(c2)==false)
00327   {
00328     if (c2==start)
00329     {
00330       ++direction;
00331       direction %= 4;
00332 
00333       current += di[direction];
00334       start = current;
00335 
00336       ++xrange.x;
00337       --xrange.y;
00338       ++yrange.x;
00339       --yrange.y;
00340     }
00341     else
00342     {
00343       ++direction;
00344       direction %= 4;
00345 
00346       //start = current;
00347       current += di[direction];
00348     }
00349   }
00350   else
00351     current = c2;
00352 }

template<typename T = int>
void spiralindex< T >::pos ( T k2  )  const [inline]

The current position as an index from the start.

Definition at line 72 of file spiralindex.h.

References spiralindex< T >::columns.

00073     { k2 = current.x*columns+current.y; }

template<typename T = int>
void spiralindex< T >::pos ( T row,
T col 
) const [inline]

The current position as a 2D indexed point.

Definition at line 69 of file spiralindex.h.

Referenced by spiralindex< T >::indexsequence(), spiralindextest::test01(), and spiralindextest::test02().

00070     { row=current.x; col=current.y; }

template<typename T >
void spiralindex< T >::reset (  )  [inline]

Reset the iterator.

Definition at line 303 of file spiralindex.h.

References spiralindex< T >::columns, spiralindex< T >::direction, and spiralindex< T >::rows.

Referenced by spiralindex< T >::indexsequence(), spiralindextest::test01(), and spiralindextest::test02().

00304 {
00305   current.x = 0;
00306   current.y = 0;
00307   count = 0;
00308 
00309   direction=0;
00310 
00311   start.x = 0;
00312   start.y = 0;
00313 
00314   xrange.x = 0;
00315   xrange.y = columns-1;
00316   yrange.x = 0;
00317   yrange.y = rows-1;
00318 }

template<typename T >
boolc spiralindex< T >::valid ( point2< T > const &  p  )  const [inline]

Is the point within the matrix?

Definition at line 279 of file spiralindex.h.

References point2< T >::x, and point2< T >::y.

Referenced by spiralindex< T >::operator++().

00280 {
00281   if (p.x==start.x)
00282   {
00283     if (p.y==start.y)
00284       return false;
00285   }
00286 
00287   if (p.x<yrange.x)
00288     return false;
00289 
00290   if (p.y<xrange.x)
00291     return false;
00292 
00293   if (p.x>yrange.y)
00294     return false;
00295 
00296   if (p.y>xrange.y)
00297     return false;
00298 
00299   return true;
00300 }


Member Data Documentation

template<typename T = int>
T const spiralindex< T >::columns

The number of columns.

Definition at line 47 of file spiralindex.h.

Referenced by spiralindex< T >::pos(), and spiralindex< T >::reset().

template<typename T = int>
point2<T> spiralindex< T >::di[4]

The four ordered directions.

Definition at line 49 of file spiralindex.h.

Referenced by spiralindex< T >::operator++().

template<typename T = int>
uint spiralindex< T >::direction

Index to the current direction.

Definition at line 51 of file spiralindex.h.

Referenced by spiralindex< T >::operator++(), and spiralindex< T >::reset().

template<typename T = int>
T const spiralindex< T >::rows

The number of rows.

Definition at line 45 of file spiralindex.h.

Referenced by spiralindex< T >::reset().


The documentation for this class was generated from the following file:

Generated on Fri Mar 4 00:50:18 2011 for Chelton Evans Source by  doxygen 1.5.8