栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

zoj 1081 Points Within

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

zoj 1081 Points Within

#include <iostream>#include <vector>using namespace std;const int MAXSIZE = 100;const double Max = 100000000;struct Point{    double x;    double y;}points[MAXSIZE];Point pc;double CrossMultiply(const Point& a,const Point& b,const Point& c)  {    double x1,y1,x2,y2;    x1= b.x - a.x;    y1 = b.y - a.y;    x2 = c.x - b.x;    y2 = c.y - b.y;    return x1 * y2 - x2 * y1;}   double interSect(const Point& a,const Point& b,const Point& c,const Point& d){    double d1,d2,d3,d4;    d1 = CrossMultiply(a,b,c),        d2 = CrossMultiply(a,b,d),        d3 = CrossMultiply(c,d,a),        d4 = CrossMultiply(c,d,b);    if((d1 * d2) < 0 && (d3 * d4) < 0)         return 1.;    else if((d1 * d2) == 0 || (d3 * d4) == 0)         return 0;    else        return -1.;}bool IsonEdge(const Point& p,const vector<Point>& ptVect){    int i,j,size,minX,maxX,minY,maxY;    size = ptVect.size();    for (i=0;i<size;++i)    {        j= i+1;        if (j==size)        { j=0;        }        if (CrossMultiply(ptVect[i],ptVect[j],p)==0)        { if (p.x>=min(ptVect[i].x,ptVect[j].x)&&p.x<=max(ptVect[i].x,ptVect[j].x)&&p.y>=min(ptVect[i].y,ptVect[j].y)&&p.y<=max(ptVect[i].y,ptVect[j].y)) {     return true; }        }    }    return false;}bool isInside(const Point& p,const vector<Point>& ptVect){    int i,j,num,size;    double d;    num = 0;     size = ptVect.size();    for(i = 0 ; i <size; ++i)    {j= i+1;        if (j==size)        { j=0;        }        d = interSect(p,pc,ptVect[i],ptVect[j]);        if(d > 0) num++;         else if(d == 0)        {  if(ptVect[i].y > 0 || ptVect[j].y > 0)      num++;        }    }    if(num % 2 == 0)         return false;    else        return true;}int main(void){    int N,M,i,pX,pY,cases=0;    while (cin>>N&&N!=0)    {        cin>>M;        if(cases != 0)  cout << endl;        vector<Point> pointVect;        for (i=0;i<N;++i)        { cin>>points[i].x>>points[i].y; pointVect.push_back(points[i]);        }        cout << "Problem " << ++cases<< ":" << endl;        for (i=0;i<M;++i)        { Point p; cin>>p.x>>p.y; pc.x = Max,pc.y = p.y; if (IsonEdge(p,pointVect)) {     cout<<"Within"<<endl;     continue; } else if (isInside(p,pointVect)) {     cout<<"Within"<<endl; } else {     cout<<"Outside"<<endl; }        }    }    return 0;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/376534.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号