uva 319 - Pendulum(几何)

题目链接:uva 319 - Pendulum

注意高度不能高过水平线,一种周期是绕某点一直转圈,一种周期是返回起点。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <algorithm>

using namespace std;
const double pi = 4 * atan(1);
const double eps = 1e-8;

inline int dcmp (double x) { if (fabs(x) < eps) return 0; else return x < 0 ? -1 : 1; }
inline double getDistance (double x, double y) { return sqrt(x * x + y * y); }

struct Point {
	double x, y;
	Point (double x = 0, double y = 0): x(x), y(y) {}
	void read () { scanf("%lf%lf", &x, &y); }
	void write () { printf("%lf %lf", x, y); }

	bool operator == (const Point& u) const { return dcmp(x - u.x) == 0 && dcmp(y - u.y) == 0; }
	bool operator != (const Point& u) const { return !(*this == u); }
	bool operator < (const Point& u) const { return x < u.x || (x == u.x && y < u.y); }
	bool operator > (const Point& u) const { return u < *this; }
	bool operator <= (const Point& u) const { return *this < u || *this == u; }
	bool operator >= (const Point& u) const { return *this > u || *this == u; }
	Point operator + (const Point& u) { return Point(x + u.x, y + u.y); }
	Point operator - (const Point& u) { return Point(x - u.x, y - u.y); }
	Point operator * (const double u) { return Point(x * u, y * u); }
	Point operator / (const double u) { return Point(x / u, y / u); }
	double operator * (const Point& u) { return x*u.y - y*u.x; }
};

typedef Point Vector;

struct Line {
	double a, b, c;
	Line (double a = 0, double b = 0, double c = 0): a(a), b(b), c(c) {}
};

struct Circle {
	Point o;
	double r;
	Circle () {}
	Circle (Point o, double r = 0): o(o), r(r) {}
	void read () { o.read(), scanf("%lf", &r); }
	Point point(double rad) { return Point(o.x + cos(rad)*r, o.y + sin(rad)*r); }
	double getArea (double rad) { return rad * r * r / 2; }
};

namespace Punctual {
	double getDistance (Point a, Point b) { double x=a.x-b.x, y=a.y-b.y; return sqrt(x*x + y*y); }
};

namespace Vectorial {
	/* 点积: 两向量长度的乘积再乘上它们夹角的余弦, 夹角大于90度时点积为负 */
	double getDot (Vector a, Vector b) { return a.x * b.x + a.y * b.y; }

	/* 叉积: 叉积等于两向量组成的三角形有向面积的两倍, cross(v, w) = -cross(w, v) */
	/* 左正右负 */
	double getCross (Vector a, Vector b) { return a.x * b.y - a.y * b.x; }

	double getLength (Vector a) { return sqrt(getDot(a, a)); }
	double getPLength (Vector a) { return getDot(a, a); }
	double getAngle (Vector u) { return atan2(u.y, u.x); }
	double getAngle (Vector a, Vector b) { return acos(getDot(a, b) / getLength(a) / getLength(b)); }
	Vector rotate (Vector a, double rad) { return Vector(a.x*cos(rad)-a.y*sin(rad), a.x*sin(rad)+a.y*cos(rad)); }
	/* 单位法线 */
	Vector getNormal (Vector a) { double l = getLength(a); return Vector(-a.y/l, a.x/l); }
};

namespace Linear {
	using namespace Vectorial;

	Line getLine (double x1, double y1, double x2, double y2) { return Line(y2-y1, x1-x2, y1*(x2-x1)-x1*(y2-y1)); }
	Line getLine (double a, double b, Point u) { return Line(a, -b, u.y * b - u.x * a); }

	bool getIntersection (Line p, Line q, Point& o) {
		if (fabs(p.a * q.b - q.a * p.b) < eps)
			return false;
		o.x = (q.c * p.b - p.c * q.b) / (p.a * q.b - q.a * p.b);
		o.y = (q.c * p.a - p.c * q.a) / (p.b * q.a - q.b * p.a);
		return true;
	}

	/* 直线pv和直线qw的交点 */
	bool getIntersection (Point p, Vector v, Point q, Vector w, Point& o) {
		if (dcmp(getCross(v, w)) == 0) return false;
		Vector u = p - q;
		double k = getCross(w, u) / getCross(v, w);
		o = p + v * k;
		return true;
	}

