POJ2194 Stacking Cylinders(向量旋转)

题目链接:

  http://poj.org/problem?id=2194

题目描述:

Stacking Cylinders

Description

Cylinders (e.g. oil drums) (of radius 1 foot) are stacked in a rectangular bin. Each cylinder on an upper row rests on two cylinders in the row below. The cylinders in the bottom row rest on the floor. Each row has one less cylinder than the row below. 

This problem is to write a program to compute the location of the center of the top cylinder from the centers of the cylinders on the bottom row. Computations of intermediate values should use double precision.

Input

Each data set will appear in one line of the input. An input line consists of the number, n, of cylinders on the bottom row followed by n floating point values giving the x coordinates of the centers of the cylinders (the y coordinates are all 1.0 since the cylinders are resting on the floor (y = 0.0)). The value of n will be between 1 and 10 (inclusive). The end of input is signaled by a value of n = 0. The distance between adjacent centers will be at least 2.0 (so the cylinders do not overlap) but no more than 3.4 (cylinders at level k will never touch cylinders at level k – 2).

Output

The output for each data set is a line containing the x coordinate of the topmost cylinder rounded to 4 decimal places, a space and the y coordinate of the topmost cylinder to 4 decimal places. Note: To help you check your work, the x-coordinate of the center of the top cylinder should be the average of the x-coordinates of the leftmost and rightmost bottom cylinders.

Sample Input

4 1.0 4.4 7.8 11.2
1 1.0
6 1.0 3.0 5.0 7.0 9.0 11.0
10 1.0 3.0 5.0 7.0 9.0 11.0 13.0 15.0 17.0 20.4
5 1.0 4.4 7.8 14.6 11.2
0

Sample Output

6.1000 4.1607
1.0000 1.0000
6.0000 9.6603
10.7000 15.9100
7.8000 5.2143

题目大意:

  圆从最底层开始摞,求最上面的球的横纵坐标

思路:

  枚举层

  用当前层两个相邻圆去构造新圆

  很容易可以求出圆心夹角

  旋转向量即可

代码:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <cmath>
 5 #include <cstring>
 6 using namespace std;
 7
 8 const int N = 12;
 9 const double EPS = 1e-10;        //精度系数
10 const double PI = acos(-1.0);    //π
11
12 struct Point {
13     double x, y;
14     Point(double x = 0, double y = 0) :x(x), y(y) {}
15     const bool operator < (Point A)const {
16         return x == A.x ? y < A.y : x < A.x;
17     }
18 };    //点的定义
19
20 typedef Point Vector;    //向量的定义
21
22 Vector operator + (Vector A, Vector B) { return Vector(A.x + B.x, A.y + B.y); }    //向量加法
23 Vector operator - (Vector A, Vector B) { return Vector(A.x - B.x, A.y - B.y); }    //向量减法
24 Vector operator * (Vector A, double p) { return Vector(A.x*p, A.y*p); }        //向量数乘
25
26 int dcmp(double x) {
27     if (fabs(x) < EPS)return 0; else return x < 0 ? -1 : 1;
28 }    //与0的关系
29
30 Vector Rotate(Vector A, double rad) {
31     return Vector(A.x*cos(rad) - A.y*sin(rad), A.x*sin(rad) + A.y*cos(rad));
32 }    //逆时针旋转rad度
33
34 double Dot(Vector A, Vector B) { return A.x*B.x + A.y*B.y; }    //向量点乘
35 double Length(Vector A) { return sqrt(Dot(A, A)); }    //向量长度
36
37 Point Up(Point A, Point B) {
38     Vector v = B - A;
39     double L = Length(v), rad = acos(L / 4);
40     Vector u = Rotate(v, rad);
41     return A + u*(2 / L);
42 }
43
44 int main() {
45     int n;
46     Point P[N][N];
47     while (cin >> n && n) {
48         for (int i = 0; i < n; ++i)
49             scanf("%lf", &P[0][i].x), P[0][i].y = 1;
50         sort(P[0], P[0] + n);
51         for (int i = 1; i < n; ++i)
52             for (int j = 0; j < n - i; ++j)
53                 P[i][j] = Up(P[i - 1][j], P[i - 1][j + 1]);
54         printf("%.4lf %.4lf\n", P[n - 1][0].x, P[n - 1][0].y);
55     }
56 }
时间: 2024-10-25 09:11:35

POJ2194 Stacking Cylinders(向量旋转)的相关文章

一维向量旋转算法 编程珠玑 第二章

