Classes pour les directions, avec support des opérateurs + - | & == != .
This commit is contained in:
parent
30251f62c0
commit
d2d4cef8f1
7
geometry/directions.cpp
Normal file
7
geometry/directions.cpp
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
#include "all_includes.hh"
|
||||||
|
|
||||||
|
const char Cardinal::rotationTable[9] = { -1, 0, 1, -1, 2, -1, -1, -1, 3 };
|
||||||
|
const char Coin::rotationTable[9] = { -1, 0, 1, -1, 2, -1, -1, -1, 3 };
|
||||||
|
|
||||||
|
const char CoteTriangle::rotationTable[5] = { -1, 0, 1, -1, 2 };
|
||||||
|
const char SommetTriangle::rotationTable[5] = { -1, 0, 1, -1, 2 };
|
|
@ -1,37 +1,117 @@
|
||||||
#ifndef _GEOMETRY_DIRECTIONS_HH_
|
#ifndef _GEOMETRY_DIRECTIONS_HH_
|
||||||
#define _GEOMETRY_DIRECTIONS_HH_
|
#define _GEOMETRY_DIRECTIONS_HH_
|
||||||
|
|
||||||
enum Cardinal {
|
class EnsembleCardinaux {
|
||||||
N = 0,
|
protected:
|
||||||
E = 1,
|
char v;
|
||||||
S = 2,
|
EnsembleCardinaux(char _v) : v(_v) {}
|
||||||
W = 3
|
public:
|
||||||
|
friend EnsembleCardinaux operator| (const EnsembleCardinaux ec1, const EnsembleCardinaux ec2) {
|
||||||
|
return EnsembleCardinaux(ec1.v | ec2.v);
|
||||||
|
}
|
||||||
|
friend EnsembleCardinaux operator& (const EnsembleCardinaux ec1, const EnsembleCardinaux ec2) {
|
||||||
|
return EnsembleCardinaux(ec1.v & ec2.v);
|
||||||
|
}
|
||||||
|
friend bool operator== (const EnsembleCardinaux ec1, const EnsembleCardinaux ec2) {
|
||||||
|
return (ec1.v == ec2.v);
|
||||||
|
}
|
||||||
|
friend bool operator!= (const EnsembleCardinaux ec1, const EnsembleCardinaux ec2) {
|
||||||
|
return (ec1.v != ec2.v);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
inline Cardinal operator+(Cardinal c, int i) {
|
class Cardinal : public EnsembleCardinaux {
|
||||||
return Cardinal((int(c) + int(i)) & 3);
|
private:
|
||||||
//int result = int(c) << (i & 3);
|
static const char rotationTable[9];
|
||||||
//result = result | result >> 4;
|
public:
|
||||||
//return Cardinal (c & 15);
|
Cardinal(int x) : EnsembleCardinaux((char)(1 << (x & 3))) {}
|
||||||
|
operator int () const {
|
||||||
|
return rotationTable[(int)v];
|
||||||
}
|
}
|
||||||
inline Cardinal operator-(Cardinal c, int i) {
|
friend Cardinal operator+ (const Cardinal c, const int n) {
|
||||||
return c + (-i);
|
return Cardinal(rotationTable[(int)c.v] + n);
|
||||||
|
}
|
||||||
|
friend Cardinal operator- (const Cardinal c, const int n) {
|
||||||
|
return Cardinal(rotationTable[(int)c.v] - n);
|
||||||
}
|
}
|
||||||
|
|
||||||
enum Coin {
|
|
||||||
NE = 0,
|
|
||||||
SE = 1,
|
|
||||||
SW = 2,
|
|
||||||
NW = 3
|
|
||||||
};
|
};
|
||||||
|
|
||||||
inline Coin operator+(Coin c, int i) {
|
const Cardinal N = Cardinal(0);
|
||||||
return Coin((int(c) + int(i)) & 3);
|
const Cardinal E = Cardinal(1);
|
||||||
}
|
const Cardinal S = Cardinal(2);
|
||||||
inline Coin operator-(Coin c, int i) {
|
const Cardinal W = Cardinal(3);
|
||||||
return c + (-i);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// Plus ou moins la même chose que Cardinal.
|
||||||
|
class Coin {
|
||||||
|
private:
|
||||||
|
char v;
|
||||||
|
static const char rotationTable[9];
|
||||||
|
public:
|
||||||
|
Coin(int x) : v((char)(1 << (x & 3))) {}
|
||||||
|
operator int () const {
|
||||||
|
return (int)rotationTable[(int)v];
|
||||||
|
}
|
||||||
|
friend Coin operator+ (const Coin c, const int n) {
|
||||||
|
return Coin(rotationTable[(int)c.v] + n);
|
||||||
|
}
|
||||||
|
friend Coin operator- (const Coin c, const int n) {
|
||||||
|
return Coin(rotationTable[(int)c.v] - n);
|
||||||
|
}
|
||||||
|
friend bool operator== (const Coin c1, const Coin c2) {
|
||||||
|
return (c1.v == c2.v);
|
||||||
|
}
|
||||||
|
friend bool operator!= (const Coin c1, const Coin c2) {
|
||||||
|
return (c1.v != c2.v);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const Coin NE = Coin(0);
|
||||||
|
const Coin SE = Coin(1);
|
||||||
|
const Coin SW = Coin(2);
|
||||||
|
const Coin NW = Coin(3);
|
||||||
|
|
||||||
|
// Pour les triangles, c'est quasiment identique, il y a sûrement moyen de factoriser ça.
|
||||||
|
|
||||||
|
class EnsembleCotesTriangle {
|
||||||
|
protected:
|
||||||
|
char v;
|
||||||
|
EnsembleCotesTriangle(char _v) : v(_v) {}
|
||||||
|
public:
|
||||||
|
friend EnsembleCotesTriangle operator| (const EnsembleCotesTriangle ec1, const EnsembleCotesTriangle ec2) {
|
||||||
|
return EnsembleCotesTriangle(ec1.v | ec2.v);
|
||||||
|
}
|
||||||
|
friend EnsembleCotesTriangle operator& (const EnsembleCotesTriangle ec1, const EnsembleCotesTriangle ec2) {
|
||||||
|
return EnsembleCotesTriangle(ec1.v & ec2.v);
|
||||||
|
}
|
||||||
|
friend bool operator== (const EnsembleCotesTriangle ec1, const EnsembleCotesTriangle ec2) {
|
||||||
|
return (ec1.v == ec2.v);
|
||||||
|
}
|
||||||
|
friend bool operator!= (const EnsembleCotesTriangle ec1, const EnsembleCotesTriangle ec2) {
|
||||||
|
return (ec1.v != ec2.v);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class CoteTriangle : public EnsembleCotesTriangle {
|
||||||
|
private:
|
||||||
|
static const char rotationTable[5];
|
||||||
|
public:
|
||||||
|
CoteTriangle(int x) : EnsembleCotesTriangle((char)(1 << (((x % 3) + 3) % 3))) {}
|
||||||
|
operator int () const {
|
||||||
|
return rotationTable[(int)v];
|
||||||
|
}
|
||||||
|
friend CoteTriangle operator+ (const CoteTriangle c, const int n) {
|
||||||
|
return CoteTriangle(rotationTable[(int)c.v] + n);
|
||||||
|
}
|
||||||
|
friend CoteTriangle operator- (const CoteTriangle c, const int n) {
|
||||||
|
return CoteTriangle(rotationTable[(int)c.v] - n);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const CoteTriangle LEFTSIDE = CoteTriangle(0);
|
||||||
|
const CoteTriangle RIGHTSIDE = CoteTriangle(1);
|
||||||
|
const CoteTriangle BASE = CoteTriangle(2);
|
||||||
|
|
||||||
|
/*
|
||||||
enum SommetTriangle {
|
enum SommetTriangle {
|
||||||
LEFT = 0,
|
LEFT = 0,
|
||||||
TOP = 1,
|
TOP = 1,
|
||||||
|
@ -45,19 +125,34 @@ inline SommetTriangle operator+(SommetTriangle c, int i) {
|
||||||
inline SommetTriangle operator-(SommetTriangle c, int i) {
|
inline SommetTriangle operator-(SommetTriangle c, int i) {
|
||||||
return SommetTriangle((((int(c) - int(i)) % 3 ) + 3) % 3);
|
return SommetTriangle((((int(c) - int(i)) % 3 ) + 3) % 3);
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
enum CoteTriangle {
|
// Plus ou moins la même chose que CoteTriangle.
|
||||||
LEFTSIDE = 0,
|
class SommetTriangle {
|
||||||
RIGHTSIDE = 1,
|
private:
|
||||||
BASE = 2
|
char v;
|
||||||
|
static const char rotationTable[5];
|
||||||
|
public:
|
||||||
|
SommetTriangle(int x) : v((char)(1 << (((x % 3) + 3) % 3))) {}
|
||||||
|
operator int () const {
|
||||||
|
return (int)rotationTable[(int)v];
|
||||||
|
}
|
||||||
|
friend SommetTriangle operator+ (const SommetTriangle c, const int n) {
|
||||||
|
return SommetTriangle(rotationTable[(int)c.v] + n);
|
||||||
|
}
|
||||||
|
friend SommetTriangle operator- (const SommetTriangle c, const int n) {
|
||||||
|
return SommetTriangle(rotationTable[(int)c.v] - n);
|
||||||
|
}
|
||||||
|
friend bool operator== (const SommetTriangle c1, const SommetTriangle c2) {
|
||||||
|
return (c1.v == c2.v);
|
||||||
|
}
|
||||||
|
friend bool operator!= (const SommetTriangle c1, const SommetTriangle c2) {
|
||||||
|
return (c1.v != c2.v);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
inline CoteTriangle operator+(CoteTriangle c, int i) {
|
const SommetTriangle LEFT = SommetTriangle(0);
|
||||||
return CoteTriangle((((int(c) + int(i)) % 3 ) + 3) % 3);
|
const SommetTriangle TOP = SommetTriangle(1);
|
||||||
}
|
const SommetTriangle RIGHT = SommetTriangle(2);
|
||||||
|
|
||||||
inline CoteTriangle operator-(CoteTriangle c, int i) {
|
|
||||||
return CoteTriangle((((int(c) - int(i)) % 3 ) + 3) % 3);
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -3,20 +3,20 @@
|
||||||
Quad::Quad() {}
|
Quad::Quad() {}
|
||||||
|
|
||||||
Quad::Quad(Vertex ne, Vertex se, Vertex sw, Vertex nw) {
|
Quad::Quad(Vertex ne, Vertex se, Vertex sw, Vertex nw) {
|
||||||
c[NE] = ne;
|
c[(int)NE] = ne;
|
||||||
c[SE] = se;
|
c[(int)SE] = se;
|
||||||
c[SW] = sw;
|
c[(int)SW] = sw;
|
||||||
c[NW] = nw;
|
c[(int)NW] = nw;
|
||||||
}
|
}
|
||||||
|
|
||||||
Quad Quad::inset(Cardinal side, float offset) const {
|
Quad Quad::inset(Cardinal side, float offset) const {
|
||||||
Quad q = (*this) << side;
|
Quad q = (*this) << int(side);
|
||||||
Vertex offsetDirection = (q[NW]-q[NE]).perpendicularCw();
|
Vertex offsetDirection = (q[NW]-q[NE]).perpendicularCw();
|
||||||
float distE = offset / offsetDirection.cosAngle(q[SE] - q[NE]);
|
float distE = offset / offsetDirection.cosAngle(q[SE] - q[NE]);
|
||||||
float distW = offset / offsetDirection.cosAngle(q[SW] - q[NW]);
|
float distW = offset / offsetDirection.cosAngle(q[SW] - q[NW]);
|
||||||
q[NE] = q[NE] + (q[SE] - q[NE]).setNorm(distE);
|
q[NE] = q[NE] + (q[SE] - q[NE]).setNorm(distE);
|
||||||
q[NW] = q[NW] + (q[SW] - q[NW]).setNorm(distW);
|
q[NW] = q[NW] + (q[SW] - q[NW]).setNorm(distW);
|
||||||
return q >> side;
|
return q >> int(side);
|
||||||
}
|
}
|
||||||
|
|
||||||
Quad Quad::insetNESW(float offsetN, float offsetE, float offsetS, float offsetW) const {
|
Quad Quad::insetNESW(float offsetN, float offsetE, float offsetS, float offsetW) const {
|
||||||
|
@ -56,7 +56,7 @@ Quad Quad::makeParallelogram() const {
|
||||||
}
|
}
|
||||||
|
|
||||||
float Quad::length(Cardinal side) const {
|
float Quad::length(Cardinal side) const {
|
||||||
return Segment(c[NW+side],c[NE+side]).length();
|
return Segment(c[NW+int(side)],c[NE+int(side)]).length();
|
||||||
}
|
}
|
||||||
|
|
||||||
float Quad::minLengthNS() const {
|
float Quad::minLengthNS() const {
|
||||||
|
|
1
main.cpp
1
main.cpp
|
@ -1,7 +1,6 @@
|
||||||
#include "all_includes.hh"
|
#include "all_includes.hh"
|
||||||
|
|
||||||
// TODO : créer les routes dans les bâtiments
|
// TODO : créer les routes dans les bâtiments
|
||||||
// TODO : faire des enum SetCoin, SetCardinal, SetSommetTriangle, SetCoteTriangle.
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
// Générer une tile de base
|
// Générer une tile de base
|
||||||
|
|
|
@ -7,9 +7,6 @@ class TrottoirQuadNormal : public Chose {
|
||||||
private :
|
private :
|
||||||
Quad c;
|
Quad c;
|
||||||
float height;
|
float height;
|
||||||
// TODO : pas besoin de ce champ : il suffit d'orienter
|
|
||||||
// correctement le trottoir lorsqu'on le crée.
|
|
||||||
Cardinal border;
|
|
||||||
|
|
||||||
public :
|
public :
|
||||||
TrottoirQuadNormal(Quad c, float height);
|
TrottoirQuadNormal(Quad c, float height);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user