2023-10-20 09:59:36 +00:00
|
|
|
///
|
|
|
|
/// \file include/scmp/geom/Coord2D.h
|
|
|
|
/// \author K. Isom <kyle@imap.cc>
|
|
|
|
/// \date 2017-06-05
|
|
|
|
/// \brief 2D point and polar coordinate systems.
|
|
|
|
///
|
2023-10-22 00:40:01 +00:00
|
|
|
/// \section COPYRIGHT
|
|
|
|
///
|
2023-10-20 09:59:36 +00:00
|
|
|
/// Copyright 2017 K. Isom <kyle@imap.cc>
|
|
|
|
///
|
|
|
|
/// Permission to use, copy, modify, and/or distribute this software for
|
|
|
|
/// any purpose with or without fee is hereby granted, provided that
|
|
|
|
/// the above copyright notice and this permission notice appear in all /// copies.
|
|
|
|
///
|
|
|
|
/// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
|
|
|
|
/// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
|
|
|
|
/// WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
|
|
|
|
/// AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
|
|
|
|
/// DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
|
|
|
|
/// OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
|
|
|
/// TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
|
|
/// PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
///
|
|
|
|
|
2023-10-19 06:44:05 +00:00
|
|
|
#ifndef SCMATH_GEOM_COORD2D_H
|
|
|
|
#define SCMATH_GEOM_COORD2D_H
|
|
|
|
|
2023-10-20 09:59:36 +00:00
|
|
|
|
2023-10-19 06:44:05 +00:00
|
|
|
#include <cmath>
|
|
|
|
#include <ostream>
|
|
|
|
#include <vector>
|
|
|
|
|
2023-10-21 02:05:55 +00:00
|
|
|
#include <scmp/geom/Vector.h>
|
|
|
|
|
2023-10-19 06:44:05 +00:00
|
|
|
|
2023-10-19 06:57:50 +00:00
|
|
|
namespace scmp {
|
2023-10-19 06:44:05 +00:00
|
|
|
namespace geom {
|
|
|
|
|
|
|
|
|
|
|
|
class Point2D;
|
2023-10-20 09:59:36 +00:00
|
|
|
|
2023-10-19 06:44:05 +00:00
|
|
|
class Polar2D;
|
|
|
|
|
2023-10-21 04:17:18 +00:00
|
|
|
/// \brief Point2D is a cartesian (X,Y) pairing.
|
2023-10-21 02:05:55 +00:00
|
|
|
class Point2D : public Vector<int, 2> {
|
2023-10-20 09:59:36 +00:00
|
|
|
public:
|
2023-10-21 02:05:55 +00:00
|
|
|
/// \brief A Point2D defaults to (0,0).
|
2023-10-20 09:59:36 +00:00
|
|
|
Point2D();
|
2023-10-21 02:05:55 +00:00
|
|
|
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \brief Initialize a Point2D At (_x, _y).
|
2023-10-20 09:59:36 +00:00
|
|
|
Point2D(int _x, int _y);
|
2023-10-19 06:44:05 +00:00
|
|
|
|
2023-10-21 02:05:55 +00:00
|
|
|
/// \brief Initialize a Point2D from a Polar2D coordinate.
|
|
|
|
Point2D(const Polar2D &pol);
|
|
|
|
|
|
|
|
/// \brief Return the X component of the point.
|
|
|
|
int X() const;
|
|
|
|
|
|
|
|
/// \brief Set the X component of the point.
|
|
|
|
void X(int _x);
|
|
|
|
|
|
|
|
/// \brief Return the Y component of the point.
|
|
|
|
int Y() const;
|
|
|
|
|
|
|
|
/// Set the Y component of the point.
|
|
|
|
void Y(int _y);
|
|
|
|
|
|
|
|
/// \brief ToString returns a string in the format (x,y).
|
2023-10-20 09:59:36 +00:00
|
|
|
std::string ToString();
|
2023-10-19 06:44:05 +00:00
|
|
|
|
2023-10-21 02:05:55 +00:00
|
|
|
/// \brief ToPolar converts the Point2D to a polar coordinate
|
|
|
|
/// in-place.
|
|
|
|
void ToPolar(Polar2D &);
|
2023-10-19 06:44:05 +00:00
|
|
|
|
2023-10-21 02:05:55 +00:00
|
|
|
/// \brief Rotate rotates the point by theta radians.
|
|
|
|
///
|
|
|
|
/// \param rotated Stores the rotated point.
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \param theta The Angle (in radians) to Rotate the point.
|
2023-10-21 02:05:55 +00:00
|
|
|
void Rotate(Point2D& rotated, double theta);
|
|
|
|
|
|
|
|
/// \brief Rotate this point around a series of vertices.
|
|
|
|
///
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \param vertices A series of vertices to Rotate this point around.
|
|
|
|
/// \param theta The Angle to Rotate by.
|
2023-10-21 02:05:55 +00:00
|
|
|
/// \return A series of rotated points.
|
|
|
|
std::vector<Point2D> Rotate(std::vector<Polar2D> vertices, double theta);
|
|
|
|
|
|
|
|
/// \brief Translate adds this point to the first argument,
|
|
|
|
/// storing the result in the second argument.
|
|
|
|
///
|
|
|
|
/// \param other The point to translate by.
|
|
|
|
/// \param translated The point to store the translation in.
|
2023-10-20 09:59:36 +00:00
|
|
|
void Translate(const Point2D &other, Point2D &translated);
|
2023-10-19 06:44:05 +00:00
|
|
|
|
2023-10-21 02:05:55 +00:00
|
|
|
/// \brief Distance returns the distance from this point to another.
|
|
|
|
int Distance(const Point2D &other) const;
|
|
|
|
|
2023-10-20 09:59:36 +00:00
|
|
|
friend std::ostream &operator<<(std::ostream &outs, const Point2D &pt);
|
2023-10-19 06:44:05 +00:00
|
|
|
};
|
|
|
|
|
2023-10-21 04:17:18 +00:00
|
|
|
/// \brief Polar2D is a pairing of a radius r and angle θ from some
|
|
|
|
/// reference point; in this library, it is assumed to be the
|
|
|
|
/// Cartesian origin (0, 0).
|
2023-10-21 02:05:55 +00:00
|
|
|
class Polar2D : public Vector<double, 2> {
|
2023-10-20 09:59:36 +00:00
|
|
|
public:
|
2023-10-21 04:17:18 +00:00
|
|
|
/// A Polar2D can be initialised as a zeroised polar coordinate, by specifying
|
|
|
|
/// the radius and Angle directly, or via conversion from a Point2D.
|
|
|
|
|
|
|
|
/// \brief Construct a zero polar coordinate.
|
2023-10-21 02:05:55 +00:00
|
|
|
Polar2D();
|
2023-10-21 04:17:18 +00:00
|
|
|
|
|
|
|
/// \brief Construct a polar coordinate from a radius and
|
|
|
|
/// angle.
|
|
|
|
///
|
|
|
|
/// \param _r A radius
|
|
|
|
/// \param _theta An angle
|
2023-10-21 02:05:55 +00:00
|
|
|
Polar2D(double _r, double _theta);
|
2023-10-19 06:44:05 +00:00
|
|
|
|
2023-10-21 04:17:18 +00:00
|
|
|
/// \brief Construct a polar coordinate from a point.
|
|
|
|
///
|
|
|
|
/// This construct uses the origin (0,0) as the reference point.
|
|
|
|
///
|
|
|
|
/// \param point A 2D Cartesian point.
|
|
|
|
Polar2D(const Point2D& point);
|
|
|
|
|
|
|
|
/// \brief Return the radius component of this coordinate.
|
2023-10-21 02:05:55 +00:00
|
|
|
double R() const;
|
2023-10-21 04:17:18 +00:00
|
|
|
|
|
|
|
/// \brief Set the radius component of this coordinate.
|
2023-10-21 02:05:55 +00:00
|
|
|
void R(const double _r);
|
|
|
|
|
2023-10-21 04:17:18 +00:00
|
|
|
/// \brief Return the angle component of this coordinate.
|
2023-10-21 02:05:55 +00:00
|
|
|
double Theta() const;
|
2023-10-21 04:17:18 +00:00
|
|
|
|
|
|
|
/// \brief Set the angle component of this coordinate.
|
2023-10-21 02:05:55 +00:00
|
|
|
void Theta(const double _theta);
|
|
|
|
|
2023-10-21 04:17:18 +00:00
|
|
|
/// \brief Return the coordinate in string form.
|
2023-10-19 06:44:05 +00:00
|
|
|
std::string ToString();
|
|
|
|
|
2023-10-21 04:17:18 +00:00
|
|
|
/// \brief Construct a Point2D representing this Polar2D.
|
|
|
|
void ToPoint(Point2D &point);
|
2023-10-19 06:44:05 +00:00
|
|
|
|
2023-10-21 04:17:18 +00:00
|
|
|
/// \brief Rotate polar coordinate by some angle.
|
|
|
|
///
|
|
|
|
/// \param rotated The rotated Polar2D will be stored in this
|
|
|
|
/// coordinate.
|
|
|
|
/// \param delta The angle to rotate by.
|
|
|
|
void Rotate(Polar2D &rotated, double delta);
|
|
|
|
|
|
|
|
/// \brief Rotate this polar coordinate around a 2D point.
|
|
|
|
///
|
|
|
|
/// \param other The reference point.
|
|
|
|
/// \param result The point where the result will stored.
|
|
|
|
/// \param delta The angle to rotate by.
|
|
|
|
void RotateAround(const Point2D &other, Point2D &result, double delta);
|
2023-10-19 06:44:05 +00:00
|
|
|
|
2023-10-20 09:59:36 +00:00
|
|
|
friend std::ostream &operator<<(std::ostream &, const Polar2D &);
|
2023-10-19 06:44:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
} // end namespace geom
|
|
|
|
} // end namespace math
|
|
|
|
#endif
|