• HDU 1086 You can Solve a Geometry Problem too


    计算几何基础问题,具体可以参考算法导论p577,确定两个线段是否相交

     1 #include <iostream>
     2 #include <vector>
     3 #include <algorithm>
     4 
     5 using namespace std;
     6 
     7 struct point{
     8     double x,y;
     9 };
    10 
    11 struct segment{
    12     point p1,p2;
    13 };
    14 
    15 double direction(point p1, point p2, point p3){
    16     return (p2.x-p1.x)*(p3.y-p1.y)-(p2.y-p1.y)*(p3.x-p1.x);
    17 }
    18 
    19 bool on_segment(point p1, point p2, point p3){
    20     if( min(p1.x,p2.x) <= p3.x && p3.x <= max(p1.x,p2.x) && min(p1.y,p2.y) <= p3.y && p3.y <= max(p1.y,p2.y) )
    21         return true;
    22     else return false;
    23 }
    24 
    25 bool segment_intersect(point p1,point p2,point p3,point p4){
    26     double d1 = direction(p3,p4,p1);
    27     double d2 = direction(p3,p4,p2);
    28     double d3 = direction(p1,p2,p3);
    29     double d4 = direction(p1,p2,p4);
    30     if( d1*d2 <0 && d3*d4 < 0) return true;
    31     else if(d1 == 0 && on_segment(p3,p4,p1)) return true;
    32     else if(d2 == 0 && on_segment(p3,p4,p2)) return true;
    33     else if(d3 == 0 && on_segment(p1,p2,p3)) return true;
    34     else if(d4 == 0 && on_segment(p1,p2,p4)) return true;
    35     else return false;
    36 }
    37 
    38 int main(){
    39     int N;
    40     while(cin >>N && N){
    41         vector<segment> seg(N);
    42         for(int i = 0; i < N; i ++ )
    43             cin >> seg[i].p1.x>>seg[i].p1.y>>seg[i].p2.x>>seg[i].p2.y;
    44         int cnt  = 0;
    45         for(int i = 0 ; i < N; i ++ ){
    46             for(int j = i+1; j < N; j ++ ){
    47                 if(segment_intersect(seg[i].p1,seg[i].p2,seg[j].p1,seg[j].p2)) cnt++;
    48             }
    49         }
    50         cout<< cnt<<endl;
    51     }
    52     return 0;
    53 }
  • 相关阅读:
    ER模型
    一道人人的笔试题
    关系代数运算
    推荐两个不错的CAD二次开发(.Net)手册
    CAD 致命错误
    CAD二次开发(.NET)之PaletteSet和Palette
    养生
    我看面向对象
    .NET中参数化查询数据库
    自定义按照index和key访问的List
  • 原文地址:https://www.cnblogs.com/xiongqiangcs/p/3012363.html
Copyright © 2020-2023  润新知