Question: I need help pass this test case C++: SECTION(Edge cases) { // at a vertex of the square CHECK(square.contains(Point(0, 2))); //FAILED THIS ONE // on
I need help pass this test case C++:
SECTION("Edge cases") {
// at a vertex of the square
CHECK(square.contains(Point(0, 2))); //FAILED THIS ONE
// on an edge of the square
CHECK(square.contains(Point(0, 1))); //PASSED THIS ONE
}
My Code:
class Polygon: public Point { public: Polygon();
Polygon(string n, Point points1[10], int vertexs);
Polygon(const Polygon &p1){ name = p1.name; vertexCount = p1.vertexCount; for(int i = 0; i < vertexCount; i++){ points[i] = p1.points[i]; } }
~Polygon(){ cout<<"Memory released"< Polygon& operator=(const Polygon &other); string GetName(); int getVertexCount(); Point* getVertex(int vertex); bool contains(Point point); private: string name; int vertexCount; Point points[10]; }; Polygon::Polygon(){ name = ""; vertexCount = 0; points[10] = {}; } Polygon::Polygon(string n, Point points1[], int vertexs){ name = n; vertexCount = vertexs; for(int i = 0; i < vertexCount; i++){ points[i] = points1[i]; } } string Polygon::GetName(){ return name; } Point* Polygon::getVertex(int vertex){ //return specific point from array using vertex as position return &points[vertex]; } int Polygon::getVertexCount(){ return vertexCount; } Polygon::Polygon(){ name = ""; vertexCount = 0; points[10] = {}; } Polygon::Polygon(string n, Point points1[], int vertexs){ name = n; vertexCount = vertexs; for(int i = 0; i < vertexCount; i++){ points[i] = points1[i]; } } string Polygon::GetName(){ return name; } Point* Polygon::getVertex(int vertex){ //return specific point from array using vertex as position return &points[vertex]; } int Polygon::getVertexCount(){ return vertexCount; } Polygon& Polygon::operator=(const Polygon &other){ name = other.name; vertexCount = other.vertexCount; for(int i = 0; i < vertexCount; i++) { points[i] = other.points[i]; } return *this; } bool Polygon::contains(Point point){ //NEED HELP WITH THIS FUNCTION TO PASS TEST int i,j,k = 0; for (i = 0, j = vertexCount-1; i < vertexCount; j = i++){ if (((points[i].getY()>point.getY()) != (points[j].getY()>point.getY())) && (point.getX() < (points[j].getX()-points[i].getX()) * (point.getY()-points[i].getY()) / (points[j].getY()-points[i].getY()) + points[i].getX()) ){ k = !k; } } return k; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
