/** * performs one sweep, which means Nx*Ny random updates */ double performSweep() { int overlap = 0; #pragma omp parallel for for(int n=0; n<Nx*Ny; n++) { int i = randomIndex(gen); disc_t temp = discs[i]; double dx = randomDisplacement(gen); double dy = randomDisplacement(gen); temp.x += dx; temp.y += dy; if(doesOverlap(temp, i)) overlap++; else discs[i] = temp; } return ((double) overlap)/(Nx*Ny); }
void moveBall() { // update the ball's position for the next frame ball.x += ball.xv; ball.y += ball.yv; // if the ball is overlapping the rectangle if(ball.yv > 0) { // only if the ball is moving down if(doesOverlap(ball.x,ball.y, playerPaddleEdgeX + (PADDLE_WIDTH*0.0), PADDLE_POSITION_Y, playerPaddleEdgeX + (PADDLE_WIDTH*0.25), PADDLE_POSITION_Y + PADDLE_HEIGHT)==1) { ball.xv = -5; ball.yv = -3; } if(doesOverlap(ball.x,ball.y, playerPaddleEdgeX + (PADDLE_WIDTH*0.25), PADDLE_POSITION_Y, playerPaddleEdgeX + (PADDLE_WIDTH*0.5), PADDLE_POSITION_Y + PADDLE_HEIGHT)==1) { ball.xv = -3; ball.yv = -5; } if(doesOverlap(ball.x,ball.y, playerPaddleEdgeX + (PADDLE_WIDTH*0.5), PADDLE_POSITION_Y, playerPaddleEdgeX + (PADDLE_WIDTH*0.75), PADDLE_POSITION_Y + PADDLE_HEIGHT)==1) { ball.xv = 3; ball.yv = -5; } if(doesOverlap(ball.x,ball.y, playerPaddleEdgeX + (PADDLE_WIDTH*0.75), PADDLE_POSITION_Y, playerPaddleEdgeX + (PADDLE_WIDTH*1.0), PADDLE_POSITION_Y + PADDLE_HEIGHT)==1) { ball.xv = 5; ball.yv = -3; } } // draw the bricks for(int i=0;i<BRICKS_VERTICALLY;i++) { for(int ii=0;ii<BRICKS_HORIZONTALLY;ii++) { if(bricks[i][ii] != 0) { // is the brick still here? if(doesOverlap(ball.x,ball.y, ii*BRICK_WIDTH, i*BRICK_HEIGHT, (ii+1)*BRICK_WIDTH-BRICK_GAP, (i+1)*BRICK_HEIGHT-BRICK_GAP)==1) { // reverse ball's vertical direction ball.yv = -ball.yv; bricks[i][ii] = 0; // erase the brick } } } } // bounce off edges of screen if(ball.x > SCREEN_W) { ball.xv = -ball.xv; } if(ball.x < 0) { ball.xv = -ball.xv; } if(ball.y < 0) { ball.yv = -ball.yv; } // but reset ball if it goes off bottom of screen if(ball.y > SCREEN_H) { // lose! if(mouse_b) { // makes the ball reappear resetBall(); } } }
bool px_ChangeHistory::getUndo(PX_ChangeRecord ** ppcr, bool bStatic) const { if (m_bOverlap) { *ppcr = NULL; return false; } UT_sint32 iGLOB = 0; bool bGotOne = false; PX_ChangeRecord * pcr = NULL; PX_ChangeRecord * pcrFirst = NULL; bool bCorrect = false; UT_sint32 iAdjust = m_iAdjustOffset; UT_sint32 iLoop = 0; // _printHistory(50); while (!bGotOne) { if ((m_undoPosition - m_iAdjustOffset -iLoop) <= m_iMinUndo) { if (bStatic) m_iAdjustOffset = iAdjust; return false; } pcr = m_vecChangeRecords.getNthItem(m_undoPosition-m_iAdjustOffset-1-iLoop); UT_return_val_if_fail(pcr, false); // just bail out, everything seems wrong // // Do Adjustments for blocks of remote CR's. Scan through local globs // to check for remote CR's which overlap it. // if((iGLOB== 0) && !pcr->isFromThisDoc()) { bCorrect = true; m_iAdjustOffset++; UT_DEBUGMSG(("Doing undo iAdjust incremented to %d \n",m_iAdjustOffset)); } else if ((iGLOB==0) && (pcr->getType() == PX_ChangeRecord::PXT_GlobMarker) && pcr->isFromThisDoc() && !isScanningUndoGLOB() && (m_iAdjustOffset > 0)) { iGLOB++; pcrFirst = pcr; iLoop++; setScanningUndoGLOB(true); } else if((iGLOB>0) && (pcr->getType() == PX_ChangeRecord::PXT_GlobMarker) && pcr->isFromThisDoc()) { if(isScanningUndoGLOB()) pcr = pcrFirst; bGotOne = true; } else if(iGLOB == 0) { bGotOne = true; if(m_iAdjustOffset > 0) bCorrect = true; } // // we're here if we've started scanning through a glob in the local // document to see if it overlaps a later remote change. // else { PT_DocPosition low, high; PT_DocPosition lowWork = 0; PT_DocPosition highWork; UT_sint32 iAccumOffset = 0; getCRRange(pcr, low, high); for (UT_sint32 i = 0; i<m_iAdjustOffset;i++) { PX_ChangeRecord *pcrTmp = m_vecChangeRecords.getNthItem(m_undoPosition-i-1); if (!pcrTmp->isFromThisDoc()) { UT_sint32 iCur = getDoc()->getAdjustmentForCR(pcrTmp); if(pcrTmp->getPosition() <= lowWork+iCur) { iAccumOffset += iCur; } lowWork = low + iAccumOffset; highWork = high + iAccumOffset; PT_DocPosition p1,p2; getCRRange(pcrTmp,p1,p2); bool bZero = (p1 == p2); if(bZero) lowWork++; if (doesOverlap(pcrTmp,lowWork,highWork)) { *ppcr = NULL; // // OK now we have to invalidate the undo stack // to just before the first pcr we pulled off. // if(m_undoPosition-iAdjust > 0) { m_iMinUndo = m_undoPosition-iAdjust-1; } else { m_iMinUndo = 0; } m_iAdjustOffset = iAdjust; m_iAdjustOffset++; return false; } } } iLoop++; } } PX_ChangeRecord * pcrOrig = pcr; if (bCorrect) { pcr->setAdjustment(0); PT_DocPosition pos = pcr->getPosition(); UT_sint32 iAdj = 0; UT_sint32 iCurrAdj = 0; PT_DocPosition low, high; getCRRange(pcr, low, high); for (UT_sint32 i = m_iAdjustOffset-1; i>=0;i--) { pcr = m_vecChangeRecords.getNthItem(m_undoPosition-i-1); if (!pcr->isFromThisDoc()) { iCurrAdj = getDoc()->getAdjustmentForCR(pcr); if(pcr->getPosition() <= static_cast<PT_DocPosition>(static_cast<UT_sint32>(pos) + iAdj + iCurrAdj)) { iAdj += iCurrAdj; low += iCurrAdj; high += iCurrAdj; } PT_DocPosition p1,p2; getCRRange(pcr,p1,p2); bool bZero = (p1 == p2); PT_DocPosition low1 = low; if(bZero) low1++; if (doesOverlap(pcr,low1,high)) { UT_DEBUGMSG(("CR Type %d adj pos %d Overlaps found with CR pos %d \n",pcrOrig->getType(),pcrOrig->getPosition()+iAdj,pcr->getPosition())); UT_DEBUGMSG((" Orig Adj low %d high %d \n",low,high)); *ppcr = NULL; m_iMinUndo = m_undoPosition-m_iAdjustOffset-1; return false; } } } pcrOrig->setAdjustment(iAdj); m_iAdjustOffset++; } UT_ASSERT(pcrOrig->isFromThisDoc()); *ppcr = pcrOrig; if(bStatic) m_iAdjustOffset = iAdjust; return true; }
bool px_ChangeHistory::getRedo(PX_ChangeRecord ** ppcr) const { if ((m_iAdjustOffset == 0) && (m_undoPosition >= m_vecChangeRecords.getItemCount())) return false; if (m_bOverlap) return false; UT_sint32 iRedoPos = m_undoPosition-m_iAdjustOffset; if(iRedoPos <0) return false; PX_ChangeRecord * pcr = m_vecChangeRecords.getNthItem(iRedoPos); UT_return_val_if_fail(pcr, false); // leave records from external documents in place so we can correct bool bIncrementAdjust = false; if (pcr->isFromThisDoc()) { *ppcr = pcr; if (m_iAdjustOffset == 0) { return true; } else { bIncrementAdjust = true; m_iAdjustOffset--; } } while (pcr && !pcr->isFromThisDoc() && (m_iAdjustOffset > 0)) { pcr = m_vecChangeRecords.getNthItem(iRedoPos); m_iAdjustOffset--; iRedoPos++; bIncrementAdjust = true; xxx_UT_DEBUGMSG(("AdjustOffset decremented -1 %d ", m_iAdjustOffset)); } if (pcr && bIncrementAdjust) { PX_ChangeRecord * pcrOrig = pcr; pcr->setAdjustment(0); PT_DocPosition low,high; getCRRange(pcr,low,high); PT_DocPosition pos = pcr->getPosition(); UT_sint32 iAdj = 0; for (UT_sint32 i = m_iAdjustOffset; i >= 1;i--) { pcr = m_vecChangeRecords.getNthItem(m_undoPosition-i); if (!pcr->isFromThisDoc()) { UT_sint32 iCur = getDoc()->getAdjustmentForCR(pcr); if (pcr->getPosition() <= static_cast<PT_DocPosition>(static_cast<UT_sint32>(pos) + iAdj + iCur)) { iAdj += iCur; low += iCur; high += iCur; } PT_DocPosition p1,p2; getCRRange(pcr,p1,p2); bool bZero = (p1 == p2); if(bZero) m_bOverlap = doesOverlap(pcr,low+1,high); else m_bOverlap = doesOverlap(pcr,low,high); if (m_bOverlap) { *ppcr = NULL; return false; } } } pcr = pcrOrig; pcr->setAdjustment(iAdj); xxx_UT_DEBUGMSG(("Redo Adjustment set to %d \n",iAdj)); } if (pcr && pcr->isFromThisDoc()) { *ppcr = pcr; if(bIncrementAdjust) { m_iAdjustOffset += 1; // for didRedo xxx_UT_DEBUGMSG(("AdjustOffset incremented -2 %d \n", m_iAdjustOffset)); } return true; } *ppcr = NULL; return false; }
/* monLen -- length of array. numMon -- index into array to set */ void setMonitorPosition( Monitor *mon[], int monLen, int numMon, int x, int y, double threshold ) { int i; int left, top, right, bottom; Monitor *monTemp; boolean overlapping; mon[numMon]->x = x; mon[numMon]->y = y; i = 0; overlapping = FALSE; while( i < monLen && overlapping == FALSE ) { if( mon[i] != NULL && mon[i] != mon[numMon] ) { overlapping = doesOverlap( mon[numMon], mon[i] ); } i++; } /* Remove overlaps */ if( overlapping == TRUE ) { int dleft, dtop, dright, dbot; /* Remove current monitor so it doesn't contribute to bounding box */ monTemp = mon[numMon]; mon[numMon] = NULL; getBoundingRectangle( mon, monLen, &left, &top, &right, &bottom ); mon[numMon] = monTemp; dleft = mon[numMon]->x + mon[numMon]->width - left; dtop = mon[numMon]->y + mon[numMon]->height - top; dright = right - mon[numMon]->x; dbot = bottom - mon[numMon]->y; if( MINARG( absint( dleft ), absint( dtop ), absint( dright ), absint( dbot ) ) == absint( dleft ) ) { mon[numMon]->x = left - mon[numMon]->width - 1; } else if( MINARG( absint( dleft ), absint( dtop ), absint( dbot ), absint( dright ) ) == absint( dtop ) ) { mon[numMon]->y = top - mon[numMon]->height - 1; } else if( MINARG( absint( dleft ), absint( dtop ), absint( dbot ), absint( dright ) ) == absint( dbot ) ) { mon[numMon]->y = bottom + 1; } else { mon[numMon]->x = right + 1; } } /* Now that overlaps are gone, snap the rectangles together to make sure that at least one of their sides are touching */ for( i = 0; i < monLen; i++ ) { int dleft, dtop, dright, dbot, j; dleft = dtop = dright = dbot = 0; if( mon[i] != NULL ) { for( j = 0; j < monLen; j++ ) { if( mon[j] != NULL && mon[j] != mon[i] ) { if( dleft == 0 || absint( mon[j]->x + mon[j]->width - mon[i]->x ) < absint( dleft ) ) { dleft = mon[i]->x - ( mon[j]->x + mon[j]->width ); } if( dright == 0 || absint( mon[j]->x - ( mon[i]->x + mon[i]->width ) ) < absint( dright ) ) { dright = mon[j]->x - ( mon[i]->x + mon[i]->width ); } if( dtop == 0 || absint( mon[j]->y + mon[j]->height - mon[i]->y ) < absint( dtop ) ) { dtop = mon[i]->y - ( mon[j]->y + mon[j]->height ); } if( dbot == 0 || absint( mon[j]->y - (mon[i]->y + mon[i]->height ) ) < absint( dbot ) ) { dbot = mon[j]->y - ( mon[i]->y + mon[i]->height ); } } } if( MINARG( absint( dleft ), absint( dtop ), absint( dbot ), absint( dright ) ) == absint( dleft ) || absint( dleft ) < mon[i]->width * threshold ) { if( dleft != 0 ) /* 0 means no other monitors present */ { mon[i]->x = mon[i]->x - dleft + 1; } } if( MINARG( absint( dleft ), absint( dtop ), absint( dbot ), absint( dright ) ) == absint( dtop ) || absint( dtop ) < mon[i]->height * threshold ) { if( dtop != 0 ) { mon[i]->y = mon[i]->y - dtop + 1; } } if( MINARG( absint( dleft ), absint( dtop ), absint( dbot ), absint( dright ) ) == absint( dbot ) || absint( dbot ) < mon[i]->height * threshold ) { if( dbot != 0 ) { mon[i]->y = mon[i]->y + dbot - 1; } } if( MINARG( absint( dleft ), absint( dtop ), absint( dbot ), absint( dright ) ) == absint( dright ) || absint( dright ) < mon[i]->width * threshold ) { if( dright != 0 ) { mon[i]->x = mon[i]->x + dright - 1; } } printf(" Monitor %d final position: x = %d y = %d\n", i, mon[i]->x, mon[i]->y ); } } monTemp = mon[numMon]; mon[numMon] = NULL; getBoundingRectangle( mon, monLen, &left, &top, &right, &bottom ); mon[numMon] = monTemp; /* At least one side is in line, now ensure that they are actually touching */ /*for( i = 0; i < monLen; i++ ) { if( mon[i] != NULL && mon[i] != mon[numMon] ) { Check to see which edge of bounding box it is against and then which edge of rectangle it is against. Once found, align this monitor with the found monitor if( ( left == mon[numMon]->x + mon[numMon]->width + 1 && mon[numMon]->x + mon[numMon]->width - mon[i]->x == -1 ) || ( right == mon[numMon]->x - 1 && mon[numMon]->x - ( mon[i]->x + mon[i]->width ) == 1 ) ) { printf("Snapping 1\n"); if( ( mon[numMon]->y + mon[numMon]->width < mon[i]->y ) || ( mon[numMon]->y > mon[i]->y + mon[i]->height ) ) { mon[numMon]->y = mon[i]->y; } } else if( ( top == mon[numMon]->y + mon[numMon]->width + 1 && mon[numMon]->y + mon[numMon]->height - mon[i]->y == -1 ) || ( bottom == mon[numMon]->y - 1 && mon[numMon]->y - ( mon[i]->y + mon[i]->height ) == 1 )) { printf("Snapping 2\n"); if( ( mon[numMon]->x + mon[numMon]->width < mon[i]->x ) || ( mon[numMon]->x > mon[i]->x + mon[i]->width ) ) { mon[numMon]->x = mon[i]->x; } } } }*/ }