示例#1
0
文件: closest.c 项目: aurom/VACode
pairs* closest(point* P[],int size)
{
	if(size <= 3)
		return bforce(P, size);

	int lvertical = (int) size/2;
	pairs* CL = closest(P,Y,lvertical);
	pairs* CR = closest(P+lvertical,Y+lvertical,size -lvertical);
	if(CL->dist < CR->dist)
		return CL;
	else
		return CR;
}
示例#2
0
float closest(point *A,long long N,point *u,point *v)
{
	if(N<3)
	{
		return bruteForce(A,N,u,v);
	}

	long long mid=N/2;
	point midPoint = A[mid];
	point a,b,r,s;
	long long i,j;


	double dl = closest(A, mid,&a,&b);
    double dr = closest(A + mid, N-mid,&r,&s);
    double d = min(dl, dr);
    if(dl<dr)
    {
    	u[0].x=a.x;u[0].y=a.y;
    	v[0].x=b.x;v[0].y=b.y;
    }
    else
    {
    	u[0].x=r.x;u[0].y=r.y;
    	v[0].x=s.x;v[0].y=s.y;
    }

    point strip[N];
    j=0;
    for (i = 0; i < N; i++)
        if (abs(A[i].x - midPoint.x) < d)
        {
            strip[j] = A[i];
            j++;
        }

    // return min(d, stripClosest(strip, j, d) );
    double minStrip = stripClosest(strip,j,d,&a,&b);
    if(d<minStrip)
    {
    	return d;
    }
    else
    {
    	u[0].x=a.x;u[0].y=a.y;
    	v[0].x=b.x;v[0].y=b.y;
    	return minStrip;
    }
}
void closest(struct node *t,int start,int *distance)
{
	if (!t->left && !t->right)
	{
		if (*distance == -1)
			*distance = start;
		else if (start < *distance)
			*distance = start;
		return;
	}
	if (t->left)
		closest(t->left,start+1,distance);
	if (t->right)
	    closest(t->right,start+1,distance);
}
示例#4
0
文件: map.cpp 项目: Jinxit/dijkstra
    void map::precompute(const std::vector<point>& points)
    {
        for (auto& p1 : points)
        {
            auto start = closest(p1);
            std::vector<point const*> goals;
            goals.reserve(points.size());
            // find the closest for each goal
            std::transform(points.begin(), points.end(),
                           std::back_inserter(goals),
                           [&](const point& x) { return closest(x); });

            explore(start, goals);
        }
    }
示例#5
0
 bool sphereVsCuboid (const Vector3& sphere_center,
                      double sphere_radius,
                      const Vector3& cuboid_center,
                      const Vector3& cuboid_size)
 {
     Vector3 minBox = cuboid_center - cuboid_size;
     Vector3 maxBox = cuboid_center + cuboid_size;
     double x, y, z;
     
     if (sphere_center.x <= minBox.x)
         x = minBox.x;
     else if (sphere_center.x >= maxBox.x)
         x = maxBox.x;
     else
         x = sphere_center.x;
     
     if (sphere_center.y <= minBox.y)
         y = minBox.y;
     else if (sphere_center.y >= maxBox.y)
         y = maxBox.y;
     else
         y = sphere_center.y;
     
     if (sphere_center.z <= minBox.z)
         z = minBox.z;
     else if (sphere_center.z >= maxBox.z)
         z = maxBox.z;
     else
         z = sphere_center.z;
     
     Vector3 closest(x, y, z);
     return (closest.isDistanceLessThan(sphere_center, sphere_radius));
 }
