⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 d3.cpp

📁 用C++语言实现的关于已知长和宽的矩形
💻 CPP
字号:
#include"stdio.h"
#include <cmath>
#include <iostream>
#include <algorithm>
#include <ctime>
//#include "draw.h"
using namespace std;
#define E 1e-5
#define MAXSIZE 36

const double PI=2*acos(0);
#define steplen (PI/180)
typedef struct cir {
   double r,x,y;
   int put;       
}circle ;

circle sp[MAXSIZE];
double W,H,miny,minx,totalarea;
FILE *yyout;

//represent the distance of the vertex
double distance(int i,int j){
   return sqrt((sp[i].x-sp[j].x)*(sp[i].x-sp[j].x)+(sp[i].y-sp[j].y)*(sp[i].y-sp[j].y));       
}

double right_angle(double x,double y){
   return sqrt(x*x-y*y); 
}
int can_put(int current,double x,double y){
   if(x<sp[current].r||x>W-sp[current].r||y<sp[current].r||y>H-sp[current].r)
       return 0;
   sp[current].x=x;
   sp[current].y=y;
   for(int i=0;i<current;i++)
      if(sp[i].put&&distance(i,current)<sp[current].r+sp[i].r-E)  //check whether it overlap with other circle
         return 0;
   return 1;
}

int put_circle(int current,double x,double y){
   int color; 
   sp[current].x=x;
   sp[current].y=y;
   sp[current].put=1;
   totalarea+=PI*sp[current].r*sp[current].r;
   printf("%lf %lf %lf\n",sp[current].r,x,y);
   color=rand()%16;
   if(color==0) color++;
   ezdSetColor(ezdWhite+color);     //randome set color 
   delayWindow(0.5);
   ezdDrawCircle(10+x,10+y,sp[current].r);
   //printf("%lf\n",PI*sp[current].r*sp[current].r);
   return 1;
}


int check_ok(double x,double y){
	if(miny>y) {
		miny=y;
		minx=x;
	}
	else if(miny==y){
		if(minx>=x)
			minx=x;
	}
	return 1;
}

int pad(int current){
    double x,y;
	int i;
	double j;
	
	if(can_put(current,sp[current].r,sp[current].r))
		return put_circle(current,sp[current].r,sp[current].r);
	if(current==0) return 0;
	for(i=0;i<current;i++){
		if(sp[i].put){
		
		   for(j=0.0;j<2*PI;j+=steplen){
			  x=sp[i].x+(sp[i].r+sp[current].r)*cos(j);
			  y=sp[i].y+(sp[i].r+sp[current].r)*sin(j);
			  
			 // printf("x:%lf y:%lf\n",x,y);
			  if(can_put(current,x,y))
				  check_ok(x,y);
		   }
		}

	}


	if(miny<H)
		put_circle(current,minx,miny);
	return 1;
}

void copyright(){
	fprintf(yyout,"Usage:d2 sourcefile [targetfile]\n");
	fprintf(yyout,"sourcefile denotes the name of the input file\n");
	fprintf(yyout,"targetfile denotes the name of the output file\n");
}

bool compare(circle a,circle b){
	return a.r>b.r;
}

int main(int argc,char *argv[]){
	int i,n;
    double L;
	time_t temp;
	char inputname[15];
	--argc;
	++argv;
	yyout=stdout;
	if(argc==0) {copyright();exit(1);}
	strcpy(inputname,argv[0]);
    argc--;
	argv++;
    if(argc>0){
		yyout=freopen(argv[0],"w",stdout);
    }
	
	freopen(inputname,"r",stdin);
	scanf("%d%lf%lf%lf",&n,&W,&H,&L);
	for(i=0;i<n;i++)
		scanf("%lf",&sp[i].r);
	sort(sp,sp+n,compare);
	totalarea=0;
	openWindow();  //open the picture
	ezdSetOrigin(0,+30,ezdAtPoint);
	ezdSetViewport(35,-30);
	srand((unsigned)time(&temp));
	ezdSetColor(ezdBlack);
	ezdDrawLine(10,10,10,10+H);
	ezdDrawLine(10,10,10+W,10);
	ezdDrawLine(10+W,10,10+W,10+H);
	ezdDrawLine(10,10+H,10+W,10+H);
	//printf("steplen%lf\n",steplen);
	for(i=0;i<n;i++){
		sp[i].put=0;
		miny=H+1;
		minx=W+1;
		pad(i);
	}
	delayWindow(100);	
	closeWindow();
    printf("%s is %lf\n",inputname,totalarea/(W*H));
	return 1;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -