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/coord-circle.cc | |
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/coord-circle.cc')
-rw-r--r-- | crawl-ref/source/coord-circle.cc | 106 |
1 files changed, 106 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); +} |