Point* findFourthVertex(Point * a, Point *b ,Point *c ){ //http://math.stackexchange.com/questions/463867/find-the-corner-of-a-tetrahedron //https://answers.yahoo.com/question/index?qid=20091025020201AAYTQrJ //Want to find Perpendicular bisector plane for AB and AC //Find legnth of side. Vector * ab = subPointsNew(a,b); float side = magnitude(ab); free(ab); //Finding PBP for AB Point * centroid = findCentroid(a,b,c); Plane * pbp_ab = findPBP(a,b); Plane * pbp_ac = findPBP(a,c); //Finding plane intersection line Vector * dirVect = crossProduct((Point *)pbp_ab, (Point*) pbp_ac); //Calculate direction vector normalizeVector(dirVect, magnitude(dirVect)); //Want to calculate amount to extend from centroid to get forth point scalePoint(dirVect, side * SIDE_TO_AH_RATIO); Point * ans = addPointsNew(dirVect, centroid); free(centroid); free(dirVect); free(pbp_ab); free(pbp_ac); return ans; }
float B2dPlay::drawHill(int pixelStep,float xOffset,float yOffset,float width,float height,b2World* world) { float worldScale = 50; float hillStartY=yOffset; float hillWidth=120+width; int numberOfSlices=hillWidth/pixelStep; b2Vec2 hillVector; float randomHeight = height; hillStartY-=randomHeight; for (int j =0; j<numberOfSlices/2; j++) { b2Vec2 hillVector [4]; float p =j*pixelStep+xOffset; hillVector[0].Set((j*pixelStep+xOffset)/worldScale,480/worldScale); hillVector[1].Set((j*pixelStep+xOffset)/worldScale,(hillStartY+randomHeight*std::cos(2*PI/numberOfSlices*j)/worldScale)); hillVector[2].Set(((j+1)*pixelStep+xOffset)/worldScale,(hillStartY+randomHeight*std::cos(2*PI/numberOfSlices*(j+1))/worldScale)); hillVector[3].Set(((j+1)*pixelStep+xOffset)/worldScale,480/worldScale); b2BodyDef sliceBody ; b2Vec2 centre = findCentroid(hillVector,4); sliceBody.position.Set(0,0);//centre.x,centre.y); b2PolygonShape slicePoly ; slicePoly.Set(hillVector,4); b2FixtureDef sliceFixture; sliceFixture.shape=&slicePoly; b2Body* worldSlice=world->CreateBody(&sliceBody); worldSlice->CreateFixture(&sliceFixture); } for (int j=numberOfSlices/2; j<numberOfSlices; j++) { b2Vec2 hillVector [4]; hillVector[0].Set((j*pixelStep+xOffset)/worldScale,480/worldScale); hillVector[1].Set((j*pixelStep+xOffset)/worldScale,(hillStartY+randomHeight*std::cos(2*PI/numberOfSlices*j)/worldScale)); hillVector[2].Set(((j+1)*pixelStep+xOffset)/worldScale,(hillStartY+randomHeight*std::cos(2*PI/numberOfSlices*(j+1))/worldScale)); hillVector[3].Set(((j+1)*pixelStep+xOffset)/worldScale,480/worldScale); b2BodyDef sliceBody ; b2Vec2 centre = findCentroid(hillVector,4); sliceBody.position.Set(centre.x,centre.y); b2PolygonShape slicePoly ; slicePoly.Set(hillVector,4); b2FixtureDef sliceFixture; sliceFixture.shape=&slicePoly; b2Body* worldSlice=world->CreateBody(&sliceBody); worldSlice->CreateFixture(&sliceFixture); } hillStartY=hillStartY+randomHeight; return (hillStartY); }
int main(int argc, char *argv[]) { cout << "Point A is created by "; grid3d ptA(3, 4, 5); cout << "Point A is "; ptA.print(); cout << endl << endl; cout << "Point B is created by "; grid3d ptB = 2; cout << "Point B is "; ptB.print(); cout << endl << endl; cout << "Point C is created by "; grid3d ptC; ptC = -6; cout << "Point C is "; ptC.print(); cout << endl << endl; cout << "Point D is created by "; grid3d ptD(ptA); cout << "Point D is "; ptD.print(); cout << endl << endl; cout << "Point E is created by "; grid3d ptE = grid3d(-2,2,7); cout << "Point E is "; ptE.print(); cout << endl << endl; cout << "Creating an array of points... "<<endl; grid3d ptArr[2]; cout << "The points in the array are "; ptArr[0].print(); ptArr[1].print(); cout << endl << endl; cout << "Please enter three integers to set a point in the array "; cin >> ptArr[0]; cout << "The point is set to "; ptArr[0].print(); cout << endl << endl; cout << "Start to compute centroid" << endl; grid3d centroid = findCentroid(ptA, ptB, ptE); cout << "The centroid of triange "; ptA.print(); ptB.print(); ptE.print(); cout << " is "; centroid.print(); cout << endl << endl; cout << "Start to compute area" << endl; double area; area = findArea(ptA, ptB, ptE); cout << "The area of triangle "; ptA.print(); ptB.print(); ptE.print(); cout << " is " << area << endl << endl; return 0; }
int main(int argc, char** argv) { if (argc != 4) { cerr << "Usage: " << argv[0] << " dim n area." << endl; return -1; } int dim = atol(argv[1]); int n = atol(argv[2]); double area = atof(argv[3]); if(dim <= 0) { cerr << "Dimension should be larger than 0." << endl; return -1; } if(n <= 0) { cerr << "The number of query points should be larger than 0." << endl; return -1; } if(area <= 0 || area > 1) { cerr << "the area of query points should be in (0, 1]." << endl; return -1; } /*read static data set*/ vector <Point> P; ifstream in("../data.ini"); if(!in) { cerr << "Cannot open file data.ini.\n"; return -1; } P = readPoints(in, dim); uint32_t N = P.size(); try { IStorageManager* memfile = StorageManager::createNewMemoryStorageManager(); StorageManager::IBuffer* file = StorageManager::createNewRandomEvictionsBuffer(*memfile, 10, false); id_type indexIdentifier; ISpatialIndex* tree = RTree::createNewRTree(*file, 0.7, CAPACITY, CAPACITY, dim, SpatialIndex::RTree::RV_RSTAR, indexIdentifier); id_type id = 0; for(uint32_t i = 0; i < N; ++i) { std::ostringstream os; os << P[i]; std::string data = os.str(); tree->insertData(data.size() + 1, reinterpret_cast<const byte*>(data.c_str()), P[i], id); id++; } /*std::cerr << "Operations: " << N << std::endl; std::cerr << *tree; std::cerr << "Buffer hits: " << file->getHits() << std::endl; std::cerr << "Index ID: " << indexIdentifier << std::endl; bool ret = tree->isIndexValid(); if (ret == false) std::cerr << "ERROR: Structure is invalid!" << std::endl; else std::cerr << "The stucture seems O.K." << std::endl; */ for(uint32_t loop = 1; loop <= LOOPNUM; ++loop) { cout << "/**************** BEGIN " << loop << " ***************/" << endl; /*generate query set*/ vector <Point> Q; //Q = genPoints(dim, n, area, loop); stringstream ss; ss << "../query/n" << n << "M" << area << "/loop" << loop; cout << ss.str().c_str() << endl; ifstream qin(ss.str().c_str()); if(!qin) { cerr << "Cannot open query file"; return -1; } Q = readPoints(qin, dim); /*************** BEGIN MQM method ******************/ MQM(tree, Q, n, FUN); // MQM method for finding ANN of Q /*************** END MQM method *******************/ /*************** BEGIN ADM method ******************/ CATCH cost1; cost1.catch_time(); vector <uint32_t> nnIDs = nearestNeighborSet(tree, Q, n); // find the NN for every qi in Q as qi' vector <Point> newQ; for(uint32_t i = 0; i < n; ++i) { newQ.push_back(P[nnIDs[i]]); } cost1.catch_time(); cout << "proposal method: cpu cost for finding NNs of Q as Q' is " << cost1.get_cost(2) << " millisecond(s)" << endl; /***** read dist-matrix index for Q' ********/ uint32_t maxK = P.size() / RATIO; // the length of dist-matrix index uint32_t * dmindex[n]; for(uint32_t i = 0; i < n; ++i) { // read the dist-matrix index of qi' dmindex[i] = readDMIndex(nnIDs[i], maxK); if (dmindex[i] == NULL) { cerr << "error for loading Dist-Matrix Index." << endl; return -1; } } double minadist = 0; /* ADM method for finding approxiamte ANN */ Point adm_ANN = ADM(newQ, n, P, N, dmindex, maxK, FUN, minadist, ERROR_RATE); cout << "ADM: best_dist is " << getAdist(adm_ANN, Q, n, FUN) << endl << endl; /*************** END ADM method *******************/ /*************** BEGIN approxiamte vp-ANN method ******************/ /* approximate vp-ANN method for finding ANN of Q'*/ CATCH cost2; cost2.catch_time(); Point centroid = findCentroid(Q, dim, n); minadist = getAdist(centroid, Q, n, FUN); uint32_t vpID = nearestNeighbor(tree, centroid); uint32_t best_id_Q = epsilonANN(Q, n, P, N, vpID, dmindex, maxK, FUN); cost2.catch_time(); cout << "approxiamte vp-ANN method: cpu cost is " << cost2.get_cost(2) << " millisecond(s)" << endl; cout << "approximate vp-ANN method: best_dist is " << getAdist(P[best_id_Q], Q, n, FUN) << endl; cout << "approxiamte vp-ANN method: best_NN is "; displayCoordinates(P[best_id_Q]); cout << endl; /*************** END approxiamte vp-ANN method *******************/ /*************** BEGIN BF MBM method ******************/ /* MBM method for finding ANN of Q */ CATCH mbmcost; mbmcost.catch_time(); Region M = getMBR(Q, dim, n); MyQueryStrategy qs = MyQueryStrategy(M, Q, FUN); tree->queryStrategy(qs); mbmcost.catch_time(); cout << "MBM: cpu cost is " << mbmcost.get_cost(2) << " millisecond(s)" << endl; cout << "MBM: best_dist is " << qs.best_dist << endl; cout << "MBM: best_NN is "; displayCoordinates(qs.best_NN); cout << "MBM: leafIO = " << qs.mbm_leafIO << "; indexIO = " << qs.mbm_indexIO << endl << endl; /*************** END BF MBM method *******************/ /*************** BEGIN brute method ******************/ /* brute method for finding ANN of Q*/ CATCH brute_cost; brute_cost.catch_time(); uint32_t ANNid = brute_ANN(Q, n, P, N, FUN); brute_cost.catch_time(); cout << "brute method: cpu cost is " << brute_cost.get_cost(2) << " millisecond(s)" << endl; double adist = getAdist(P[ANNid], Q, n, FUN); cout << "brute method: best_dist is " << adist << endl; cout << "brute method: best_NN is "; displayCoordinates(P[ANNid]); /*************** END brute method *******************/ cout << "/**************** END " << loop << " ****************/" << endl << endl; } // end loop delete tree; delete file; delete memfile; } catch(Tools::Exception& e) { cerr << "*********ERROR**********" << endl; std::string s = e.what(); cerr << s << endl; return -1; } catch(...) { cerr << "**********ERROR********" << endl; return -1; } return 1; }
Point PolygonGeometry::doFindCentroid(const Vector<Point> &pts) const { return findCentroid(mPolyBounds, false); }
Point PolylineGeometry::doFindCentroid(const Vector<Point> &pts) const { return findCentroid(mPolyBounds, true); }