libsegl/punkt2d.cpp

184 lines
3.6 KiB
C++
Raw Normal View History

2009-01-21 01:00:31 +01:00
/* libsegl - Sebas Extended GL Library
* Collection of Opengl/3D-Math helpers
*
* Copyright (c) 2008 by Sebastian Lohff, seba@seba-geek.de
* http://www.seba-geek.de
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "punkt2d.h"
namespace segl {
Punkt2D::Punkt2D() {
x = y = 0.0f;
}
Punkt2D::Punkt2D(float _x, float _y) {
set(_x, _y);
}
void Punkt2D::set(float _x, float _y) {
x = _x;
y = _y;
}
void Punkt2D::print(std::string coordname) {
if(coordname!="")
coordname.append(" ");
std::cout << coordname << "Coord: (" << x << ", " << y << ")" << std::endl;
}
float Punkt2D::abs() {
return sqrt(x*x + y*y);
}
2008-09-19 17:34:01 +02:00
void Punkt2D::normalize() {
float a = abs();
x /= a;
y /= a;
}
Punkt2D Punkt2D::getNormalized() const {
Punkt2D ret(*this);
ret.normalize();
return ret;
}
Punkt2D Punkt2D::operator+(const Punkt2D &b) {
Punkt2D c;
c.x = x + b.x;
c.y = y + b.y;
// c.z = z + b.z;
return c;
}
Punkt2D Punkt2D::operator-(const Punkt2D &b) {
Punkt2D c;
c.x = x - b.x;
c.y = y - b.y;
// c.z = z - b.z;
return c;
}
Punkt2D& Punkt2D::operator+=(const Punkt2D &b) {
x += b.x;
y += b.y;
// z += b.z;
return *this;
}
Punkt2D& Punkt2D::operator-=(const Punkt2D &b) {
x -= b.x;
y -= b.y;
// z -= b.z;
return *this;
}
Punkt2D Punkt2D::operator+(const float &_m) {
return Punkt2D(x+_m, y+_m);
}
Punkt2D Punkt2D::operator-(const float &_m) {
return Punkt2D(x-_m, y-_m);
}
Punkt2D Punkt2D::operator*(const float &_m) {
return Punkt2D(x*_m, y*_m);
}
Punkt2D Punkt2D::operator/(const float &_m) {
return Punkt2D(x/_m, y/_m);
}
Punkt2D& Punkt2D::operator+=(const float &_m) {
x += _m;
y += _m;
// z += _m;
return *this;
}
Punkt2D& Punkt2D::operator-=(const float &_m) {
x -= _m;
y -= _m;
// z -= _m;
return *this;
}
Punkt2D& Punkt2D::operator*=(const float &_m) {
x *= _m;
y *= _m;
// z *= _m;
return *this;
}
Punkt2D& Punkt2D::operator/=(const float &_m) {
x /= _m;
y /= _m;
// z /= _m;
return *this;
}
float Punkt2D::operator*(const Punkt2D& _m) {
return x * _m.x + y * _m.y;
}
Punkt2D Punkt2D::operator-() {
return Punkt2D(-x, -y);
}
bool Punkt2D::operator==(const Punkt2D& b) {
return ( x==b.x && y==b.y);
}
bool Punkt2D::operator!=(const Punkt2D& b) {
return !(*this==b);
}
// Freunde
segl::Punkt2D operator+(const float& _m, const segl::Punkt2D& b) {
return segl::Punkt2D(b.x+_m, b.y+_m);
}
segl::Punkt2D operator-(const float& _m, const segl::Punkt2D& b) {
return segl::Punkt2D(b.x-_m, b.y-_m);
}
segl::Punkt2D operator*(const float& _m, const segl::Punkt2D& b) {
return segl::Punkt2D(b.x*_m, b.y*_m);
}
segl::Punkt2D operator/(const float& _m, const segl::Punkt2D& b) {
return segl::Punkt2D(b.x/_m, b.y/_m);
}
} // namespace segl
void glTexCoord2f(segl::Punkt2D p) {
2008-04-11 15:57:56 +02:00
glTexCoord2f(p.x, p.y);
}
float abs(segl::Punkt2D p) {
return p.abs();
}
// Fixed Headers
void glTexCoordP2D(segl::Punkt2D p) {
glTexCoord2f(p.x, p.y);
}