const-correctness in location/rectangle

This commit is contained in:
2023-01-29 23:37:34 -05:00
parent 066a922443
commit f2b438d5c5
2 changed files with 16 additions and 16 deletions

View File

@@ -54,7 +54,7 @@ bool loc_compare::operator()(location a, location b) const {
return false; return false;
} }
bool location::in(rectangle r){ bool location::in(rectangle r) const {
if(y >= r.top && y <= r.bottom && x >= r.left && x <= r.right) if(y >= r.top && y <= r.bottom && x >= r.left && x <= r.right)
return true; return true;
return false; return false;
@@ -88,13 +88,13 @@ rectangle::rectangle(int t, int l, int b, int r) : top(t), left(l), right(r), bo
rectangle::rectangle(const sf::Texture& texture) : top(0), left(0), right(texture.getSize().x), bottom(texture.getSize().y) {} rectangle::rectangle(const sf::Texture& texture) : top(0), left(0), right(texture.getSize().x), bottom(texture.getSize().y) {}
rectangle::rectangle(const sf::RenderTarget& texture) : top(0), left(0), right(texture.getSize().x), bottom(texture.getSize().y) {} rectangle::rectangle(const sf::RenderTarget& texture) : top(0), left(0), right(texture.getSize().x), bottom(texture.getSize().y) {}
bool rectangle::contains(location p){ bool rectangle::contains(location p) const {
if(p.y >= top && p.y <= bottom && p.x >= left && p.x <= right) if(p.y >= top && p.y <= bottom && p.x >= left && p.x <= right)
return true; return true;
return false; return false;
} }
bool rectangle::contains(int x, int y) { bool rectangle::contains(int x, int y) const {
return contains(location(x,y)); return contains(location(x,y));
} }
@@ -166,23 +166,23 @@ const rectangle_size_delegate rectangle::height() const {
return rectangle_size_delegate(*const_cast<rectangle*>(this), &rectangle::top, &rectangle::bottom); return rectangle_size_delegate(*const_cast<rectangle*>(this), &rectangle::top, &rectangle::bottom);
} }
location rectangle::centre() { location rectangle::centre() const {
return location((left + right) / 2, (top + bottom) / 2); return location((left + right) / 2, (top + bottom) / 2);
} }
location rectangle::topLeft() { location rectangle::topLeft() const {
return location(left, top); return location(left, top);
} }
location rectangle::topRight() { location rectangle::topRight() const {
return location(right, top); return location(right, top);
} }
location rectangle::bottomLeft() { location rectangle::bottomLeft() const {
return location(left, bottom); return location(left, bottom);
} }
location rectangle::bottomRight() { location rectangle::bottomRight() const {
return location(right, bottom); return location(right, bottom);
} }

View File

@@ -34,7 +34,7 @@ struct location {
location(int x, int y); location(int x, int y);
template<typename T> template<typename T>
location(sf::Vector2<T> other) : location(other.x, other.y) {} location(sf::Vector2<T> other) : location(other.x, other.y) {}
bool in(rectangle r); bool in(rectangle r) const;
template<typename T> template<typename T>
operator typename sf::template Vector2<T>() { operator typename sf::template Vector2<T>() {
return sf::Vector2<T>(x,y); return sf::Vector2<T>(x,y);
@@ -81,13 +81,13 @@ struct rectangle {
rectangle_size_delegate height(); rectangle_size_delegate height();
const rectangle_size_delegate width() const; const rectangle_size_delegate width() const;
const rectangle_size_delegate height() const; const rectangle_size_delegate height() const;
location centre(); location centre() const;
location topLeft(); location topLeft() const;
location topRight(); location topRight() const;
location bottomLeft(); location bottomLeft() const;
location bottomRight(); location bottomRight() const;
bool contains(location p); bool contains(location p) const;
bool contains(int x, int y); bool contains(int x, int y) const;
void offset(int h, int v); void offset(int h, int v);
void offset(location diff); void offset(location diff);
void move_to(int x, int y); void move_to(int x, int y);