Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

套模板。。。

Posted by makuiyu at 2014-03-03 23:05:26 on Problem 1269
#include <algorithm>
#include <iostream>
#include <vector>
#include <string>
#include <queue>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>

#define eps 1e-8
#define zero(x) (((x)>0?(x):-(x))<eps)
#define N 40

using namespace std;

struct point
{
    double x, y;
} p1, p2, p3, p4;

// 判两直线平行
bool Parallel(point u1, point u2, point v1, point v2)
{
    return zero((u1.x-u2.x)*(v1.y-v2.y)-(v1.x-v2.x)*(u1.y-u2.y));
}

// 计算cross product (P1-P0)x(P2-P0)
double Xmult(point p1, point p2, point p0)
{
    return (p1.x-p0.x)*(p2.y-p0.y) - (p2.x-p0.x)*(p1.y-p0.y);
}

// 两点距离
double Distance(point p1,point p2)
{
    return sqrt((p1.x-p2.x)*(p1.x-p2.x) + (p1.y-p2.y)*(p1.y-p2.y));
}

// 点到直线距离
double Disptoline(point p, point l1, point l2)
{
    return fabs(Xmult(p, l1, l2)) / Distance(l1, l2);
}

// 计算两直线交点,注意事先判断直线是否平行
point Intersection(point u1, point u2, point v1, point v2)
{
    point ret = u1;
    double t = ((u1.x-v1.x)*(v1.y-v2.y) - (u1.y-v1.y)*(v1.x-v2.x))
             / ((u1.x-u2.x)*(v1.y-v2.y) - (u1.y-u2.y)*(v1.x-v2.x));
    ret.x += (u2.x-u1.x) * t;
    ret.y += (u2.y-u1.y) * t;
    return ret;
}

int main()
{
    int n, x1, y1, x2, y2, x3, y3, x4, y4;
    scanf("%d", &n);
    printf("INTERSECTING LINES OUTPUT\n");
    while(n--)
    {
        scanf("%d %d %d %d %d %d %d %d", &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4);
        p1.x = x1, p1.y = y1;
        p2.x = x2, p2.y = y2;
        p3.x = x3, p3.y = y3;
        p4.x = x4, p4.y = y4;
        if(Parallel(p1, p2, p3, p4))
        {
            if(zero(Disptoline(p2, p3, p4)))
                printf("LINE\n");
            else
                printf("NONE\n");
        }
        else
        {
            point p = Intersection(p1, p2, p3, p4);
            printf("POINT %.2lf %.2lf\n", p.x, p.y);
        }
    }
    printf("END OF OUTPUT\n");
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator