[HDOJ 3432][BNUOJ 7543] Wax [计算几何+二分答案]

69 篇文章 0 订阅
6 篇文章 0 订阅

给一个矩形,和矩形上一点,用过这个点的n-1条直线将这个矩形分成等面积的n份,求这n-1个交点

只用到叉积求面积和二分答案求长度就够了...

#include <cstdio>
#include <cstring>

using namespace std;

struct Point {
	double x,y;
	Point() {}
	Point(double xx,double yy) {
		x=xx;y=yy;
	}
	void print() {
		printf("%.3lf %.3lf ",x,y);
	}
	friend Point operator - (const Point &a,const Point &b) {
		return Point(a.x-b.x,a.y-b.y);
	}
	friend Point operator + (const Point &a,const Point &b) {
		return Point(a.x+b.x,a.y+b.y);
	}
	friend Point operator * (const Point &a,double b) {
		return Point(a.x*b,a.y*b);
	}
	friend double crossMul(const Point &a,const Point &b) {
		return b.x*a.y-a.x*b.y;
	}
};
struct Segment {
	Point s,t;
	Segment() {}
	Segment(Point ss,Point tt) {
		s=ss;t=tt;
	}
	operator Point () {
		return t-s;
	}
};

int w,h,d,n;
double s,curs;

double calS(Point a,Point b,Point c) {
	return curs-0.5*crossMul(b-a,c-b);
}
Point cal(Point door,Point tmp1,Point tmp2) {
	double l=0,r=1;
	Point ans;
	while (r-l>1e-9) {
		double t=(l+r)/2;
		ans=tmp1+(tmp2-tmp1)*t;
		/*
		printf("!! \n!!");
		printf("%lf\n",t);
		((tmp2-tmp1)*t).print();
		ans.print();
		printf("\n");
		printf("!! %lf %lf\n",t,calS(door,tmp1,ans));
		*/
		if (calS(door,tmp1,ans)<s) l=t;
		else r=t;
	}
	curs=0;
	ans=tmp1+(tmp2-tmp1)*l;
	return ans;
}

int main() {
	Point door,tmp1,tmp2;
	while (scanf("%d%d%d%d",&w,&h,&d,&n),n!=0) {
		curs=0;
		s=(double)w*h/n;
		door=Point(d,0);
		tmp1=Point(w,0);
		tmp2=Point(w,h);
		while (calS(door,tmp1,tmp2)>=s) {
			tmp1=cal(door,tmp1,tmp2);
			tmp1.print();
			/*
			printf("\n--test--\n");
			door.print();
			tmp1.print();
			tmp2.print();
			printf("\n--test over--\n");
			getchar();
			*/
		}
		curs=calS(door,tmp1,tmp2);
		tmp1=Point(w,h);
		tmp2=Point(0,h);
		while (calS(door,tmp1,tmp2)>=s) {
			tmp1=cal(door,tmp1,tmp2);
			tmp1.print();
			/*
			printf("\n--test--\n");
			door.print();
			tmp1.print();
			tmp2.print();
			printf("\n--test over--\n");
			getchar();
			*/
		}
		curs=calS(door,tmp1,tmp2);
		tmp1=Point(0,h);
		tmp2=Point(0,0);
		while (calS(door,tmp1,tmp2)>=1.5*s) {
			tmp1=cal(door,tmp1,tmp2);
			tmp1.print();
			/*
			printf("\n--test--\n");
			door.print();
			tmp1.print();
			tmp2.print();
			printf("\n--test over--\n");
			getchar();
			*/
		}
		printf("\n");
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值