Beispiel #1
0
std::list<DHTNode*> KBucket::getClosest(const crypto::Hash& hash, int n) {
	std::list<DHTNode*> result_list;

	if(!isSplit()){
		decltype(n) i = 0;
		auto it = bucket_contents.begin();
		while(i < n || it != bucket_contents.end()){
			result_list.push_back(*it);

			i++;
		}
		return result_list;
	}else{
		auto first = determineBucket(hash);
        auto& second = (split_buckets.first.get() == first ? split_buckets.second : split_buckets.first);

		result_list = first->getClosest(hash, n);

		if(result_list.size() < n){
			std::list<DHTNode*> result_list_appendix = second->getClosest(hash, n);
			result_list.insert(result_list.end(), result_list_appendix.begin(), result_list_appendix.end());
		}

		return result_list;
	}
}
Beispiel #2
0
void GraphicArea::mousePressEvent(QMouseEvent *e)
{
    PointFigure* closest = getClosest(e->pos());
    if(e->button() == Qt::RightButton){
        if(closest != 0)
            emit rightClickedValid(QPoint( closest->x()+16, closest->y()+32 ));
    }else{
        if(closest != 0)
            emit clickedExisting(QPoint( closest->x()+16, closest->y()+32 ));
        else
            emit clickedEmpty(e->pos());
    }

}
Beispiel #3
0
quadTreeNode* quadTree::determineLocation(point2d* point,boolean closest) {
	for(int a=0;a<mainQuadTree->getChildrenVector()->size();a++) {
		quadTreeNode* tmpChild = mainQuadTree->getChildrenVector()->at(a);
		point2d* childLoc = tmpChild->getLocation();

		rectangle tmpOne(new point2d(childLoc->getX(),childLoc->getY()),new point2d(childLoc->getX() + tmpChild->getWidth(),childLoc->getY() + tmpChild->getWidth())); 
		if(pointToRectCollision(point,tmpOne)) 
		{
			return tmpChild;
		}
	}
	if(closest == true) {
		return getClosest(point);
	}
	return NULL;
}
		double interpolatePoint(point *p)
		{
			double x = p->getX();
			double y = p->getY();
			double z = p->getZ();
			double loc[] = {x,y,z};
			physics *p2 = getClosest(loc);
			//find volume intersect
			double ave=0;
			for(int i=0;i<4;i++)
			{
				if(&p2[i] != NULL)
				{
					x++;
					ave+=p2[i].getTemp();
				}
			}
			return ave=ave/x;
		}
