HDU1086 You can Solve a Geometry Problem too

微微发亮的传送

计算几何的基础题,给定一些线段,判断有多少个交点,同时遇到很多条直线相交的时候可以重复计算,非常良心了。

我搬出了我的线段类和点类,写的那叫一个复杂,不过好在还是A了,如果不许重复计算的话我们就得求交点了……

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
using namespace std;
const double eps = 1e-8;
int cmp(double x){
	if (fabs(x) < eps) return 0;
	if (x > 0) return 1;
	return -1;
}
const double pi = acos(-1.0);
inline double sqr(double x){
	return x * x;
}
struct point{
	double x, y;
	point() {}
	point(double a, double b) : x(a), y(b){}
	void input(){
		scanf("%lf%lf", &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 bool operator == (const point &a, const point &b){
		return cmp(a.x - b.x) == 0 && cmp(a.y - b.y) == 0;
	}
	friend point operator * (const double &b, const point &a){
		return point(a.x * b, a.y * b);
	}
	friend point operator / (const point &a, const double &b){
		return point(a.x / b, a.y / b);
	}
	friend bool operator < (const point &a, const point &b){
		return a.x < b.x;
	}
	double norm(){
		return sqrt(sqr(x) + sqr(y));
	}
};
double det(const point &a, const point &b){
	return a.x * b.y - a.y * b.x;
}
double dot(const point &a, const point &b){
	return a.x * b.x + a.y * b.y;
}
double dist(const point &a, const point &b){
	return (a - b).norm();
}
point rotate_point(const point &p, double A){
	double tx = p.x, ty = p.y;
	return point(tx * cos(A) - ty * sin(A), tx * sin(A) + ty * cos(A));
}
struct line{
	point a, b;
	line() {};
	line(point x, point y) : a(x), b(y){}
};
line point_make_line(const point a, const point b){
	return line(a, b);
}
double dist_point_line(const point p, const point s, const point t){
	if (cmp(dot(p - s, t - s)) < 0) return (p - s).norm();
	if (cmp(dot(p - t, s - t)) < 0) return (p - t).norm();
	return fabs(det(s - p, t - p) / dist(s, t));
}
void PointProjLine(const point p, const point s, const point t, point &cp){
	double r = dot((t - s), (p - s)) / dot(t - s, t - s);
	cp = s + r * (t - s);
}
bool PointOnSegment(point p, point s, point t){
	return cmp(det(p - s, t - s)) == 0 && cmp(dot(p - s, p - t) <= 0);
}
bool parallel(line a, line b){
	return !cmp(det(a.a - a.b, b.a - b.b));
}
bool line_make_point(line a, line b, point &res){
	if (parallel(a, b)) return 0;
	double s1 = det(a.a - b.a, b.b - b.a);
	double s2 = det(a.b - b.a, b.b - b.a);
	res = (s1 * a.b - s2 * a.a) / (s1 - s2);
	return 1;
}
line line_move_d(line a, const double &len){
	point d = a.b - a.a;
	d = d / d.norm();
	d = rotate_point(d, pi / 2);
	return line(a.a + len * d, a.b + len * d);
}
bool seg_make_point(line a, line b){
	double d1, d2, d3, d4;
	d1 = det(a.b - a.a, b.a - a.a);
	d2 = det(a.b - a.a, b.b - a.a);
	d3 = det(b.b - b.a, a.a - b.a);
	d4 = det(b.b - b.a, a.b - b.a);
	if (d1 * d2 < 0 && d3 * d4 < 0) return 1;
	if (d1 == 0 && dot(a.b - b.a, a.a - b.a) <= 0) return 1;
	if (d2 == 0 && dot(a.b - b.b, a.a - b.b) <= 0) return 1;
	if (d3 == 0 && dot(b.b - a.a, b.a - a.a) <= 0) return 1;
	if (d4 == 0 && dot(b.b - a.b, b.a - a.b) <= 0) return 1;
	return 0; 
}
int n;
line seg[110];
point tmp;
int main(){
	#ifndef ONLINE_JUDGE
	freopen("in", "rt", stdin);
	#endif
	while(scanf("%d", &n) == 1 && n){
		point s, t;
		for (int i = 0; i < n; i++){
			s.input(); t.input();
			seg[i] = point_make_line(s, t);
		}
		int ans = 0;
		for (int i = 0; i < n; i++)
			for (int j = i + 1; j < n; j++)
				if (seg_make_point(seg[i], seg[j])) ans += 1;
		printf("%d\n", ans);
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值