void Polygon::_constrainVertex(const QPolygonF& polygon, int i, QPointF& v) { // Weird, but nothing to do. if (polygon.size() <= 3) return; // Save previous position of vertex. QPointF prevV = polygon.at(i); // Look at the two adjunct segments to vertex i and see if they // intersect with any non-adjacent segments. // Construct the list of segments (with the new candidate vertex). QVector<QLineF> segments = _getSegments(polygon); int prev = wrapAround(i - 1, segments.size()); int next = wrapAround(i + 1, segments.size()); segments[prev] = QLineF(polygon.at(prev), v); segments[i] = QLineF(v, polygon.at(next)); // We now stretch segments a little bit to cope with approximation errors. for (QVector<QLineF>::Iterator it = segments.begin(); it != segments.end(); ++it) { QLineF& seg = *it; QPointF p1 = seg.p1(); QPointF p2 = seg.p2(); seg.setP1( p1 + (p1 - p2) * 0.35f); seg.setP2( p2 + (p2 - p1) * 0.35f); } // For each adjunct segment. for (int adj=0; adj<2; adj++) { int idx = wrapAround(i + adj - 1, segments.size()); for (int j=0; j<segments.size(); j++) { // If the segment to compare to is valid (ie. if it is not // the segment itself nor an adjacent one) then check for // intersection. if (j != idx && j != wrapAround(idx-1, segments.size()) && j != wrapAround(idx+1, segments.size())) { QPointF intersection; if (segments[idx].intersect(segments[j], &intersection) == QLineF::BoundedIntersection) { // Rearrange segments with new position at intersection point. v = intersection; segments[prev] = QLineF(polygon.at(prev), v); segments[i] = QLineF(v, polygon.at(next)); } } } } }
/*JSON{ "type" : "staticmethod", "class" : "Trig", "name" : "setTrigger", "generate" : "jswrap_trig_setTrigger", "params" : [ ["num","int","The trigger number (0..7)"], ["pos","float","The position (in degrees) to fire the trigger at"], ["pins","JsVar","An array of pins to pulse (max 4)"], ["pulseLength","float","The time (in msec) to pulse for"] ] } Set a trigger for a certain point in the cycle */ void jswrap_trig_setTrigger(JsVarInt num, JsVarFloat position, JsVar *pins, JsVarFloat pulseLength) { TriggerStruct *trig = &mainTrigger; if (num<0 || num>=TRIGGER_TRIGGERS_COUNT) { jsWarn("Invalid trigger number\n"); return; } if (!jsvIsArray(pins)) { jsWarn("Second argument must be an array of pins\n"); return; } if (jsvGetArrayLength(pins) > TRIGGERPOINT_TRIGGERS_COUNT) { jsWarn("Too many pins in array\n"); return; } // convert from degrees to teeth position = wrapAround(((position - trig->keyPosition) * trig->teethTotal / 360), trig->teethTotal); TriggerPointStruct *tp = &trig->triggers[num]; tp->newTooth = (unsigned char)position; tp->newToothFraction = (unsigned char)((position - tp->tooth)*256); tp->pulseLength = jshGetTimeFromMilliseconds(pulseLength); int i, l=(int)jsvGetArrayLength(pins); for (i=0;i<TRIGGERPOINT_TRIGGERS_COUNT;i++) { tp->pins[i] = (Pin)((i<l) ? jshGetPinFromVarAndUnLock(jsvGetArrayItem(pins, i)) : PIN_UNDEFINED); } // now copy over data if we need to do it immediately if (tp->tooth==TRIGGERPOINT_TOOTH_DISABLE || tp->newTooth==TRIGGERPOINT_TOOTH_DISABLE) { tp->tooth = tp->newTooth; tp->toothFraction = tp->newToothFraction; } // all done! }
/*JSON{ "type" : "staticmethod", "class" : "Trig", "name" : "getTrigger", "generate" : "jswrap_trig_getTrigger", "params" : [ ["num","int","The trigger number (0..7)"] ], "return" : ["JsVar","A structure containing all information about the trigger"] } Get the current state of a trigger */ JsVar *jswrap_trig_getTrigger(JsVarInt num) { TriggerStruct *trig = &mainTrigger; if (num<0 || num>=TRIGGER_TRIGGERS_COUNT) { jsWarn("Invalid trigger number\n"); return 0; } TriggerPointStruct *tp = &trig->triggers[num]; // get offset in teeth JsVarFloat position = tp->tooth + (tp->toothFraction/256.0); // convert from teeth to degrees position = wrapAround((position * 360 / trig->teethTotal) + trig->keyPosition, 360); JsVar *obj = jsvNewWithFlags(JSV_OBJECT); if (!obj) return 0; JsVar *v; v = jsvNewFromFloat(position); jsvUnLock(jsvAddNamedChild(obj, v, "pos")); jsvUnLock(v); v = jsvNewFromFloat(jshGetMillisecondsFromTime(tp->pulseLength)); jsvUnLock(jsvAddNamedChild(obj, v, "pulseLength")); jsvUnLock(v); v = jsvNewWithFlags(JSV_ARRAY); int i; if (v) { for (i=0;i<TRIGGERPOINT_TRIGGERS_COUNT;i++) if (tp->pins[i] != PIN_UNDEFINED) { jsvArrayPushAndUnLock(v, jsvNewFromPin(tp->pins[i])); } } jsvUnLock(jsvAddNamedChild(obj, v, "pins")); jsvUnLock(v); return obj; }
int queueExtract(Queue *q, int *value) { if (q->used == 0) return -1; *value = q->buffer[q->first]; q->first = wrapAround(q->first + 1, q->size); q->used--; return 0; }
int queueAdd(Queue *q, int value) { if (q->used == q->size) return -1; q->buffer[q->last] = value; q->last = wrapAround(q->last + 1, q->size); q->used++; return 0; }
void Patch::applyForces(){ int i, x, y, z, x1, y1, z1; // if all forces are received, then it must recompute particles location if (forceCount == numNbrs) { CkVec<Particle> *outgoing = new CkVec<Particle>[numNbrs]; // Received all it's forces from the interactions. forceCount = 0; // Update properties on own particles updateProperties(); // sending particles to neighboring cells x = thisIndex.x; y = thisIndex.y; z = thisIndex.z; if (stepCount > 0 && (stepCount % migrateStepCount) == 0){ for(i=0; i<particles.length(); i++) { migrateToPatch(particles[i], x1, y1, z1); if(x1 !=0 || y1!=0 || z1 !=0) { //CkPrintf("PARTICLE MIGRATING!\n"); outgoing[(x1+1)*nbrsY*nbrsZ + (y1+1)*nbrsZ + (z1+1)].push_back(wrapAround(particles[i])); particles.remove(i); } } for(int num=0; num<numNbrs; num++) { x1 = num / (nbrsY * nbrsZ) - nbrsX/2; y1 = (num % (nbrsY * nbrsZ)) / nbrsZ - nbrsY/2; z1 = num % nbrsZ - nbrsZ/2; patchArray(WRAP_X(x+x1), WRAP_Y(y+y1), WRAP_Z(z+z1)).receiveParticles(outgoing[num]); } } else incomingFlag = true; updateFlag = true; // checking whether to proceed with next step thisProxy(x, y, z).checkNextStep(); //checkNextStep(); delete [] outgoing; // thisProxy(x, y, z).checkNextStep(); //checkNextStep(); } // else { CkPrintf("forcecount = %d/%d on patch %d %d %d\n", forceCount, numNbrs, thisIndex.x, thisIndex.y, thisIndex.z); } }
void mixing(TH1F* h1, TH1F* h2, TH1F* d, TH1F* mix, int sub=1){ int t1=h1->GetEntries(); int t2=h1->GetEntries(); int n1=h1->GetNbinsX(); int n2=h2->GetNbinsX(); for(int i=1; i<=n1; i++){ double p1=h1->GetBinCenter(i); double c1=h1->GetBinContent(i); for(int j=1; j<=n2; j++){ double p2=h2->GetBinCenter(j); double c2=h2->GetBinContent(j); double dp=wrapAround(p1-p2+PI/n1/100.0); mix->Fill(dp,c1*c2); } } if(sub==1) mix->Add(d,-1.0); mix->Scale(n1/mix->Integral()); }
void CConsole::InsertMessageRaw(const CStrW& message) { // (TODO: this text-wrapping is rubbish since we now use variable-width fonts) //Insert newlines to wraparound text where needed CStrW wrapAround(message); CStrW newline(L"\n"); size_t oldNewline=0; size_t distance; //make sure everything has been initialized if ( m_charsPerPage != 0 ) { while ( oldNewline+m_charsPerPage < wrapAround.length() ) { distance = wrapAround.find(newline, oldNewline) - oldNewline; if ( distance > m_charsPerPage ) { oldNewline += m_charsPerPage; wrapAround.insert( oldNewline++, newline ); } else oldNewline += distance+1; } } // Split into lines and add each one individually oldNewline = 0; { CScopeLock lock(m_Mutex); // needed for safe access to m_deqMsgHistory while ( (distance = wrapAround.find(newline, oldNewline)) != wrapAround.npos) { distance -= oldNewline; m_deqMsgHistory.push_front(wrapAround.substr(oldNewline, distance)); oldNewline += distance+1; } m_deqMsgHistory.push_front(wrapAround.substr(oldNewline)); } }
/*JSON{ "type" : "staticmethod", "class" : "Trig", "name" : "getPosAtTime", "generate" : "jswrap_trig_getPosAtTime", "params" : [ ["time","float","The time at which to find the position"] ], "return" : ["float","The position of the trigger wheel in degrees - as a floating point number"] } Get the position of the trigger wheel at the given time (from getTime) */ JsVarFloat jswrap_trig_getPosAtTime(JsVarFloat time) { JsSysTime sTime = (JsSysTime)(time * (JsVarFloat)jshGetTimeFromMilliseconds(1000)); TriggerStruct *trig = &mainTrigger; JsVarFloat position = trigGetToothAtTime(trig, sTime); return wrapAround((position * 360 / trig->teethTotal) + trig->keyPosition, 360); }
void moveFish( short x, short y){ // Check n,e,s,w short newElement = 0; short randomPath = 0; randomPath = rand() % 4; switch(randomPath) { // North case(0): if( map[x][wrapAround( y - 1 )].m_shark == 0 && map[x][wrapAround( y - 1 )].m_fish == 0) { newElement = wrapAround( y - 1 ); // Copy the Shark to the new position. map[x][newElement].m_fish = map[x][y].m_fish; // Set the old Shark position to null map[x][y].m_fish = 0; map[x][newElement].m_fish->m_breed -= 1; if( map[x][newElement].m_fish->m_breed <= 0) { breed( 'f' , x, y); map[x][newElement].m_fish->m_breed = maxFishBreedTime; } // Mark the tile as updated for this turn. map[x][newElement].m_updated = true; } break; case(1): if( map[ wrapAround( x + 1 ) ][ y ].m_shark == 0 && map[ wrapAround( x + 1 ) ][ y ].m_fish == 0) { newElement = wrapAround( x + 1 ); // Copy the Shark to the new position. map[newElement][ y].m_fish = map[x][y].m_fish; // Set the old Shark position to null map[x][y].m_fish = 0; map[newElement][ y].m_fish->m_breed -= 1; if( map[newElement][ y].m_fish->m_breed <= 0) { breed( 'f' , x, y); map[newElement][ y].m_fish->m_breed = maxFishBreedTime; } // Mark the tile as updated for this turn. map[newElement][ y].m_updated = true; } break; case(2): if( map[ x ][ wrapAround( y + 1 ) ].m_shark == 0 && map[ x ][ wrapAround( y + 1 ) ].m_fish == 0) { newElement = wrapAround( y + 1 ); // Copy the Shark to the new position. map[x][newElement].m_fish = map[x][y].m_fish; // Set the old Shark position to null map[x][y].m_fish = 0; map[x][newElement].m_fish->m_breed -= 1; if( map[x][newElement].m_fish->m_breed <= 0) { breed( 'f' , x, y); map[x][newElement].m_fish->m_breed = maxFishBreedTime; } // Mark the tile as updated for this turn. map[x][newElement].m_updated = true; } break; case(3): if( map[ wrapAround( x -1 ) ][ y ].m_shark == 0 && map[ wrapAround( x -1 ) ][ y ].m_fish== 0) { newElement = wrapAround( x - 1 ); // Copy the Shark to the new position. map[newElement][ y].m_fish = map[x][y].m_fish; // Set the old Shark position to null map[x][y].m_fish = 0; map[newElement][ y].m_fish->m_breed -= 1; if( map[newElement][ y].m_fish->m_breed <= 0) { breed( 'f' , x, y); map[newElement][ y].m_fish->m_breed = maxFishBreedTime; } // Mark the tile as updated for this turn. map[newElement][ y].m_updated = true; } break; } }
void sharkMove( short x, short y ){ // Check n,e,s,w short newElement = 0; short randomPath = 0; randomPath = rand() % 4; switch(randomPath) { // North case(0): if( map[x][wrapAround( y - 1 )].m_shark == 0 ) { newElement = wrapAround( y - 1 ); // Copy the Shark to the new position. map[x][newElement].m_shark = map[x][y].m_shark; // Set the old Shark position to null map[x][y].m_shark = 0; // Update the sharks starve time as he just ate. map[x][newElement].m_shark->m_starve -= 1; if ( map[x][newElement].m_shark->m_starve <= 0) { map[x][newElement].m_shark = 0; } else { map[x][newElement].m_shark->m_breed -= 1; if( map[x][newElement].m_shark->m_breed <= 0) { //breed( 's' , x, y); map[x][newElement].m_shark->m_breed = maxSharkBreedTime; } } // Mark the tile as updated for this turn. map[x][newElement].m_updated = true; } break; case(1): if( map[ wrapAround( x + 1 ) ][ y ].m_shark == 0 ) { newElement = wrapAround( x + 1 ); // Copy the Shark to the new position. map[newElement][ y].m_shark = map[x][y].m_shark; // Set the old Shark position to null map[x][y].m_shark = 0; // Update the sharks starve time as he just ate. map[newElement][ y].m_shark->m_starve -= 1; if ( map[newElement][ y].m_shark->m_starve <= 0) { map[newElement][ y].m_shark = 0; } else { map[newElement][ y].m_shark->m_breed -= 1; if( map[newElement][ y].m_shark->m_breed <= 0) { //breed( 's' , x, y); map[newElement][ y].m_shark->m_breed = maxSharkBreedTime; } } // Mark the tile as updated for this turn. map[newElement][ y].m_updated = true; } break; case(2): if( map[ x ][ wrapAround( y + 1 ) ].m_shark == 0 ) { newElement = wrapAround( y + 1 ); // Copy the Shark to the new position. map[x][newElement].m_shark = map[x][y].m_shark; // Set the old Shark position to null map[x][y].m_shark = 0; // Update the sharks starve time as he just ate. map[x][newElement].m_shark->m_starve -= 1; if ( map[x][newElement].m_shark->m_starve <= 0) { map[x][newElement].m_shark = 0; } else { map[x][newElement].m_shark->m_breed -= 1; if( map[x][newElement].m_shark->m_breed <= 0) { //breed( 's' , x, y); map[x][newElement].m_shark->m_breed = maxSharkBreedTime; } } // Mark the tile as updated for this turn. map[x][newElement].m_updated = true; } break; case(3): if( map[ wrapAround( x -1 ) ][ y ].m_shark == 0 ) { newElement = wrapAround( x - 1 ); // Copy the Shark to the new position. map[newElement][ y].m_shark = map[x][y].m_shark; // Set the old Shark position to null map[x][y].m_shark = 0; // Update the sharks starve time as he just ate. map[newElement][ y].m_shark->m_starve -= 1; if ( map[newElement][ y].m_shark->m_starve <= 0) { map[newElement][ y].m_shark = 0; } else { map[newElement][ y].m_shark->m_breed -= 1; if( map[newElement][ y].m_shark->m_breed <= 0) { //breed( 's' , x, y); map[newElement][ y].m_shark->m_breed = maxSharkBreedTime; } } // Mark the tile as updated for this turn. map[newElement][ y].m_updated = true; } break; } }
bool sharkHunt( short x, short y ){ bool ate = false; // Check n,e,s,w short newElement = 0; short randomPath = 0; randomPath = rand() % 4; switch(randomPath) { // North case(0): if( map[x][wrapAround( y - 1 )].m_fish != 0 ) { newElement = wrapAround( y - 1 ); // Copy the Shark to the new position. map[x][newElement].m_shark = map[x][y].m_shark; // Set the old Shark position to null map[x][y].m_shark = 0; // Set the old dead fish to 0 map[x][newElement].m_fish = 0; // Update the sharks starve time as he just ate. map[x][newElement].m_shark->m_starve = sharkStarveTime; map[x][newElement].m_shark->m_breed -= 1; if( map[x][newElement].m_shark->m_breed <= 0) { breed( 's' , x, y); map[x][newElement].m_shark->m_breed = maxSharkBreedTime; } // Mark the tile as updated for this turn. map[x][newElement].m_updated = true; ate = true; } break; case(1): if( map[ wrapAround( x + 1 ) ][ y ].m_fish != 0 ) { newElement = wrapAround( x + 1 ); // Copy the Shark to the new position. map[newElement][y].m_shark = map[x][y].m_shark; // Set the old Shark position to null map[x][y].m_shark = 0; // Set the old dead fish to 0 map[newElement][y].m_fish = 0; // Update the sharks starve time as he just ate. map[newElement][y].m_shark->m_starve = sharkStarveTime; map[newElement][y].m_shark->m_breed -= 1; if( map[newElement][y].m_shark->m_breed <= 0) { breed( 's' , x, y); map[newElement][y].m_shark->m_breed = maxSharkBreedTime; } // Mark the tile as updated for this turn. map[newElement][y].m_updated = true; ate = true; } break; case(2): if( map[ x ][ wrapAround( y + 1 ) ].m_fish != 0 ) { newElement = wrapAround( y + 1 ); // Copy the Shark to the new position. map[x][newElement].m_shark = map[x][y].m_shark; // Set the old Shark position to null //delete ( map[x][y].m_shark ); map[x][y].m_shark = 0; // Set the old dead fish to 0 map[x][newElement].m_fish = 0; // Update the sharks starve time as he just ate. map[x][newElement].m_shark->m_starve = sharkStarveTime; map[x][newElement].m_shark->m_breed -= 1; if( map[x][newElement].m_shark->m_breed <= 0) { breed( 's' , x, y); map[x][newElement].m_shark->m_breed = maxSharkBreedTime; } // Mark the tile as updated for this turn. map[x][newElement].m_updated = true; ate = true; } break; case(3): if( map[ wrapAround( x -1 ) ][ y ].m_fish != 0 ) { newElement = wrapAround( x - 1 ); // Copy the Shark to the new position. map[newElement][y].m_shark = map[x][y].m_shark; // Set the old Shark position to null //delete ( map[x][y].m_shark ); map[x][y].m_shark = 0; // Set the old dead fish to 0 map[newElement][y].m_fish = 0; // Update the sharks starve time as he just ate. map[newElement][y].m_shark->m_starve = sharkStarveTime; map[newElement][y].m_shark->m_breed -= 1; if( map[newElement][y].m_shark->m_breed <= 0) { breed( 's' , x, y); map[newElement][y].m_shark->m_breed = maxSharkBreedTime; } // Mark the tile as updated for this turn. map[newElement][y].m_updated = true; ate = true; } break; //else //{ // ate = false; //} } return ate; }