zoukankan      html  css  js  c++  java
  • poj 1269 计算几何

     1 /**
     2 判断直线位置关系
     3 **/
     4 #include <iostream>
     5 #include <cmath>
     6 #include <cstdio>
     7 using namespace std;
     8 struct point {
     9     double x,y;
    10     point(double x=0,double y=0):x(x),y(y){}
    11 };
    12 
    13 typedef point Vector;
    14 
    15 Vector operator - (point A,point B){
    16     return Vector(A.x-B.x,A.y-B.y);
    17 }
    18 
    19 struct line {
    20     point a,b;
    21 };
    22 double length(Vector v){
    23     return sqrt(v.x*v.x+v.y*v.y);
    24 }
    25 
    26 double  cross(Vector A,Vector B){
    27     return A.x*B.y-A.y*B.x;
    28 }
    29 
    30 double distoline(point P,point A,point B){
    31     Vector v1 =B-A,v2 = P-A;
    32     return fabs(cross(v1,v2))/length(v1);
    33 }
    34 
    35 int main()
    36 {
    37     int n;
    38     cin>>n;
    39     line l1,l2;
    40     cout<<"INTERSECTING LINES OUTPUT"<<endl;
    41     while(n--){
    42         cin>>l1.a.x>>l1.a.y>>l1.b.x>>l1.b.y;
    43         cin>>l2.a.x>>l2.a.y>>l2.b.x>>l2.b.y;
    44         Vector tmp1,tmp2;
    45         tmp1.x = l1.b.x-l1.a.x;
    46         tmp1.y = l1.b.y-l1.a.y;
    47         tmp2.x = l2.b.x-l2.a.x;
    48         tmp2.y = l2.b.y-l2.a.y;
    49         //cout<<tmp1.x<<" "<<tmp1.y<<endl;
    50         //cout<<tmp2.x<<" "<<tmp2.y<<endl;
    51         //cout<<cross(tmp1,tmp2)<<endl;
    52         if(cross(tmp1,tmp2)==0){
    53             if(distoline(l1.a,l2.a,l2.b)==0){
    54                 cout<<"LINE"<<endl;
    55             }else
    56                 cout<<"NONE"<<endl;
    57         }else{
    58             double x,y;
    59             if(l1.a.x==l1.b.x&&l2.a.x!=l2.b.x){
    60                 x = l1.a.x;
    61                 double k = (l2.b.y-l2.a.y)/(l2.b.x-l2.a.x);
    62                 double b = l2.a.y-k*l2.a.x;;
    63                 y = k*x+b;
    64             }else if(l1.a.x!=l1.b.x&&l2.a.x==l2.b.x){
    65                 x = l2.a.x;
    66                 double k = (l1.b.y-l1.a.y)/(l1.b.x-l1.a.x);
    67                 double b = l1.a.y-k*l1.a.x;
    68                 y = k*x+b;
    69             }else{
    70                 double k1= (l1.b.y-l1.a.y)/(l1.b.x-l1.a.x);
    71                 double b1=l1.a.y-k1*l1.a.x;
    72                 double k2 = (l2.b.y-l2.a.y)/(l2.b.x-l2.a.x);
    73                 double b2=l2.a.y-k2*l2.a.x;
    74                 x =(b2-b1)/(k1-k2);
    75                 y = k1*x+b1;
    76             }
    77 
    78             printf("POINT %.2lf %.2lf
    ",x,y);
    79         }
    80     }
    81     cout<<"END OF OUTPUT"<<endl;
    82     return 0;
    83 }
  • 相关阅读:
    Vue+element UI实现“回到顶部”按钮组件
    JS判断字符串长度的5个方法(区分中文和英文)
    从vue源码看Vue.set()和this.$set()
    mac下git安装与使用
    JS数组reduce()方法详解及高级技巧
    vue中router.go、router.push和router.replace的区别
    上传及更新代码到github(以及如何在vscode上提交自己的代码)
    VSCode打开多个项目文件夹的解决方法
    get请求和post请求的区别
    android 进程的优先级
  • 原文地址:https://www.cnblogs.com/Bang-cansee/p/3724241.html
Copyright © 2011-2022 走看看