多边形内有2枚钉子的图形_当多边形内没有钉子

多边形内有2枚钉子的图形_当多边形内没有钉子Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)Total Submission(s) : 24 Accepted Submission(s) : 7Problem DescriptionStatement of the Problem Several drawing applications allow us to draw polygons and almost all of the

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 24 Accepted Submission(s) : 7
Problem Description
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

题解
判断点在多边形内部

#include<bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
double pi = acos(-1);
int sgn(double x){ 
   
    if(fabs(x) < eps)return 0;
    if(x < 0)return -1;
    return 1;
}
int dcmp(double x,double y){ 
   
    if(fabs(x - y) < eps)return 0;
    if(x < y)return -1;
    return 1;
}
struct Point{ 
   
    double x,y;
    Point(){ 
   }
    Point(double x,double y):x(x),y(y){ 
   }
    Point operator+(Point B){ 
   return Point(x + B.x,y +B.y);}
    Point operator-(Point B){ 
   return Point(x - B.x,y - B.y);}
    Point operator*(double k){ 
   return Point(x * k,y * k);}
    Point operator/(double k){ 
   return Point(x / k,y / k);}
    bool operator==(Point B){ 
   return !dcmp(x,B.x) && !dcmp(y,B.y);}
};
typedef Point Vector;
double Dot(Vector A,Vector B){ 
   
    return A.x * B.x + A.y * B.y;
}
double Cross(Vector A,Vector B){ 
   
    return A.x * B.y - A.y * B.x;
}
double Distance(Point A,Point B){ 
   
    return sqrt((A.x - B.x) * (A.x - B.x) + (A.y - B.y) * (A.y - B.y));
}
struct Line{ 
   
    Point p1,p2;
    Line(){ 
   }
    Line(Point p1,Point p2):p1(p1),p2(p2){ 
   }
    Line(Point p,double angle){ 
   
        p1 = p;
        if(sgn(angle - pi / 2) == 0)p2 = (p1 + Point(0,1));
        else p2 = (p1 + Point(1,tan(angle)));
    }
    Line(double a,double b,double c){ 
   
        if(sgn(a) == 0)p1 = Point(0,-c / b),p2 = Point(1,-c / b);
        else if(sgn(b) == 0)p1 = Point(-c / a,0),p2 = Point(-c / a,1);
        else p1 = Point(0,-c / b),p2 = Point(1,(-c - a) / b);
    }
};
typedef Line Segment;
int Point_Lint_Relation(Point p,Line v){ 
   
    int c = sgn(Cross(p - v.p1,v.p2 - v.p1));
    if(c == -1)return 1;//点在直线的左边
    else if(c == 0)return 0;//点在直线上
    else return 2;//点在直线的右边
}
bool Point_On_Line(Point p,Segment v){ 
   
    return sgn(Cross(p - v.p1,p - v.p2)) == 0 && Dot(v.p1 - p,v.p2 - p) <= 0;
}


bool Point_In_Polygon(Point pt,Point *p,int n){ 
   
    for(int i = 0;i < n;i ++)
        if(p[i] == pt)return true;
    for(int i = 0;i < n;i ++){ 
   
        Line v(p[i],p[(i + 1) % n]);
        if(Point_On_Line(pt,v))return true;
    }
    int num = 0;
    for(int i = 0;i < n;i ++){ 
   
        Point p1 = p[i],p2 = p[(i + 1) % n];
        int dir = Cross(p2 - p1,pt - p1);
        int u = p1.y - pt.y,v = p2.y - pt.y;
        if(dir > 0 && sgn(u) < 0 && sgn(v) >= 0)num ++;
        else if(dir < 0 && sgn(u) >= 0 && sgn(v) < 0)num --;
    }
    return num != 0;
}
const int N = 110;
Point polygons[N];
int main()
{ 
   
    int n,m;
    int x,y;
    int T = 0;
    while(cin>>n,n){ 
   
        cin>>m;
        if(T != 0)printf("\n");
        printf("Problem %d:\n", ++T);
        for(int i = 0;i < n;i ++){ 
   
            cin>>x>>y;
            polygons[i].x = x,polygons[i].y = y;
        }
        for(int i = 0;i < m;i ++){ 
   
            cin>>x>>y;
            Point pt(x,y);
            if(Point_In_Polygon(pt,polygons,n))cout<<"Within"<<endl;
            else cout<<"Outside"<<endl;
        }
    }
    return 0;
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/168620.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Qt 资料大全[通俗易懂]

    Qt 资料大全[通俗易懂]全网最强整理,Qt官网、编码风格、GitHub&Third-Party、社区论坛、博客、书籍等资源,应有尽有。

    2022年7月17日
    21
  • 【学习】——提问的智慧[通俗易懂]

    【学习】——提问的智慧[通俗易懂]声明许多项目在他们的使用协助/说明网页中链接了本指南,这么做很好,我们也鼓励大家都这么做。但如果你是负责管理这个项目网页的人,请在超链接附近的显著位置上注明:本指南不提供此项目的实际支持服务!我们已经深刻领教到少了上述声明所带来的痛苦。因为少了这点声明,我们不停地被一些白痴纠缠。这些白痴认为既然我们发布了这本指南,那么我们就有责任解决世上所有的技术问题。如果你是因为需要某些协助而正…

    2022年7月26日
    7
  • oracle物化视图的刷新命令_物化视图增量刷新

    oracle物化视图的刷新命令_物化视图增量刷新物化视图(MATERIALIZEDVIEW)是一个包含查询结果的数据库对象。将经常使用的数据拷贝并存储下来,在查询时就可以直接返回数据。本质上是一个物理表,会占用磁盘空间。本文主要记录了物化视图刷新的方法、时机等相关特性。

    2025年6月6日
    0
  • Mac安装yarn

    Mac安装yarn1.全局安装yarnnpmi-gyran#查看安装是否成功yarn–version2.设置淘宝镜像,下载速度更快yarnconfigsetregistryhttps://registry.npm.taobao.com3.配置环境变量echo”PATH=$PATH:~/.yarn/bin”>>~/.bash_profile&&source.bash_profile…

    2022年5月26日
    350
  • Visual Studio 2012 Ultimate旗舰版序列号

    Visual Studio 2012 Ultimate旗舰版序列号VisualStudio2012Ultimate旗舰版序列号:YKCW6-BPFPF-BT8C9-7DCTH-QXGWCYQ7PR-QTHDM-HCBCV-9GKGG-TB2TM转载于:https://www.cnblogs.com/jiayue360/p/3166844.html

    2022年7月20日
    11
  • host配置_win10hosts文件配置异常

    host配置_win10hosts文件配置异常host添加地址今天是我第一天入职,坐到工位的第一件事就是配置host,因为连接测试环境需要本地授权,所以要配置。这里简单记录下配置中遇到的问题和操作的步骤操作环境是win10,之前公司一直使用的win7系统,所以对win10系统不是很熟悉,还要多多使用才行。使用win自带功能添加1.点击【此电脑】,路径为:C:\Windows\System32\drivers\etc选择hos…

    2022年4月20日
    200

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号