	/* 点p到直线ab的距离 */
	double getDistanceToLine (Point p, Point a, Point b) { return fabs(getCross(b-a, p-a) / getLength(b-a)); }
	double getDistanceToSegment (Point p, Point a, Point b) {
		if (a == b) return getLength(p-a);
		Vector v1 = b - a, v2 = p - a, v3 = p - b;
		if (dcmp(getDot(v1, v2)) < 0) return getLength(v2);
		else if (dcmp(getDot(v1, v3)) > 0) return getLength(v3);
		else return fabs(getCross(v1, v2) / getLength(v1));
	}

	/* 点p在直线ab上的投影 */
	Point getPointToLine (Point p, Point a, Point b) { Vector v = b-a; return a+v*(getDot(v, p-a) / getDot(v,v)); }

	/* 判断线段是否存在交点 */
	bool haveIntersection (Point a1, Point a2, Point b1, Point b2) {
		double c1=getCross(a2-a1, b1-a1), c2=getCross(a2-a1, b2-a1), c3=getCross(b2-b1, a1-b1), c4=getCross(b2-b1,a2-b1);
		return dcmp(c1)*dcmp(c2) < 0 && dcmp(c3)*dcmp(c4) < 0;
	}

	/* 判断点是否在线段上 */
	bool onSegment (Point p, Point a, Point b) { return dcmp(getCross(a-p, b-p)) == 0 && dcmp(getDot(a-p, b-p)) < 0; }
}

namespace Triangular {
	using namespace Vectorial;

	double getAngle (double a, double b, double c) { return acos((a*a+b*b-c*c) / (2*a*b)); }
	double getArea (double a, double b, double c) { double s =(a+b+c)/2; return sqrt(s*(s-a)*(s-b)*(s-c)); }
	double getArea (double a, double h) { return a * h / 2; }
	double getArea (Point a, Point b, Point c) { return fabs(getCross(b - a, c - a)) / 2; }
	double getDirArea (Point a, Point b, Point c) { return getCross(b - a, c - a) / 2; }
};

namespace Polygonal {
	using namespace Vectorial;
	using namespace Linear;

	double getArea (Point* p, int n) {
		double ret = 0;
		for (int i = 1; i < n-1; i++)
			ret += getCross(p[i]-p[0], p[i+1]-p[0]);
		return fabs(ret)/2;
	}

	/* 凸包 */
	int getConvexHull (Point* p, int n, Point* ch) {
		sort(p, p + n);
		int m = 0;
		for (int i = 0; i < n; i++) {
			/* 可共线 */
			//while (m > 1 && dcmp(getCross(ch[m-1]-ch[m-2], p[i]-ch[m-1])) < 0) m--;
			while (m > 1 && dcmp(getCross(ch[m-1]-ch[m-2], p[i]-ch[m-1])) <= 0) m--;
			ch[m++] = p[i];
		}
		int k = m;
		for (int i = n-2; i >= 0; i--) {
			/* 可共线 */
			//while (m > k && dcmp(getCross(ch[m-1]-ch[m-2], p[i]-ch[m-2])) < 0) m--;
			while (m > k && dcmp(getCross(ch[m-1]-ch[m-2], p[i]-ch[m-2])) <= 0) m--;
			ch[m++] = p[i];
		}
		if (n > 1) m--;
		return m;
	}

	int isPointInPolygon (Point o, Point* p, int n) {
		int wn = 0;
		for (int i = 0; i < n; i++) {
			int j = (i + 1) % n;
			if (onSegment(o, p[i], p[j])) return 0; // 边界上
			int k = dcmp(getCross(p[j] - p[i], o-p[i]));
			int d1 = dcmp(p[i].y - o.y);
			int d2 = dcmp(p[j].y - o.y);
			if (k > 0 && d1 <= 0 && d2 > 0) wn++;
			if (k < 0 && d2 <= 0 && d1 > 0) wn--;
		}
		return wn ? -1 : 1;
	}
};

namespace Circular {
	using namespace Linear;
	using namespace Vectorial;
	using namespace Triangular;

	/* 直线和原的交点 */
	int getLineCircleIntersection (Point p, Point q, Circle O, double& t1, double& t2, vector<Point>& sol) {
		Vector v = q - p;
		/* 使用前需清空sol */
		//sol.clear();
		double a = v.x, b = p.x - O.o.x, c = v.y, d = p.y - O.o.y;
		double e = a*a+c*c, f = 2*(a*b+c*d), g = b*b+d*d-O.r*O.r;
		double delta = f*f - 4*e*g;
		if (dcmp(delta) < 0) return 0;
		if (dcmp(delta) == 0) {
			t1 = t2 = -f / (2 * e);
			sol.push_back(p + v * t1);
			return 1;
		}

		t1 = (-f - sqrt(delta)) / (2 * e); sol.push_back(p + v * t1);
		t2 = (-f + sqrt(delta)) / (2 * e); sol.push_back(p + v * t2);
		return 2;
	}

