zoukankan      html  css  js  c++  java
  • 51nod 1298 圆与三角形 (计算几何)

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1298

    求出圆心到三条线段的最短距离,然后判断是否有顶点在圆外,就把全部情况举出来。

      1 #include <cstdio>
      2 #include <cstring>
      3 #include <cmath>
      4 #include <algorithm>
      5 using namespace std;
      6 const double PI = acos(-1.0);
      7 double torad(double deg) { return deg/180 * PI; }
      8 
      9 struct Point
     10 {
     11   double x, y;
     12   Point(double x=0, double y=0):x(x),y(y) { }
     13 };
     14 
     15 typedef Point Vector;
     16 
     17 Vector operator + (const Vector& A, const Vector& B) { return Vector(A.x+B.x, A.y+B.y); }
     18 Vector operator - (const Point& A, const Point& B) { return Vector(A.x-B.x, A.y-B.y); }
     19 Vector operator * (const Vector& A, double p) { return Vector(A.x*p, A.y*p); }
     20 Vector operator / (const Vector& A, double p) { return Vector(A.x/p, A.y/p); }
     21 
     22 bool operator < (const Point& a, const Point& b)    //结构体运算符的重载
     23 {
     24   return a.x < b.x || (a.x == b.x && a.y < b.y);
     25 }
     26 
     27 const double eps = 1e-8;
     28 int dcmp(double x) { if(fabs(x) < eps) return 0; else return x < 0 ? -1 : 1; }
     29 
     30 bool operator == (const Point& a, const Point &b)
     31 {
     32   return dcmp(a.x-b.x) == 0 && dcmp(a.y-b.y) == 0;
     33 }
     34 
     35 //基本运算:
     36 double dist(const Vector& A, const Vector& B) {return sqrt(pow(A.x-B.x,2)+pow(A.y-B.y,2));}
     37 double Dot(const Vector& A, const Vector& B) { return A.x*B.x + A.y*B.y; }
     38 double Length(const Vector& A) { return sqrt(Dot(A, A)); }
     39 double Angle(const Vector& A, const Vector& B) { return acos(Dot(A, B) / Length(A) / Length(B)); }
     40 double Cross(const Vector& A, const Vector& B) { return A.x*B.y - A.y*B.x; }
     41 double Area2(Point A, Point B, Point C) {return Cross(B-A, C-A);}
     42 
     43 //向量旋转 rad是弧度
     44 Vector Rotate(const Vector& A, double rad)
     45 {
     46   return Vector(A.x*cos(rad)-A.y*sin(rad), A.x*sin(rad)+A.y*cos(rad));
     47 }
     48 //点和直线:
     49 //两直线的交点
     50 Point GetLineIntersection(const Point& P, const Point& v, const Point& Q, const Point& w)
     51 {
     52   Vector u = P-Q;
     53   double t = Cross(w, u) / Cross(v, w);
     54   return P+v*t;
     55 }
     56 
     57 //点到直线的距离
     58 double DistanceToLine(const Point& P, const Point& A, const Point& B)
     59 {
     60   Vector v1=B-A, v2=P-A;
     61   return fabs(Cross(v1,v2)) / Length(v1);
     62 }
     63 
     64 //点到线段的距离
     65 double DistanceToSegment(const Point& P, const Point& A, const Point& B)
     66 {
     67   if(A == B) return Length(P-A);
     68   Vector v1 = B - A, v2 = P - A, v3 = P - B;
     69   if(dcmp(Dot(v1, v2)) < 0) return Length(v2);
     70   else if(dcmp(Dot(v1, v3)) > 0) return Length(v3);
     71   else return fabs(Cross(v1, v2)) / Length(v1);
     72 }
     73 
     74 //点在直线上的投影
     75 Point GetLineProjection(const Point &P, const Point &A,const Point &B)
     76 {
     77     Vector v = B - A;
     78     return A+v*(Dot(v, P-A) / Dot(v, v));
     79 }
     80 
     81 //线段相交判定
     82 bool SegmentProperIntersection(const Point& a1, const Point& a2, const Point& b1, const Point& b2)
     83 {
     84   double c1 = Cross(a2-a1,b1-a1), c2 = Cross(a2-a1,b2-a1),
     85   c3 = Cross(b2-b1,a1-b1), c4=Cross(b2-b1,a2-b1);
     86   return dcmp(c1)*dcmp(c2)<0 && dcmp(c3)*dcmp(c4)<0;
     87 }
     88 
     89 
     90 //判断点在线段上(两个端点除外)
     91 bool OnSegment(const Point& p, const Point& a1, const Point& a2)
     92 {
     93   return dcmp(Cross(a1-p, a2-p)) == 0 && dcmp(Dot(a1-p, a2-p)) < 0;
     94 }
     95 
     96 int main()
     97 {
     98     freopen("a.txt","r",stdin);
     99     int t;
    100     Vector s,p[5];
    101     double r,x;
    102     scanf("%d",&t);
    103     while(t--)
    104     {
    105         scanf("%lf%lf%lf",&s.x,&s.y,&r);
    106         for(int i=0;i<3;i++)
    107             scanf("%lf%lf",&p[i].x,&p[i].y);
    108         int ans=0;
    109         x=DistanceToSegment(s,p[0],p[1]);
    110         if(x<=r) ans++;
    111         x=DistanceToSegment(s,p[0],p[2]);
    112         if(x<=r) ans++;
    113         x=DistanceToSegment(s,p[1],p[2]);
    114         if(x<=r) ans++;
    115         bool flag=0;
    116         for(int i=0;i<3;i++)
    117         {
    118             if(dist(s,p[i])>r)
    119             {
    120                 flag=1;break;
    121             }
    122         }
    123      //   printf("%d %d
    ",ans,flag);
    124         if(ans>=1&&flag) puts("Yes");
    125         else puts("No");
    126     }
    127     return 0;
    128 }
  • 相关阅读:
    iscsi序列一、搭建iscsi存储系统
    LVS系列三、LVS集群-IP-TUN模式
    LVS系列二、LVS集群-DR模式
    LVS系列一、LVS集群-NAT模式
    LINUX 的网站压力测试工具 webbench
    关于 /proc/sys/net/ipv4/下 文件的详细解释
    CentOS7+rsync+sersync实现数据实时同步
    Linux Shell 自动备份脚本
    Vim的强大配置文件
    nginx出现403 Forbidden解决方法
  • 原文地址:https://www.cnblogs.com/nowandforever/p/4593855.html
Copyright © 2011-2022 走看看