知识点
相关文章
更多最近更新
更多POJ 1408 Fishnet(几何--叉积求面积 + 求直线的交点坐标)
2019-03-02 23:46|来源: 网路
Fishnet
Time Limit: 1000MS |
Memory Limit: 10000K |
|
Total Submissions: 1302 |
Accepted: 824 |
Description
A fisherman named Etadokah awoke in a very small island. He could see calm, beautiful and blue sea around the island. The previous night he had encountered a terrible storm and had reached this uninhabited island. Some wrecks of his ship were spread around him. He found a square wood-frame and a long thread among the wrecks. He had to survive in this island until someone came and saved him.
In order to catch fish, he began to make a kind of fishnet by cutting the long thread into short threads and fixing them at pegs on the square wood-frame. He wanted to know the sizes of the meshes of the fishnet to see whether he could catch small fish as well as large ones.
The wood frame is perfectly square with four thin edges on meter long: a bottom edge, a top edge, a left edge, and a right edge. There are n pegs on each edge, and thus there are 4n pegs in total. The positions of pegs are represented by their (x,y)-coordinates. Those of an example case with n=2 are depicted in figures below. The position of the ith peg on the bottom edge is represented by (ai,0). That on the top edge, on the left edge and on the right edge are represented by (bi,1), (0,ci) and (1,di), respectively. The long thread is cut into 2n threads with appropriate lengths. The threads are strained between (ai,0) and (bi,1),and between (0,ci) and (1,di) (i=1,...,n).
You should write a program that reports the size of the largest mesh among the (n+1)2 meshes of the fishnet made by fixing the threads at the pegs. You may assume that the thread he found is long enough to make the fishnet and the wood-frame is thin enough for neglecting its thickness.
Input
The input consists of multiple sub-problems followed by a line containing a zero that indicates the end of input. Each sub-problem is given in the following format.
n
a1 a2 ... an
b1 b2 ... bn
c1 c2 ... cn
d1 d2 ... dn
you may assume 0 < n <= 30, 0 < ai,bi,ci,di < 1
Output
For each sub-problem, the size of the largest mesh should be printed followed by a new line. Each value should be represented by 6 digits after the decimal point, and it may not have an error greater than 0.000001.
Sample Input
2
0.2000000 0.6000000
0.3000000 0.8000000
0.1000000 0.5000000
0.5000000 0.6000000
2
0.3333330 0.6666670
0.3333330 0.6666670
0.3333330 0.6666670
0.3333330 0.6666670
4
0.2000000 0.4000000 0.6000000 0.8000000
0.1000000 0.5000000 0.6000000 0.9000000
0.2000000 0.4000000 0.6000000 0.8000000
0.1000000 0.5000000 0.6000000 0.9000000
2
0.5138701 0.9476283
0.1717362 0.1757412
0.3086521 0.7022313
0.2264312 0.5345343
1
0.4000000
0.6000000
0.3000000
0.5000000
0
Sample Output
0.215657
0.111112
0.078923
0.279223
0.348958
Source
解题报告:题意求直线划分四边形中最大四边形面积,利用叉积求四边形面积,关键是如何求矩形中间的直线交点的坐标,求出坐标之后就是套用模板了!
代码如下:
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> using namespace std; const int MAX = 105; int n; struct Point { double x; double y; }; Point a[MAX], b[MAX], c[MAX], d[MAX], p[MAX][MAX]; double area[MAX][MAX], ans; Point GetPoint(Point p1, Point p2, Point p3, Point p4)//已知四个点求直线相交的点的坐标 //(这种求法包括了直线斜率不存在或为0的情况下) { Point p0; double A1 = p2.y - p1.y; double B1 = p1.x - p2.x; double C1 = p1.y * (-B1) - p1.x * A1; double A2 = p4.y - p3.y; double B2 = p3.x - p4.x; double C2 = p3.y *(-B2) - p3.x * A2; p0.x = (C2 * B1 - C1 * B2) / (A1 * B2 - A2 * B1); p0.y = (C2 * A1 - C1 * A2) / (B1 * A2 - B2 * A1); return p0; } void GetPoints() { int i, j; //矩形四个角的坐标 p[1][1].x = 0.0; p[1][1].y = 0.0; p[1][n + 2].x = 0.0; p[1][n + 2].y = 1.0; p[n + 2][1].x = 1.0; p[n + 2][1].y = 0.0; p[n + 2][n + 2].x = 1.0; p[n + 2][n + 2].y = 1.0; //矩形四条边的上点的坐标 for (i = 2; i < n + 2; ++i) { p[i][1] = a[i - 1];//y为0的边(下) p[i][n + 2] = b[i - 1];//x为1的边(右) p[1][i] = c[i - 1];//x为0的边(左) p[n + 2][i] = d[i -1];//y为1的边(上) } for (i = 2; i < n + 2; ++i)//获取矩阵中间的点的坐标 { for (j = 2; j < n + 2; ++j) { p[i][j] = GetPoint(p[i][1],p[i][n+2],p[1][j],p[n+2][j]); } } } double GetArea(Point p1, Point p2, Point p3, Point p4)//已知四个点的坐标求四边形的面积 { int i, j; double ar = 0; Point ac[4]; ac[0] = p1; ac[1] = p2; ac[2] = p3; ac[3] = p4; for (i = 0; i < 4; ++i)//叉积求四边形面积 { j = (i + 1) % 4; ar += ac[i].x * ac[j].y - ac[i].y * ac[j].x; } ar = ar / 2.0;//要除以2.0 if (ar < 0)//面积是正数 { ar = - ar; } return ar; } void GetAreas() { memset(area, 0, sizeof(area)); int i, j; for (i = 1; i <= n + 1; ++i) { for (j = 1; j <= n + 1; ++j)//枚举四个点组成的四边形的面积 { area[i][j] = GetArea(p[i][j], p[i + 1][j], p[i + 1][j + 1], p[i][j + 1]); if (ans < area[i][j])//找出最大的网格面积 { ans = area[i][j];//更新 } } } } int main() { int i; while (scanf("%d", &n) != EOF && n) { memset(a, 0, sizeof(a)); memset(b, 0, sizeof(b)); memset(c, 0, sizeof(c)); memset(d, 0, sizeof(d)); memset(p, 0, sizeof(p)); for (i = 1; i <= n; ++i) { scanf("%lf", &a[i].x); a[i].y = 0.0; } for (i = 1; i <= n; ++i) { scanf("%lf", &b[i].x); b[i].y = 1.0; } for (i = 1; i <= n; ++i) { scanf("%lf", &c[i].y); c[i].x = 0.0; } for (i = 1; i <= n; ++i) { scanf("%lf", &d[i].y); d[i].x = 1.0; } ans = 0; GetPoints(); GetAreas(); printf("%lf\n", ans); } return 0; }
转自:http://www.cnblogs.com/lidaojian/archive/2012/05/03/2481304
相关问答
更多-
高中数学学过啊,朋友 import sys ''' 输入两点,建立直线方程y=kx+b。 输入第3点,求点到直线的距离。 ''' #输入两点p1, p2坐标 sys.stdout.write('Input two points:\n') line = sys.stdin.readline() x1, y1, x2, y2 = (float(x) for x in line.split()) #计算k,b k = (y2 - y1) / (x2 - x1) b = y1 - k * x1 #输入第三点p3坐标 ...
-
回炉了一下几何,图形学用到 import math class Point: def __init__(self): self.x=0 self.y=0 def input(self,pname): self.x=int(input("Enter the x of point {0}: ".format(pname))) self.y=int(input("Enter the y of point {0}: ".format(pname))) a=Point() b=Point() c=Point() a. ...
-
OI中文刷题网站[2022-08-13]
中文题只有rqnoj了...vijos挂了... 英文题库题目质量明显高一些...poj题目难度参差不齐,不如去spoj,sgu -
在hdu和poj上刷题和自己学习算法知识应该怎样结合?[2022-05-28]
比如,你想学习搜索算法的话,那么你就先看看书,学习一下,之后到各oj上去刷题,刷搜索方面的题,在网上有很多给oj题目归类的文章,你就去找找,我觉得这样会很快的提高的! -
用pathon编程输入两个点,建立起直线方程y=kx+b.输入第三个点,求点到直线的距[2022-03-12]
import math print 'please input first two point p1(x1,y1) p2(x2,y2)' x1=raw_input() y1=raw_input() x2=raw_input() y2=raw_input() x1=float(x1) y1=float(y1) x2=float(x2) y2=float(y2) k=(y2-y1)/(x2-x1) b=(x1*y2-x2*y1)/(x2-x1) if b -
不完整,但它可能会给你一个好的开始。 let c = document.getElementById("myCanvas"), ctx = c.getContext("2d"), startX = 50, startY = 50, endX = 250, endY = 150, rS = 30, rE = 50, alpha = Math.atan((startY - endY) / (startX - endX)), dir = 0, // start ...
-
您的Arquillian部署档案中没有接口。 您需要将这些类中使用的所有内容都包含在Arquillian部署中。 You don't have the interfaces in your Arquillian deployment archive. You'll need to include everything that is used in those classes into Arquillian deployment.
-
* Uses proxy env variable http_proxy == 'https://proxy.in.tum.de:8080' ^^^^^ https://是错误的,它应该是http:// 。 即使目标URL是HTTPS,代理本身也应该被HTTP访问,而不是HTTPS。 代理仍然会正确处理HTTPS连接并保留端到端加密。 有关详细信息,请参阅HTTP CONNECT方法 。 * Uses proxy en ...
-
在D_FlipFlop端口中, Q是输出。 输出应该被分配给一个信号,而不是一个语句。 将输出分配给信号,然后将其反转为端口D的输入。 这是一个建议 signal q0, q1, q2 : std_logic; signal not_q0, not_q1, not_q2 : std_logic; begin FF1 : D_FlipFlop port map( clk => clk, clr => clr, D => not_q0, ...
-
我已经概括了以下内容,以便可以使用除a=10之外a=10其他值... a = 10; % constant line offset th = 0; % constant angle of line % rl = ?? - variable to find % Coordinates of line: % [xl, yl] = [ a + rl * cos(th), a + rl * sin(th) ]; rc = 1; % constant radius of circle % alpha = ?? ...