Last active
June 5, 2021 17:42
-
-
Save ThePyProgrammer/915d6a9fe00aedd7c675267b06c2cefe to your computer and use it in GitHub Desktop.
A Point class in CPP.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Created by ThePyProgrammer on 5/6/2021. | |
// | |
#include <iostream> | |
#include "Point.cpp" | |
// #include <cassert> | |
auto main() -> int { | |
auto o = *(new Point()); // (0, 0) | |
o.setX(-10); // (-10, 0) | |
o.setY(-5); // (-10, -5) | |
o.translateX(15); // (5, -5) | |
o.translateY(15); // (5, 10) | |
double magnitude = o.hypotenuse(); | |
auto p = *(new Point(1, 2)); // (1, 2) | |
p.scale(5); // (5, 10) | |
double angle = p.angle(); | |
double x = magnitude * std::cos(angle); // should output 5 | |
double y = magnitude * std::sin(angle); // should output 10 | |
// assert(x * y == 50.0); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Created by ThePyProgrammer on 5/6/2021. | |
// | |
#include <cmath> | |
#include "Point.h" | |
#include <limits> | |
Point::Point(double x, double y) { | |
set(x, y); | |
} | |
Point::Point(double x) { | |
setX(x); | |
} | |
auto Point::getX() const -> double { | |
return x; | |
} | |
auto Point::getY() const -> double { | |
return y; | |
} | |
auto Point::set(double xNew, double yNew) -> void { | |
setX(xNew); setY(yNew); | |
} | |
auto Point::setX(double xNew) -> void { | |
this->x = xNew; | |
} | |
auto Point::setY(double yNew) -> void { | |
this->y = yNew; | |
} | |
/** | |
* @return angle from origin | |
*/ | |
auto Point::angle() const -> double { | |
return std::atan(y / x); | |
} | |
/** | |
* @return distance from origin | |
*/ | |
auto Point::hypotenuse() const -> double { | |
return std::sqrt( std::pow(x, 2) + std::pow(y, 2) ); | |
} | |
/** | |
* @param o: The new origin | |
* @return The object pointer of reset origin | |
*/ | |
auto Point::resetOrigin(Point o) const -> Point * { | |
auto p = copy(); | |
p->translate(-o.x, -o.y); | |
return p; | |
} | |
/** | |
* @param p The point to reset the origin to | |
* @return The hypotenuse of the reset origin | |
*/ | |
auto Point::hypotenuseFrom(Point p) const -> double { | |
return resetOrigin(p)->hypotenuse(); | |
} | |
/** | |
* @param dx Change in x | |
* @param dy Change in y | |
*/ | |
auto Point::translate(double dx, double dy) -> void { | |
translateX(dx); translateY(dy); | |
} | |
/** | |
* @param dx Change in x | |
*/ | |
auto Point::translateX(double dx) -> void { | |
x += dx; | |
} | |
/** | |
* @param dy Change in y | |
*/ | |
auto Point::translateY(double dy) -> void { | |
x += dy; | |
} | |
/** | |
* @param factorX Scale Factor for x-axis | |
* @param factorY Scale Factor for y-axis | |
*/ | |
auto Point::scale(double factorX, double factorY) -> void { | |
scaleX(factorX); scaleY(factorY); | |
} | |
/** | |
* @param factor Scale Factor for x-axis and y-axis | |
*/ | |
auto Point::scale(double factor) -> void { | |
scale(factor, factor); | |
} | |
/** | |
* @param factorX Scale Factor for x-axis | |
*/ | |
auto Point::scaleX(double factor) -> void { | |
x *= factor; | |
} | |
/** | |
* @param factorY Scale Factor for y-axis | |
*/ | |
auto Point::scaleY(double factor) -> void { | |
y *= factor; | |
} | |
auto Point::reflectX() -> void { | |
scaleX(-1.0); | |
} | |
auto Point::reflectY() -> void { | |
scaleY(-1.0); | |
} | |
auto Point::reflect() -> void { | |
scale(-1); | |
} | |
auto Point::absX() -> void { | |
if(x < 0) reflectX(); | |
} | |
auto Point::absY() -> void { | |
if(y < 0) reflectY(); | |
} | |
auto Point::abs() -> void { | |
absX(); absY(); | |
} | |
auto Point::inverseX() -> void { | |
auto inf = std::numeric_limits<double>::infinity(); | |
if(std::abs(x) == inf) x = 0; | |
else if(x == 0.0) x = inf; | |
else x = 1 / x; | |
} | |
auto Point::inverseY() -> void { | |
auto inf = std::numeric_limits<double>::infinity(); | |
if(std::abs(y) == inf) y = 0; | |
else if(y == 0.0) y = inf; | |
else y = 1 / y; | |
} | |
auto Point::inverse() -> void { | |
inverseX(); inverseY(); | |
} | |
auto Point::copy() const -> Point * { | |
return new Point(x, y); | |
} | |
auto Point::clone() const -> Point * { | |
return copy(); | |
} | |
auto Point::compareTo(Point p) const -> int { | |
if(x != p.x) return (int) (x - p.x); | |
else if(x != p.y ) return (int) (y - p.y); | |
else return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Created by ThePyProgrammer on 5/6/2021. | |
// | |
#include <cmath> | |
#ifndef APP_POINT_H | |
#define APP_POINT_H | |
class Point { | |
private: | |
double x = 0, y = 0; | |
public: | |
// Constructors | |
Point() = default; | |
Point(double x, double y); | |
explicit Point(double x); | |
// Getters and Setters | |
auto getX() const -> double; | |
auto getY() const -> double; | |
auto set(double xNew, double yNew) -> void; | |
auto setX(double xNew) -> void; | |
auto setY(double yNew) -> void; | |
// Mathematical Values | |
auto angle() const -> double; | |
auto hypotenuse() const -> double; | |
// Mathematical Operations | |
auto resetOrigin(Point o) const -> Point *; | |
auto hypotenuseFrom(Point o) const -> double; | |
// Translation | |
auto translate(double dx, double dy) -> void; | |
auto translateX(double dx) -> void; | |
auto translateY(double dy) -> void; | |
// Scaling | |
auto scale(double factorX, double factorY) -> void; | |
auto scale(double factor) -> void; | |
auto scaleX(double factor) -> void; | |
auto scaleY(double factor) -> void; | |
// Reflection | |
auto reflectX() -> void; | |
auto reflectY() -> void; | |
auto reflect() -> void; | |
// Modulus of Point | |
auto absX() -> void; | |
auto absY() -> void; | |
auto abs() -> void; | |
// Inverse of Point | |
auto inverseX() -> void; | |
auto inverseY() -> void; | |
auto inverse() -> void; | |
// Class Operations | |
auto copy() const -> Point *; | |
auto clone() const -> Point *; | |
auto compareTo(Point p) const -> int; | |
}; | |
#endif //APP_POINT_H |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment