New Line class and related algorithms and functions.

This commit is contained in:
King_DuckZ 2014-07-16 23:42:37 +02:00
parent 3f79507beb
commit 06e3bfdeb7
10 changed files with 416 additions and 0 deletions

View file

@ -56,8 +56,10 @@ add_executable(${PROJECT_NAME}
src/sizeratio.cpp
src/sizenotifiable.cpp
src/platform.cpp
src/vectormath.cpp
src/platformsystem.cpp
src/movers/moverworld.cpp
src/line.cpp
)
target_link_libraries(${PROJECT_NAME}

View file

@ -31,6 +31,7 @@
#define DEF_RANDOM_SEED 1984
#define MAX_PLATFORMS_ON_SCREEN 8
/* TODO: make this path relative */
#define GAME_BASE_PATH "@CMAKE_SOURCE_DIR@"
#endif

24
src/line.cpp Normal file
View file

@ -0,0 +1,24 @@
/*
Copyright 2014 Michele "King_DuckZ" Santullo
This file is part of CloonelJump.
CloonelJump 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 3 of the License, or
(at your option) any later version.
CloonelJump is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with CloonelJump. If not, see <http://www.gnu.org/licenses/>.
*/
#include "line.hpp"
#include "line_helpers.hpp"
namespace cloonel {
} //namespace cloonel

72
src/line.hpp Normal file
View file

@ -0,0 +1,72 @@
/*
Copyright 2014 Michele "King_DuckZ" Santullo
This file is part of CloonelJump.
CloonelJump 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 3 of the License, or
(at your option) any later version.
CloonelJump is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with CloonelJump. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef id56F112C6551D44039D0C0270F573B35B
#define id56F112C6551D44039D0C0270F573B35B
#include "vector.hpp"
#include "vectormath.hpp"
namespace cloonel {
template <typename T, uint32_t S>
class Line {
public:
typedef Vector<T, S> Point;
typedef T Scalar;
Line ( void ) = default;
Line ( const Line& parOther );
Line ( const Point& parStart, const Point& parEnd );
Line ( const Point& parStart, const Point& parDirection, Scalar parLength ) : Line(parStart, parDirection * parLength) { }
~Line ( void ) noexcept = default;
Point& Start ( void ) { return m_points.x(); }
Point& End ( void ) { return m_points.y(); }
const Point& Start ( void ) const { return m_points.x(); }
const Point& End ( void ) const { return m_points.y(); }
Line& operator+= ( const Point& parRhs ) __attribute__((flatten));
Line& operator-= ( const Point& parRhs ) __attribute__((flatten));
private:
Vector<Point, 2> m_points;
};
template <typename T, uint32_t S>
Line<T, S> operator+ ( Line<T, S> parLhs, const Vector<T, S>& parRhs ) __attribute__((pure)) __attribute__((flatten));
template <typename T, uint32_t S>
Line<T, S> operator- ( Line<T, S> parLhs, const Vector<T, S>& parRhs ) __attribute__((pure)) __attribute__((flatten));
template <typename T, uint32_t S>
Line<T, S> operator+ ( const Vector<T, S>& parLhs, Line<T, S> parRhs ) __attribute__((pure)) __attribute__((flatten));
template <typename T, uint32_t S>
Line<T, S> operator- ( const Vector<T, S>& parLhs, Line<T, S> parRhs ) __attribute__((pure)) __attribute__((flatten));
template <typename T>
bool operator> ( const Vector<T, 2>& parLhs, const Line<T, 2>& parRhs ) __attribute__((pure));
template <typename T>
bool operator< ( const Vector<T, 2>& parLhs, const Line<T, 2>& parRhs ) __attribute__((pure));
template <typename T>
bool operator>= ( const Vector<T, 2>& parLhs, const Line<T, 2>& parRhs ) __attribute__((pure));
template <typename T>
bool operator<= ( const Vector<T, 2>& parLhs, const Line<T, 2>& parRhs ) __attribute__((pure));
} //namespace cloonel
#include "line.inl"
#endif

112
src/line.inl Normal file
View file

@ -0,0 +1,112 @@
namespace cloonel {
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
template <typename T, uint32_t S>
Line<T, S>::Line (const Line& parOther) :
m_points(parOther.m_points)
{
}
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
template <typename T, uint32_t S>
Line<T, S>::Line (const Point& parStart, const Point& parEnd) :
m_points(parStart, parEnd)
{
}
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
template <typename T, uint32_t S>
Line<T, S>& Line<T, S>::operator+= (const Point& parRhs) {
for (uint32_t z = 0; z < S; ++z) {
m_points[z] += parRhs;
}
return *this;
}
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
template <typename T, uint32_t S>
Line<T, S>& Line<T, S>::operator-= (const Point& parRhs) {
for (uint32_t z = 0; z < S; ++z) {
m_points[z] -= parRhs;
}
return *this;
}
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
template <typename T, uint32_t S>
Line<T, S> operator+ ( const Vector<T, S>& parLhs, Line<T, S> parRhs ) {
parRhs += parLhs;
return parRhs;
}
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
template <typename T, uint32_t S>
Line<T, S> operator- ( const Vector<T, S>& parLhs, Line<T, S> parRhs ) {
parRhs -= parLhs;
return parRhs;
}
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
template <typename T, uint32_t S>
Line<T, S> operator+ ( Line<T, S> parLhs, const Vector<T, S>& parRhs ) {
parLhs += parRhs;
return parLhs;
}
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
template <typename T, uint32_t S>
Line<T, S> operator- ( Line<T, S> parLhs, const Vector<T, S>& parRhs ) {
parLhs -= parRhs;
return parLhs;
}
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
template <typename T>
bool operator> (const Vector<T, 2>& parLhs, const Line<T, 2>& parRhs) {
const Vector<T, 2> line(parRhs.End() - parRhs.Start());
const Vector<T, 2> linePerp(line.y(), -line.x());
const Vector<T, 2> pt(parLhs - parRhs.Start());
const T dotproduct = dot(linePerp, pt);
return (dotproduct > T(0));
}
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
template <typename T>
bool operator< (const Vector<T, 2>& parLhs, const Line<T, 2>& parRhs) {
const Vector<T, 2> line(parRhs.End() - parRhs.Start());
const Vector<T, 2> linePerp(line.y(), -line.x());
const Vector<T, 2> pt(parLhs - parRhs.Start());
const T dotproduct = dot(linePerp, pt);
return (dotproduct < T(0));
}
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
template <typename T>
bool operator>= (const Vector<T, 2>& parLhs, const Line<T, 2>& parRhs) {
const Vector<T, 2> line(parRhs.End() - parRhs.Start());
const Vector<T, 2> linePerp(line.y(), -line.x());
const Vector<T, 2> pt(parLhs - parRhs.Start());
const T dotproduct = dot(linePerp, pt);
return (dotproduct >= T(0));
}
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
template <typename T>
bool operator<= (const Vector<T, 2>& parLhs, const Line<T, 2>& parRhs) {
const Vector<T, 2> line(parRhs.End() - parRhs.Start());
const Vector<T, 2> linePerp(line.y(), -line.x());
const Vector<T, 2> pt(parLhs - parRhs.Start());
const T dotproduct = dot(linePerp, pt);
return (dotproduct <= T(0));
}
} //namespace cloonel

37
src/line_helpers.hpp Normal file
View file

@ -0,0 +1,37 @@
/*
Copyright 2014 Michele "King_DuckZ" Santullo
This file is part of CloonelJump.
CloonelJump 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 3 of the License, or
(at your option) any later version.
CloonelJump is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with CloonelJump. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef idDF0B3D1FA4714EF3AB1241C3DA0D4E3D
#define idDF0B3D1FA4714EF3AB1241C3DA0D4E3D
#include "vector.hpp"
#include "line.hpp"
#include <cmath>
namespace cloonel {
template <typename T, uint32_t S>
T len ( const Line<T, S>& parLine ) __attribute__((pure));
template <typename T, uint32_t S>
T len_sq ( const Line<T, S>& parLine ) __attribute__((pure));
} //namespace cloonel
#include "line_helpers.inl"
#endif

42
src/line_helpers.inl Normal file
View file

@ -0,0 +1,42 @@
/*
Copyright 2014 Michele "King_DuckZ" Santullo
This file is part of CloonelJump.
CloonelJump 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 3 of the License, or
(at your option) any later version.
CloonelJump is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with CloonelJump. If not, see <http://www.gnu.org/licenses/>.
*/
namespace cloonel {
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
template <typename T, uint32_t S>
T len (const Line<T, S>& parLine) {
return std::sqrt(len_sq(parLine));
}
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
template <typename T, uint32_t S>
T len_sq (const Line<T, S>& parLine) {
T res(0);
const typename Line<T, S>::Point& start = parLine.Start();
const typename Line<T, S>::Point& end = parLine.End();
for (uint32_t z = 0; z < S; ++z) {
const T diff(end[z] - start[z]);
res += diff * diff;
}
return res;
}
} //namespace cloonel

20
src/vectormath.cpp Normal file
View file

@ -0,0 +1,20 @@
/*
Copyright 2014 Michele "King_DuckZ" Santullo
This file is part of CloonelJump.
CloonelJump 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 3 of the License, or
(at your option) any later version.
CloonelJump is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with CloonelJump. If not, see <http://www.gnu.org/licenses/>.
*/
#include "vectormath.hpp"

43
src/vectormath.hpp Normal file
View file

@ -0,0 +1,43 @@
/*
Copyright 2014 Michele "King_DuckZ" Santullo
This file is part of CloonelJump.
CloonelJump 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 3 of the License, or
(at your option) any later version.
CloonelJump is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with CloonelJump. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef id5DB098DAA5534A0A869DEF23888F96B0
#define id5DB098DAA5534A0A869DEF23888F96B0
#include "vector.hpp"
#include <type_traits>
#include <cstdint>
#include <cmath>
namespace cloonel {
template <typename T, typename U, uint32_t S>
typename std::common_type<T, U>::type dot ( const Vector<T, S>& parA, const Vector<U, S>& parB ) __attribute__((pure));
template <typename T, typename U>
Vector<typename std::common_type<T, U>::type, 3> cross ( const Vector<T, 3>& parA, const Vector<U, 3>& parB ) __attribute__((pure));
template <typename T, uint32_t S>
T len ( const Vector<T, S>& parVector ) __attribute__((pure));
template <typename T, uint32_t S>
T len_sq ( const Vector<T, S>& parVector ) __attribute__((pure));
template <typename T, uint32_t S>
Vector<T, S> normalized ( const Vector<T, S>& parVector ) __attribute__((pure));
} //namespace cloonel
#include "vectormath.inl"
#endif

63
src/vectormath.inl Normal file
View file

@ -0,0 +1,63 @@
/*
Copyright 2014 Michele "King_DuckZ" Santullo
This file is part of CloonelJump.
CloonelJump 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 3 of the License, or
(at your option) any later version.
CloonelJump is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with CloonelJump. If not, see <http://www.gnu.org/licenses/>.
*/
namespace cloonel {
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
template <typename T, typename U, uint32_t S>
typename std::common_type<T, U>::type dot (const Vector<T, S>& parA, const Vector<U, S>& parB) {
typename std::common_type<T, U>::type retVal(0);
for (uint32_t z = 0; z < S; ++z) {
retVal += parA[z] * parB[z];
}
return retVal;
}
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
template <typename T, typename U>
Vector<typename std::common_type<T, U>::type, 3> cross (const Vector<T, 3>& parA, const Vector<U, 3>& parB) {
}
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
template <typename T, uint32_t S>
T len (const Vector<T, S>& parVector) {
return std::sqrt(len_sq<T, S>(parVector));
}
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
template <typename T, uint32_t S>
T len_sq (const Vector<T, S>& parVector) {
T retVal(0);
for (uint32_t z = 0; z < S; ++z) {
retVal += parVector[z] * parVector[z];
}
return retVal;
}
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
template <typename T, uint32_t S>
Vector<T, S> normalized (const Vector<T, S>& parVector) {
return parVector / len(parVector);
}
} //namespace cloonel