算法復習——計算幾何基礎(zoj1081)

題目:

Statement of the Problem

Several drawing applications allow us to draw polygons and almost all of them allow us to fill them with some color. The task of filling a polygon reduces to knowing which points are inside it, so programmers have to colour only those points.

You're expected to write a program which tells us if a given point lies inside a given polygon described by the coordinates of its vertices. You can assume that if a point is in the border of the polygon, then it is in fact inside the polygon.

Input Format

The input file may contain several instances of the problem. Each instance consists of: (i) one line containing integers N, 0 < N < 100 and M, respectively the number of vertices of the polygon and the number of points to be tested. (ii) N lines, each containing a pair of integers describing the coordinates of the polygon's vertices; (iii) M lines, each containing a pair of integer coordinates of the points which will be tested for "withinness" in the polygon.

You may assume that: the vertices are all distinct; consecutive vertices in the input are adjacent in the polygon; the last vertex is adjacent to the first one; and the resulting polygon is simple, that is, every vertex is incident with exactly two edges and two edges only intersect at their common endpoint. The last instance is followed by a line with a 0 (zero).

Output Format

For the ith instance in the input, you have to write one line in the output with the phrase "Problem i:", followed by several lines, one for each point tested, in the order they appear in the input. Each of these lines should read "Within" or "Outside", depending on the outcome of the test. The output of two consecutive instances should be separated by a blank line.

Sample Input

3 1
0 0
0 5
5 0
10 2
3 2
4 4
3 1
1 2
1 3
2 2
0

Sample Output

Problem 1:
Outside

Problem 2:
Outside
Within

題解:

一道很基礎的計算幾何題···判斷點是否在一個多邊形內··方法是過該點做一條平行的射線看與多邊形的交點是否為奇數個···開始的時候可以先判斷下共線來優化一下···

代碼:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<cctype>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
int R()
{int i=1,f=0;char c;for(c=getchar();(c<'0'||c>'9')&&c!='-';c=getchar());if(c=='-'){i=-1;c=getchar();}for(;c>='0'&&c<='9';c=getchar())f=(f<<3)+(f<<1)+c-'0';return i*f;
}
const int N=105;
struct point 
{int x;int y;
}q[N],p;
int n,m,T;inline point operator - (point a,point b)
{point t;t.x=a.x-b.x;t.y=a.y-b.y;return t;
}inline int operator * (point a,point b)
{return a.x*b.y-a.y*b.x;
}inline int dot(point a,point b)
{return a.x*b.x+a.y*b.y;
}inline void pre()
{q[n]=q[0];int temp;for(int i=0;i<n;i++)temp+=q[i]*q[i+1];if(temp<=0){reverse(q,q+n);q[n]=q[0];}
}inline bool check(point a,point b,point c)
{point temp1=a-c;point temp2=b-c;if(temp1*temp2!=0)  return false;else{if(dot(temp1,temp2)<=0)  return true;else return false;}
}inline bool jud()
{int cnt=0;for(int i=0;i<n;i++){if(check(q[i],q[i+1],p))  return true;int t1=q[i].y-p.y;int t2=q[i+1].y-p.y;point temp1=q[i]-p;point temp2=q[i+1]-p;if((temp1*temp2>=0&&t1>=0&&t2<0)||(temp1*temp2<=0&&t1<0&&t2>=0))cnt++;}if(cnt%2!=0)  return true;else return false;
}
int main()
{//freopen("a.in","r",stdin);while(true){n=R();if(n==0)  break;T++;if(T!=1)  cout<<endl;cout<<"Problem "<<T<<":"<<endl;m=R();for(int i=0;i<n;i++)q[i].x=R(),q[i].y=R();pre();for(int i=1;i<=m;i++){p.x=R();p.y=R();if(jud())  cout<<"Within"<<endl;else cout<<"Outside"<<endl;}}return 0;
}

?

轉載于:https://www.cnblogs.com/AseanA/p/6662189.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/252980.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/252980.shtml
英文地址,請注明出處:http://en.pswp.cn/news/252980.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

亞馬遜Rekognition發布針對人臉檢測、分析和識別功能的多項更新

今天亞馬遜Rekognition針對人臉檢測、分析和識別功能推出了一系列更新。這些更新將為用戶帶來多項能力的改今&#xff0c;包括從圖像中檢測出更多人臉、執行更高精度的人臉匹配以及獲得圖像中的人臉得到更準確的年齡、性別和情感屬性。Amazon Rekognition的客戶可以從今天開始使…

(轉)CentOS分區操作詳解

CentOS分區操作詳解 原文&#xff1a;http://blog.csdn.net/yonggeit/article/details/77924393 磁盤分區 分區格式的兩種選擇&#xff1a;MBR和GPT 分區命令&#xff1a; parted的操作都是實時生效的&#xff0c;小心使用&#xff0c;主要是用于大于2T硬盤&#xff0c;支持MBR…

linux驅動中地址空間轉換

在linux kernel 中&#xff0c;物理地址是不能直接使用的&#xff0c;必須通過轉換才可以。轉換分為兩種&#xff0c; 靜態和動態。 靜態就是下面那種&#xff0c;不過&#xff0c;靜態的地址轉換&#xff0c;還需要在kernel 初始化的時候作映射。 動態映射是使用 ioremap 函…

getClass()和.class的區別

getClass()和.class的區別 在學習反射時想到了這個問題&#xff0c;.getClass()和.class有沒有什么區別&#xff1f; 當然&#xff0c;最明顯的區別就是.getClass()是一個對象實例的方法&#xff0c;只有對象實例才有這個方法&#xff0c;具體的類是沒有的。類的Class類實例是通…

華為敏捷 DevOps 實踐:產品經理如何開好敏捷回顧會議