示例#6
0
Array<TV> SurfacePins::closest_points(Array<const TV> X) {
  update_position(X,false);
  Array<TV> closest(particles.size(),uninit);
  for (int i=0;i<particles.size();i++)
    closest[i] = X[particles[i]]-info[i].phi*info[i].normal;
  return closest;
}
示例#7
0
int main()
{
        int i;
        point a, b;
 
        point pts  = malloc(sizeof(point_t) * NP);
        point* s_x = malloc(sizeof(point) * NP);
        point* s_y = malloc(sizeof(point) * NP);
 
        for(i = 0; i < NP; i++) {
                s_x[i] = pts + i;
                pts[i].x = 100 * (double) rand()/RAND_MAX;
                pts[i].y = 100 * (double) rand()/RAND_MAX;
        }
 
/*      printf("brute force: %g, ", sqrt(brute_force(s_x, NP, &a, &b)));
        printf("between (%f,%f) and (%f,%f)\n", a->x, a->y, b->x, b->y);        */
 
        memcpy(s_y, s_x, sizeof(point) * NP);
        qsort(s_x, NP, sizeof(point), cmp_x);
        qsort(s_y, NP, sizeof(point), cmp_y);
 
        printf("min: %g; ", sqrt(closest(s_x, NP, s_y, NP, &a, &b)));
        printf("point (%f,%f) and (%f,%f)\n", a->x, a->y, b->x, b->y);
 
        /* not freeing the memory, let OS deal with it.  Habit. */
        return 0;
}
// Driver program to test above functions
int main()
{
    Point P[] = {{0,0}, {0,1}, {100,45}, {2,3}, {9,9}};
    int n = sizeof(P) / sizeof(P[0]);
    printf("The smallest distance is %f ", closest(P, n));
    return 0;
}
示例#9
0
TEST_F(BoxTests, ClosestVertexIsDirectlyToLeftWhenInLeftRegion)
{
	Box b(Point(-6, -9), 5, 7);
	std::auto_ptr<OrderedPair> closest(b.getClosestVertex(Point(-5.5, -10.7)));

	EXPECT_EQ(Point(-6, -10.7), *closest);
}
示例#10
0
// Driver program to test above functions
int main()
{
    Point P[] = {{2, 3}, {12, 30}, {40, 50}, {5, 1}, {12, 10}, {3, 4}};
    int n = sizeof(P) / sizeof(P[0]);
    printf("The smallest distance is %f ", closest(P, n));
    return 0;
}
示例#11
0
TEST_F(BoxTests, ClosestVertexIsDirectlyToTheRightWhenInRightRegion)
{
	Box b(Point(1, 5), 3, 2);
	std::auto_ptr<OrderedPair> closest(b.getClosestVertex(Point(3.8, 3.5)));

	EXPECT_EQ(Point(4, 3.5), *closest);
}
示例#12
0
    bool TimeMap::isTimestepInFreqSequence (size_t timestep, size_t start_timestep, size_t frequency, bool years) const {
        bool timestep_right_frequency = false;
        const std::vector<size_t>& timesteps = (years) ? getFirstTimestepYears() : getFirstTimestepMonths();

        std::vector<size_t>::const_iterator ci_timestep = std::find(timesteps.begin(), timesteps.end(), timestep);
        std::vector<size_t>::const_iterator ci_start_timestep = std::find(timesteps.begin(), timesteps.end(), start_timestep);

        //Find new start_timestep if the given one is not a value in the timesteps vector
        bool start_ts_in_timesteps = false;
        if (ci_start_timestep != timesteps.end()) {
            start_ts_in_timesteps = true;
        } else if (ci_start_timestep == timesteps.end()) {
            size_t new_start = closest(timesteps, start_timestep);
            if (0 != new_start) {
                ci_start_timestep = std::find(timesteps.begin(), timesteps.end(), new_start);
                start_ts_in_timesteps = true;
            }
        }


        if (start_ts_in_timesteps) {
            //Pick every n'th element, starting on start_timestep + (n-1), that is, every n'th element from ci_start_timestep - 1 for freq n > 1
            if (ci_timestep >= ci_start_timestep) {
                int dist = std::distance( ci_start_timestep, ci_timestep ) + 1;
                if ((dist % frequency) == 0) {
                    timestep_right_frequency = true;
                }
            }
        }

        return timestep_right_frequency;
    }
