void Line::Move(int x, int y){ int cx,cy; cx = x - getMiddle().x; cy = y - getMiddle().y; pt[0].x += cx; pt[0].y += cy; pt[1].x += cx; pt[1].y += cy; }
void _quickSort_1(int data[],int begin,int end){ if(begin >= end) return ; int middle = getMiddle(data,begin,end); _quickSort_1(data,begin,middle-1); _quickSort_1(data,middle+1,end); }
/** S = [lo] + L(lo,mi] + G(mi,k) + U[k,hi] L < pivot <= G **/ int partition2(int * elem , int lo , int hi){ int pivot , k; int swap_index = getMiddle(elem,lo,hi); // 三数取中 int mi = lo; swap_t(int ,elem[lo],elem[swap_index]); // 然后和首元素交换 pivot = elem[lo]; for ( k = lo + 1; k <= hi; k++ ) //自左向右扫描 if(elem[k] < pivot){//若当前元素_elem[k]小于pivot,则 mi++; swap_t(int ,elem[mi],elem[k]); // 将_elem[k]交换至原mi之后,使L子序列向右扩展 } swap_t(int ,elem[lo], elem[mi] ); //候选轴点归位 return mi; }
int Reader::keyParser(const char* buf, size_t length, struct pair_t* pair) { const char *p = buf; while(*p != 0 && *p != ':'){ ++p; } if(0 == *p || p == buf){ //到达末尾或在开头 return -1; } if(getWord(buf, p, &(pair->key)) != 0) { return -1; } pair->value = p+1; pair->value = getMiddle(pair->value); //value : 引号转义? return 0; }
void RCmode() { bool exit = true; #if DEBUG puts("LF RC MODE"); // delay_ms(100); #endif while (exit) { LD.setLeftDigit('G'); LPC_GPIO0->FIOCLR = (1 << AMUX); // 0b00 = RC mode LPC_GPIO0->FIOCLR = (1 << BMUX); if(getLLeft()&&getRRight()) { exit=false; } if( !getLLeft() && getLeft() && !getMiddle() && getRight() && !getRRight() ) { SWmode(); } } }
//轴点构造算法:通过调整元素位置构造区间[lo, hi]的轴点,并返回其秩 int partition(int * elem , int lo , int hi){ int pivot; //int pivot = elem[lo]; // 默认首元素为轴点 int swap_index = getMiddle(elem,lo,hi); // 三数取中 swap_t(int ,elem[lo],elem[swap_index]); // 然后和首元素交换 pivot = elem[lo]; while(lo < hi){ while((lo < hi) && (elem[hi] >= pivot)) hi--; if ( lo < hi ) elem[lo++] = elem[hi]; while(lo < hi && elem[lo] < pivot) lo++; if ( lo < hi ) elem[hi--] = elem[lo]; } elem[lo] = pivot; return lo; }
void IsoSurfacePolygonizer::splitCube(const StackedCube &cube) { Array<StackedCube> cubeArray; const BYTE newLevel = cube.getLevel()+1; for(int i = 0; i < 8; i++) { cubeArray.add(StackedCube(cube.m_key.i*2+BIT(i,0), cube.m_key.j*2+BIT(i,1), cube.m_key.k*2+BIT(i,2), newLevel)); } for(int i = 0; i < 8; i++) { cubeArray[i].m_corners[i] = cube.m_corners[i]; } cubeArray[LBN].m_corners[RBN] = cubeArray[RBN].m_corners[LBN] = getMiddle(*cube.m_corners[LBN],*cube.m_corners[RBN],newLevel); cubeArray[LBF].m_corners[RBF] = cubeArray[RBF].m_corners[LBF] = getMiddle(*cube.m_corners[LBF],*cube.m_corners[RBF],newLevel); cubeArray[LTN].m_corners[RTN] = cubeArray[RTN].m_corners[LTN] = getMiddle(*cube.m_corners[LTN],*cube.m_corners[RTN],newLevel); cubeArray[LTF].m_corners[RTF] = cubeArray[RTF].m_corners[LTF] = getMiddle(*cube.m_corners[LTF],*cube.m_corners[RTF],newLevel); cubeArray[LTN].m_corners[LBN] = cubeArray[LBN].m_corners[LTN] = getMiddle(*cube.m_corners[LTN],*cube.m_corners[LBN],newLevel); cubeArray[LTF].m_corners[LBF] = cubeArray[LBF].m_corners[LTF] = getMiddle(*cube.m_corners[LTF],*cube.m_corners[LBF],newLevel); cubeArray[RTN].m_corners[RBN] = cubeArray[RBN].m_corners[RTN] = getMiddle(*cube.m_corners[RTN],*cube.m_corners[RBN],newLevel); cubeArray[RTF].m_corners[RBF] = cubeArray[RBF].m_corners[RTF] = getMiddle(*cube.m_corners[RTF],*cube.m_corners[RBF],newLevel); cubeArray[LBN].m_corners[LBF] = cubeArray[LBF].m_corners[LBN] = getMiddle(*cube.m_corners[LBN],*cube.m_corners[LBF],newLevel); cubeArray[LTN].m_corners[LTF] = cubeArray[LTF].m_corners[LTN] = getMiddle(*cube.m_corners[LTN],*cube.m_corners[LTF],newLevel); cubeArray[RBN].m_corners[RBF] = cubeArray[RBF].m_corners[RBN] = getMiddle(*cube.m_corners[RBN],*cube.m_corners[RBF],newLevel); cubeArray[RTN].m_corners[RTF] = cubeArray[RTF].m_corners[RTN] = getMiddle(*cube.m_corners[RTN],*cube.m_corners[RTF],newLevel); cubeArray[LBN].m_corners[LTF] = cubeArray[LBF].m_corners[LTN] = cubeArray[LTN].m_corners[LBF] = cubeArray[LTF].m_corners[LBN] = getMiddle(*cubeArray[LBN].m_corners[LTN],*cubeArray[LBF].m_corners[LTF],newLevel); cubeArray[RBN].m_corners[RTF] = cubeArray[RBF].m_corners[RTN] = cubeArray[RTN].m_corners[RBF] = cubeArray[RTF].m_corners[RBN] = getMiddle(*cubeArray[RBN].m_corners[RTN],*cubeArray[RBF].m_corners[RTF],newLevel); cubeArray[LBN].m_corners[RBF] = cubeArray[LBF].m_corners[RBN] = cubeArray[RBN].m_corners[LBF] = cubeArray[RBF].m_corners[LBN] = getMiddle(*cubeArray[LBN].m_corners[LBF],*cubeArray[RBN].m_corners[RBF],newLevel); cubeArray[LTN].m_corners[RTF] = cubeArray[LTF].m_corners[RTN] = cubeArray[RTN].m_corners[LTF] = cubeArray[RTF].m_corners[LTN] = getMiddle(*cubeArray[LTN].m_corners[LTF],*cubeArray[RTN].m_corners[RTF],newLevel); cubeArray[LBN].m_corners[RTN] = cubeArray[RBN].m_corners[LTN] = cubeArray[LTN].m_corners[RBN] = cubeArray[RTN].m_corners[LBN] = getMiddle(*cubeArray[LBN].m_corners[LTN],*cubeArray[RBN].m_corners[RTN],newLevel); cubeArray[LBF].m_corners[RTF] = cubeArray[RBF].m_corners[LTF] = cubeArray[LTF].m_corners[RBF] = cubeArray[RTF].m_corners[LBF] = getMiddle(*cubeArray[LBF].m_corners[LTF],*cubeArray[RBF].m_corners[RTF],newLevel); cubeArray[LBN].m_corners[RTF] = cubeArray[RBN].m_corners[LTF] = cubeArray[LTN].m_corners[RBF] = cubeArray[RTN].m_corners[LBF] = cubeArray[LBF].m_corners[RTN] = cubeArray[RBF].m_corners[LTN] = cubeArray[LTF].m_corners[RBN] = cubeArray[RTF].m_corners[LBN] = getMiddle(*cube.m_corners[LBN],*cube.m_corners[RTF],newLevel); for(int i = 0; i < 8; i++) { StackedCube &c = cubeArray[i]; // c.validate(); if(c.intersectSurface()) { pushNextLevelCube(c); } } }
// this method is deprecated in 006 please use getMiddle ofxPoint2f ofxPoint2f::middled( const ofxPoint2f& pnt ) const{ return getMiddle(pnt); }
double adaptativeQuadrature(int id, double (*func)(double), double a, double b, double err, int preparingBuffer) { double m, funcB, funcSleft, funcSright, areaS1, areaS2, areaB; params *input; int preservePos; /* funcB, funcSleft, funcSright: alturas dos retangulos, calculado a partir do ponto médio entre cada um dos retangulos. areaB: area do retangulo maior (b - a) * funcB areaS1, areaS2: area dos retangulos menores (m - a) * funcSleft e (b - m) * funcSright (left e right indicam apenas se está a esquerda ou direita do ponto médio) */ m = getMiddle(a, b); funcB = func(m); funcSleft = func(getMiddle(a, m)); funcSright = func(getMiddle(m, b)); areaB = (b - a) * funcB; areaS1 = (m - a) * funcSleft; areaS2 = (b - m) * funcSright; // agora, se o módulo da diferença entre a area do retangulo maior e da soma dos retangulos menores for maior que o erro máximo, calcula a área dos dois intervalos [a,m], [b,m] // porém, agora na versão concorrente, checamos se existe a possibilidade de delegar essa tarefa a uma nova threads // assim, se nem todas as threads foram iniciadas, então iremos iniciar uma nova thread que ficará responsável por um dos retangulos e a thread atual fica responsável apenas pelo outro retangulo. // senão, apenas retorna o valor da area do retangulo maior if (fabs(areaB - (areaS1 + areaS2)) > err) { if (!allinstantiated) { //mesmo estando fora de um mutex, não há problema por ter uma confirmação dentro. pthread_mutex_lock(&theMutex); if (instantiated < nThreads) { // checa se é possível instanciar. input = (params *) malloc(sizeof(params)); input->id = instantiated; input->err = err; input->a = m; input->b = b; input->func = func; pthread_create(&threads[instantiated], NULL, calcIntegral, (void *) input); instantiated++; pthread_mutex_unlock(&theMutex); areaB = adaptativeQuadrature(id, func, a, m, err, preparingBuffer); } else { //se não for, apenas avisa que não é mais possível instanciar e continua a trabalhar normalmente allinstantiated = 1; pthread_mutex_unlock(&theMutex); areaB = adaptativeQuadrature(id, func, a, m, err, preparingBuffer) + adaptativeQuadrature(id, func, m, b, err, preparingBuffer); } } else { // Preparing buffer é para que não ocorra que threads que antes estavam ociosas busquem por threads atualmente ociosas para calcular um retangulo // é necessário para que o processamento continue, e não é problema pois a ociosidade da outra thread acabará rapido, basta uma thread ainda no cálculo inicial a chame if (!anythingSent && anyoneFree && !preparingBuffer) { // checa se tem alguém ocioso e se ninguém enviou dado ainda pthread_mutex_lock(&theMutex); if (!anythingSent) { // recheca a condição, agora numa seção crítica (entre locks) globala = m; globalb = b; anythingSent = id + 1; freePos = pos[id]; pos[id] = (pos[id] + 1 < TAM) ? pos[id] + 1 : 0; anyoneFree = 0; preservePos = freePos; // avisa as threads que estão esperando que já enviou dados pthread_cond_broadcast(&theCond); pthread_mutex_unlock(&theMutex); areaB = removeFromBuffer(id, preservePos) + adaptativeQuadrature(id, func, a, m, err, preparingBuffer); } else { // senão, simplesmente avisa que saiu do loop e continua execução pthread_cond_broadcast(&theCond); pthread_mutex_unlock(&theMutex); areaB = adaptativeQuadrature(id, func, a, m, err, preparingBuffer) + adaptativeQuadrature(id, func, m, b, err, preparingBuffer); } } else { // senão, simplesmente calcula mais retangulos areaB = adaptativeQuadrature(id, func, a, m, err, preparingBuffer) + adaptativeQuadrature(id, func, m, b, err, preparingBuffer); } } } return areaB; }
inline bool contains (const BoundingBox & b) const { for (unsigned int i = 0; i < 3; i++) if (fabs (getMiddle (i) - b.getMiddle (i)) - BOUNDINGBOX_EPSILON > (getWHL (i) + b.getWHL (i)) / 2.0) return false; return true; }