From 9b83919815f6a6ce5d73da1c28483970d0ca5589 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 3 Feb 2021 14:22:28 -0600 Subject: added dev/MinGfx/ --- dev/MinGfx/src/point2.cc | 104 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 dev/MinGfx/src/point2.cc (limited to 'dev/MinGfx/src/point2.cc') diff --git a/dev/MinGfx/src/point2.cc b/dev/MinGfx/src/point2.cc new file mode 100644 index 0000000..9d98777 --- /dev/null +++ b/dev/MinGfx/src/point2.cc @@ -0,0 +1,104 @@ +/* + Copyright (c) 2017,2018 Regents of the University of Minnesota. + All Rights Reserved. + See corresponding header file for details. + */ + +#include "point2.h" + +#include + +namespace mingfx { + +static const Point2 s_zerop2d = Point2(0,0); +static const Point2 s_onep2d = Point2(1,1); + +const Point2& Point2::Origin() { return s_zerop2d; } +const Point2& Point2::Zero() { return s_zerop2d; } +const Point2& Point2::One() { return s_onep2d; } + + +Point2::Point2() { + p[0] = 0.0; + p[1] = 0.0; +} + +Point2::Point2(float x, float y) { + p[0] = x; + p[1] = y; +} + +Point2::Point2(float *ptr) { + p[0] = ptr[0]; + p[1] = ptr[1]; +} + +Point2::Point2(const Point2& other) { + p[0] = other[0]; + p[1] = other[1]; +} + +Point2::~Point2() { +} + +bool Point2::operator==(const Point2& other) const { + return (fabs(other[0] - p[0]) < MINGFX_MATH_EPSILON && + fabs(other[1] - p[1]) < MINGFX_MATH_EPSILON); +} + +bool Point2::operator!=(const Point2& other) const { + return (fabs(other[0] - p[0]) >= MINGFX_MATH_EPSILON || + fabs(other[1] - p[1]) >= MINGFX_MATH_EPSILON); +} + +Point2& Point2::operator=(const Point2& other) { + p[0] = other[0]; + p[1] = other[1]; + return *this; +} + +float Point2::operator[](const int i) const { + if ((i>=0) && (i<=1)) { + return p[i]; + } + else { + // w component of a point is 1 so return the constant 1.0 + return 1.0; + } +} + +float& Point2::operator[](const int i) { + return p[i]; +} + + +Point2 Point2::Lerp(const Point2 &b, float alpha) const { + float x = (1.0f-alpha)*(*this)[0] + alpha*b[0]; + float y = (1.0f-alpha)*(*this)[1] + alpha*b[1]; + return Point2(x,y); +} + +Point2 Point2::Lerp(const Point2 &a, const Point2 &b, float alpha) { + float x = (1.0f-alpha)*a[0] + alpha*b[0]; + float y = (1.0f-alpha)*a[1] + alpha*b[1]; + return Point2(x,y); +} + + +const float * Point2::value_ptr() const { + return p; +} + + +std::ostream & operator<< ( std::ostream &os, const Point2 &p) { + return os << "(" << p[0] << ", " << p[1] << ")"; +} + +std::istream & operator>> ( std::istream &is, Point2 &p) { + // format: (x, y) + char dummy; + return is >> dummy >> p[0] >> dummy >> p[1] >> dummy; +} + + +} // end namespace -- cgit v1.2.3