示例#13
0
EyeCalibration::CalibrationPoint EyeCalibration::getClosestPoint(CalibrationPoint a, CalibrationPoint b, CalibrationPoint point, bool segmentClamp) {
	int ap_x = point.x() - a.x();
	int ap_y = point.y() - a.y();

	int ab_x = b.x() - a.x();
	int ab_y = b.y() - a.y();

	float ab2 = ab_x*ab_x + ab_y*ab_y;
	float ap_ab = ap_x*ab_x + ap_y*ab_y;

	float t = ap_ab / ab2;

	if (segmentClamp) {
		if (t < 0.f) t = 0.f;
		else if (t > 1.f) t = 1.f;
	}

	int closest_x = a.x() + ab_x * t;
	int closest_y = a.y() + ab_y * t;

	osg::Vec3 ab_ray = b.ray() - a.ray();
	osg::Vec3 ray = a.ray() + ab_ray * t;

	CalibrationPoint closest(closest_x, closest_y, ray);
	return closest;
}
示例#14
0
TEST_F(BoxTests, ClosestPointIsBottomRightCornerWhenInBottomRightRegion)
{
	Box b(OrderedPair(-1, 1), 2, 2);
	OrderedPair p(3, -4);

	std::auto_ptr<OrderedPair> closest(b.getClosestVertex(p));
	EXPECT_EQ(OrderedPair(1, -1), *closest);
}
示例#15
0
TEST_F(BoxTests, ClosestVertexIsDirectlyAboveWhenInTopInnerRegion)
{
	Box b(Point(-1, 1), 2, 2);
	std::auto_ptr<OrderedPair> closest(b.getClosestVertex(Point(0.5, 0.6)));

	EXPECT_EQ(Point(0.5, 1), *closest);

}
示例#16
0
TEST_F(BoxTests, ClosestPointIsTopLeftCornerWhenInTopLeftRegion)
{
	Box b(OrderedPair(-1, 1), 2, 2);
	OrderedPair p(-2, 5);

	std::auto_ptr<OrderedPair> closest(b.getClosestVertex(p));
	EXPECT_EQ(Point(-1, 1), *closest);
}
示例#17
0
bool MsqLine::intersect( const MsqLine& other, double& param, double epsilon ) const
{
  if (!closest( other, param ))
    return false;
  Vector3D p1 = point(param);
  Vector3D p2 = other.point(other.closest(p1));
  return (p1 - p2).length_squared() < epsilon*epsilon;
}
示例#18
0
void KJSeeker::paint(QPainter *p, const QRect &)
{
	closest();
	QPixmap *pixmap = toPixmap(g);
	pixmap->setMask(barModeMask);
	bitBlt(p->device(), rect().topLeft().x(), rect().topLeft().y(),
		pixmap, 0, 0, rect().width(), rect().height(), Qt::CopyROP);
}
int get_closest(struct node *t)
{
	int distance = -1;
		if (t)
			closest(t, 0, &distance);
		else return -1;
	return distance + 1;
}
        void sample(double U1, double U2, double& PE, double& PT)
        {
            PE=photon_energy_sampler.sample(U1);

            auto result=photon_theta_samplers.lookup(PE);
            auto theta_sampler=result.closest(PE);
            theta_sampler->sample(U2, PT);
        }
示例#21
0
TEST_F(BoxTests, ClosestVertexIsDirectlyBelowWhenInBottomInnerRegion)
{
	Box b(Point(2, 2), 2, 2);
	std::auto_ptr<OrderedPair> closest(b.getClosestVertex(Point(2.5, 0.4)));

	EXPECT_EQ(Point(2.5, 0), *closest);

}
void closest(struct KD_TREE *tree, struct Node *u, int k, struct Point *x, int h[], int *mndist) {
	if (u == NULL || heuristic(tree, h) >= *mndist)
		return ;
	int dist = pt_dist(&(u->pid), x), old;
	*mndist = min(*mndist, dist), old = h[k];
	if (x->d[k] < u->pid.d[k]) {    		
		closest(tree, u->lson, (k+1)%tree->kD, x, h, mndist);
		h[k] = abs(x->d[k] - u->pid.d[k]);
		closest(tree, u->rson, (k+1)%tree->kD, x, h, mndist);
		h[k] = old;
	} else {
		closest(tree, u->rson, (k+1)%tree->kD, x, h, mndist);
		h[k] = abs(x->d[k] - u->pid.d[k]);
		closest(tree, u->lson, (k+1)%tree->kD, x, h, mndist);
		h[k] = old;
	}
}
示例#23
0
文件: map.cpp 项目: Jinxit/dijkstra
    path& map::find(const point& start_approx, const point& goal_approx)
    {
        auto start = closest(start_approx);
        auto goal = closest(goal_approx);

        // if it already exists: it's memoized, return it
        auto it = paths.find(std::make_pair(start, goal));
        if (it != paths.end())
        {
            return it->second;
        }

        // otherwise do some exploring
        explore(start, {goal});

        // entry should now exist
        return paths[std::make_pair(start, goal)];
    }
示例#24
0
 //NOTE: For now, we assume OBB max = -min.
 bool operator()(const shape::Box& a, const shape::Sphere& b)const{
     auto pos = pb_.pos_;
     auto extent = pa_.size_ * a.extent() + feather_;
     clam::Vec3d closest(
         std::min(std::max(pos[0], -extent[0]), extent[0]),
         std::min(std::max(pos[1], -extent[1]), extent[1]),
         std::min(std::max(pos[2], -extent[2]), extent[2])
     );
     return (closest - pos).length2() < sqr(pb_.size_ * b.radius() + feather_);
 }
