Question: (C++) Help me fix my function to output the correct true or false statement in the function findParcelName: This function is supposed to tell the

(C++) Help me fix my function to output the correct true or false statement in the function findParcelName:

This function is supposed to tell the program which polygon the specific coordinates (x,y) are contained in. If the coordinates are not inside any of the polygons in the text file then it should output Not Found.

I just need to pass the last section of the test case and I am done. I would really appreciate the help!

Test case:

TEST_CASE("Testing container (GIS) operations") { GIS taxMap; SECTION("Check file availability") { REQUIRE(!taxMap.readFile("zzzzz.zzzz")); //WORKS REQUIRE(taxMap.readFile("simple-polygons.txt")); //WORKS } SECTION("Check point that isn't inside any of the polygons") { taxMap.readFile("simple-polygons.txt"); //WORKS CHECK("Not Found" == taxMap.findParcelName(Point(-1, -1))); //WORKS } SECTION("Check for point inside polygon") { //NEED HELP WITH THIS SECTION taxMap.readFile("simple-polygons.txt"); CHECK("squareParcel" == taxMap.findParcelName(Point(1, 1))); //DOES NOT WORK CHECK("wideRectangleParcel" == taxMap.findParcelName(Point(5, 4))); //DOES NOT WORK } } GIS class:

class GIS{ public: GIS(){ } ~GIS(){ cout<<"memory release"<

private: string fileName; };

bool GIS::readFile(string fileName){ ifstream file; file.open(fileName); //checks if file exist if(file){ this->fileName = fileName; file.close(); return true; } return false; }

string GIS::findParcelName(Point point){ ifstream file(fileName); // open the file specified in the parameter Polygon poly; while (file >> poly) { // read polygons from the file if (poly.contains(point)) { file.close(); return poly.GetName(); //Return name of polygon if point exist within it } } file.close(); return "Not Found"; // return "Not Found" if the point is not inside any polygon } Polygon class: This has the contain function

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(){ //delete [] points; cout<<"Memory released"<

Polygon& operator=(const Polygon &other); friend istream& operator>>(istream& in, Polygon& p); 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; for (int i = 0; i < 10; i++) { points[i] = Point(); } }

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

istream& operator>>(istream& in, Polygon& p){ string name; int vertexCount; in >> name >> vertexCount; p.name = name; p.vertexCount = vertexCount; for(int i = 0; i < vertexCount; i++){ in >> p.points[i]; } return in; }

bool Polygon::contains(Point point){ int i; int j; bool x = false; for (i = 0, j = vertexCount-1; i < vertexCount; j = i++){ if (points[i].getX() == point.getX() && points[i].getY() == point.getY()) { return true; } 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())) { x = !x; } } return x; } simple-polygons.txt:

squareParcel 0 0 0 2 2 2 2 0 wideRectangleParcel 0 2 0 4 6 4 6 2

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!