示例#1
0
/*
 * update classification (assignment) by calculated mean vectors.
 */
static void
update_r(myvector inputs, int dim, int N, int k, myvector mean, int *r)
{
	int			i, klass;

	for (i = 0; i < N; i++)
	{
		float8		dist;
		float8		curr_dist;
		int			curr_klass;

		curr_dist = calc_distance(&inputs[i * dim], &mean[0], dim);
		curr_klass = 0;
		/*
		 * Search the nearst mean point.
		 */
		for (klass = 1; klass < k; klass++)
		{
			dist = calc_distance(&inputs[i * dim], &mean[klass * dim], dim);
			if (dist < curr_dist)
			{
				curr_dist = dist;
				curr_klass = klass;
			}
		}
#ifdef KMEANS_DEBUG
elog(LOG, "r[%d] = %d -> %d", i, r[i], curr_klass);
#endif
		r[i] = curr_klass;
	}
}
示例#2
0
    //------------------------------------------------------------------------
    bool scale_ctrl_impl::on_mouse_button_down(double x, double y)
    {
        inverse_transform_xy(&x, &y);

        double xp1 = m_xs1 + (m_xs2 - m_xs1) * m_value1;
        double xp2 = m_xs1 + (m_xs2 - m_xs1) * m_value2;
        double ys1 = m_y1  - m_border_extra / 2.0;
        double ys2 = m_y2  + m_border_extra / 2.0;
        double yp = (m_ys1 + m_ys2) / 2.0;

        if(x > xp1 && y > ys1 && x < xp2 && y < ys2)
        {
            m_pdx = xp1 - x;
            m_move_what = move_slider;
            return true;
        }

        if(x < xp1 && calc_distance(x, y, xp1, yp) <= m_y2 - m_y1)
        {
            m_pdx = xp1 - x;
            m_move_what = move_value1;
            return true;
        }

        if(x > xp2 && calc_distance(x, y, xp2, yp) <= m_y2 - m_y1)
        {
            m_pdx = xp2 - x;
            m_move_what = move_value2;
            return true;
        }


        return false;
    }
示例#3
0
    //------------------------------------------------------------------------
    bool gamma_ctrl_impl::on_mouse_button_down(double x, double y)
    {
        inverse_transform_xy(&x, &y);
        calc_points();

        if(calc_distance(x, y, m_xp1, m_yp1) <= m_point_size + 1)
        {
            m_mouse_point = 1;
            m_pdx = m_xp1 - x;
            m_pdy = m_yp1 - y;
            m_p1_active = true;
            return true;
        }

        if(calc_distance(x, y, m_xp2, m_yp2) <= m_point_size + 1)
        {
            m_mouse_point = 2;
            m_pdx = m_xp2 - x;
            m_pdy = m_yp2 - y;
            m_p1_active = false;
            return true;
        }

        return false;
    }
示例#4
0
fixed
OLCSISAT::calc_score() const
{
  // build convex hull from solution
  SearchPointVector spv;
  for (unsigned i = 0; i < num_stages; ++i)
    spv.push_back(solution[i]);

  prune_interior(spv);

  // now add leg distances making up the convex hull
  fixed G = fixed_zero;

  if (spv.size() > 1) {
    for (unsigned i = 0; i + 1 < spv.size(); ++i)
      G += spv[i].distance(spv[i + 1].get_location());

    // closing leg (end to start)
    G += spv[spv.size() - 1].distance(spv[0].get_location());
  }

  // R distance (start to end)
  const fixed R = solution[0].distance(solution[num_stages - 1].get_location());

  // V zigzag-free distance
  const fixed V = G - R;

  // S = total distance
  const fixed S = calc_distance();

  return apply_handicap((V + fixed(3) * S) * fixed(0.00025));
}
示例#5
0
文件: homework11.c 项目: cwj/myNCCL
int main(void)
{
	int i,j
	double 	distance,max_distance;
	point point_t[5];

	for (i=0 ;i<5 ;i++ )
	{
		scanf("%d,%d",&(point_t[i].x),&(point_t[i].y));
	}
	
	max_distance=0;
	for (i=0 ;i<5 ;i++ )
	{
		for (j=0 ;j<5 ;j++ )
		{
			distance=calc_distance(point_t[i],point_t[j]);
			if (distance>max_distance)
			{
				max_distance=distance;
				printf("%d,%d and %d,%d",point_t[i].x,point_t[i].y,point_t[j].x,point_t[j].y);
			}
		}
	}

	return 0;
}
示例#6
0
int
check_location(pam_handle_t *pamh,
               struct options *opts,
               char *location_string,
               struct locations *geo)
{
    struct locations *list;
    struct locations *loc;
    double distance;

