//Point vs circle bool hasCollided(sf::Vector2f c1, sf::Vector2f c2, float r2) { return getDist(c1, c2) <= r2; }
//Circle vs circle bool hasCollided(sf::Vector2f c1, float r1, sf::Vector2f c2, float r2) { return (r1 + r2 > getDist(c1, c2)); }
int main(int argc, char* argv[]){ int i, s, max, min, d, n=35; List C = newList(); // central vertices List P = newList(); // peripheral vertices List E = newList(); // eccentricities Graph G = NULL; // Build graph G G = newGraph(n); for(i=1; i<n; i++){ if( i%7!=0 ) addEdge(G, i, i+1); if( i<=28 ) addEdge(G, i, i+7); } addEdge(G, 9, 31); addEdge(G, 17, 13); addEdge(G, 14, 33); // Print adjacency list representation of G printGraph(stdout, G); // Calculate the eccentricity of each vertex for(s=1; s<=n; s++){ BFS(G, s); max = getDist(G, 1); for(i=2; i<=n; i++){ d = getDist(G, i); max = ( max<d ? d : max ); } append(E, max); } // Determine the Radius and Diameter of G, as well as the Central and // Peripheral vertices. append(C, 1); append(P, 1); min = max = front(E); moveTo(E,0); moveNext(E); for(i=2; i<=n; i++){ d = getElement(E); if( d==min ){ append(C, i); }else if( d<min ){ min = d; clear(C); append(C, i); } if( d==max ){ append(P, i); }else if( d>max ){ max = d; clear(P); append(P, i); } moveNext(E); } // Print results printf("\n"); printf("Radius = %d\n", min); printf("Central vert%s: ", length(C)==1?"ex":"ices"); printList(stdout, C); printf("\n"); printf("Diameter = %d\n", max); printf("Peripheral vert%s: ", length(P)==1?"ex":"ices"); printList(stdout, P); printf("\n"); // Free objects freeList(&C); freeList(&P); freeList(&E); freeGraph(&G); return(0); }
int main(int argc, char* argv[]){ int i, s, max, min, d, n=35; //int n=35; List C = newList(); // central vertices List P = newList(); // peripheral vertices List E = newList(); // eccentricities Graph G = newGraph(n); // Build graph G for(i=1; i<n; i++){ if( i%7!=0 ) addEdge(G, i, i+1); if( i<=28 ) addEdge(G, i, i+7); } addEdge(G, 9, 31); addEdge(G, 17, 13); addEdge(G, 14, 33); // Print adjacency list representation of G printGraph(stdout, G); // Calculate the eccentricity of each vertex for(s=1; s<=n; s++){ BFS(G, s); max = getDist(G, 1); for(i=2; i<=n; i++){ d = getDist(G, i); max = ( max<d ? d : max ); } append(E, max); } printf("Source of G %d Size of G %d Order of G %d",getSource(G),getSize(G),getOrder(G)); // Determine the Radius and Diameter of G, as well as the Central and // Peripheral vertices. append(C, 1); append(P, 1); min = max = front(E); moveTo(E,0); moveNext(E); for(i=2; i<=n; i++){ d = getElement(E); if( d==min ){ append(C, i); }else if( d<min ){ min = d; clear(C); append(C, i); } if( d==max ){ append(P, i); }else if( d>max ){ max = d; clear(P); append(P, i); } moveNext(E); } // Print results printf("\n"); printf("Radius = %d\n", min); printf("Central vert%s: ", length(C)==1?"ex":"ices"); printList(stdout, C); printf("\n"); printf("Diameter = %d\n", max); printf("Peripheral vert%s: ", length(P)==1?"ex":"ices"); printList(stdout, P); printf("\n"); freeList(&C); freeList(&P); freeList(&E); freeGraph(&G); //testing undirected graph printf("testing undirected graph G2\n"); Graph G2= newGraph(6); addEdge(G2, 1, 2); addEdge(G2, 1, 3); addEdge(G2, 2, 4); addEdge(G2, 2, 5); addEdge(G2, 2, 6); addEdge(G2, 3, 4); addEdge(G2, 4, 5); addEdge(G2, 5, 6); //test access functions printGraph(stdout, G2); BFS(G2, 3); printf("Source of G2:%d Size of G2:%d Order of G2:%d \n",getSource(G2),getSize(G2),getOrder(G2)); printf("getParent(G2,6)%d\n",getParent(G2,6)); List Pa=newList(); printf("getDist 3 to 6:%d\n",getDist(G2,6)); printf("getPath 3 to 6:\n"); getPath(Pa,G2,6); printList(stdout,Pa); BFS(G2, 1); printf("getDist 1 to 5:%d\n",getDist(G2,5)); clear(Pa); getPath(Pa,G2,5); printf("getPath 1 to 5:\n"); printList(stdout,Pa); printf("makeNull(G2)\n"); makeNull(G2); printGraph(stdout, G2); printf("Source of G:%d Size of G:%d Order of G:%d \n",getSource(G2),getSize(G2),getOrder(G2)); printf("getParent(G2,5)%d\n",getParent(G2,5)); printf("getDist(G2,6)%d\n",getDist(G2,6)); freeList(&Pa); freeGraph(&G2); //testing directed graph printf("\ntesting directed graph G3\n"); Graph G3=newGraph(4); addArc(G3,1,2); addArc(G3,1,3); addArc(G3,3,2); addArc(G3,3,4); addArc(G3,4,1); //test access functions printGraph(stdout, G3); BFS(G3, 3); printf("Source of G3:%d Size of G3:%d Order of G3:%d \n",getSource(G3),getSize(G3),getOrder(G3)); printf("getDist 3 to 6:%d\n",getDist(G3,1)); printf("getDist 3 to 2:%d\n",getDist(G3,2)); printf("getParent(3):%d\n",getParent(G3,3)); List Pa2=newList(); printf("getPath 3 to 1:\n"); getPath(Pa2,G3,1); printList(stdout,Pa2); printf("getParent(1):%d\n",getParent(G3,1)); // Free objects freeList(&Pa2); freeGraph(&G3); return(0); }
void drawTrailLines(Player *p, PlayerVisual *pV) { segment2 *s; float height; float *normal; float dist; float alpha; Data *data; Camera *cam; float trail_top[] = { 1.0, 1.0, 1.0, 1.0 }; data = p->data; cam = p->camera; height = data->trail_height; if(height <= 0) return; /* glDepthMask(GL_FALSE); glDisable(GL_DEPTH_TEST); */ if (gSettingsCache.antialias_lines) { glEnable(GL_LINE_SMOOTH); /* enable line antialiasing */ } glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBegin(GL_LINES); s = data->trails; while(s != data->trails + data->trailOffset) { /* the current line is not drawn */ /* compute distance from line to eye point */ dist = getDist(s, cam->cam); // alpha = (game2->rules.grid_size - dist / 2) / game2->rules.grid_size; alpha = (400 - dist / 2) / 400; if(alpha < 0) alpha = 0; // trail_top[3] = alpha; glColor4fv(trail_top); if(s->vDirection.v[1] == 0) normal = normal1; else normal = normal2; glNormal3fv(normal); glVertex3f(s->vStart.v[0], s->vStart.v[1], height); glVertex3f(s->vStart.v[0] + s->vDirection.v[0], s->vStart.v[1] + s->vDirection.v[1], height); s++; polycount++; } glEnd(); /* compute distance from line to eye point */ dist = getDist(s, cam->cam); // alpha = (game2->rules.grid_size - dist / 2) / game2->rules.grid_size; alpha = (400 - dist / 2) / 400; if(alpha < 0) alpha = 0; // trail_top[3] = alpha; glColor4fv(trail_top); glBegin(GL_LINES); glVertex3f(s->vStart.v[0], s->vStart.v[1], height); glVertex3f( getSegmentEndX(data, 0), getSegmentEndY(data, 0), height ); glEnd(); glDisable(GL_BLEND); glDisable(GL_LINE_SMOOTH); /* disable line antialiasing */ /* glEnable(GL_DEPTH_TEST); glDepthMask(GL_TRUE); */ }
/* print out the dist matrix */ void printDistMatrix( double * D, size_t d, size_t N) { size_t i,j; #ifdef CLI printf("\n\tMatrix Dim:"); printf(F_SIZET, d); printf("\n"); if( D == NULL) { printf("\nNothing to print.\n"); return; } for( i = 0; i < N-1; i++) { printf("\tcolumn "); printf(F_SIZET,i); } printf("\n"); for( i = 1; i < N; i++) { printf(F_SIZET,i); printf(":\t"); for( j = 0; j < i; j++) { printf("%f\t", getDist(i,j,D,d,N) ); } printf("\n"); } #endif #ifndef CLI Rprintf("\n\tMatrix Dim:"); Rprintf(F_SIZET, d); Rprintf("\n"); if( D == NULL) { Rprintf("\nNothing to print.\n"); return; } for( i = 0; i < N-1; i++) { Rprintf("\tcolumn "); Rprintf(F_SIZET,i); } Rprintf("\n"); for( i = 1; i < N; i++) { Rprintf(F_SIZET,i); Rprintf(":\t"); for( j = 0; j < i; j++) { Rprintf("%f\t", getDist(i,j,D,d,N) ); } Rprintf("\n"); } #endif return; }
float gsl_get_dist (int x, int y, int x2 ,int y2) { return getDist(x, y, x2, y2); }
void Analyzer_Reading::AnalyzeStd(Play* _play) { std::vector<Hitobject*>& hitobjects = _play->beatmap->getHitobjects(); osu::TIMING timing; std::vector<osu::TIMING>& timingsRate = *AnalysisStruct::beatmapAnalysis.getAnalyzer("note rate")->getData(); std::vector<osu::TIMING>& timingsVel = *AnalysisStruct::beatmapAnalysis.getAnalyzer("velocity (px/ms)")->getData(); vector2df avgCoor, distractor; int i = 0, previ = 0; double delay = 10.0; double avgCoeff = 0.4; // higher coeff = more precise for (int ms = 0; ms < hitobjects[hitobjects.size() - 1]->getTime(); ms += delay) { // Let index catch up to the time. Bail if out of bounds while (hitobjects[i]->getEndTime() < ms) i++; if (i >= hitobjects.size()) break; // Out of bound check if (!BTWN(1, i, hitobjects.size() - 1)) continue; previ = i; int iNoteRate = FindTimingAt(timingsRate, ms); int iNoteVel = FindTimingAt(timingsVel, ms); double noteRateVel = timingsVel[iNoteVel].data*timingsRate[iNoteRate].data; const double diffCoeff = 1.02; // interpreted easiness of the note rate velocity. Use this to adjust interpreted reading difficulty. (higher = easier, SENSITIVE to nearest 0.01) if (timingsVel[iNoteVel].press == false) noteRateVel /= 1.01; else noteRateVel /= 1.03; avgCoeff = 1.0 / ((noteRateVel/*/diffCoeff*/) + 1.0); // Apply cursor movement averaging if (!hitobjects[i]->isHitobjectLong()) { avgCoor.X = avgCoeff*((double)hitobjects[i]->getPos().X + distractor.X) + (1.0 - avgCoeff)*avgCoor.X; avgCoor.Y = avgCoeff*((double)hitobjects[i]->getPos().Y + distractor.Y) + (1.0 - avgCoeff)*avgCoor.Y; } else { avgCoor.X = avgCoeff*(double)(hitobjects[i]->getSlider()->GetSliderPos(ms).X + distractor.X) + (1.0 - avgCoeff)*avgCoor.X; avgCoor.Y = avgCoeff*(double)(hitobjects[i]->getSlider()->GetSliderPos(ms).Y + distractor.Y) + (1.0 - avgCoeff)*avgCoor.Y; } timing.press = false; if (!hitobjects[i]->isHitobjectLong()) // hitcircle { if (BTWN(ms - delay / 2.0, hitobjects[i]->getTime(), ms + delay / 2.0)) { double dist = getDist(hitobjects[i]->getPos(), vector2d<double>(avgCoor.X, avgCoor.Y)); double radius = CS2px(_play->getMod()->getCS()) / 2.0; const double divider = 1.0; // SENSITIVE to nearest 1.0 const double distSensitivity = 1.4; // shifts distance by x radii (1.0 = 1.0*CSpx). Use this to fix imbalance due to interpreted slider reading difficulty. Higher = easier. SENSITIVE to nearest 0.01 // formula set x=0 to y=0 timing.data = exp(dist - radius*distSensitivity)/divider - exp(0 - radius*distSensitivity)/divider; timing.press = true; } } else // slider { if (BTWN(hitobjects[i]->getTime(), ms, hitobjects[i]->getEndTime())) { vector2d<double> pos = hitobjects[i]->getSlider()->GetSliderPos(ms); double dist = getDist(vector2d<double>(pos.X, pos.Y), vector2d<double>(avgCoor.X, avgCoor.Y)); double radius = CS2px(_play->getMod()->getCS()) / 2.0; const double divider = 1.0; // SENSITIVE to nearest 1.0 const double distSensitivity = 1.36; // shifts distance by x radii (1.0 = 1.0*CSpx). Use this to adjust interpreted slider reading difficulty. Higher = easier. SENSITIVE to nearest 0.01 // formula set x=0 to y=0 timing.data = exp(dist - radius*distSensitivity)/divider - exp(0 - radius*distSensitivity)/divider; timing.press = true; } } timing.pos = avgCoor; timing.time = ms; // i > 1 to remove averaging errors when starting if (i > 1) data.push_back(timing); } }
GRA_tpCondRet GRA_BuscarCaminho( GRA_tppGrafo pGrafo , int idVerticeOrigem, int idVerticeDestino, LIS_tppLista * pLista ) { tpVertice * v = NULL; tpVertice * u = NULL; tpVertice * origem1 = NULL; tpVertice * origem2 = NULL; int lenV = 0; LIS_tppLista Q = NULL; //FILA LIS_tppLista arestas = NULL; LIS_tppLista retorno = NULL; int t; int len = 0; int achou = 0; int ok = 0; int i,j,in; int lenD; int alt = 0; int* visitados = NULL; // Vetor de vertices visitados int* vizinhos = NULL; int* idAux = NULL; Dist** dists = NULL; Dist* dist = NULL; //aux; Dist* currDist = NULL; lenD = 1; v = get_by_id(pGrafo, idVerticeOrigem); u = get_by_id(pGrafo, idVerticeDestino); if(v == NULL || u == NULL) { return GRA_CondRetNaoEhVertice; } origem1 = ObterOrigem(pGrafo, v); origem2 = ObterOrigem(pGrafo, u); if (origem1 != origem2) { return GRA_CondRetNaoEhConexo; }//Else: É conexo, devia retornar Ok. for (;;) { dists = (Dist**)calloc(LIS_NumeroDeElementos(pGrafo->vertices)+1, sizeof(Dist*)); if (dists == NULL) {break;} dists[0] = newDist(idVerticeOrigem, 0); retorno = LIS_CriarLista(free); if (retorno == NULL) { break; } else if (v == u) { if( LIS_InserirElementoApos(retorno, newInt(idVerticeOrigem)) == LIS_CondRetOK) { *pLista = retorno; return GRA_CondRetOK; } else { break; } } visitados = (int*) calloc(LIS_NumeroDeElementos(pGrafo->vertices)+1,sizeof(int)); if (visitados == NULL) { break; } Q = LIS_CriarLista(free); if (Q == NULL) { break; } visitados[0] = idVerticeOrigem; lenV = 1; if (LIS_InserirElementoApos(Q, newInt(idVerticeOrigem)) != LIS_CondRetOK) { break;} //enque ok = 1; break; } if (!ok) { free(dists); LIS_DestruirLista(retorno); free(visitados); LIS_DestruirLista(Q); return GRA_CondRetFaltouMemoria; } while (LIS_NumeroDeElementos(Q) > 0) { //dequeue LIS_IrInicioLista(Q); t = getInt(LIS_ObterValor(Q)); LIS_ExcluirElemento(Q); //Iterar sobre vizinhos GRA_ObterVizinhos(pGrafo, t, &arestas); vizinhos = converteListaParaVetorDeInteiros(arestas, &len); LIS_DestruirLista(arestas); arestas = NULL; currDist = getDist(dists, t); if(!currDist) { return GRA_CondRetFaltouMemoria; } else { } alt = currDist->dist + 1; for (i=0; i < len; i++) { in = 0; for (j=0; j < lenV; j++) { if (visitados[j] == vizinhos[i]) { in = 1; } } if (!in) { dist = getDist(dists, vizinhos[i]); if (dist == NULL) { //infinity dists[lenD] = newDist(vizinhos[i], alt); dists[lenD]->prev = currDist; dist = dists[lenD]; lenD++; } else if (alt < dist->dist) { dist->dist = alt; dist->prev = currDist; } if (idVerticeDestino == vizinhos[i]) { currDist = dist; achou = 1; } visitados[lenV] = vizinhos[i]; lenV++; LIS_InserirElementoAntes(Q, newInt(vizinhos[i])); } } free(vizinhos); if (achou) { currDist = dist; break; } if(lenV == LIS_NumeroDeElementos(pGrafo->vertices)) { break; } } if (achou) { //printf("\n"); // for(i=0; i < lenD; i++) { // printf("endr: %p, id: %d, dist: %d, prev: %p \n", *(dists+i), dists[i]->id, dists[i]->dist, dists[i]->prev); // } while (currDist) { LIS_InserirElementoAntes(retorno, newInt(currDist->id)); currDist = currDist->prev; } } //Limpando a memória for (i=0; i < lenD; i++) { free(dists[i]); } free(dists); free(visitados); LIS_DestruirLista(Q); *pLista = retorno; return GRA_CondRetOK; }
void BFS ( Graph G, int s ) { // pre-condition if ( G == NULL ) { printf ( "Graph Error: BFS() on NULL graph" ); exit(1); } // pre-condition if ( s < 1 || s > getOrder(G) ) { printf ( "Graph Error: s undefined" ); exit(1); } // this part of the code was strictly from the textbook // on page 595 from section 22.2. Also mentioned in pa4 pdf // received help from a cs student G->source = s; int i, j, k; for ( i = 1; i <= getOrder(G); i++ ) { G->color[i] = WHITE; G->d[i] = INF; G->p[i] = NIL; } G->color[s] = GRAY; G->d[s] = 0; G->p[s] = NIL; List L = newList(); // Q != /0 append(L, s); // received help on this while loop from a student while( length(L) != 0) { j = front(L); deleteFront(L); moveTo(G->adjacency[j], 0); while(getIndex(G->adjacency[j]) != -1) { k = getElement(G->adjacency[j]); // need help with the for loop // ask professor if(G->color[k] == WHITE) { G->color[k] = GRAY; G->d[k] = (getDist(G, j) +1); G->p[k] = j; append(L, k); } moveNext(G->adjacency[j]); } G->color[j] = BLACK; } // free the list after use freeList(&L); }
double QxrdCenterFinder::getDist(QPointF pt) const { return getDist(pt.x(), pt.y()); }
//o---------------------------------------------------------------------------o //| Function - bool objInRange(CBaseObject *a, CBaseObject *b, UI16 distance) //| Programmer - Unknown //o---------------------------------------------------------------------------o //| Purpose - Check if an object is within a certain distance of another //| object //o---------------------------------------------------------------------------o bool objInRange(CBaseObject *a, CBaseObject *b, UI16 distance) { return (getDist(a, b) <= distance); }
double SosUtil::getDist(PointXYLong pt1, PointXYLong pt2) { return getDist((long)pt1.x,(long)pt1.y,(long)pt2.x,(long)pt2.y); }
double SosUtil::getDist(PointXY pt1, PointXY pt2) { return (float)getDist((double)pt1.x,(double)pt1.y,(double)pt2.x,(double)pt2.y); }
int main (void) { printf ("This program tests the graph client\n"); Graph test_one = newGraph (6); Graph test_two = newGraph (10); /************************************************ * Graph test_one has a representation as follows * 1--------------2 * | /|\ * | / | \ * | / | \ * 3----------4---5---6 * In this case, all the edges are undirected * and we will initialize our adjacency lists to * represent this graph * We will perform BFS on vertex 3 and test other * appropriate functions in the ADT as well ***********************************************/ addArc (test_one, 1, 2); addArc (test_one, 1, 3); addArc (test_one, 2, 1); addArc (test_one, 2, 4); addArc (test_one, 2, 5); addArc (test_one, 2, 6); addArc (test_one, 3, 1); addArc (test_one, 3, 4); addArc (test_one, 4, 2); addArc (test_one, 4, 3); addArc (test_one, 4, 5); addArc (test_one, 5, 2); addArc (test_one, 5, 4); addArc (test_one, 5, 6); addArc (test_one, 6, 2); addArc (test_one, 6, 5); BFS (test_one, 3); List threesix = newList(); List threefiv = newList(); getPath (threesix, test_one, 6); getPath (threefiv, test_one, 5); printf ("Path from three to five, Expected output: 3 1 2 6, Test output: "); printList (stdout, threesix); printf ("\n"); printf ("Path from three to six: Expected output: 3 4 5, Test output: "); printList (stdout, threefiv); printf ("\n"); freeList (&threesix); freeList (&threefiv); printf ("Order: Expected = 6, Output = %d\n", getOrder (test_one)); printf ("Parent (6) = Expected = 2, Output = %d\n", getParent (test_one, 6)); printf ("Distance (6) = Expected = 3, Output = %d\n", getDist (test_one, 6)); printf ("___Adjacency List of test_one, Expected___\n"); printf ("1: 2 3\n2: 1 4 5 6\n3: 1 4\n4: 2 3 5\n5: 2 4 6\n6: 2 5\n"); printf ("___Adjacency List of test_two, Output___\n"); printGraph (stdout, test_one); printf ("\n"); printf ("The source of this graph is: Expected = 3, Output = %d\n", getSource (test_one)); //Finish test of graph one, tests graph two now addEdge (test_two, 1, 2); addEdge (test_two, 1, 4); addEdge (test_two, 2, 3); addEdge (test_two, 2, 5); addEdge (test_two, 3, 6); addEdge (test_two, 4, 7); addEdge (test_two, 5, 6); addEdge (test_two, 5, 8); addEdge (test_two, 6, 8); addEdge (test_two, 7, 8); addEdge (test_two, 8, 9); addEdge (test_two, 9, 10); /************************************************** * visual representation of this graph * 1----------2----3 * | | | * | | | * 4 5----6 * | | / * | | / * | | / * | | / * 7-----------8---9----10 ***************************************************/ BFS (test_two, 3); printf ("Parent list of each vertex after BFS (test_two, 3) (Expected)\n"); printf ("parent 1 = 2\n"); printf ("parent 2 = 3\n"); printf ("parent 3 = NIL\n"); printf ("parent 4 = 1\n"); printf ("parent 5 = 2\n"); printf ("parent 6 = 3\n"); printf ("parent 7 = 8\n"); printf ("parent 8 = 6\n"); printf ("parent 9 = 8\n"); printf ("parent 10 = 9\n"); printf ("The parent vertex of each vertex after BFS (test_two, 3)\n"); for (int i = 1; i <= getOrder (test_two); ++i) { printf ("parent %d = ", i); getParent (test_two, i) == -1 ? printf ("NIL\n") : printf ("%d\n", getParent (test_two, i)); } printf ("The size of test_two is: Expected = 24, Output = %d\n", getSize (test_two)); makeNull (test_two); printf ("The size of test_two after makeNull (test_two) is "); printf ("Expected = 0, Output =%d\n", getSize (test_two)); freeGraph (&test_one); freeGraph (&test_two); }