码迷,mamicode.com
首页 > 其他好文 > 详细

Geometry

时间:2014-10-04 12:46:36      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   os   ar   sp   div   问题   c   

模版 未完成

 

#include <bits/stdc++.h>
using namespace std;

/*
点、直线、线段、三角形、正方形、矩形、凸多边形、多边形

点/向量与点/向量:
    旋转
    点积叉积
    定比分点
    判平行
    求两点的中垂线
    * 判三点共线
直线与直线:
    判关系
    求交点
    求距离
线段与直线:
    判关系
    求交点
线段与线段:
    判关系(端点)
    求交点
点与直线:
    判关系
    求对称点
    求距离
    最近点
点与线段:
    判关系
    求距离
    最近点
    * 两点在线段同侧/异侧
* 镜面反射问题
*/

const double eps = 1e-8;
const double pi = acos(-1.0);

inline int dcmp(double x)
{
    if(fabs(x)<eps) return 0;
    return x>0?1:-1;
}

//弧度与角度互换
double radian_to_angle(double r) {return r*180/pi;}
double angle_to_radian(double a) {return a*pi/180;}

struct point {
    double x,y;
    point(double _x=0, double _y=0) {x=_x;y=_y;}
    void in() {scanf("%lf%lf",&x,&y);}
    void out() {printf("%lf %lf\n",x,y);}
    friend bool operator < (point &a, point &b) {return a.x<b.x || (a.x==b.x && a.y<b.y);}
    friend point operator + (point &a, point &b) {return point(a.x+b.x,a.y+b.y);}
    friend point operator - (point &a, point &b) {return point(a.x-b.x,a.y-b.y);}
    friend double operator * (point a, point b) {return a.x*b.x+a.y*b.y;}
    friend double operator ^ (point a, point b) {return a.x*b.y-a.y*b.x;}
    void transXY(double B) // 逆时针旋转B度,B是弧度
    {
        double tx = x, ty = y;
        x = tx*cos(B) - ty*sin(B);
        y = tx*sin(B) + ty*cos(B);
    }
};
typedef point Vector;
double dot(point &a,point &b) {return a*b;}
double cross(point &a, point &b) {return a^b;}
double dist(point &a, point &b) {return sqrt((a-b)*(a-b));}

bool Vector_para(Vector a, Vector b)
{
    return dcmp(a^b)==0;
}
bool three_point_in_line(point &a,point &b,point &c)
{
    return Vector_para(b-a,c-a);
}

struct line {
    point u,v;
    double a,b,c;
    line() {}
    line(point _u,point _v)
    {
        u=_u,v=_v;
        a = v.y-u.y;
        b = -(v.x-u.x);
        c = v.y*(v.x-u.x)-v.x*(v.y-u.y);
    }
};

// 判断两直线位置,0重合,1平行,2相交
//verson1: use u,v
int judge_2line_coincide_or_para_or_inter_v1(line &a,line &b)
{
    if(dcmp((a.u-a.v)^(b.u-b.v))==0)
        return dcmp((a.u-b.v)^(b.u-b.v))!=0;
    else return 2;
}
//verson2: use a,b,c
int judge_2line_coincide_or_para_or_inter_v2(line &a,line &b)
{
    if(dcmp(a.a*b.b-b.a*a.b)==0)
        return dcmp(a.a*b.c-a.c*b.a)!=0;
    else return 2;
}

// 求两直线交点,需要先判重合平行
//verson1: use u,v
point interpoint_of_2line(line &a,line &b)
{
    point res = a.u;
    double t = ((a.u-b.u)^(b.u-b.v))/((a.u-a.v)^(b.u-b.v));
    res.x += (a.v.x-a.u.x)*t;
    res.y += (a.v.y-a.u.y)*t;
    return res;
}
//verson2:

//判断两线段相交

int main()
{
    freopen("case.txt","r",stdin);
    point a,b;
    int _;
    cin>>_;
    while(_--)
    {
        a.in(); b.in();
        cout<<Vector_para(a,b)<<endl;
    }
    return 0;
}

 

Geometry

标签:style   blog   color   os   ar   sp   div   问题   c   

原文地址:http://www.cnblogs.com/someblue/p/4005695.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!