Click here to Skip to main content
15,880,503 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Consider the following code:
#include <string>
class Intersect {
public:
static bool overlap(std::string polygonA, std::string polygonB);
};
bool Intersect::overlap(std::string polygonA, std::string polygonB) {
// implementation goes here
return true;
}

Implement the method Intersect::overlap, so that it returns true, if an overlap exists with the two convex polygons polygonA and polygonB provided as input and it returns false if no such overlap exists. It should work with non convex polygons also.

everything should be implemented inside a static method. Check if points are in clockwise are not.

ex:
input format: std::string polygonAlpha = "0 0,0 1,2 0,0 0";
std::string polygonBravo = "1 0,1 1,3 0,1 0";
std::string polygonCharlie = "2 2,2 3,4 2,2 2";

What I have tried:

std::vector<int> vect;
    std::vector<int> vect1;
    
    std::stringstream ss(polygonA);
	std::stringstream ss1(polygonB);

    for (int i; ss >> i;) {
        vect.push_back(i);    
        if (ss.peek() == ',')
            ss.ignore();
    }
	for (int i; ss1 >> i;) {
        vect1.push_back(i);    
        if (ss.peek() == ',')
            ss.ignore();
    }
	// if signed area of a polygon is negative then it's clockwise oriented if it's positive counter clockwise oriented
	int signedarea=0;
   
    signedarea=signedarea+ (vect[0]*vect[3]-vect[1]*vect[2])+vect[2]*vect[5]-vect[3]*vect[4]+vect[4]*vect[7]-vect[5]*vect[6];
    std::cout << signedarea << std::endl;
Posted
Updated 16-Jul-20 21:42pm
Comments
Richard MacCutchan 16-Jul-20 5:05am    
What is the question?

You better implement a polygon class which takes the string as input. In the constructor or better parse function use must convert the string numbers into numbers with some function as atoi because math with strings is nonsense . This is also the best point to check the values for some conditions.

Write such class has the side advantage that you can write test code to verify the correctness.

Good luck with your homework. And take the time to solve your task at such advanced level. It is worth it.
 
Share this answer
 
Just looking at your code, there are two issues:

1. In your second for loop, you are referencing ss (twice), where you should use ss1 instead

2. While you did use loops to interpret the string of coordinates, you did not use loops to calculate the signed area. You should change that into loops as well, otherwise your program will not work if you have less or more than 4 points in your polygons.

Other than that, you are missing code for the intersection. For convex polygons you could simply check whether any corner of one polygon happens to lie within the other. But for concave polygons that won't work in general. Here you need to write a function that checks whether two line segments intersect, and then you need to check intersections for all sides of the two polygons. (n*m checks if your polygons have n and m sides, respectively)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900