Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Fork of el17dg by
game/geometry.h
- Committer:
- Noximilien
- Date:
- 2019-04-15
- Revision:
- 29:579e00b7f118
- Parent:
- 28:35af3843de8f
- Child:
- 30:d454d0cb72bc
File content as of revision 29:579e00b7f118:
#ifndef GEOMETRY_H
#define GEOMETRY_H
#include <math.h>
////////////////////////////////////////////////////////////////////////
/**
* A structure to represent the positions of sprites
*/
struct Point {
/*@{*/
int x; /**< the x position */
int y; /**< the y position */
Point() : x(0), y(0) {}
Point(int _x, int _y) : x(_x), y(_y) {}
Point operator+ (const Point& rhs) const {
Point result(x + rhs.x, y + rhs.y);
return result;
}
Point operator- (const Point& rhs) const {
Point result(x - rhs.x, y - rhs.y);
return result;
}
int lengthSquared() {
return x * x + y * y;
}
};
/** CicleBounds Class
* @brief A library for appointing the sprites' bounds.
* @author Dmitrijs Griskovs
* @date 15/04/2019
*/
class CircleBounds {
public:
///////////////////////////////////////////////////////////////////////////////////////
/** Constructor */
CircleBounds() {};
CircleBounds(Point _center, float _radius): center(_center), radius(_radius) {};
Point center;
float radius;
};
/** @brief a function that determines whether the two sprites have collided.
* @details the function takes in the position and the bounds (center and radius)
* two sprites (x and y positions and the circle area of a sprite) then performs the
* pythagoras calaculation and returns a bool statment if there is an overlap
* in the circle area of two sprites.
*
* @param pos_a positon of x and y of first sprite. (const Point&).
* @param bounds_a center point and radius of the first sprite. (const CircleBounds&).
* @param pos_b positon of x and y of second sprite. (const Point&).
* @param bounds_b center point and radius of the second sprite. (const CircleBounds&).
* @return difference.lengthSquared() if the areas of two sprites overlap, it will return true.
*/
inline bool circleCollideTwoObjects(
const Point& pos_a, const CircleBounds& bounds_a,
const Point& pos_b, const CircleBounds& bounds_b
) {
Point center_a = pos_a + bounds_a.center;
Point center_b = pos_b + bounds_b.center;
Point difference = center_a - center_b;
return difference.lengthSquared() <= pow(bounds_a.radius + bounds_b.radius, 2);
}
#endif
