ZOJ--1010:Area(线段判交问题)_jerry, a middle school student, addicts himself to-程序员宅基地

技术标签: 几何算法  java  ZOJ  

Area

Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge

Jerry, a middle school student, addicts himself to mathematical research. Maybe the problems he has thought are really too easy to an expert. But as an amateur, especially as a 15-year-old boy, he had done very well. He is so rolling in thinking the mathematical problem that he is easily to try to solve every problem he met in a mathematical way. One day, he found a piece of paper on the desk. His younger sister, Mary, a four-year-old girl, had drawn some lines. But those lines formed a special kind of concave polygon by accident as Fig. 1 shows.


Fig. 1 The lines his sister had drawn

"Great!" he thought, "The polygon seems so regular. I had just learned how to calculate the area of triangle, rectangle and circle. I'm sure I can find out how to calculate the area of this figure." And so he did. First of all, he marked the vertexes in the polygon with their coordinates as Fig. 2 shows. And then he found the result--0.75 effortless.


Fig.2 The polygon with the coordinates of vertexes

Of course, he was not satisfied with the solution of such an easy problem. "Mmm, if there's a random polygon on the paper, then how can I calculate the area?" he asked himself. Till then, he hadn't found out the general rules on calculating the area of a random polygon. He clearly knew that the answer to this question is out of his competence. So he asked you, an erudite expert, to offer him help. The kind behavior would be highly appreciated by him.


Input

The input data consists of several figures. The first line of the input for each figure contains a single integer n, the number of vertexes in the figure. (0 <= n <= 1000).

In the following n lines, each contain a pair of real numbers, which describes the coordinates of the vertexes, (xi, yi). The figure in each test case starts from the first vertex to the second one, then from the second to the third, ���� and so on. At last, it closes from the nth vertex to the first one.

The input ends with an empty figure (n = 0). And this figure not be processed.


Output

As shown below, the output of each figure should contain the figure number and a colon followed by the area of the figure or the string "Impossible".

