void NBodyWorldApp::pickParticle( Particle2D*& picked ){ Particle2D* screenObjects[256]; // mouse picking double rmax = 2.0d; double r2max = rmax*rmax; Vec2d vmouse; vmouse.set( mouse_begin_x, mouse_begin_y ); UINT mfound = world.map.getObjectsInRect( (float)(vmouse.x - rmax ), (float)(vmouse.y - rmax ), (float)(vmouse.x + rmax ), (float)(vmouse.y + rmax ), &(screenObjects[0]) ); //printf( "mfound %i \n", mfound ); int imin = -1; double r2min = 1e+300; for( int i=0; i<mfound; i++ ){ Particle2D* p = screenObjects[i]; Vec2d d; d.set_sub( p->pos, vmouse ); double r2 = d.norm2(); if( r2 < r2max ){ if( r2 < r2min ){ r2min = r2; imin = i; } } //printf( " r2 %3.3f r2max %3.3f r2min %3.3f imin %i \n", r2, r2max, r2min, imin ); } if( imin >= 0 ){ picked = screenObjects[imin]; }else{ picked = NULL; } //if( world.picked != NULL ) printf( " MOUSE picked (%f,%f) \n", world.picked->pos.x, world.picked->pos.y ); }
int MoleculeWorldApp::pickMol( const Vec2d& mpos ){ int imin = -1; double r2min = 1e+300; for(int i=0; i<world.nMols; i++){ Vec2d d; d.set_sub( mpos, world.pos[i] ); double r2 = d.norm2(); if (r2<r2min){ r2min=r2; imin=i; } } return imin; }