	/* 圆和圆的交点 */
	int getCircleCircleIntersection (Circle o1, Circle o2, vector<Point>& sol) {
		double d = getLength(o1.o - o2.o);

		if (dcmp(d) == 0) {
			if (dcmp(o1.r - o2.r) == 0) return -1;
			return 0;
		}

		if (dcmp(o1.r + o2.r - d) < 0) return 0;
		if (dcmp(fabs(o1.r-o2.r) - d) > 0) return 0;

		double a = getAngle(o2.o - o1.o);
		double da = acos((o1.r*o1.r + d*d - o2.r*o2.r) / (2*o1.r*d));

		Point p1 = o1.point(a-da), p2 = o1.point(a+da);

		sol.push_back(p1);
		if (p1 == p2) return 1;
		sol.push_back(p2);
		return 2;
	}

	/* 过定点作圆的切线 */
	int getTangents (Point p, Circle o, Vector* v) {
		Vector u = o.o - p;
		double d = getLength(u);
		if (d < o.r) return 0;
		else if (dcmp(d - o.r) == 0) {
			v[0] = rotate(u, pi / 2);
			return 1;
		} else {
			double ang = asin(o.r / d);
			v[0] = rotate(u, -ang);
			v[1] = rotate(u, ang);
			return 2;
		}
	}

	/* a[i] 和 b[i] 分别是第i条切线在O1和O2上的切点 */
	int getTangents (Circle o1, Circle o2, Point* a, Point* b) {
		int cnt = 0;
		if (o1.r < o2.r) { swap(o1, o2); swap(a, b); }
		double d2 = getLength(o1.o - o2.o); d2 = d2 * d2;
		double rdif = o1.r - o2.r, rsum = o1.r + o2.r;
		if (d2 < rdif * rdif) return 0;
		if (dcmp(d2) == 0 && dcmp(o1.r - o2.r) == 0) return -1;

		double base = getAngle(o2.o - o1.o);
		if (dcmp(d2 - rdif * rdif) == 0) {
			a[cnt] = o1.point(base); b[cnt] = o2.point(base); cnt++;
			return cnt;
		}

		double ang = acos( (o1.r - o2.r) / sqrt(d2) );
		a[cnt] = o1.point(base+ang); b[cnt] = o2.point(base+ang); cnt++;
		a[cnt] = o1.point(base-ang); b[cnt] = o2.point(base-ang); cnt++;

		if (dcmp(d2 - rsum * rsum) == 0) {
			a[cnt] = o1.point(base); b[cnt] = o2.point(base); cnt++;
		} else if (d2 > rsum * rsum) {
			double ang = acos( (o1.r + o2.r) / sqrt(d2) );
			a[cnt] = o1.point(base+ang); b[cnt] = o2.point(base+ang); cnt++;
			a[cnt] = o1.point(base-ang); b[cnt] = o2.point(base-ang); cnt++;
		}
		return cnt;
	}

	/* 三点确定外切圆 */
	Circle CircumscribedCircle(Point p1, Point p2, Point p3) {
		double Bx = p2.x - p1.x, By = p2.y - p1.y;
		double Cx = p3.x - p1.x, Cy = p3.y - p1.y;
		double D = 2 * (Bx * Cy - By * Cx);
		double cx = (Cy * (Bx * Bx + By * By) - By * (Cx * Cx + Cy * Cy)) / D + p1.x;
		double cy = (Bx * (Cx * Cx + Cy * Cy) - Cx * (Bx * Bx + By * By)) / D + p1.y;
		Point p = Point(cx, cy);
		return Circle(p, getLength(p1 - p));
	}

	/* 三点确定内切圆 */
	Circle InscribedCircle(Point p1, Point p2, Point p3) {
		double a = getLength(p2 - p3);
		double b = getLength(p3 - p1);
		double c = getLength(p1 - p2);
		Point p = (p1 * a + p2 * b + p3 * c) / (a + b + c);
		return Circle(p, getDistanceToLine(p, p1, p2));
	} 

