diff options
author | Robert Vollmert <rvollmert@gmx.net> | 2009-11-07 17:57:53 +0100 |
---|---|---|
committer | Robert Vollmert <rvollmert@gmx.net> | 2009-11-08 01:41:37 +0100 |
commit | dbb39bdfc8ddc1c4b06b07f09d626791e46ed4bb (patch) | |
tree | 28c17990d45da16913eb686c9ddf3b702794287a /crawl-ref/source | |
parent | ef9199151b8f3e969fc006ad4c52282b44ab8133 (diff) | |
download | crawl-ref-dbb39bdfc8ddc1c4b06b07f09d626791e46ed4bb.tar.gz crawl-ref-dbb39bdfc8ddc1c4b06b07f09d626791e46ed4bb.zip |
Create coord-circle.cc containing circle_def.
circle_def unifies the shapes that a radius_iterator can iterate
over. It's meant to replace bounds_func in losparam.h and eventually
provide a base for radius_iterator.
Diffstat (limited to 'crawl-ref/source')
-rw-r--r-- | crawl-ref/source/coord-circle.cc | 106 | ||||
-rw-r--r-- | crawl-ref/source/coord-circle.h | 76 | ||||
-rw-r--r-- | crawl-ref/source/makefile.obj | 1 |
3 files changed, 183 insertions, 0 deletions
diff --git a/crawl-ref/source/coord-circle.cc b/crawl-ref/source/coord-circle.cc new file mode 100644 index 0000000000..4170662b10 --- /dev/null +++ b/crawl-ref/source/coord-circle.cc @@ -0,0 +1,106 @@ +#include "AppHdr.h" + +#include "coord-circle.h" + +#include "coordit.h" + +#include <cmath> + +rectangle_iterator rect_def::iter() const +{ + return (rectangle_iterator(min, max)); +} + +circle_def::circle_def() + : los_radius(true), origin(coord_def(0,0)) +{ + // Set up bounding box and shape. + init(LOS_MAX_RADIUS, C_ROUND); +} + +circle_def::circle_def(int param, circle_type ctype) + : los_radius(false), origin(coord_def(0,0)) +{ + init(param, ctype); +} + +circle_def::circle_def(const coord_def &origin_, int param, + circle_type ctype) + : los_radius(false), origin(origin_) +{ + init(param, ctype); +} + +void circle_def::init(int param, circle_type ctype) +{ + switch (ctype) + { + case C_SQUARE: + shape = SH_SQUARE; + radius = param; + break; + case C_CIRCLE: + shape = SH_CIRCLE; + radius = static_cast<int>(ceil(sqrt(radius_sq))); + radius_sq = param; + break; + case C_ROUND: + shape = SH_CIRCLE; + radius = param; + radius_sq = radius * radius + 1; + break; + case C_POINTY: + shape = SH_CIRCLE; + radius = param; + radius_sq = radius * radius; + } + bbox = rect_def(origin - coord_def(radius, radius), + origin + coord_def(radius, radius)); +} + +const rect_def& circle_def::get_bbox() const +{ + return (bbox); +} + +bool circle_def::contains(const coord_def &p) const +{ + switch (shape) + { + case SH_SQUARE: + return ((p - origin).rdist() <= radius); + case SH_CIRCLE: + return ((p - origin).abs() <= radius_sq); + default: + return (false); + } +} + +circle_iterator::circle_iterator(const circle_def &circle_) + : circle(circle_), iter(circle_.get_bbox().iter()) +{ + while (iter && !circle.contains(*iter)) + ++iter; +} + +circle_iterator::operator bool() const +{ + return ((bool)iter); +} + +coord_def circle_iterator::operator*() const +{ + return (*iter); +} + +void circle_iterator::operator++() +{ + do + ++iter; + while (iter && !circle.contains(*iter)); +} + +void circle_iterator::operator++(int) +{ + ++(*this); +} diff --git a/crawl-ref/source/coord-circle.h b/crawl-ref/source/coord-circle.h new file mode 100644 index 0000000000..172e2600b6 --- /dev/null +++ b/crawl-ref/source/coord-circle.h @@ -0,0 +1,76 @@ +#ifndef COORD_CIRCLE_H +#define COORD_CIRCLE_H + +#include "coordit.h" + +enum shape_type +{ + SH_SQUARE, // square around an origin + SH_CIRCLE // circle around an origin +}; + +enum circle_type +{ + C_SQUARE, + C_CIRCLE, + C_POINTY, + C_ROUND +}; + +class rect_def +{ + coord_def min; + coord_def max; + +public: + rect_def() {} + rect_def(const coord_def &min_, const coord_def &max_) + : min(min_), max(max_) {} + + rectangle_iterator iter() const; +}; + +class circle_iterator; +class circle_def +{ + bool los_radius; + shape_type shape; + + coord_def origin; + int radius; + int radius_sq; + rect_def bbox; + +public: + // Circle around (0,0) with radius that tracks global LOS radius. + circle_def(); + // Circle around (0,0) of specified shape and size. + explicit circle_def(int param, circle_type ctype = C_SQUARE); + // Circle around given origin of specified shape and size. + circle_def(const coord_def &origin_, int param, circle_type ctype = C_SQUARE); + + bool contains(const coord_def &p) const; + const rect_def& get_bbox() const; + + circle_iterator iter() const; + +private: + void init(int param, circle_type ctype); +}; + +class circle_iterator +{ + const circle_def &circle; + rectangle_iterator iter; + +public: + circle_iterator(const circle_def &circle_); + + operator bool() const; + coord_def operator*() const; + + void operator++(); + void operator++(int); +}; + +#endif diff --git a/crawl-ref/source/makefile.obj b/crawl-ref/source/makefile.obj index 588ab56676..90bc0375b9 100644 --- a/crawl-ref/source/makefile.obj +++ b/crawl-ref/source/makefile.obj @@ -19,6 +19,7 @@ colour.o \ command.o \ coord.o \ coordit.o \ +coord-circle.o \ ctest.o \ database.o \ dbg-asrt.o \ |