看了编程珠玑第二章,这里面讲了三道题目,这里说一下第二题,一维向量旋转算法. 题目:将一个n元一维向量(例数组)向左旋转i个位置. 解决方法:书上讲解了5种方法,自己只想起来2种最简单方法(下面讲的前两种). 1.原始方法. 从左向右依次移动一位,对所有数据平移:这样循环i次,算法最坏时间复杂度达n^2.耗时不推荐. 2.空间换时间. 顾名思义,申请一个i长度的空间,把前i半部分放到申请空间中,再把后面的所有数据向左移动i个位置,最后把申请的空间中的数据放到后半部分.浪费空间,不推荐. 3.杂技

sdut 2603:Rescue The Princess(第四届山东省省赛原题,计算几何,向量旋转 + 向量交点)

Rescue The Princess Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 Several days ago, a beast caught a beautiful princess and the princess was put in prison. To rescue the princess, a prince who wanted to marry the princess set out immedia

向量旋转 UPC 2217

这道题目是13山东省省赛的签到题,题目大意是给等边三角形的两个定点,让求逆时针旋转之后的第三个点的坐标,原来不会向量的旋转,在网上找了找,找到一篇挺好的,直接贴过来. 向量的旋转 实际做题中我们可能会遇到很多有关及计算几何的问题,其中有一类问题就是向量的旋转问题,下面我们来具体探讨一下有关旋转的问题. 首先我们先把问题简化一下,我们先研究一个点绕另一个点旋转一定角度的问题.已知A点坐标(x1,y1),B点坐标(x2,y2),我们需要求得A点绕着B点旋转θ度后的位置. 如图:A' 就是A点绕B点旋

计算几何之向量旋转

实际做题中我们可能会遇到很多有关及计算几何的问题,其中有一类问题就是向量的旋转问题,下面我们来具体探讨一下有关旋转的问题. 首先我们先把问题简化一下,我们先研究一个点绕另一个点旋转一定角度的问题.已知A点坐标(x1,y1),B点坐标(x2,y2),我们需要求得A点绕着B点旋转θ度后的位置. A点绕B点旋转θ角度后得到的点,问题是我们要如何才能得到A' 点的坐标.(向逆时针方向旋转角度正,反之为负)研究一个点绕另一个点旋转的问题,我们可以先简化为一个点绕原点旋转的问题,这样比较方便我们的研究.之后

HDU 1700 Points on Cycle (几何 向量旋转)

http://acm.hdu.edu.cn/showproblem.php?pid=1700 题目大意: 二维平面,一个圆的圆心在原点上.给定圆上的一点A,求另外两点B,C,B.C在圆上,并且三角形ABC的周长是最长的. 解题思路: 我记得小学的时候给出个一个定理,在园里面正多边形的的周长是最长的,这个定理我不会证明. 所以这里是三角形,当三角形为正三角形的时候,周长是最长的. 因为圆心在原点,所以我就向量(x,y)绕原点逆时针旋转120度和顺时针旋转120度.给定的点A可以看成(x,y)向量.

UVA_11178_Morley&#39;s_Theorem_(向量旋转+直线相交)

描述 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=23&page=show_problem&problem=2119 Morley定理:作三角形ABC每个内角的三等分线,相交形成三角形DEF,则三角形DEF是等边三角形. 给出三角形的三个顶点ABC的坐标,求出DEF的坐标. 11178 - Morley's Theorem Time limit: 3.000 s

sicily 1012/1206 Stacking Cylinders

1206. Stacking Cylinders Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description Problem Cylinders (e.g. oil drums) (of radius 1 foot) are stacked in a rectangular bin. Each cylinder on an upper row rests on two cylinders in the row below. Th

向量旋转

// 向量v0沿着Y轴旋转45度得到v1 Vector3 v0; Vector3 v1 = Quaternion.AngleAxis(45, Vector3.up) * v0; // 某游戏物体绕Y轴旋转30度得到的新Quaternion Quaternion rotation = Quaternion.Euler(0f,30f,0f) * Target.rotation; // 沿着Target的X轴延伸10米求目标点的3D坐标 Vector3 newPos = Target.position

简单几何(向量旋转+凸包+多边形面积) UVA 10652 Board Wrapping

题目传送门 题意:告诉若干个矩形的信息,问他们在凸多边形中所占的面积比例 分析:训练指南P272,矩形面积长*宽,只要计算出所有的点,用凸包后再求多边形面积.已知矩形的中心,向量在原点参考点再旋转,角度要转换成弧度制. /************************************************ * Author :Running_Time * Created Time :2015/11/10 星期二 10:34:43 * File Name :UVA_10652.cpp