If the figure is a polygon, compute its area (accurate to two fractional digits). According to the input vertexes, if they cannot form a polygon (that is, one line intersects with another which shouldn't be adjoined with it, for example, in a figure with four lines, the first line intersects with the third one), just display "Impossible", indicating the figure can't be a polygon. If the amount of the vertexes is not enough to form a closed polygon, the output message should be "Impossible" either.

Print a blank line between each test cases.


Sample Input

5
0 0
0 1
0.5 0.5
1 1
1 0
4
0 0
0 1
1 0
1 1
0


Output for the Sample Input

Figure 1: 0.75

Figure 2: Impossible


思路:这个题很明显,是个线段判交问题。。。先用跨立实验判断是否有相交的线段,若有则直接输出impossible、相反若没有则保留两位小数输出Area(注:这里面积输出要注意啊!我刚开始就是没注意这,让我想了半天 都没找到哪错了)

判断两条线断是否相交,首先我门要提出两个实验:

(1)快速排斥实验

(2)跨立实验

这两个实验的具体实现内容,大家可以看我的另一篇博客(是这个题的简化版): ZOJ--1648:Circuit Board(跨立实验线段判交)


最后 求面积的 就直接用叉乘的方法求。

叉乘:假设A、B、C、D四个点AB×CD=(A.x-B.x)*(C.y-D.y)-(A.y-B.y)*(C.x-D.x)


java AC代码:

import java.util.Scanner;

public class ZOJ_1010 {
	static int n,flag=0;
	static double arr[][]=new double[1000][2];
	public static void main(String[] args) {
		Scanner s=new Scanner(System.in);
		while((n=s.nextInt())!=0){
			for(int i=0;i<n;i++){
				arr[i][0]=s.nextDouble();
				arr[i][1]=s.nextDouble();
			}
			if(flag!=0)System.out.println();
			flag++;
			System.out.print("Figure "+flag+": ");
			if(n<3||!judge()){//少于3个点就围不成一个多边形
				System.out.println("Impossible");
				continue;
			}
			if(judge())
				System.out.printf("%.2f\n",area());
		}
	}
	public static boolean judge(){
		for(int i=3;i<n;i++){//不判断最后一条,因为最后一个点是从第n个点到第0个点
			for(int j=1;j<i-1;j++)
				if(cross(i-1,i,j-1,j))
					return false;
		}
		for(int i=2;i<n-1;i++){//判断最后一条
			if(cross(i-1,i,n-1,0))
				return false;
		}
		return true;
	}
	public static boolean cross(int i1,int i2,int j1,int j2){
		double a=cal(arr[i1][0]-arr[j1][0],arr[i1][1]-arr[j1][1],arr[j2][0]-arr[j1][0],arr[j2][1]-arr[j1][1]);
		double b=cal(arr[j2][0]-arr[j1][0],arr[j2][1]-arr[j1][1],arr[i2][0]-arr[j1][0],arr[i2][1]-arr[j1][1]);
		double c=cal(arr[j1][0]-arr[i1][0],arr[j1][1]-arr[i1][1],arr[i2][0]-arr[i1][0],arr[i2][1]-arr[i1][1]);
		double d=cal(arr[i2][0]-arr[i1][0],arr[i2][1]-arr[i1][1],arr[j2][0]-arr[i1][0],arr[j2][1]-arr[i1][1]);
		if(a*b>=0&&c*d>=0)
			return true;
		return false;
	}
	public static double cal(double a,double b,double c,double d){
		return a*d-c*b;
	}
	public static double area(){
		double area=0;
		for(int i=2;i<n;i++){
			area+=cal(arr[i-1][0]-arr[0][0],arr[i-1][1]-arr[0][1],arr[i][0]-arr[0][0],arr[i][1]-arr[0][1]);
		}
		return Math.abs(area)/2.0;
	}
}


版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/yf224/article/details/73471739

智能推荐

API网关之动态路由_api路由-程序员宅基地

文章浏览阅读1.3k次。AIP网关 动态路由_api路由

强一致性 弱一致性 最终一致性-程序员宅基地

文章浏览阅读4.5k次,点赞4次,收藏22次。在足球比赛里,一个球员在一场比赛中进三个球,称之为帽子戏法(Hat-trick)。在分布式数据系统中,也有一个帽子原理(CAP Theorem),不过此帽子非彼帽子。CAP原理中,有三个要素:一致性(Consistency)可用性(Availability)分区容忍性(Partition tolerance)CAP原理指的是,这三个要素最多只能同时实现两点,不可能三者兼顾。因此在进行分..._强一致性 弱一致性 最终一致性

Android 相册图库功能,按时间排序_android 加载网络图片列表时间轴相册-程序员宅基地

文章浏览阅读8.6k次,点赞4次,收藏21次。TimeAlbum 时间相册功能说明1、图片和视频资源根据日期排序显示。 2、图片视频预览功能,图片、视频预览带缓存功能。 3、单个图片或视频可进行删除及分享操作。 4、多张图片进行分享功能,多张图片和视频进行删除功能。 5、Decoration可自定义扩展。 6、图片显示可自定义扩展。 7、图片视频可自定义预览操作。依赖如何使用只需要继承AlbumFr..._android 加载网络图片列表时间轴相册

echarts动态时间轴,以秒为单位更新_echarts实现以秒为单位的动态折线图显示-程序员宅基地

文章浏览阅读2.2w次。echarts官网上的案例是按天来更新数据的http://echarts.baidu.com/demo.html#dynamic-data2 现在我需要改成以秒为单位动态刷新的案例,类似于股票实时刷新的那种,代码位置http://download.csdn.net/download/u013720726/9963108_echarts实现以秒为单位的动态折线图显示

macOS VSCode 配置 Go 编程环境_failed to run "go env env,-json,goprivate,gomod,go-程序员宅基地

文章浏览阅读1.5k次。macOS VSCode 配置 Go 编程环境笔者使用 macOS BigSur 安装完 Go 1.16.6 和 VSCode Go插件,然后运行时,往往会报诸如下面的错误:build esc: cannot load xxx : malformed module path “xxx”: missing dot in first path elementwarning: GOPATH set to GOROOT (/Users/xxx/go/) has no effect实际上,这都是由于 GO_failed to run "go env env,-json,goprivate,gomod,gowork,goenv,gotoolchain": s

C++多线程并发(三)---线程同步之条件变量_线程同步condition variables-程序员宅基地

文章浏览阅读1.7w次,点赞66次,收藏303次。一、何为条件变量在前一篇文章《C++多线程并发编程(二)—线程同步之互斥锁》中解释了线程同步的原理和实现,使用互斥锁解决数据竞争访问问题,算是线程同步的加锁原语,用于排他性的访问共享数据。我们在使用mutex时,一般都会期望加锁不要阻塞,总是能立刻拿到锁,然后尽快访问数据,用完之后尽快解锁,这样才能不影响并发性和性能。如果需要等待某个条件的成立,我们就该使用条件变量(condition var..._线程同步condition variables

随便推点

SQL语句操作优先级顺序_inner left优先级-程序员宅基地

文章浏览阅读1.7w次,点赞4次,收藏18次。SQL语句操作优先级顺序原文所在SQL 不同于与其他编程语言的最明显特征是处理代码的顺序。在大数编程语言中,代码按编码顺序被处理,但是在SQL语言中,第一个被处理的子句是FROM子句,尽管SELECT语句第一个出现,但是几乎总是最后被处理。 每个步骤都会产生一个虚拟表,该虚拟表被用作下一个步骤的输入。这些虚拟表对调用者(客户端应用程序或者外部查询)不可用。只是最后一步生成的表才会返回_inner left优先级

C51单片机:点击一次按键,实现LED显示状态的亮灭转变_单片机按键按一次亮一个灯-程序员宅基地

文章浏览阅读9.1k次,点赞3次,收藏30次。#include <REGX52.H>sbit led=P1^0;//p1.0口接ledsbit button=P3^0;//p3.0口接控制int i,j;//整数i,jvoid main( )//主函数{ led=1;//led初始状态 while(1)//循环 { if(button==0)//按下开关 { for(i=0;i<10;i++);//延时去抖 while(button==0);//检测松手 l._单片机按键按一次亮一个灯

Python 爬取红酒网站https://www.vivino.com_vivino网站爬虫-程序员宅基地

文章浏览阅读2.6w次。用到了进程池,代理import requestsimport jsonimport jsonpathimport pymysqlimport queuefrom multiprocessing import Poolimport randomrequests.packages.urllib3.disable_warnings()# 创建连接db = pymysql.c..._vivino网站爬虫

nginx中try_files $uri $uri/ /index.html的作用 和 $uri的含义

的含义是:首先尝试按照请求的URI去寻找对应的文件,如果找不到,再尝试将请求作为目录处理,如果还是找不到,最后就返回。这对于单页应用来说非常有用,因为无论用户请求的是什么URL,服务器都会返回同一个HTML文件(即。:这是Nginx内置的一个变量,代表当前请求的URI,不包括参数部分。例如,如果请求的URL是。:尝试将请求作为目录处理,如果这个目录存在,Nginx会试图返回该目录下的默认文件(通常是。这句话是Nginx服务器配置中的一条指令,用于设置处理请求的策略。都无法找到对应的文件或目录,那么就返回。

KUKA机器人KR3 R540维护保养——更换齿形带

我们知道机器人长时间运行后,部分轴的齿形带会发生磨损,张力也会发生变化,这时就需要更换齿形带。本篇文章还是以KUKA机器人KR3 R540的A5轴为例,对KUKA机器人更换轴A2、A3、A5齿形带的操作方法进行介绍,有需要的可以参考。4、从齿形带轮上取下旧的齿形带A5。2、接下来用T10规格的内梅花扳手将盖罩A5上的4 颗螺丝拧出,放到指定位置,易于保管。1、我们前期需要准备一些工具:开口扳手(7毫米)、内六角梅花扳手、内六角扳手。三、对机器人A2、A3轴齿形带的更换方法步骤和以上类似。

【Qt QML】QLibrary加载共享库中的类

QLibrary是一个用于加载动态链接库(或称为共享库)的类。它提供了一种独立于平台的方式来访问库中的功能。在QLibrary中,可以通过构造函数或setFileName()方法设置要加载的库文件名。当加载库文件时,QLibrary会搜索所有平台特定的库位置,除非传入的文件名具有绝对路径。如果传入的文件名具有绝对路径,那么会首先尝试加载该目录。如果该文件找不到,QLibrary会使用不同的平台特定的文件前缀或后缀再次尝试。