Show maptilepos.cpp syntax highlighted
/***************************************************************************
maptilepos.cpp - description
-------------------
begin : Thu Sep 19 2002
copyright : (C) 2002 by Brendon Higgins
email : freepop-devel@lists.sourceforge.net
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include <maptilepos.h>
#include <corner.h>
#include <rotation.h>
#include <misc.h>
MapTilePos::MapTilePos(float px, float py) {
x = px;
y = py;
}
MapTilePos::MapTilePos(const MapTilePos& p) {
x = p.x;
y = p.y;
}
bool MapTilePos::operator==(const MapTilePos& m) const {
return x == m.x && y == m.y;
}
MapTilePos& MapTilePos::operator+=(const MapTilePos& p) {
x += p.x;
y += p.y;
return *this;
}
MapTilePos& MapTilePos::operator-=(const MapTilePos& p) {
x -= p.x;
y -= p.y;
return *this;
}
float MapTilePos::getX() const {
return x;
}
float MapTilePos::getY() const {
return y;
}
Corner MapTilePos::calcNearestCorner() const {
float xSq = x * x;
float ySq = y * y;
float xAltSq = 1.0f - x;
xAltSq *= xAltSq;
float yAltSq = 1.0f - y;
yAltSq *= yAltSq;
float distSqNorth = xSq + ySq;
float distSqEast = xAltSq + ySq;
float distSqSouth = xAltSq + yAltSq;
float distSqWest = xSq + yAltSq;
float distSqMin = min(distSqNorth, distSqEast, distSqSouth, distSqWest);
if (distSqMin == distSqNorth) {
return Corner::NORTH;
} else if (distSqMin == distSqEast) {
return Corner::EAST;
} else if (distSqMin == distSqSouth) {
return Corner::SOUTH;
} else {
return Corner::WEST;
}
}
MapTilePos& MapTilePos::operator*=(const Rotation& r) {
float ox = x;
float oy = y;
switch (r.getClock()) {
case 1:
x = 1.0f - oy;
y = ox;
break;
case 2:
x = 1.0f - ox;
y = 1.0f - oy;
break;
case 3:
x = oy;
y = 1.0f - ox;
break;
default:
break;
}
return *this;
}
bool MapTilePos::isValid() const {
return 0.0f <= x && x <= 1.0f && 0.0f <= y && y <= 1.0f;
}
See more files for this project here