-
Notifications
You must be signed in to change notification settings - Fork 53
Add polygon testing #62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
…t and reflect function stubs
|
This is a great contribution. I assume your polygon has a winding order in order to facilitate these operations? Could you elaborate on how a polygon should be constructed? |
|
Thanks Javid. Polygons should be convex and constructed with clockwise wound vertices in order to work with these implementations. For the purposes of demonstration I added a function "create_regular_convex_polygon()" that constructs and returns a regular convex polygon based on a specified number of points and an optional scale and translation transformation. |
|
hello there template<typename T>
struct polygon
{
std::vector<olc::v_2d<T>> points;
inline polygon(const std::vector<olc::v_2d<T>>& verts) : points(verts) {}
int vertices_num{ points.size() };
// returns the middle point of the polygon
inline olc::v_2d<T> middle() const
{
double x{ 0.0 };
double y{ 0.0 };
double sum{ 0.0 };
for (int i{ 0 }; i < vertices_num; i++)
{
sum += points[i].x;
}
x = sum / vertices_num;
sum = 0;
for (int i{ 0 }; i < vertices_num; i++)
{
sum += points[i].y;
}
y = sum / vertices_num;
olc::v_2d<T> result{ x , y };
return result;
}
// returns the area of the polygon
inline constexpr T area() const
{
//this func use the The Shoelace formula
double sumx{ 0.0 };
double sumy{ 0.0 };
double result{ 0.0 };
for(int i{ 0 }; i < vertices_num; i++)
{
if (i != vertices_num - 1)
{
sumx += points[i].x * points[i + 1].y;
}
else(sumx += points[i].x * points[0].y;)
}
for(int i{ 0 }; i < vertices_num; i++)
{
if (i != vertices_num - 1)
{
sumy += points[i].y * points[i + 1].x
}
else(sumy += points[i].y * points[0].x)
}
result = std::abs(sumx - sumy) / 2;
return result;
}
//TO DO
inline constexpr T perimeter() const
{
}
// Returns the number of sides
inline constexpr size_t side_count() const
{
return vertices_num;
}
}; |
Hello, from what I can see it looks like our contributions affect different aspects of the polygon type. My pull request is concerned with the addition of various polygon - shape interactions and uses the polygon type as it currently exists within the main repository olcUTIL_Geometry2D.h file. While there is a |
I see , well since you worked with the poly struct i have a question that will help me to complete my work before making a pull request for it, should i expect the 2d vectors in the std::vector to be in order or random? Like does the 2d_vec in index 0 connect with the 2d_vec in the index 1 and index 1 connect with index 2 and etc? |
|
The vertices of a polygon can be comprised of any set of points in space but they are ordered in the sense that adjacent vertices are connected and form edges. Typically vertices have either a clockwise or anti clockwise winding order but that shouldn't affect perimeter calculations. Self intersecting edges might be problematic but if polygons arranged in this configuration are beyond the scope of your intended provision it may be sufficient to advise of this with documentation. |
Add CMake generated files and directories to gitignore
Add (polygon, x), (x, polygon) overlaps, contains, intersects, closest and reflect function stubs
Add (polygon, point) contains function
Add (polygon, polygon) overlaps function
Add (polygon, point) overlaps function
Add Polygon type to ShapeWrap variant
Add draw_internal function for Polygon type
Add regular convex Polygons to ShapeWrap vector
Add (polygon, triangle) overlaps function
Add (triangle, polygon) overlaps function
Add (point, polygon) overlaps function
Add (polygon, rect) overlaps function
Add (rect, polygon) overlaps function
Add (polygon, polygon) contains function
Add (polygon, line) contains function
Add (polygon, rect) contains function
Add (polygon, triangle) contains function
Add (rect, polygon) contains function
Add (triangle, polygon) contains function
Refactor (polygon, point) contains function
Add (polygon, line) overlaps function
Add (line, polygon) overlaps function
Add (polygon, circle) overlaps function
Add (circle, polygon) overlaps function
Add (polygon, circle) contains function
Add (circle, polygon) contains function