示例#25
0
文件: closest.c 项目: aurom/VACode
int main(int argc, char const *argv[])
{	
	point* pares[100];
	int size = 100 
	srand(time(NULL));
	int i;
	for (i = 0; i < size; i++) {
		int x = rand();
		int y = rand();
		pares[i] = newpoint(x, y);
	}
	otro_y = copia(pares, size)
	qsort(pares,size,sizeof(point), compareX);
	qsort(pares,size,sizeof(point), compareY);
	pairs* min_x = closest(pares, size);
	pairs* min_y = closest(pares, size);
	pairs* min_abs =  min_x->dist < min_y->dist ? min_x : min_y;
	printpairs (min_abs);
	return 0;
}
示例#26
0
/** rayTrace **/
intensity_t rayTrace(scene_t *scene, point_t base, vector_t unitDir,
                 double total_dist, entity_t *self) {
  intensity_t intensity = ((intensity_t){0, 0, 0});
  entity_t * closestEnt;
  hitinfo_t * hit = malloc(sizeof(hitinfo_t));

  closestEnt = closest(scene, base, unitDir, self, hit);
  if (closestEnt == NULL) {
    free(hit);
    return (intensity);
  }

  total_dist += hit->distance;

  window_t * window = ((window_t *)(scene->window->entDerived));
  sobj_t   * sobj   = ((sobj_t   *)(closestEnt->entDerived));

  intensity = ((tuple_t){window->ambient.x * sobj->color.r,
                         window->ambient.y * sobj->color.g,
                         window->ambient.z * sobj->color.b});

  intensity_t light = lighting(scene, closestEnt, hit);
  intensity.x += light.x;
  intensity.y += light.y;
  intensity.z += light.z;

  intensity.x /= 255;
  intensity.y /= 255;
  intensity.z /= 255;
  
  intensity.x /= total_dist;
  intensity.y /= total_dist;
  intensity.z /= total_dist;

  sobj_t * closestSobj = closestEnt->entDerived;
  if (length(((tuple_t)(closestSobj->reflective))) != 0) {
    vector_t U = scale(unitDir, -1);
    vector_t N = hit->normal;
    vector_t V = unitize(add(scale(N, (2 * dot(U, N))), unitDir));

    intensity_t reflection;
    reflection = rayTrace(scene, hit->hitpoint, V, total_dist, closestEnt);

    multiply(reflection, closestSobj->reflective);
    intensity = add(intensity, reflection);
  }


  free(hit);
  return (intensity);

} /* End rayTrace */
示例#27
0
Point2I GFXDevice::closestRes(Point2I &res)
{
   Point2I closest(0,0);
   RES_VECTOR::iterator i = resVector.begin();
   for ( ; i != resVector.end(); i++)
   {
      if ( abs(res.x-(*i).res.x) <= abs(res.x-closest.x) &&
              abs(res.y-(*i).res.y) <= abs(res.y-closest.y)   )
            closest = (*i).res;
   }
   if ( !closest.x ) return ( res );
   return ( closest );
}
示例#28
0
文件: R3Box.C 项目: acplus/peptalk
const R3Point R3Box::
ClosestPoint(const R3Point& point) const
{
    // Return closest point in box
    R3Point closest(point);
    if (closest.X() < XMin()) closest[RN_X] = XMin();
    else if (closest.X() > XMax()) closest[RN_X] = XMax();
    if (closest.Y() < YMin()) closest[RN_Y] = YMin();
    else if (closest.Y() > YMax()) closest[RN_Y] = YMax();
    if (closest.Z() < ZMin()) closest[RN_Z] = ZMin();
    else if (closest.Z() > ZMax()) closest[RN_Z] = ZMax();
    return closest;
}
示例#29
0
TEST_F(BoxTests, ClosestPointIsOnEdgeDirectlyAboveWhenBelow)
{
	Box b(OrderedPair(-1, 1), 2, 2);
	OrderedPair p(0, -2);

	std::auto_ptr<OrderedPair> closest(b.getClosestVertex(p));
	EXPECT_EQ(OrderedPair(0, -1), *closest);
	
	p = OrderedPair(-0.5, -2);

	std::auto_ptr<OrderedPair> closest2(b.getClosestVertex(p));
	EXPECT_EQ(OrderedPair(-0.5, -1), *closest2);
}
示例#30
0
TEST_F(BoxTests, ClosestPointIsOnEdgeDirectlyHorizontalAndShit)
{
	Box b(OrderedPair(-1, 1), 2, 2);
	OrderedPair p(10, 0);

	std::auto_ptr<OrderedPair> closest(b.getClosestVertex(p));
	EXPECT_EQ(OrderedPair(1, 0), *closest);
	
	p = OrderedPair(10, 0.5);

	std::auto_ptr<OrderedPair> closest2(b.getClosestVertex(p));
	EXPECT_EQ(OrderedPair(1, 0.5), *closest2);
}