    list = loc = parse_locations(pamh, opts, location_string);

    while (list) {
        if (list->country == NULL) {
            if (strcmp(geo->country, "UNKNOWN") == 0) {
                list = list->next;
                continue;
            }
            if (opts->is_city_db) {
                distance = calc_distance(list->latitude, list->longitude,
                                         geo->latitude, geo->longitude);
                if (distance <= list->radius) {
                    pam_syslog(pamh, LOG_INFO, "distance(%.3f) < radius(%3.f)",
                               distance, list->radius);
                    sprintf(location_string, "%.3f {%f,%f}", distance, geo->latitude, geo->longitude);
                    free_locations(loc);
                    return 1;
                }
            }
            else
                pam_syslog(pamh, LOG_INFO, "not a city db edition, ignoring distance entry");
        }
        else {
            if (opts->debug)
                pam_syslog(pamh, LOG_INFO, "location: (%s,%s) geoip: (%s,%s)",
                           list->country, list->city, geo->country, geo->city);

            if (
                (list->country[0] == '*' ||
                 strcmp(list->country, geo->country) == 0)
                &&
                (list->city[0]    == '*' ||
                 strcmp(list->city,    geo->city   ) == 0)
            )
            {
                if (opts->debug)
                    pam_syslog(pamh, LOG_INFO, "location [%s,%s] matched: %s,%s",
                               geo->country, geo->city,
                               list->country, list->city);
                sprintf(location_string, "%s,%s", geo->country, geo->city);
                free_locations(loc);
                return 1;
            }
        }
        list = list->next;
    }
    if (loc) /* may be NULL */
        free_locations(loc);
    return 0;
}
示例#7
0
/*
    This moves an individual fly torwards another
*/
static int 
move_fly(ffly *fly, ffly *old, const size_t nparams,
         const double alpha, const double gamma, const double mins[], const double maxs[])
{
    size_t i = 0;
    int moved = 0;
    const double beta0 = BETA_ZERO;	//base attraction

    if (fly->val > old->val)
    {
        //get the distance to the other fly
        double r = calc_distance(fly, old, nparams);

        //determine attractiveness with air density [gamma]
        double beta = beta0 * exp((-gamma) * pow(r, 2.0));
				beta = (beta < BETA_MIN) ? BETA_MIN : beta;

        //adjust position with a small random step
        for (i = 0; i < nparams; i++)
        {
            double val = fly->params[i] + (beta * (old->params[i] - fly->params[i])) + (alpha * (drand48() - 0.5));

            //keep within bounds
            fly->params[i] = (val < mins[i]) ? mins[i] : (val > maxs[i]) ? maxs[i] : val;
        }
        moved = 1;
    }
    return moved;
};
示例#8
0
void points::set_max_r(void) {
	for (int i = 0; i < NUM; i++) {
		int to = Points[i].get_to_idx();
		double dist = calc_distance(Points[i], Points[to]);
		if (max_r < dist) max_r = dist;
	}
}
示例#9
0
// if has a player in proximity, return this player's index
// else return -1
int find_best_player_to_pass(int direction){
    int i;
    extern int ball[2], kickpower;
    extern int teaminfo[NUM_PLAYERS][5];
    // distance is measured between me and the other player, location is the coordinates of the other player
    int distance, location[2]; 
    int best_thus_far = -1, best_dribb = -1;
    
    for (i=0; i<NUM_PLAYERS; i++) {
        location[0] = teaminfo[i][INFO_XCOORD]; // xcoord
        location[1] = teaminfo[i][INFO_YCOORD]; // ycoord
        distance = calc_distance(player.newlocation, location);
        if (direction == KICKLEFT) {
            if (location[0] < player.newlocation[0] && 
                distance <= kickpower*2 && best_dribb < teaminfo[i][INFO_DRIBB]) {
                //get the best player with highest dribbling skill
                best_thus_far = i;
                best_dribb = teaminfo[i][INFO_DRIBB]; 
            }
        }
        if (direction == KICKRIGHT) {
            if (location[0] > player.newlocation[0] &&
                distance <= kickpower*2 && best_dribb < teaminfo[i][INFO_DRIBB]) {
                //get the best player with highest dribbling skill
                best_thus_far = i;
                best_dribb = teaminfo[i][INFO_DRIBB]; 
            }
        }
    }
    
    return best_thus_far;
}
void waypointspath_control::waypoints_autopath(){
	commcount = coilchange_track = jiggle_track= 0;
	qDebug() << "Waypoints control thread running";
	//qDebug() << " Command entered : " << command;
	command = start_command;
	command_x = command_y = start_command;
	TimeDelay(1000);
	if(w->clicks)
		{
		CString comport = "\\\\.\\COM4";
		w->serialHandle = CreateFile(comport, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
		if(w->serialHandle == INVALID_HANDLE_VALUE)
			emit autopath_finished();
		DCB serialParams = { 0 };
		serialParams.DCBlength = sizeof(serialParams);
		GetCommState(w->serialHandle, &serialParams);
		serialParams.BaudRate = 9600;
		serialParams.ByteSize = 8;
		serialParams.StopBits = ONESTOPBIT;
		serialParams.Parity = PARITY_NONE;
		GetCommState(w->serialHandle, &serialParams);

		// Set timeouts
		COMMTIMEOUTS timeout = { 0 };
		timeout.ReadIntervalTimeout = 50;
		timeout.ReadTotalTimeoutConstant = 50;
		timeout.ReadTotalTimeoutMultiplier = 50;
		timeout.WriteTotalTimeoutConstant = 50;
		timeout.WriteTotalTimeoutMultiplier = 10;
		SetCommTimeouts(w->serialHandle, &timeout);
		COM = w->COM;
		for(int l = 1; l <= cycles_no; l++){
		for(wayptno = 0; wayptno < w->clicks; wayptno++){
			prevdist = calc_distance(w->waypoints[wayptno]);
			xdist = std::abs(COM.x - w->waypoints[wayptno].x * conv_factor[0]);
			ydist = std::abs(COM.y - w->waypoints[wayptno].y * conv_factor[1]);
			prevangle = w->angle;
			COMprev = w->COM;
			while(wayptno < w->clicks && sqrt(pow(std::abs(w->COM.x - w->waypoints[wayptno].x),2) + pow(std::abs(w->COM.y - w->waypoints[wayptno].y),2)) > tolerance)
				{
					//data_command = determine_command(COM,waypoints,l);
					commcount++;
					//data_command[3] = 0xaa;
					determine_command(wayptno);
					//WriteFile(w->serialHandle,(LPCVOID)data_command, toBeWritten, written,NULL);
					qDebug() << "Check state waypoint control : "<< WriteFile(w->serialHandle,(LPVOID)data_command, toBeWritten, written,NULL);
					TimeDelay(1000);
					if(!WriteFile(w->serialHandle,(LPCVOID)w->prevsent,toBeWritten, written, NULL));
						emit autopath_finished();
					TimeDelay(100);
					//for(int i = 0; i < 7; i++)
						//data_command[i] = 0x00;
				}		
		}
		}
	}
	CloseHandle(w->serialHandle);
	emit autopath_finished();
}
示例#11
0
Pixel rad_dot(const Ink *inky, const SHORT x, const SHORT y)
/* This one is so slow don't bother to have a hline routine... */
{
	if(vl.rgr == 0) /* can't be zero */
		vl.rgr = 1;
	return(dfrom_range(calc_distance(x,y,vl.rgc.x,vl.rgc.y)%vl.rgr,
			vl.rgr,x,y,inky->dither));
}
示例#12
0
void MimRec::calcForTrainingData(const cv::Mat& trackingPoints, const std::string& outputFilePath, const cv::Mat fittingImage)
{
	currentPoints = trackingPoints;
	areaFace();
	calc_distance();
	normalizeDistances();
	calcIntensityScoring(outputFilePath, fittingImage);
}
示例#13
0
double get_sub_min_distance(
    struct point* points,
    int dest_point_count,
    int* dest_point_index,
    int point_count,
    int light_range
)
{
    int i;
    char point_is_added[point_count];
    double low_cost[point_count];
    memset(point_is_added, 0, point_count);

    for (i = 0; i < point_count; ++i) {
        low_cost[i] = INF;
    }

    int num = 0;
    int e = dest_point_index[0];
    point_is_added[e] = 1;
    double distance = 0;

    while (++num < dest_point_count) {
        double min_distance = INF;
        int min_edge = -1;
        int idx;

        for (idx = 0; idx < dest_point_count; ++idx) {
            int i = dest_point_index[idx];

            if (point_is_added[i]) {
                continue;
            }

            double cur_cost = calc_distance(points + i,
                    points + e, light_range);

            if (cur_cost < low_cost[i]) {
                low_cost[i] = cur_cost;
            }

            if (low_cost[i] < min_distance) {
                min_edge = i;
                min_distance = low_cost[i];
            }

            LOG("i=%d e=%d low_cost:%lf\n", i, e, low_cost[i]);
        }

        distance += min_distance;
        e = min_edge;
        point_is_added[e] = 1;
        LOG("%d cur distance:%lf\n", e, distance);
        //++num;
    }

    return distance;
}
示例#14
0
void calc_all_distances(int dim, int n, int k, double *X, double *centroid, double *distance_output)
  {
    for (int ii = 0; ii < n; ii++) // for each point
      for (int jj = 0; jj < k; jj++) // for each cluster
        {
         // calculate distance between point and cluster centroid
          distance_output[ii*k + jj] = calc_distance(dim, &X[ii*dim], &centroid[jj*dim]);
        }
  }
示例#15
0
double points::config_score(void) {
	double score = 0.0;
	for (int i = 0; i < NUM; i++) {
		int to = Points[i].get_to_idx();
		double dist = calc_distance(Points[i], Points[to]);
		score += dist;
	}
	cout << "score: " << score << endl;
	return score;
}
示例#16
0
/*
 * Evaluation function. kmeans tries to minimize value of this function.
 */
static float8
J(myvector inputs, int dim, int N, int k, myvector mean, int *r)
{
	int		i;
	float8	sum = 0.0;

	for (i = 0; i < N; i++)
	{
		sum += calc_distance(&inputs[i * dim], &mean[r[i] * dim], dim);
	}
	return sum;
}
fixed
XContestTriangle::calc_score() const
{
  // DHV-XC: 2.0 or 1.75 points per km for FAI vs non-FAI triangle
  // XContest: 1.4 or 1.2 points per km for FAI vs non-FAI triangle

  const fixed score_factor = is_dhv?
    (is_fai? fixed(0.002): fixed(0.00175))
    :(is_fai? fixed(0.0014): fixed(0.0012));

  return apply_handicap(calc_distance()*score_factor);
}
示例#18
0
    double path_length(VertexSource& vs, unsigned path_id = 0)
    {
        double len = 0.0;
        double start_x = 0.0;
        double start_y = 0.0;
        double x1 = 0.0;
        double y1 = 0.0;
        double x2 = 0.0;
        double y2 = 0.0;
        bool first = true;

        unsigned cmd;
        vs.rewind(path_id);
        while(!is_stop(cmd = vs.vertex(&x2, &y2)))
        {
            if(is_vertex(cmd))
            {
                if(first || is_move_to(cmd))
                {
                    start_x = x2;
                    start_y = y2;
                }
                else
                {
                    len += calc_distance(x1, y1, x2, y2);
                }
                x1 = x2;
                y1 = y2;
                first = false;
            }
            else
            {
                if(is_close(cmd) && !first)
                {
                    len += calc_distance(x1, y1, start_x, start_y);
                }
            }
        }
        return len;
    }
示例#19
0
void SnakeClass::Snake_algorithm()
{
    CPoint temp;
    Snake_points=pos;
    double max_curvature;
    int i;
    for(i=0; i<snake_point_num; i++)
        avg_distance+=calc_distance(i, Snake_points[i]);
    avg_distance=avg_distance/(double)snake_point_num;
    max_curvature=-1000000000.0;
    for(i=0; i<snake_point_num; i++)
    {
        temp=calc_min_energy(i,Snake_points[i],avg_distance);
        if(temp!=Snake_points[i]
                &&temp!=Snake_points[(i-1+snake_point_num)%snake_point_num]
                &&temp!=Snake_points[(i+1+snake_point_num)%snake_point_num])
        {
            Snake_points[i]=temp;
        }
        curvature[i]=curvature_ene(i, Snake_points[i]);
        if(max_curvature<curvature[i])
            max_curvature=curvature[i];
    }
    avg_distance=0.0;

    //归一化各点曲率能量
    for(i=0; i<snake_point_num; i++)
        curvature[i]=curvature[i]/max_curvature;

    for(i=0; i<snake_point_num; i++)
    {
        avg_distance+=calc_distance(i, Snake_points[i]);
        if(curvature[i]>threshold_curvature
                &&curvature[i]>curvature[(i+1)%snake_point_num]
                &&curvature[i]>curvature[(i-1+snake_point_num)%snake_point_num]
                &&grad_mag[i]>threshold_grad)
            *(beta+i)=0;
    }
}
示例#20
0
    //------------------------------------------------------------------------
    bool vcgen_contour::calc_miter(const vertex_dist& v0, 
                                   const vertex_dist& v1, 
                                   const vertex_dist& v2)
    {
        double dx1, dy1, dx2, dy2;

        dx1 = m_signed_width * (v1.y - v0.y) / v0.dist;
        dy1 = m_signed_width * (v1.x - v0.x) / v0.dist;

        dx2 = m_signed_width * (v2.y - v1.y) / v1.dist;
        dy2 = m_signed_width * (v2.x - v1.x) / v1.dist;

        double xi;
        double yi;

        if(!calc_intersection(v0.x + dx1, v0.y - dy1,
                              v1.x + dx1, v1.y - dy1,
                              v1.x + dx2, v1.y - dy2,
                              v2.x + dx2, v2.y - dy2,
                              &xi, &yi))
        {
            m_x1 = v1.x + dx1;
            m_y1 = v1.y - dy1;
            return false;
        }
        else
        {
            double d1 = calc_distance(v1.x, v1.y, xi, yi);
            double lim = m_abs_width * m_miter_limit;
            if(d1 > lim)
            {
                d1  = lim / d1;
                m_x1 = v1.x + dx1;
                m_y1 = v1.y - dy1;
                m_x2 = v1.x + dx2;
                m_y2 = v1.y - dy2;

                m_x1 += (xi - m_x1) * d1;
                m_y1 += (yi - m_y1) * d1;
                m_x2 += (xi - m_x2) * d1;
                m_y2 += (yi - m_y2) * d1;
                return true;
            }
            else
            {
                m_x1 = xi;
                m_y1 = yi;
            }
        }
        return false;
    }
示例#21
0
int points::nearest_nei(int curr_idx) {
	double min_dist = 9999.0;
	int nearest_nei_idx;
	for (int i = 0; i < NUM; i++) {
		if (i == curr_idx) continue;
		double dist = calc_distance(Points[i], Points[curr_idx]);
		if (dist < min_dist) {
			min_dist = dist;
			nearest_nei_idx = i;
		}
	}
	if (max_r < min_dist) max_r = min_dist;
	return nearest_nei_idx;
}
示例#22
0
MimRec::MimRec(const TrainingData& neutral)
{
	currentPoints = neutral.getPoints();

	vAUDetectionValue.resize(actionUnits.size());
	vBackPropAUDetectionValue.resize(actionUnits.size());

	areaFace();
	calc_distance();

	neutral_area = areas;
	neutral_distances.swap(current_distances);
	
}
/* Triangle initialization */
static int triangle_init() 
{
	point_2d a, b, c;
	
	// hard coded values for testing :
	X1 = 15;
	Y1 = 15;
	X2 = 23;
	Y2 = 30;
	X3 = 50;
	Y3 = 25;
	
	a = (point_2d){.x = X1, .y = Y1};
	b = (point_2d){.x = X2, .y = Y2};
	c = (point_2d){.x = X3, .y = Y3};
	given_triangle.A = a;
	given_triangle.B = b;
	given_triangle.C = c;
	given_triangle.side_a = calc_distance(&c, &b);
	given_triangle.side_b = calc_distance(&a, &c);
	given_triangle.side_c = calc_distance(&a, &b);
	return is_triangle_possible(&given_triangle);
}
示例#24
0
    //------------------------------------------------------------------------
    bool slider_ctrl_impl::on_mouse_button_down(double x, double y)
    {
        inverse_transform_xy(&x, &y);

        double xp = m_xs1 + (m_xs2 - m_xs1) * m_value;
        double yp = (m_ys1 + m_ys2) / 2.0;

        if(calc_distance(x, y, xp, yp) <= m_y2 - m_y1)
        {
            m_pdx = xp - x;
            m_mouse_move = true;
            return true;
        }
        return false;
    }
示例#25
0
static Errcode make_circle_mask(int tscale0,int tscale1,Pframedat *pfd)
{
Rcel *dest = pfd->dest;
int radius;
int cenx;
int ceny;
(void)tscale0;

	cenx = dest->width/2;
	ceny = dest->height/2;
	radius = (tscale1 * (5+calc_distance(0,0,cenx,ceny)))/SCALE_ONE;
	pj_set_rast(dest,pfd->invert);
	circle(dest,!pfd->invert, cenx, ceny, radius<<1, TRUE);
	return(Success);
}
示例#26
0
 //------------------------------------------------------------------------
 bool rbox_ctrl_impl::on_mouse_button_down(real x, real y)
 {
     inverse_transform_xy(&x, &y);
     unsigned i;
     for(i = 0; i < m_num_items; i++)  
     {
         real xp = m_xs1 + m_dy / 1.3;
         real yp = m_ys1 + m_dy * i + m_dy / 1.3;
         if(calc_distance(x, y, xp, yp) <= m_text_height / 1.5)
         {
             m_cur_item = int(i);
             return true;
         }
     }
     return false;
 }
示例#27
0
bool
AbstractContest::save_solution()
{
  const fixed score = calc_score();
  const bool improved = (score>best_result.score);

  if (improved) {
    best_result.score = score;
    best_result.distance = calc_distance();
    best_result.time = calc_time();
    if (positive(best_result.time))
      best_result.speed = best_result.distance / best_result.time;
    else
      best_result.speed = fixed_zero;
    return true;
  }
  return false;
}
示例#28
0
 //------------------------------------------------------------------------
 bool spline_ctrl_impl::on_mouse_button_down(double x, double y)
 {
     inverse_transform_xy(&x, &y);
     unsigned i;
     for(i = 0; i < m_num_pnt; i++)  
     {
         double xp = calc_xp(i);
         double yp = calc_yp(i);
         if(calc_distance(x, y, xp, yp) <= m_point_size + 1)
         {
             m_pdx = xp - x;
             m_pdy = yp - y;
             m_active_pnt = m_move_pnt = int(i);
             return true;
         }
     }
     return false;
 }
示例#29
0
double calc_total_distance(int dim, int n, int k, double *X, double *centroids, int *cluster_assignment_index)
 // NOTE: a point with cluster assignment -1 is ignored
  {
    double tot_D = 0;
    
   // for every point
    for (int ii = 0; ii < n; ii++)
      {
       // which cluster is it in?
        int active_cluster = cluster_assignment_index[ii];
        
       // sum distance
        if (active_cluster != -1)
          tot_D += calc_distance(dim, &X[ii*dim], &centroids[active_cluster*dim]);
      }
      
    return tot_D;
  }
示例#30
0
bool 
	AIManager::CheckEated(FoodName name, Ogre::Vector3 pos)
{
	for (std::size_t m = 0; m < FoodEatlist[name].size(); m++ )
	{
		AnimalName aniname = FoodEatlist[name][m];
		for (std::size_t i = 0; i < mAnimalList[aniname].size(); i++ )
		{
			if (calc_distance(mAnimalList[aniname][i]->getPosition(), pos) < 20000) //(mAnimalList[aniname][i]->getRadius() + 25))
			{
				delete mAnimalList[aniname][i];
				mAnimalList[aniname].erase(mAnimalList[aniname].begin()+i);
				return true;
			}
		}
	}
	return false;
}