summaryrefslogtreecommitdiffstats
path: root/crawl-ref/source/coord-circle.cc
blob: 8580313e01eb9d1753bba9fd5a244bfe56840f2a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#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_sq = param;
        radius = static_cast<int>(ceil(sqrt(radius_sq)));
        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);
}

const coord_def& circle_def::get_center() const
{
    return (origin);
}

circle_iterator circle_def::iter() const
{
    return (circle_iterator(*this));
}

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);
    }
}