	/* 三角形一顶点为圆心 */
	double getPublicAreaToTriangle (Circle O, Point a, Point b) {
		if (dcmp((a-O.o)*(b-O.o)) == 0) return 0;
		int sig = 1;
		double da = getPLength(O.o-a), db = getPLength(O.o-b);
		if (dcmp(da-db) > 0) {
			swap(da, db);
			swap(a, b);
			sig = -1;
		}

		double t1, t2;
		vector<Point> sol;
		int n = getLineCircleIntersection(a, b, O, t1, t2, sol);

		if (dcmp(da-O.r*O.r) <= 0) {
			if (dcmp(db-O.r*O.r) <= 0)	return getDirArea(O.o, a, b) * sig;

			int k = 0;
			if (getPLength(sol[0]-b) > getPLength(sol[1]-b)) k = 1;

			double ret = getArea(O.o, a, sol[k]) + O.getArea(getAngle(sol[k]-O.o, b-O.o));
			double tmp = (a-O.o)*(b-O.o);
			return ret * sig * dcmp(tmp);
		}

		double d = getDistanceToSegment(O.o, a, b);
		if (dcmp(d-O.r) >= 0) {
			double ret = O.getArea(getAngle(a-O.o, b-O.o));
			double tmp = (a-O.o)*(b-O.o);
			return ret * sig * dcmp(tmp);
		}

		double k1 = O.r / getLength(a - O.o), k2 = O.r / getLength(b - O.o);
		Point p = O.o + (a - O.o) * k1, q = O.o + (b - O.o) * k2;
		double ret1 = O.getArea(getAngle(p-O.o, q-O.o));
		double ret2 = O.getArea(getAngle(sol[0]-O.o, sol[1]-O.o)) - getArea(O.o, sol[0], sol[1]);
		double ret = (ret1 - ret2), tmp = (a-O.o)*(b-O.o);
		return ret * sig * dcmp(tmp);
	}

	double getPublicAreaToPolygon (Circle O, Point* p, int n) {
		if (dcmp(O.r) == 0) return 0;
		double area = 0;
		for (int i = 0; i < n; i++) {
			int u = (i + 1) % n;
			area += getPublicAreaToTriangle(O, p[i], p[u]);
		}
		return fabs(area);
	}
};

using namespace Linear;
using namespace Polygonal;
const int maxn = 505;
//Vector rotate (Vector a, double rad) { return Vector(a.x*cos(rad)-a.y*sin(rad), a.x*sin(rad)+a.y*cos(rad)); }

int N, E;
double ans;
Point P[maxn];

void solve (Point o, Point s, double r) {
	int idx = -1;
	double rad = 2 * pi;

	for (int i = 0; i < N; i++) {
		double d = getLength(P[i] - o);
		if (dcmp(d - r) > 0) continue;
		double k = getAngle(s - o, P[i] - o);
		if (dcmp((s-o)*(P[i]-o)) < 0) k = 2 * pi - k;

		if (dcmp(k - rad) == 0 && (idx == -1 || dcmp(getLength(P[idx]-o) - getLength(P[i]-o)) < 0))
			idx = i;

		if (dcmp(k - rad) < 0) {
			rad = k;
			idx = i;
		}
	}

	if (dcmp(o.y + r) <= 0) {
		if (idx == -1) {
			E = 1, ans = rad * r; // 达不到水平线并且没有下一个折点,无限绕圈,最后周期为该圆的周长
			return;
		}
	} else {
		Point t = Point(o.x + sqrt(r * r - o.y * o.y), 0);
		double k = getAngle(s - o, t - o);
		if (dcmp((s-o)*(t-o)) < 0) k = 2 * pi - k;

		if (idx == -1 || dcmp(k - rad) <= 0) {
			rad = k;
			idx = -1;
		}
	}

	if (idx != -1) {
		rad = getAngle(s - o, P[idx] - o);
		solve(P[idx], o + rotate(s-o, rad), r - getLength(P[idx]-o));
		if (E == 0)
			ans += 2 * rad * r;
	} else
		ans = 2 * rad * r;
}

