void triangle() { static int tid=0; double x1,y1,x2,y2,x3,y3; double a,b,c; ++tid; scanf("%lf%lf%lf%lf%lf%lf",&x1,&y1,&x2,&y2,&x3,&y3); //求得三角形3边长 a=getDis(x1,y1,x2,y2); b=getDis(x1,y1,x3,y3); c=getDis(x2,y2,x3,y3); sprintf(sh[shIndex].name,"triangle%d",tid); sh[shIndex].area=(1.0/4)*sqrt((a+b+c)*(a+b-c)*(b+c-a)*(a+c-b)) ; ++shIndex; }
void getDis(int u,int fa,int d,int& cnt){ dis[cnt++] = d; for(int i = head[u];~i;i = next[i]){ Edge& ed = e[i]; if(ed.v==fa||vis[ed.v]) continue; getDis(ed.v,u,d+ed.w,cnt); } }
int solve(int u,int fa,int d){ int cnt = 0; getDis(u,-1,d,cnt); sort(dis,dis+cnt); int l = 0,r = cnt-1,ret = 0; while(l<r){ if(dis[l]+dis[r]<=k){ ret+=r-l; l++; }else r--; } return ret; }
void AStarInfo::SearchNodes(PathItem* node, PathItem* sideNode, PathItem* endNode)//把相邻的节点放入开放节点中 { if (sideNode != nullptr) { float fX = abs(endNode->_nX - sideNode->_nX); float fY = abs(endNode->_nY - sideNode->_nY); float F, G, H2; sideNode->_fDisToStart = node->_fDisToStart + getDis(node, sideNode);//获得累计的路程 G = sideNode->_fDisToStart; H2 = hypot(fX, fX); int nFunction = 0; if (nFunction == 0) { //A*算法 = Dijkstra算法 + 最佳优先搜索 F = G + H2; } else if (nFunction == 1) { //Dijkstra算法 F = G; } else if (nFunction == 2) { //最佳优先搜索 F = H2; } sideNode->_fFValue = F; sideNode->_pParentPath = node;//设置父节点 _vSearchedList.pushBack(sideNode); node->_pChildPath = sideNode;//设置子节点 _aSearchArr[sideNode->_nX][sideNode->_nY] = nullptr;//把检测过的点从检测列表中删除 _vOpenList.pushBack(sideNode);//加入开放列表 } }
double grahamScan(point* p, int numOfpoint, int* stack) { double distance=0; // total distance.. double tempDis=0; int tempIndex=0; int isCCW=0; int i; int countFail=0; // count the insert failed int countCheck=0; // count the total try for(i=0; i<=numOfpoint; i++) { if(i==0 || i==1) { countCheck++; stack[i] = i; continue; } if(getAng(p[0].x, p[0].y, p[stack[i-2]].x, p[stack[i-2]].y) <=180) { isCCW = ccw(p[i-2],p[i-1],p[i]); if(isCCW == 1) { stack[i-(countFail)]=i; tempIndex = i-(countFail); } else { countFail++; stack[i-1]=i; tempIndex = i-1; } countCheck++; if(countCheck==numOfpoint) { break; } } else { isCCW = ccw(p[i-2],p[i-1],p[i]); if(isCCW == -1) { stack[i-(countFail)]=i; tempIndex = i-(countFail); } else { countFail++; stack[i-1]=i; tempIndex = i-1; } countCheck++; if(countCheck==numOfpoint) { break; } } } //calcultate the distance using stack...// for(i=0; i<tempIndex; i++) { tempDis = getDis(p[stack[i]].x,p[stack[i]].y,p[stack[i+1]].x,p[stack[i+1]].y); printf("%d to %d // temp Dis : %.2lf\n",stack[i], stack[i+1], tempDis); distance += tempDis; } tempDis = getDis(p[0].x,p[0].y,p[stack[tempIndex]].x,p[stack[tempIndex]].y); printf("%d to %d // temp Dis : %.2lf\n",stack[tempIndex],stack[0],tempDis); distance += tempDis; // return distance; }
int main(void) { int numOfTest=0; int numOfPoint=0; int i, j; int min=360; int startLoc=0; int prevLoc=0; int tempAng=0; // save the angel of each step... int tempLoc=0; // save the location of each step... double tempDis=0; // temp distance of minimum angel... double dis=0; // total distance... point* p; //input the each information and dynamic allocation...// printf("Enter the number of the test : "); scanf("%d", &numOfTest); for(j=0; j<numOfTest; j++) { while(1) { printf("Enter the number of point : "); scanf("%d", &numOfPoint); if(numOfPoint<=100) { break; } printf("[ERROR] number of point <=100 \n\n"); } numOfPoint++; //automatically, add the start point (0,0) p = (point*)malloc(sizeof(point)*(numOfPoint)); p[0].x =0; // automatically, set location (0,0) value... p[0].y =0; // same as above step p[0].index=0; // same as above step p[0].check=0; // sames as above step for(i=1; i<numOfPoint; i++) { printf("Enter the [%d]loc : ", i); scanf("%lf %lf",&p[i].x, &p[i].y); p[i].check=0; // set the every check value is '0'... p[i].index = i; // set index number each location... } // //calculate the needed silk... startLoc = 0; // set start location index '0'... while(1) { min = 360;//set min value is max angle(360)... for(i=0; i<numOfPoint; i++) { if(startLoc==i || i==prevLoc) { continue; } tempAng = getAng(p[startLoc].x, p[startLoc].y, p[i].x, p[i].y); if(tempAng == min) { if(tempDis >= getDis(p[startLoc].x,p[startLoc].y,p[i].x,p[i].y)) { continue; } } if(tempAng<=min) { min = tempAng; tempLoc = i; tempDis = getDis(p[startLoc].x, p[startLoc].y, p[i].x, p[i].y); } } if(p[tempLoc].check==1) { dis += getDis(p[startLoc].x, p[startLoc].y, p[0].x, p[0].y); break; } dis+=getDis(p[startLoc].x, p[startLoc].y, p[tempLoc].x, p[tempLoc].y); if(startLoc!=0) { p[startLoc].check=1; } printf("[start loc : %d / dest loc : %d]\n",startLoc, tempLoc); prevLoc = startLoc; startLoc = tempLoc; } dis+=2; // //dis += 0.05; printf("\ntotal dis %.2lf\n\n",dis); dis=0; } }
void getClosest(PointX X[], PointY Y[], PointY Z[], int left, int right, PointX &a, PointX &b, float &d) { /*剩下两个点*/ if(right -left ==1) { d = getDis(X[left], X[right]); a = X[left]; b = X[right]; return; } /*剩下三个点*/ if(right - left ==2) { float d1, d2, d3; d1 = getDis(X[left], X[right-1]); d2 = getDis(X[left], X[right]); d3 = getDis(X[left+1], X[right]); if(d1<=d2 && d1<=d3) { a=X[left]; b=X[right -1]; d=d1; return; } if(d2<=d3) { a=X[left]; b=X[right]; d=d2; } else { a=X[left+1]; b=X[right]; d=d3; } return; } int center = (left + right)/2; int k = left; int g= center +1; /*将已经按Y排序的点,根据中线,分别放入中线左边和右边*/ for(int i = left; i <=right; i++) { if(Y[i].xID <= center) Z[k++] = Y[i]; else Z[g++] = Y[i]; } PointX atmp, btmp; float dtmp; getClosest(X, Z, Y, left, center, a, b, d); getClosest(X, Z, Y, center+1, right, atmp, btmp, dtmp); if(dtmp< d) { a = atmp; b = btmp; d = dtmp; } Merge(Z, Y, left, center+1, right); int f = left; /*距离中心线距离在d内的点放入Z中,并且这些点是按照y坐标从小到大排序的 */ for(int i =left; i<=right; i++) { if(ABS(X[center].x - Y[i].x) <d) Z[f++] = Y[i]; } /*搜索刚刚加入strip中的点, 如果y之间的距离大于d,就开始搜索*/ for(int i=left; i<f; i++) for(int j = i+1; j< f && (Y[j].y-Y[i].y) < d; j++) { dtmp =getDis(Y[i], Y[j]); if(dtmp <d) { d = dtmp; a = X[Y[i].xID]; b = X[Y[j].xID]; } } }
void raySelect(int mouseX, int mouseY){ int objNumb; // indicate how many objects in the scene //SG has two children (one child is a translation at child(0) and the other is nodeModel at child(1)). All objects are linked to NodeTransform objNumb = SG->rootNode->children->at(0)->children->size(); printf("objNumb is %d\n", objNumb); bool hitRes[objNumb]; //array to store the hit result of each object Vector3D currentNear, currentFar; //temp vertex to store the current node near and far vertex; bool currentHitRes = false; // temp bool to store the hit result float nearestDis = 0.0;//use this temp variable to store the shortest distance with the ray origin int nearObj = 0;// use this temp variable to store the children index and it is about the nearest object within the hitted ones //use for loop to check if the ray hits each object or not for (int i = 0; i < objNumb; i++) { //pointer "currentNode" points to the current object; copy current object near vertex to the temp "near" vertex SG->goToParent(); SG->goToChild(i); currentNear = SG->currentNode->nodeNear; //copy object's far vertex to the temp "far" vertex currentFar = SG->currentNode->nodeFar; printf("currentNear x, y, z is %f,%f,%f\n", currentNear.x, currentNear.y, currentNear.z); printf("currentFar x, y, z is %f,%f,%f\n", currentFar.x, currentFar.y, currentFar.z); //compare near and far vertex first, because after transform, near would be far and far be near float dNear = getDis(currentNear, mouseX, mouseY); float dFar = getDis(currentFar, mouseX, mouseY); printf("dNear is %f\n", dNear); printf("dFar is %f\n", dFar); if (dNear > dFar) { currentFar = SG->currentNode->nodeNear; currentNear = SG->currentNode->nodeFar; } //use intersect func to check if there is a hit currentHitRes = intersect(currentNear, currentFar, mouseX, mouseY); //use currentDis to store the distance with the near and ray origin float currentDis; if (currentHitRes == true) { printf("have Intersection!!!\n"); currentDis = getDis(currentNear, mouseX, mouseY); }else{ printf("no Intersection!!!\n"); } if ((currentDis < nearestDis) && (currentHitRes == true)) { nearestDis = currentDis; nearObj = i; printf("current nearest distance is %f\n", nearestDis); printf("current nearest object is %d\n", nearObj); } //store the hit result to the array hitRes[i] = currentHitRes; } printf("nearest object is child %d\n", nearObj); //after the for loop, we could get the shortest distance between the nearest object and the ray origin. And get the index of the child "nearObj" //then move the pointer "currentNode" points to this child[nearObj] SG->goToRoot(); SG->goToChild(0);//go to the Transform node SG->goToChild(nearObj);// go to the nearest object where it is located at the childfren vector and the "currentNode" is pointing to it }
int SharpA02::getDisCM() { int _raw = getDis(); float voltFromRaw=map(_raw, 0, 1023, 0, 5000); return (int)(61.573*pow(voltFromRaw/1000, -1.1068)); }