This commit is contained in:
TinyAtoms
2020-04-17 13:44:10 -03:00
parent e1979c3da9
commit 1ba6d33557
8 changed files with 14 additions and 271 deletions

58
src/Point.h Normal file
View File

@@ -0,0 +1,58 @@
#ifndef POINT
#define POINT
class Vector {
public:
int x_;
int y_;
explicit Vector(int x, int y) : x_{x}, y_{y} {};
Vector() = default;
};
Vector operator*(Vector lhs, const int rhs) {
return Vector{lhs.x_ * rhs, lhs.y_ * rhs};
}
Vector operator*(int lhs, const Vector rhs) {
return Vector{rhs.x_ * lhs, rhs.y_ * lhs};
}
Vector operator+(Vector lhs, const Vector &rhs) {
return Vector{lhs.x_ + rhs.x_, lhs.y_ + rhs.y_};
}
Vector operator-(Vector lhs, const Vector &rhs) { return lhs + -1 * rhs; }
class Point {
public:
int x;
int y;
Point(int xx, int yy) : x{xx}, y{yy} {};
};
Point operator+(Vector lhs, Point rhs) {
return Point{rhs.x + lhs.x_, rhs.x + lhs.x_};
}
Point operator+(Point lhs, Vector rhs) {
return Point{rhs.x_ + lhs.x, rhs.x_ + lhs.x};
}
Point operator-(Vector lhs, Point rhs) {
return Point{rhs.x - lhs.x_, rhs.x - lhs.x_};
}
Point operator-(Point lhs, Vector rhs) {
return Point{rhs.x_ - lhs.x, rhs.x_ - lhs.x};
}
Vector operator-(Point lhs, Point rhs){
return Vector{lhs.x - rhs.x, lhs.y - rhs.y};
}
#endif /* GENERATOR_H */

6
src/main.cpp Normal file
View File

@@ -0,0 +1,6 @@
#include "shapes.h"
int main(){
}

15
src/shapes.h Normal file
View File

@@ -0,0 +1,15 @@
#include "Point.h"
class Rect {
public:
Point tl;
Point br;
Rect(Point tl_, Point br_): tl{tl_}, br{br_}{};
void move(Vector dist){
tl = tl + dist;
br = br + dist;
};
void scale(int scalar){
br = tl + (br - tl) * scalar;
}
};

126
src/test.cpp Normal file
View File

@@ -0,0 +1,126 @@
#define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do this
// in one cpp file
#include "Point.h"
#include "../external/catch2.hpp"
#include "shapes.h"
TEST_CASE("Points are created correctly", "[POINT]") {
SECTION("constructor") {
Point test{5, 6};
REQUIRE(test.x == 5);
REQUIRE(test.y == 6);
}
}
TEST_CASE("Point operations", "[POINT]") {
SECTION("addition") {
Point test{5, 6};
Vector translation {1,1};
test = test + translation;
REQUIRE(test.x == 6);
REQUIRE(test.y == 7);
test = translation + test;
REQUIRE(test.x == 7);
REQUIRE(test.y == 8);
}
SECTION("subtraction") {
Point test{5, 6};
Vector translation {1,1};
test = test - translation;
REQUIRE(test.x == 4);
REQUIRE(test.y == 5);
test = translation - test;
REQUIRE(test.x == 3);
REQUIRE(test.y == 4);
}
}
TEST_CASE("Vectors are created correctly", "[VECTOR]") {
SECTION("constructor") {
Vector test{5, 6};
REQUIRE(test.x_ == 5);
REQUIRE(test.y_ == 6);
}
}
TEST_CASE("Vector operations work correctly", "[VECTOR]") {
SECTION("multiply") {
Vector test{5, 6};
const int scalar = 2;
test = test * scalar;
REQUIRE(test.x_ == 10);
REQUIRE(test.y_ == 12);
test = scalar * test;
REQUIRE(test.x_ == 20);
REQUIRE(test.y_ == 24);
}
SECTION("addition") {
Vector test{5, 6};
Vector test2{1,1};
test = test + test2;
REQUIRE(test.x_ == 6);
REQUIRE(test.y_ == 7);
}
SECTION("subtraction") {
Vector test{5, 6};
Vector test2{1,1};
test = test - test2;
REQUIRE(test.x_ == 4);
REQUIRE(test.y_ == 5);
}
}
TEST_CASE("Vectors are created correctly from points", "[VECTOR][POINT]") {
SECTION("constructor") {
Point a {5,6};
Point b {2,5};
auto test {a-b};
REQUIRE(test.x_ == 3);
REQUIRE(test.y_ == 1);
}
}
TEST_CASE("rects are created correctly from points", "[RECTANGLE]") {
SECTION("constructor") {
Point a {5,6};
Point b {2,5};
Rect test {a,b};
REQUIRE(test.br.x == b.x);
REQUIRE(test.br.y == b.y);
REQUIRE(test.tl.x == a.x);
REQUIRE(test.tl.y == a.y);
}
}
TEST_CASE("rect operations", "[RECTANGLE]") {
SECTION("MOVE") {
Point a {5,6};
Point b {2,5};
Rect test {a,b};
const int scale = 2;
Vector move {1,1};
test.move(move);
REQUIRE(test.br.x == b.x + 1);
REQUIRE(test.br.y == b.y + 1);
REQUIRE(test.tl.x == a.x+1);
REQUIRE(test.tl.y == a.y+1);
}
SECTION("SCALE") {
Point a {5,6};
Point b {2,5};
Rect test {a,b};
const int scale = 2;
Vector move = (b-a) * 2;
Point newpoint = a + move;
test.move(move);
REQUIRE(test.br.x == newpoint.x);
REQUIRE(test.br.y == newpoint.y);
REQUIRE(test.tl.x == a.x);
REQUIRE(test.tl.y == a.y);
}
}