int main () {
	int cas = 1;
	double r;
	while (scanf("%d%lf", &N, &r) == 2 && r != 0) {
		for (int i = 0; i < N; i++) P[i].read();
		ans = E = 0;
		solve(Point(0, 0), Point(-r, 0), r);
		printf("Pendulum #%d\nLength of periodic orbit = %.2lf\n\n", cas++, ans);
	}
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-11-03 20:44:54

uva 319 - Pendulum(几何)的相关文章

UVA 11346 Probability (几何概型, 积分)

题目链接:option=com_onlinejudge&Itemid=8&page=show_problem&problem=2321">https://uva.onlinejudge.org/index.php? option=com_onlinejudge&Itemid=8&page=show_problem&problem=2321 题目大意:在A是一个点集 A = {(x, y) | x ∈[-a, a],y∈[-b, b]},求取出

UVA 10641 - Barisal Stadium(DP + 几何)

题目链接:10641 - Barisal Stadium 题意:逆时针给定n个点,在给m个灯,每个灯有一个花费,要求最小花费使得所有边能被灯照到 思路:用向量叉积判断向量的顺逆时针关系,从而预处理出每个灯能照到的边,然后由于n个点是环的,所以可以直接扩大两倍,dp时候去枚举起点即可 状态为dp[i]表示现在照到i条边之前的边全部照亮需要的最小花费 代码: #include <stdio.h> #include <string.h> const double eps = 1e-6;

uva 12508 - Triangles in the Grid(几何+计数)

题目链接:uva 12508 - Triangles in the Grid 题目大意:给出n,m,A和B,要求计算在(n+1)?(m+1)的矩阵上,可以找出多少个三角形,面积在AB之间. 解题思路:首先枚举矩阵,然后计算有多少个三角形以该矩阵为外接矩阵,并且要满足体积在AB之间.然后对于每个矩阵,要确定在大的范围内可以确定几个. 枚举矩阵的内接三角形可以分为三类: 1.三角型的两点在一条矩阵边上的顶点,另一点在该边的对边上(不包括顶点) 2.以对角线为三角形的一边 这样可以枚举x,然后求出l和

UVa 11178 Morley&#39;s Theorem (几何问题)

题意:给定三角形的三个点,让你求它每个角的三等分线所交的顶点. 析:根据自己的以前的数学知识,应该很容易想到思想,比如D点,就是应该求直线BD和CD的交点, 以前还得自己算,现在计算机帮你算,更方便,主要注意的是旋转是顺时针还是逆时针,不要搞错了. 要求BD和CD就得先求那个夹角ABC和ACD,然后三等分. 代码如下: #include <iostream> #include <cstdio> #include <cstring> #include <cmath&

uva :10123 - No Tipping(dfs + 几何力距 )

题目:uva :10123 - No Tipping 题目大意:给出l, m, n 分别表示 长度为l 的杠杆, 重量为 m, 有n个物体放在上方.问每次从上面挑选一个物品移除,是否能使杠杆继续平衡.这个过程中都可以的话,就输出移除顺序(不唯一) 否则就输出 impossible ,一开始,这个杠杆就不平衡的情况也会是有的.因为杠杆也是有重量的. 解题思路; 1.这题先前我就不明白什么怎么样的情况下,双支撑点的杠杆不平横,后面看了别人的报告才明白. 首先  我这里有两个支撑点 (1, 2) 左边

简单几何 UVA 11178 Morley&#39;s Theorem

题目传送门 题意:莫雷定理,求三个点的坐标 分析:训练指南P259,用到了求角度,向量旋转,求射线交点 /************************************************ * Author :Running_Time * Created Time :2015/10/21 星期三 15:56:27 * File Name :UVA_11178.cpp ************************************************/ #include

uva 10641 - Barisal Stadium(dp+几何)

题目链接:uva 10641 - Barisal Stadium 题目大意:按照顺时针给出操场的周边点,然后给出周围可以建设照明灯的位置,以及在该位置建设照明灯的代价,照明灯照射的范围与操场的边界相切,现在要求一个最小的花费,要求操场的所有边都被照射到. 解题思路:dp[i][j]表示从第i个点到第j个点之间的边都被照射到的最小代价,这样转移方程也很好写,只要有某个等得照射范围有覆盖到i,j,就可以向外扩展. 然而现在最主要的问题是如何求各个点的照射范围,一开始我是用灯的位置和边界所有点求斜率,

UVA 1543 - Telescope(dp+几何)

题目链接:1543 - Telescope 题意:按顺序给定圆周上一些点,问用选一些点组成m边形面积的最大值. 思路:dp,dp[i][j][k] 表示第一个点为i,最后一个点为j,当前选择k的最大值,因为多选一个点,会多的面积为他和第一个点和最后一个点构成的三角形面积,然后利用海伦公式求面积,状态转移为:dp[i][j][x] = max(dp[i][j][x], dp[i - 1][j][k] + s); 代码: #include <stdio.h> #include <string

UVA 11971 - Polygon(概率+几何概型)

UVA 11971 - Polygon 题目链接 题意:给一条长为n的线段,要选k个点,分成k + 1段,问这k + 1段能组成k + 1边形的概率 思路:对于n边形而言,n - 1条边的和要大于另外那条边,然后先考虑3边和4边形的情况,根据公式在坐标系中画出来的图,总面积为x,而不满足的面积被分成几块,每块面积为x/2k,然后在观察发现一共是k + 1块,所以符合的面积为x?x?(k+1)/2k,这样一来除以总面积就得到了概率1?(k+1)/2k 代码: #include <cstdio>