zoukankan      html  css  js  c++  java
  • ●POJ 1269 Intersecting Lines

    题链:

    http://poj.org/problem?id=1269

    题解:

    计算几何,直线交点

    模板题,试了一下直线的向量参数方程求交点的方法。

    (方法详见《算法竞赛入门经典——训练指南》P257)

    代码:

    #include<cstdio>
    #include<cstring>
    #include<iostream>
    using namespace std;
    struct Point{
    	double x,y;
    	Point(double _x=0,double _y=0):x(_x),y(_y){}
    };
    typedef Point Vector;
    Vector operator + (Vector A,Vector B){return Vector(A.x+B.x,A.y+B.y);}
    Vector operator - (Point A,Point B){return Vector(A.x-B.x,A.y-B.y);}
    Vector operator * (Vector A,double p){return Vector(A.x*p,A.y*p);}
    double operator ^ (Vector A,Vector B){return A.x*B.y-A.y*B.x;}
    double operator * (Vector A,Vector B){return A.x*B.x+A.y*B.y;}
    int N;
    bool Point_on_Line(Point P,Vector v,Point Q){
    	return ((Q-P)^v)==0;
    }
    Point Line_Intersection(Point P,Vector v,Point Q,Vector w){
    	static Vector u; static double t1;
    	u=P-Q;
    	t1=(w^u)/(v^w);
    	return P+v*t1;
    }
    int main(){
    	Vector v,w;
    	Point P,_P,Q,_Q,D;
    	scanf("%d",&N);
    	printf("INTERSECTING LINES OUTPUT
    ");
    	while(N--){
    		scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&P.x,&P.y,&_P.x,&_P.y,&Q.x,&Q.y,&_Q.x,&_Q.y);
    		v=_P-P;	w=_Q-Q;
    		if((v^w)==0){//向量共线
    			if(Point_on_Line(P,v,Q)) printf("LINE");
    			else printf("NONE");
    		}
    		else{
    			D=Line_Intersection(P,v,Q,w);
    			printf("POINT %.2lf %.2lf",D.x,D.y);
    		}
    		printf("
    ");
    	}
    	printf("END OF OUTPUT
    ");
    	return 0;
    }
    

      

  • 相关阅读:
    负环操作 的队列
    The Prices(有依赖性的状压dp)(去不同商店买东西先花不同路费)
    BFS
    滚动数组(细节)(坑点)
    P1908 逆序对(细节)(树状数组解法)
    head
    DaoVoice JS 代码接入示例
    DaoVoice JS 代码接入示例
    DaoVoice JS 代码接入示例
    DaoVoice JS 代码接入示例
  • 原文地址:https://www.cnblogs.com/zj75211/p/8227566.html
Copyright © 2011-2022 走看看