Skip to content

Conversation

@AlterEgoIV
Copy link

@AlterEgoIV AlterEgoIV commented Feb 6, 2024

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

@OneLoneCoder
Copy link
Owner

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?

@AlterEgoIV
Copy link
Author

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.

@ibraheem-emad
Copy link

hello there
i happen to have startred working on the issue #5 without noticing that someone already started working on it so pls link your pull request with the issue so nothing like this happen to someone else
and also i noticed that your polygon struct are lacking a constructor and some member functions that other shapes has (like area , middle point etc) and i happen to have already wrote some of them so i would like to share what i made with you and hopefully you find them useful and consider adding it to your pull request
thanks for your efforts and good luck

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;
	}
};

@AlterEgoIV
Copy link
Author

AlterEgoIV commented Feb 20, 2024

hello there i happen to have startred working on the issue #5 without noticing that someone already started working on it so pls link your pull request with the issue so nothing like this happen to someone else and also i noticed that your polygon struct are lacking a constructor and some member functions that other shapes has (like area , middle point etc) and i happen to have already wrote some of them so i would like to share what i made with you and hopefully you find them useful and consider adding it to your pull request thanks for your efforts and good luck

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 std::vector<olc::v_2d<T>> pos; in the polygon struct additions made to the type shouldn't come into conflict with additions proposed by this pull request.

@ibraheem-emad
Copy link

ibraheem-emad commented Feb 20, 2024

hello there i happen to have startred working on the issue #5 without noticing that someone already started working on it so pls link your pull request with the issue so nothing like this happen to someone else and also i noticed that your polygon struct are lacking a constructor and some member functions that other shapes has (like area , middle point etc) and i happen to have already wrote some of them so i would like to share what i made with you and hopefully you find them useful and consider adding it to your pull request thanks for your efforts and good luck

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 std::vector<olc::v_2d<T>> pos; in the polygon struct additions made to the type shouldn't come into conflict with additions proposed by this pull request.

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?
I marked the perimeter function as (TO DO) because i didn't know the answer of that question

@AlterEgoIV
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants