int main() {
    int n, i;
    while(scanf("%d", &n) == 1) {
        for(i = 0; i < n; i++)
            scanf("%lf %lf", &P[i].x, &P[i].y);
        n = convex(n);
        double A = calc_area(n, P);
        double l = 0xfffffff, r = 0, m;
        double x, y;
        A = A*0.5;
        for(i = 0; i < n; i++) {
            m = P[i].y/P[i].x;
            if(m < l)   l = m;
            if(m > r)   r = m;
        }
        while(fabs(l-r) > eps) {
            m = (l+r)*0.5;
            int it = 0, idx = 0;
            Pt pp[2];
            for(i = 0; i < n-1; i++) {
                if(it == 1)
                    H[idx++] = P[i];
                if((m*P[i].x - P[i].y)*(m*P[i+1].x - P[i+1].y) <= 0) {
                    x = (P[i].x*P[i+1].y - P[i].y*P[i+1].x)/
                        (m*(P[i].x - P[i+1].x) - P[i].y + P[i+1].y);
                    y = m*x;
                    if(it < 2) {
                        if(it == 1) {
                            if(fabs(x-pp[0].x) <= eps && fabs(y-pp[0].y) <= eps)
                                continue;
                        }
                        pp[it].x = x, pp[it].y = y;
                        H[idx++] = pp[it];
                        it++;
                    }
                }
            }
            H[idx++] = pp[0];
            double sum = calc_area(idx, H);
            if(fabs(sum-A) <= eps)
                break;
            if(m*H[1].x-H[1].y <= 0) {
                /*puts("left");*/
                if(sum < A)
                    r = m;
                else
                    l = m;
            } else {
                /*puts("right");*/
                if(sum < A)
                    l = m;
                else
                    r = m;
            }
        }
        printf("%.4lf\n", m);
    }
    return 0;
}
int main (int argc, char *argv[])   // accumulative ET (Apr - Jul) for each year [km3]
{
  int i,j,nfiles,ndays;
  int syear,year,month,day,y;
  double tot_area;
  double lat,lon,cellsize,area;
  double prec,evap,runoff,baseflow;
  double airT,sm1,sm2,sm3,swe;
  double evap_year[MAXY];
  char filename[MAXC];
  FILE *fpin,*fplist,*fpout;

  if(argc!=4){
    printf("Usage: %s latlonlist cellsize outfile\n",argv[0]);
    exit(0);
  }
  if((fplist = fopen(argv[1],"r"))==NULL){
    printf("ERROR: can't open %s\n", argv[1]);
    exit(0);
  }
  nfiles=get_length(fplist);
  cellsize=atof(argv[2]);
  for(j=0;j<MAXY;j++){
    evap_year[j]=0;
  }
  tot_area=0;
  for(i=0;i<nfiles;i++){
    fscanf(fplist,"%lf %lf",&lat,&lon);
    area=calc_area(lat,lon,cellsize);
    tot_area+=area;
    sprintf(filename,"/raid2/ymao/other/CA_drought_0.0625/run_vic/model_output/fluxes_1920_20140731_old2014_adjustT_P_pivot1920/fluxes_%.5f_%.5f",lat,lon);
    if((fpin = fopen(filename,"r"))==NULL){
      printf("ERROR: can't open %s\n", filename);
      exit(0);
    }
    ndays=get_length(fpin);
    for(j=0;j<ndays;j++){
      fscanf(fpin,"%d %d %d",&year,&month,&day);
      if(j==0)syear=year;
      fscanf(fpin,"%lf %lf %lf %lf",&prec,&evap,&runoff,&baseflow);
      fscanf(fpin,"%lf %lf %lf %lf %lf",&airT,&sm1,&sm2,&sm3,&swe);
	if(month>=4 && month<=7)  // if Apr-Jul, add it to this water year
	{
		evap_year[year-syear]+=evap*area/1000/1000;
	}
    }
    fclose(fpin);
  }
  fclose(fplist);
  if((fpout = fopen(argv[3],"w"))==NULL){
    printf("ERROR: can't open %s\n", argv[3]);
    exit(0);
  }
  for(y=syear;y<=year;y++){    // print water years 1920-2014
    fprintf(fpout,"%d %.4f\n",y,evap_year[y-syear]);
  }
  fclose(fpout);
  printf("%.1f\n",tot_area);
  return(0);
}
Esempio n. 3
0
void Character::perform_tests(){				//perform all tests
	calc_bit_quads();
	area = calc_area();
	perimeter = calc_perimeter();
	euler = calc_euler_number();
	calc_spatial_moment(); // calcs the horizontal & vertical centroids
	calc_sym();            // calcs horizontal and vertical symmetry
	aspect = calc_aspect_ratio();
}
int get_area(const bmp * image, int columnFrom, int columnTo, int rowFrom, int rowTo) {
	int i, j;
	int color = get_pixel(rowFrom, columnFrom, image);

	for (i = rowFrom; i <= rowTo; i++) {
		for (j = columnFrom; j <= columnTo; j++) {
			if (get_pixel(i, j, image) != color) return 0;
		}
	}

	return calc_area(columnFrom, columnTo, rowFrom, rowTo);
}
Esempio n. 5
0
void console_widget_t::mouse_drag(int x, int y)
{
	/* selection is being performed by dragging the mouse */
	if (!mouse_copy)
		return;

	/* clear old selection */
	invert_area(mouse_sx, mouse_sy, mouse_wx, mouse_wy);
	damage_console(0, 0, console->config.x, console->config.y);

	/* calculate selection area */
	calc_area(x, y);

	/* set new selection */
	invert_area(mouse_sx, mouse_sy, mouse_wx, mouse_wy);
	damage_console(0, 0, console->config.x, console->config.y);

	return;
}
Esempio n. 6
0
inline vector<double> area(const vector<circle> &a) {
	// return the area covered by at least x circlex
	int n = a.size();
	vector<double> ret(n + 1, 0);
	for (int i = 0; i < n; ++i) {
		vector<node> s;
		int part = 0;
		double last;
		for (int j = 0; j < n; ++j)
			get_intersect(a[i], a[j], i < j, s);
		add_interval(s, -PI, PI);
		sort(s.begin(), s.end(), cmp);
		for (int j = 0; j < (int)s.size(); ++j) {
			if (part)
				ret[part] += calc_area(a[i], last, s[j].key);
			part += s[j].value;
			last = s[j].key;
		}
	}
	return ret;
}
Esempio n. 7
0
void ram(void) {
    getInputWaitRelease();
    reset_area();
    random_area(life,1,1,RESX,RESY,40);

    static int nickx=2,nicky=10;
    signed char movy=1;
    static int nickwidth,nickheight;
    static int nickoff=10;
    static char delaytime=10;
    static char speedmode=0;
    static char LCDSHIFTX_EVERY_N=2;
    static char LCDSHIFTY_EVERY_N=2;
    static char ITER_EVERY_N=1;

    lcdClear();
    setExtFont(GLOBAL(nickfont));
    
    nickwidth=DoString(nickx,nicky,GLOBAL(nickname));
    if(nickwidth<50)nickoff=30;
    nickheight=getFontHeight();

    char stepmode=0;
    while (1) {
        draw_area(); // xor life pattern over display content
        lcdDisplay();
        lcdClear();
        // draw_area(); // xor life pattern again to restore original display content
        // Old shift code. Can't handle longer Nicks...
        // if(iter%LCDSHIFT_EVERY_N==0) lcdShift(1,-2,1);
        // if(iter%LCDSHIFT_EVERY_N==0) { nickx=(nickx+1)%100-nickwidth; nicky=(nicky+1)%50;}
        if(iter%LCDSHIFTX_EVERY_N==0) { nickx--; 
        if(nickx<(-1*nickwidth-nickoff))nickx=0; }
        if(iter%LCDSHIFTY_EVERY_N==0) { nicky+=movy;
        if(nicky<1 || nicky>RESY-nickheight) movy*=-1; }
        DoString(nickx,nicky,GLOBAL(nickname));
        DoString(nickx+nickwidth+nickoff,nicky,GLOBAL(nickname));
        if(nickwidth<RESX) DoString(nickx+2*(nickwidth+nickoff),nicky,GLOBAL(nickname));
	char key=stepmode?getInputWait():getInputRaw();
	stepmode=0;
	switch(key) {
	case BTN_ENTER:
	  return;
	case BTN_RIGHT:
	  getInputWaitRelease();
          speedmode=(speedmode+1)%7;
          delaytime=15;
          switch(speedmode) {
            case 0:
              ITER_EVERY_N=1; LCDSHIFTX_EVERY_N=1; LCDSHIFTY_EVERY_N=1; break;
            case 1:
              ITER_EVERY_N=1; LCDSHIFTX_EVERY_N=2; LCDSHIFTY_EVERY_N=2; break;
            case 2:
              ITER_EVERY_N=1; LCDSHIFTX_EVERY_N=4; LCDSHIFTY_EVERY_N=4; break;
            case 3:
              ITER_EVERY_N=2; LCDSHIFTX_EVERY_N=1; LCDSHIFTY_EVERY_N=1; break;
            case 4:
              ITER_EVERY_N=4; LCDSHIFTX_EVERY_N=1; LCDSHIFTY_EVERY_N=1; break;
            case 5:
              ITER_EVERY_N=8; LCDSHIFTX_EVERY_N=1; LCDSHIFTY_EVERY_N=1; break;
            case 6:
              delaytime=5; ITER_EVERY_N=8; LCDSHIFTX_EVERY_N=1; LCDSHIFTY_EVERY_N=1; break;
          }
	  break; 
	case BTN_DOWN:
	  stepmode=1;
	  getInputWaitRelease();
	  break;
	case BTN_LEFT:
	  pattern=(pattern+1)%PATTERNCOUNT;
	case BTN_UP:
	  stepmode=1;
	  reset_area();
	  getInputWaitRelease();
	  break;
	}
        delayms_queue_plus(delaytime,0);
#ifdef SIMULATOR
  fprintf(stderr,"Iteration %d - x %d, y %d \n",iter,nickx,nicky);
#endif
        if(iter%ITER_EVERY_N==0) calc_area(); else ++iter;
    }
    return;
}
int main (int argc, char *argv[])   // average T (Nov 1 - Mar 19) for each year [deg C]
{
  int i,j,k,l,nfiles,ndays, nyear;
  int syear,year,month,day,y;
  double tot_area;
  double lat,lon,cellsize,area;
  double prec,evap,runoff,baseflow;
  double airT,sm1,sm2,sm3,swe;
  double T_year[MAXY];
  int nday_year[MAXY];   // nday_year: count the days in Nov1-Mar19 in each water year
  char filename[MAXC];
  FILE *fpin,*fplist,*fpout;
  double ave_T[MAXMON][MAXDAY];
  double T_anom[MAXY];  // anomaly for each year, within the loop of each file
  double anom;
  double T_anom_ave_grid[MAXY]; // the final anomaly for each year (ave over all grid cells)

  //////////////////////// take in arguments  ////////////////////////////
  if(argc!=4){
    printf("Usage: %s latlonlist cellsize outfile\n",argv[0]);
    exit(0);
  }
  if((fplist = fopen(argv[1],"r"))==NULL){
    printf("ERROR: can't open 1 %s\n", argv[1]);
    exit(0);
  }

  ////////////////////////// initialize parameters /////////////////////////
  nfiles=get_length(fplist);  // number of files
  cellsize=atof(argv[2]);  // cell size
  for(j=0; j<=MAXY; j++)
  {
	T_anom_ave_grid[j] = 0;
  }

 
  /////// for each grid cell: 
  tot_area=0;
  for(i=0;i<nfiles;i++){
    fscanf(fplist,"%lf %lf",&lat,&lon);
    area = calc_area(lat,lon,cellsize);
    tot_area += area;
    sprintf(filename,"./VIC_forecast_for_CA/whole_period/fluxes_%.5f_%.5f",lat,lon);
    if((fpin = fopen(filename,"r"))==NULL){
      printf("ERROR: can't open 2 %s\n", filename);
      exit(0);
    }
    ndays=get_length(fpin);
    
    /////// calculate ave T on each day (Nov1 - Mar23)
    for(j=0; j<=MAXMON; j++)
		for(k=0; k<=MAXDAY; k++)
		{
			ave_T[j][k] = 0;
		}
    for(j=0;j<ndays;j++){
      fscanf(fpin,"%d %d %d",&year,&month,&day);
      if(j==0) syear=year;
      fscanf(fpin,"%lf %lf %lf %lf",&prec,&evap,&runoff,&baseflow);
      fscanf(fpin,"%lf %lf %lf %lf %lf",&airT,&sm1,&sm2,&sm3,&swe); 
      if(month>=11 || month<=3 || (month==4 && day<=1))
		{  ave_T[month-1][day-1] += airT;  }
    }
    fclose(fpin);
    nyear = year - syear + 1;
    for(j=0; j<MAXMON; j++)
		for(k=0; k<MAXDAY; k++)
		{
			// if Feb 29, there are NLEAP years
			if((j+1==2) && (k+1==29))  ave_T[j][k] = ave_T[j][k] / NLEAP;
			// if Nov or Dec, there are only nyear-1 years
			else if(j+1>=11)  ave_T[j][k] = ave_T[j][k] / (nyear-1);
			// if Jan, Feb or Mar, there are nyear years of record
			else if((j+1<=3) || ((j+1==4) && (k+1<=1))) ave_T[j][k] = ave_T[j][k] / nyear;
		}


	//////// for each day in each year, calculate the anomaly, then average for each year
    sprintf(filename,"./VIC_forecast_for_CA/whole_period/fluxes_%.5f_%.5f",lat,lon);
    if((fpin = fopen(filename,"r"))==NULL){
      printf("ERROR: can't open 3 %s\n", filename);
      exit(0);
    }
	for(j=0; j<=MAXY; j++)
	{
		T_anom[j] = 0;
		nday_year[j] = 0;
	}
	for(j=0;j<ndays;j++){
      fscanf(fpin,"%d %d %d",&year,&month,&day);
      if(j==0) syear=year;
      fscanf(fpin,"%lf %lf %lf %lf",&prec,&evap,&runoff,&baseflow);
      fscanf(fpin,"%lf %lf %lf %lf %lf",&airT,&sm1,&sm2,&sm3,&swe);
      anom = airT - ave_T[month-1][day-1];
      if(month>=11 && prec>=THRED)   // if Nov or Dec && rainy, add it to the next water year
	  {
		T_anom[year-syear+1] += anom;
		nday_year[year-syear+1]++;
	  }
	  else if((month<=3 || (month==4 && day<=1)) && prec>=THRED)  // if Jan - Mar 23 && rainy, add it to this water year
	  {
		T_anom[year-syear] += anom;
		nday_year[year-syear]++;
	  }
    }
    fclose(fpin); 
	for(j=0; j<nyear; j++) 
	{
		T_anom[j] = T_anom[j] / nday_year[j];
		T_anom_ave_grid[j] += T_anom[j] * area;  // add the anomaly at all grid cells together 
	}

  }
    fclose(fplist);
  
  //////////////// for each year, average the anomaly over all grid cells (spacially weighted) ///////////
  if((fpout = fopen(argv[3],"w"))==NULL){
    printf("ERROR: can't open %s\n", argv[3]);
    exit(0);
  }
  for(j=1;j<nyear;j++){     // only print from the second year (the first water year)
      T_anom_ave_grid[j] = T_anom_ave_grid[j] / tot_area;
      fprintf(fpout, "%d %f\n", j+syear, T_anom_ave_grid[j]);
  }
  fclose(fpout);
  printf("%.1f\n",tot_area);


  return(0);
}
Esempio n. 9
0
File: xrr.c Progetto: FHe/tdl
/******************************************************************************
* xref()
* Master function for reflectivity and reflection xsw calculations
*
* Parameters
* ---------
*
* Returns
* -------
*
* Notes
* -----
* take all raw arrays, use static copies of structs and assign pointers
* then pass along.  This is the main public function
*
* Note theta array and results
* arrays are dim nthet
* int     nthet;
* double *theta_arr;
* double *R; 
* double *Y;
******************************************************************************/
int xref( int nlayer, int nelem, int nthet, double *calc_params, 
          double *d, double *rho, double *sigma, double **comp,
          double *elem_z, double *fp, double *fpp, double *amu, double *mu_at,
          double *theta_arr, double *R, double *Y, 
          double *del, double *bet, double *amu_t, double *mu_t,
          double *Re_X, double *Im_X, double *Re_Ai, double *Im_Ai, 
          double *Re_Ar, double *Im_Ar, double *Re_g, double *Im_g)
{

    int         j, ret, fy_idx;
    double      energy, ref_scale;
    double      theta, k, lam, y, wconv;
    ref_model   ref;
    layer_calc  lay_c;    
    angle_calc  ang_c;
    gsl_complex Xtop;

    //calc k
    energy = calc_params[0];
    lam = 12398.0 / (energy);
    k = 2*M_PI/lam;

    // get fy_idx 
    fy_idx = (int) calc_params[6];

    //ref scale factor
    ref_scale = calc_params[13];

    //fill in the data structure
    //ref_model
    ref.calc_params = calc_params;
    ref.k           = k;
    ref.nlayer      = nlayer;
    ref.d           = d;
    ref.rho         = rho;
    ref.comp        = comp;    
    ref.sigma       = sigma;
    ref.nelem       = nelem;
    ref.elem_z      = elem_z;    
    ref.fp          = fp;
    ref.fpp         = fpp;
    ref.amu         = amu;
    ref.mu_at       = mu_at;

    //layer_calc
    lay_c.nlayer    = nlayer;
    lay_c.del       = del;
    lay_c.bet       = bet;
    lay_c.mu_t      = mu_t;
    lay_c.amu_t     = amu_t;

    //angle_calc
    ang_c.nlayer    = nlayer;
    ang_c.Re_X      = Re_X;
    ang_c.Im_X      = Im_X;
    ang_c.Re_Ai     = Re_Ai;
    ang_c.Im_Ai     = Im_Ai;
    ang_c.Re_Ar     = Re_Ar;
    ang_c.Im_Ar     = Im_Ar;
    ang_c.Re_g      = Re_g;
    ang_c.Im_g      = Im_g;

    // get layer_calc data
    // this stuff doesnt depend on
    // incidence angle
    ret = calc_del_bet_mu(&ref, &lay_c);
    if (ret == FAILURE) return(FAILURE);

    // loop over theta
    for (j=0;j<nthet;j++){

        theta = theta_arr[j];
   
        // calc X's and compute reflectivity     
        ret = calc_X(theta, &ref, &ang_c, &lay_c);
        if (ret == FAILURE) return(FAILURE);

        GSL_SET_COMPLEX(&Xtop,ang_c.Re_X[nlayer-1],ang_c.Im_X[nlayer-1]);  
        R[j] = gsl_complex_abs2(Xtop);

        if (calc_params[5] > 0.0){
            R[j] = R[j]*calc_spilloff(theta,calc_params[3],calc_params[2]);
        }
        if (ref_scale > 0.0){
            R[j] = R[j]*ref_scale;
        }

        if (fy_idx >= 0) {
            // calculate A's
            ret = calc_A(theta, &ref, &ang_c, &lay_c);
            if (ret == FAILURE) return(FAILURE);
    
            // calc FY
            y = calc_FY(theta, &ref, &ang_c, &lay_c);

            // mult by area
            if (calc_params[5] > 0.0){
                y = y*calc_area(theta,calc_params[3],calc_params[4],calc_params[2]);
                y = y*calc_spilloff(theta,calc_params[3],calc_params[2]);
            }
            Y[j] = y; 
        }
    }

    // convolve and normalize
    wconv = calc_params[1];

    // Note convolve is not padded!
    if (wconv > 0.0) {
        convolve(theta_arr, R, nthet, wconv);
    } 

    if (fy_idx >= 0){
        //convolve yield
        if (wconv > 0.0){
            convolve(theta_arr, Y, nthet, wconv);
        }
        // normalize yield
        norm_array(theta_arr, Y, nthet, calc_params[9], 1.0);
    }

    return(SUCCESS);
}