Code cleanup
[crawl.git] / crawl-ref / source / coord-circle.cc
blob1701a1473431ae13028729807d116b570ef493c4
1 #include "AppHdr.h"
3 #include "coord-circle.h"
5 #include "coordit.h"
6 #include "los.h"
8 #include <cmath>
10 bool rect_def::contains(const coord_def& p) const
12 return (p.x >= min.x && p.x <= max.x && p.y >= min.y && p.y <= max.y);
15 rect_def rect_def::intersect(const rect_def& other) const
17 rect_def res;
18 res.min.x = std::max(min.x, other.min.x);
19 res.min.y = std::max(min.y, other.min.y);
20 res.max.x = std::min(max.x, other.max.x);
21 res.max.y = std::min(max.y, other.max.y);
22 return (res);
25 rectangle_iterator rect_def::iter() const
27 return (rectangle_iterator(min, max));
30 circle_def::circle_def()
31 : los_radius(true), origin(coord_def(0,0)), check_bounds(false)
33 // Set up bounding box and shape.
34 init(LOS_MAX_RADIUS, C_ROUND);
37 circle_def::circle_def(const coord_def& origin_, const circle_def& bds)
38 : los_radius(bds.los_radius), shape(bds.shape),
39 origin(origin_), check_bounds(true),
40 radius(bds.radius), radius_sq(bds.radius_sq)
42 // Set up bounding box.
43 init_bbox();
46 circle_def::circle_def(int param, circle_type ctype)
47 : los_radius(false), origin(coord_def(0,0)), check_bounds(false)
49 init(param, ctype);
52 circle_def::circle_def(const coord_def &origin_, int param,
53 circle_type ctype)
54 : los_radius(false), origin(origin_), check_bounds(true)
56 init(param, ctype);
59 void circle_def::init(int param, circle_type ctype)
61 switch (ctype)
63 case C_SQUARE:
64 shape = SH_SQUARE;
65 radius = param;
66 break;
67 case C_CIRCLE:
68 shape = SH_CIRCLE;
69 radius_sq = param;
70 radius = ceil(sqrt((float)radius_sq));
71 break;
72 case C_ROUND:
73 shape = SH_CIRCLE;
74 radius = param;
75 radius_sq = radius * radius + 1;
76 break;
77 case C_POINTY:
78 shape = SH_CIRCLE;
79 radius = param;
80 radius_sq = radius * radius;
82 init_bbox();
85 void circle_def::init_bbox()
87 bbox = rect_def(origin - coord_def(radius, radius),
88 origin + coord_def(radius, radius));
89 if (check_bounds)
90 bbox = bbox.intersect(RECT_MAP_BOUNDS);
93 const rect_def& circle_def::get_bbox() const
95 return (bbox);
98 const coord_def& circle_def::get_center() const
100 return (origin);
103 circle_iterator circle_def::iter() const
105 return (circle_iterator(*this));
108 bool circle_def::contains(const coord_def &p) const
110 if (!bbox.contains(p))
111 return (false);
112 switch (shape)
114 case SH_SQUARE:
115 return ((p - origin).rdist() <= radius);
116 case SH_CIRCLE:
118 int r_sq = los_radius ? get_los_radius_sq() : radius_sq;
119 return ((p - origin).abs() <= r_sq);
121 default:
122 return (false);