Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
4b4ab65
Add CMake generated files and directories to gitignore
AlterEgoIV Feb 6, 2024
517db1d
Add (polygon, x), (x, polygon) overlaps, contains, intersects, closes…
AlterEgoIV Feb 6, 2024
28f3553
Add (polygon, point) contains function
AlterEgoIV Feb 6, 2024
f487f18
Add (polygon, polygon) overlaps function
AlterEgoIV Feb 6, 2024
804a2d3
Add (polygon, point) overlaps function
AlterEgoIV Feb 6, 2024
723ebc2
Add Polygon type to ShapeWrap variant
AlterEgoIV Feb 6, 2024
9a37908
Add draw_internal function for Polygon type
AlterEgoIV Feb 6, 2024
1fa1182
Add regular convex Polygons to ShapeWrap vector
AlterEgoIV Feb 6, 2024
210cb46
Add (polygon, triangle) overlaps function
AlterEgoIV Feb 6, 2024
438ab85
Add (triangle, polygon) overlaps function
AlterEgoIV Feb 6, 2024
c641ea8
Add (point, polygon) overlaps function
AlterEgoIV Feb 6, 2024
c441de4
Add (polygon, rect) overlaps function
AlterEgoIV Feb 6, 2024
08ff5d8
Add (rect, polygon) overlaps function
AlterEgoIV Feb 6, 2024
bb12a49
Add (polygon, polygon) contains function
AlterEgoIV Feb 6, 2024
b534e9b
Add (polygon, line) contains function
AlterEgoIV Feb 6, 2024
3eed4cb
Add (polygon, rect) contains function
AlterEgoIV Feb 6, 2024
d364c81
Add (polygon, triangle) contains function
AlterEgoIV Feb 6, 2024
a0d18ed
Add (rect, polygon) contains function
AlterEgoIV Feb 6, 2024
fb8d0b8
Add (triangle, polygon) contains function
AlterEgoIV Feb 6, 2024
c5f7e71
Refactor (polygon, point) contains function
AlterEgoIV Feb 6, 2024
5e9ec17
Add (polygon, line) overlaps function
AlterEgoIV Feb 6, 2024
3bf6cfa
Add (line, polygon) overlaps function
AlterEgoIV Feb 6, 2024
54d3e10
Add (polygon, circle) overlaps function
AlterEgoIV Feb 18, 2024
b3e122c
Add (circle, polygon) overlaps function
AlterEgoIV Feb 18, 2024
69d80ff
Add (polygon, circle) contains function
AlterEgoIV Feb 18, 2024
82066b4
Add (circle, polygon) contains function
AlterEgoIV Feb 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,19 @@
/Test_Geometry2D.vcxproj
/Test_Geometry2D.sln
build

#CMake generated files and directories
/ALL_BUILD.vcxproj
/ALL_BUILD.vcxproj.filters
/ALL_BUILD.vcxproj.user
/CMakeCache.txt
/Test_Geometry2D.vcxproj.user
/ZERO_CHECK.vcxproj
/ZERO_CHECK.vcxproj.filters
/ZERO_CHECK.vcxproj.user
/cmake_install.cmake
/olcUTIL_Geometry2D.sln

CMakeFiles/
TEST_Geometry2D.dir/
bin/
39 changes: 37 additions & 2 deletions TEST_Geometry2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,22 @@ class Test_Geometry2D : public olc::PixelGameEngine
olc::vf2d points[2]; // origin, direction
};

struct Polygon
{
std::vector<olc::vf2d> points; // vertices
};

// Create desired shapes using a sequence of points
static auto make_internal(const Point& p) { return p.points[0]; }
static auto make_internal(const Line& p) { return line<float>{ p.points[0], p.points[1] }; }
static auto make_internal(const Rect& p) { return rect<float>{ p.points[0], (p.points[1] - p.points[0]) }; }
static auto make_internal(const Circle& p) { return circle<float>{ p.points[0], (p.points[1]-p.points[0]).mag() }; }
static auto make_internal(const Triangle& p) { return triangle<float>{ p.points[0], p.points[1], p.points[2] }; }
static auto make_internal(const Ray& p) { return ray<float>{ p.points[0], (p.points[1]-p.points[0]).norm() }; }
static auto make_internal(const Polygon& p) { return polygon<float>{ p.points }; }

// The clever bit (and a bit new to me - jx9)
using ShapeWrap = std::variant<Point, Line, Rect, Circle, Triangle, Ray>;
using ShapeWrap = std::variant<Point, Line, Rect, Circle, Triangle, Ray, Polygon>;



Expand Down Expand Up @@ -216,6 +222,17 @@ class Test_Geometry2D : public olc::PixelGameEngine
DrawLine(t.origin, t.origin+t.direction * 1000.0f, col, 0xF0F0F0F0);
}

void draw_internal(const Polygon& x, const olc::Pixel col)
{
const auto t = make_internal(x);

for(uint32_t i = 0; i < t.pos.size(); ++i)
{
uint32_t next = (i + 1) % t.pos.size();
DrawLine(t.pos[i], t.pos[next], col);
}
}

void DrawShape(const ShapeWrap& shape, const olc::Pixel col = olc::WHITE)
{
std::visit([&](const auto& x)
Expand All @@ -238,14 +255,18 @@ class Test_Geometry2D : public olc::PixelGameEngine
vecShapes.push_back({ Line{ { { 80.0f, 10.0f }, {10.0f, 20.0f} } } });

vecShapes.push_back({ Rect{ { { 80.0f, 10.0f }, {110.0f, 60.0f} } } });
vecShapes.push_back({ Rect{ { { 80.0f, 10.0f }, {200.0f, 150.0f} } }});

vecShapes.push_back({ Circle{ { { 130.0f, 20.0f }, {170.0f, 20.0f} } } });
vecShapes.push_back({ Circle{ { { 330.0f, 300.0f }, {420.0f, 300.0f} } } });
vecShapes.push_back({ Circle{ { { 330.0f, 300.0f }, {400.0f, 300.0f} } } });

vecShapes.push_back({ Triangle{{ {50.0f, 100.0f}, {10.0f, 150.0f}, {90.0f, 150.0f}} }});
vecShapes.push_back({ Triangle{{ {50.0f, 100.0f}, {90.0f, 150.0f}, {10.0f, 150.0f}} }});
vecShapes.push_back({ Triangle{{ {350.0f, 200.0f}, {500.0f, 150.0f}, {450.0f, 400.0f}} }});

vecShapes.push_back({create_regular_convex_polygon(5, 50, {150, 150})});
vecShapes.push_back({create_regular_convex_polygon(8, 80, {300, 150})});

return true;
}

Expand Down Expand Up @@ -430,6 +451,20 @@ class Test_Geometry2D : public olc::PixelGameEngine

return true;
}

Polygon create_regular_convex_polygon(uint32_t point_count, float scale = 1, const olc::vf2d& translation = {})
{
Polygon polygon;
float angle = olc::utils::geom2d::pi * 2 / (float)point_count;

for(uint32_t i = 0; i < point_count; ++i)
{
olc::vf2d point = olc::vf2d{std::cos(angle * i), std::sin(angle * i)} * scale + translation;
polygon.points.push_back(point);
}

return polygon;
}
};

int main()
Expand Down
Loading