void getNearDis(PointX X[], int N, PointX &a, PointX &b, float &d)
{
	MergeSort(X, N);

	PointY *Y = new PointY[N];

	for(int i=0; i<N; i++)
	{
		Y[i].xID = i;
		Y[i].x = X[i].x;
		Y[i].x = X[i].y;
	}

	MergeSort(Y, N);

	PointY *Z = new PointY[N];
	getClosest(X, Y, Z, 0, N-1, a, b, d);

	delete [] Y;
	delete [] Z;
}
Beispiel #6
0
bool EditIsoView::eventMouseMotion(const AGEvent *m)
{
  if(!mEditing)
    CompleteIsoView::eventMouseMotion(m);
  const AGSDLEvent *e=reinterpret_cast<const AGSDLEvent*>(m);
  if(e)
    {
      if(getScreenRect().contains(e->getMousePosition()))
        {
          AVItem *closest=getClosest(e->getMousePosition());
          if(closest)
            {
              VoxelImage *i=dynamic_cast<VoxelImage*>(closest);
              if(mOldPoint)
                mOldPoint->setTexture("white_pin");
              mOldPoint=i;
              if(i)
                i->setTexture("blue_pin");
            }

        }
    }
  return IsoView::eventMouseMotion(m);
}
Beispiel #7
0
int main(int argc, char* argv[])
{
  //reset random
  srand (time(NULL));
  //create 2D array of nodes
  xyNode* disk[TRACKS][SECTORS];
  int i, j, counter;
  int seek = 0;
  
  counter = 1;
  //initialize values into 2D array sequentially
  for(i = 0; i < TRACKS; i++)
  {
    for(j = 0; j < SECTORS; j++)
    {
      disk[i][j] = (xyNode*)malloc(sizeof(xyNode));
      disk[i][j]-> x = j;
      disk[i][j]-> y = i;
      disk[i][j]->value = counter;
      counter++;
    }
  } 

  //Fill request list a value from each track
  int k;
  xyNode* requestList[TRACKS];
  for(k = 0; k < TRACKS; k++){
    //choose 1 random processs on every track
    requestList[k] = disk[k][(int) rand() % (SECTORS)];
  }
 
  //Set head to point random disk location
  int xHead = rand() % (SECTORS);
  int yHead = rand() % (TRACKS);
  xyNode* head = disk[yHead][xHead];
  xyNode* tracker;
  printf("%s%d\n", "Head Originally set at ", head->value);  
  printf("\n");

 
  
  while(!isProcessed(requestList))  
  {
    tracker = head;
    //move head to closest
    head = getClosest(head, requestList);

    //seek time between head and next closest node
    seek = distance(head,tracker) + seek;
    printf("\nseek distance: %d\n", seek);
    
      printf("%s%d\n", "HEAD is now on ", head->value);

    //process request
    head = processRequest(head);  
   
    printRequestList(requestList);
  }
 
  printf("\n average seek time for sstf: %d" , (seek/TRACKS));  
  printf("\nOUTPUT OF THE DISK\n");
 for(i = 0; i <TRACKS; i++)
    {

      for(j = 0; j < SECTORS; j++)
	{
	  printf(" %d ", disk[i][j]->value);
	} 
      printf("\n");
    } 
  return 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];
			}
		}
}
Beispiel #9
0
void loop(){
    api.getMyZRState(me);
    api.getOtherZRState(other);
    int targ = -1;
    switch(part){
        case 0:
            if(distance(me, items[7]) <= distance(me, items[8])){
                target[0] = items[7][0];
                target[1] = items[7][1];
                target[2] = items[7][2];
            }else{
                target[0] = items[8][0];
                target[1] = items[8][1];
                target[2] = items[8][2];
            }
            api.setPositionTarget(target);
            if(game.getNumMirrorsHeld() > 0){
                int score = getClosest(me, 1);
                if(score != -1 && game.posInLight(items[score])){
                    game.useMirror();
                    mirror = TRUE;
                    target[0] = items[score][0];
                    target[1] = items[score][1];
                    target[2] = items[score][2];
                    targ = score;
                    part = 1;
                }else if(score == -1){
                    part = 3; //I'm gonna steal all the energy
                }else{
                    target[0] = items[score][0];
                    target[1] = items[score][1];
                    target[2] = items[score][2];
                    targ = score;
                    part = 1;
                }
            }
            break;
        case 1:
            if(game.hasItem(targ) > -1){
                int score = getClosest(me, 1);
                if(score == -1){
                    part = 4;
                    break;
                }
                targ = score;
                target[0] = items[targ][0];
                target[1] = items[targ][1];
                target[2] = items[targ][2];
            }
            //api.setPositionTarget(target);
            if(mirror && game.getLightSwitchTime() - game.getMirrorTimeRemaining() > 0
                && game.posInArea(me) < 1){
                part = 4;
                break;
            }
            if(game.getMirrorTimeRemaining() <=0){
                mirror = FALSE;
            }
            if(!mirror && game.posInArea(me) == 1){
                part = 4;
            }
            if(game.getLightSwitchTime() < 7 && game.posInArea(me) == -1){
                float x[3];
                x[0] = me[0];
                x[1] = -1 * me[1];
                x[2] = me[2];
                api.setPositionTarget(x);
            }else{
                api.setPositionTarget(target);
            }
            break;
        case 3:
            game.useMirror();
            mirror = TRUE;
            targ = getClosest(me, 0);
            if(targ != -1){
                target[0] = items[targ][0];
                target[1] = items[targ][1];
                target[2] = items[targ][2];
                api.setPositionTarget(target);
            }else{
                part = 4;
            }
            if(game.getMirrorTimeRemaining()<=0){
                mirror = FALSE;
            }
            if(!mirror && game.posInArea(me) == 1){
                part = 4;
            }
            break;
        case 4:
            float x[3];
            if(game.posInArea(me) == 1){
                x[0] = me[0];
                x[1] = -1 * me[1];
                x[2] = me[2];
                api.setPositionTarget(x);
            }
            if(game.posInArea(me) == 0){
                api.setPositionTarget(x);
            }
            if(game.posInArea(me) == -1){
                int z = getClosest(me, 1);
                if(z != -1 && game.posInArea(items[z]) == -1){
                    target[0] = items[z][0];
                    target[1] = items[z][1];
                    target[2] = items[z][2];
                    api.setPositionTarget(target);
                }
            }
    }
}
Beispiel #10
0
//--------------------------------------------------------------
void ofApp::update(){
    ofBackground(100,100,100);
    
    bool bNewFrame = false;
    
#ifdef _USE_LIVE_VIDEO
    vidGrabber.update();
	   bNewFrame = vidGrabber.isFrameNew();
#else
    if(mNextFrame)
    {
        vidPlayer.update();
        bNewFrame = vidPlayer.isFrameNew();
    }
#endif
    
    if (bNewFrame){
        
#ifdef _USE_LIVE_VIDEO
        colorImg.setFromPixels(vidGrabber.getPixels());
#else
        colorImg.setFromPixels(vidPlayer.getPixels());
#endif
        
        grayImage = colorImg;
        if (bLearnBakground == true){
            grayBg = grayImage;		// the = sign copys the pixels from grayImage into grayBg (operator overloading)
            bLearnBakground = false;
        }
        
        // take the abs value of the difference between background and incoming and then threshold:
        grayDiff.absDiff(grayBg, grayImage);
        grayDiff.threshold(Settings::sWhiteThreshold);
        
        // find contours which are between the size of 20 pixels and 1/3 the w*h pixels.
        // also, find holes is set to true so we will get interior contours as well....
        contourFinder.findContours(grayDiff, 20, (340*240)/3, 10, false, false);	// find holes

        for(int i = 0; i < mRecordObjects.size(); i++)
        {
            mRecordObjects[i]->startNewFrame();
        }
                
        mBlobMapper.clear();
        for (int i = 0; i < contourFinder.nBlobs; i++){
            int lIndexClosest;
            getClosest(i, lIndexClosest);
            mBlobMapper.push_back(lIndexClosest);
            if(lIndexClosest >= 0)
            {
                mRecordObjects[lIndexClosest]->addNewMatch(contourFinder.blobs[i]);
            }
        }
        
        for(int i = 0; i < mRecordObjects.size(); i++)
        {
            mRecordObjects[i]->digestFrame();
            if(mRecordObjects[i]->wantToSendMessage())
            {
                ofxOscMessage lMess = mRecordObjects[i]->getOSCMessage();
                sender.sendMessage(lMess, false);
            }
        }
    }
}
Beispiel #11
0
            inFile.ignore(); // ignore newline left by extraction operator
            cout << first << " " << last << " guessed " << guess << endl;
			Contestant contestant(first, last, guess);
			contestants.push_back(contestant);
            getline(inFile, last, '#');
        }
    }
    else
        cout << "Error opening guesses file.  Talk to Sarah." << endl;

// Verify vector contents
//	cout << endl;
//	for (int i = 0; i < contestants.size(); i++)
//		contestants[i].display();

	index = getClosest(contestants, 1400);
	cout << "\nThe person with the closest guess is: "; 
	contestants[index].display(); 
	cout << "Who guessed " <<  contestants[index].getGuess() << endl << endl;

	cout << "Enter actual number of jelly beans: ";
	cin >> actualNumberOfJellyBeans;

	index = getClosest(contestants, actualNumberOfJellyBeans);
	cout << "\nThe person with the closest guess is: "; 
	contestants[index].display(); 
	cout << "Who guessed " <<  contestants[index].getGuess();

    return 0;
}