開篇小故事&#xff1a;前幾年&#xff0c;一本叫《沉思錄》的書在國內突然曝光度很多&#xff0c;因為前某國家領導人“擺案頭&#xff0c;讀百遍”。《沉思錄》是古羅馬皇帝馬可奧勒寫給自己的書&#xff0c;內容大部分是在鞍馬勞頓中寫的。其實有一句“我們所聽到的不過只是…

斐波那契數列的鬼畜的性質

斐波那契數列的鬼畜的性質 斐波那契數列定理1 \(gcd(f[i],f[i1])1\) 利用輾轉相減法 證明&#xff1a;\(gcd(f[i],f[i1])\)\(gcd(f[i1]-f[i],f[i])\)\(gcd(f[i-1],f[i])\)\(....\)\(gcd(f[1],f[2])1\) 斐波那契數列定理2 \(f[mn]f[m-1]f[n]f[m]f[n1]\) 證明&#xff1a;\(f[mn]…

xml與java對象轉換 -- XStreamAlias

XStreamAlias使用 一、 特點: 簡化的API; 無映射文件; 高性能,低內存占用; 整潔的XML; 不需要修改對象;支持內部私有字段,不需要setter/getter方法 提供序列化接口; 自定義轉換類型策略; XStream的優點很多&#xff0c;但是也有一些小bug&#xff0c;比如在定義別名中的下劃線…

windows下安裝zabbix_agent

Server端在linux系統上&#xff0c;server端版本為2.2.6&#xff0c;是以前就裝好的已經跑了很久的穩定版。目前的需求是要將新業務的服務器添加到該監控隊列。而這些服務器是windows系統。 第一次下載了最新版的zabbix_agent for windows。按照正常程序安裝完成后&#xff0c;…

JS和Jquery獲取和修改label的值

獲取值&#xff1a; label標簽在JS和Jquery中使用不能像其他標簽一樣用value獲取它的值&#xff1a; var labeldocument.getElementById("id");var valuelabel.value;var value$("#id").val();可以這樣&#xff1a;JS: var labeldocument.getElementById(&…

Linux內核訪問外設I/O--動態映射(ioremap)和靜態映射(map_desc)

本篇文章主要介紹了"Linux內核訪問外設I/O--動態映射(ioremap)和靜態映射(map_desc)"&#xff0c;主要涉及到Linux內核訪問外設I/O--動態映射(ioremap)和靜態映射(map_desc)方面的內容&#xff0c;對于Linux內核訪問外設I/O--動態映射(ioremap)和靜態映射(map_desc)感…

點擊顯示隱藏盒子函數

示例&#xff1a;&#xff08;手機導航欄&#xff09; <header> <div class"logo"></div> <p class"text">微蜂傳媒</p> <div class"nav_btn" οnclick"showHide(.dropdown_menu)"></div> …

MyBatis 緩存機制

Mybatis 有兩級緩存&#xff1a; 一級緩存&#xff1a; 也稱為本地緩存&#xff0c;SqlSession級別的緩存。一級緩存是一直開啟的&#xff1b; 與數據庫同一次會話期間查詢到的數據會放在本地緩存中&#xff0c;以后如果需要獲取相同的數據&#xff0c;直接從緩存中拿&#xff…

Android虛擬化引擎VirtualApp探究

2019獨角獸企業重金招聘Python工程師標準>>> 介紹 首先需要說明的是&#xff0c;VirtualApp并不是前些陣子滴滴開源的插件化框架VirtualApk。 VirtualApp是一個更加黑科技的東西&#xff0c;他可以創建一個虛擬空間&#xff0c;你可以在虛擬空間內任意的安裝、啟動和…

揭開全景相機的創業真相

&#xff08;Bubl全景相機&#xff09; 國外一開源&#xff0c;國內就自主。這在VR&#xff08;虛擬現實&#xff09;領域體現的淋漓盡致——Google的Cardborad一開源&#xff0c;國內就有數百家廠商蜂擁做了各種插手機的VR盒子。到了全景相機&#xff0c;這一幕似乎又開始重演…

一個厲害的網站

2019獨角獸企業重金招聘Python工程師標準>>> dromara 發現一個網站&#xff0c;發現上面的開源項目真的都非常厲害誒。 轉載于:https://my.oschina.net/miaojiangmin/blog/2934221

最全VR產業鏈全景圖(必收藏)

http://www.360doc.com/content/16/0324/20/28622037_544974325.shtml

本地計算機綁定域名訪問

我們知道localhost綁定的是本地主機IP&#xff08;127.0.0.1&#xff09;&#xff0c;那么我們能不能自定義綁定本地主機IP地址呢&#xff1f;答案是肯定的&#xff0c;同修改hosts文件&#xff0c;我們可以實現上面的需求。 打開本地C盤&#xff0c;找到Windows文件夾-->Sy…

Tomcat配置及原理文章

同一tomcat實現多端口多域名訪問 tomcat源碼分析(第一篇 從整體架構開始) tomcat源碼分析(第二篇 tomcat啟動過程詳解) tomcat源碼分析(第三篇 tomcat請求原理解析--Connector源碼分析) tomcat源碼分析(第四篇 tomcat請求處理原理解析--Container源碼分析)轉載于:https://www.c…

windwon安裝macaca環境

一 安裝配置java1.安裝java_jdk &#xff0c;安裝過程中順帶一起安裝jre(1)選擇【新建系統變量】--彈出“新建系統變量”對話框&#xff0c;在“變量名”文本框輸入“JAVA_HOME”,在“變量值”文本框輸入JDK的安裝路徑&#xff0c; 如“C&#xff1a;/Java/jdk1.6.0_25”(2)在“…