1. 程式人生 > >Intersection (計算幾何)

Intersection (計算幾何)

Output

For each test case in the input file, the output file should contain a line consisting either of the letter "T" if the line segment intersects the rectangle or the letter "F" if the line segment does not intersect the rectangle. 果然需要積累模板...一下子就用到了

簡單說就是先判斷線段是不是在矩形內,再分別判斷和四條邊是否相交...

判斷相交貌似一直是比較棘手的問題啊,不過這種線線相交還好...

#include<cstdio>
#include<cstring>
#include<cmath>
#define eps 1e-8
#define zero(x) (((x)>0?(x):-(x))<eps)
struct point
{
    double x, y;
};

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 dmult(point p1, point p2, point p0)
{
    return (p1.x - p0.x)*(p2.x - p0.x) + (p1.y - p0.y)*(p2.y - p0.y);
}

int dots_inline(point p1, point p2, point p3)
{
    return zero(xmult(p1, p2, p3));
}

int dot_online_in(point p, point l1, point l2)
{
    return zero(xmult(p, l1, l2)) && (l1.x - p.x)*(l2.x - p.x)<eps && (l1.y - p.y)*(l2.y - p.y)<eps;
}

int same_side(point p1, point p2, point l1, point l2)
{
    return xmult(l1, p1, l2)*xmult(l1, p2, l2)>eps;
}

int intersect_in(point u1, point u2, point v1, point v2)
{
    if (!dots_inline(u1, u2, v1) || !dots_inline(u1, u2, v2))
        return !same_side(u1, u2, v1, v2) && !same_side(v1, v2, u1, u2);
    return dot_online_in(u1, v1, v2) || dot_online_in(u2, v1, v2) || dot_online_in(v1, u1, u2) || dot_online_in(v2, u1, u2);
}
int main()
{
    point x, y;
    point d1, d2, d3, d4;
    int cas, i, j, k;
    scanf("%d", &cas);
    while (cas--)
    {
        scanf("%lf%lf%lf%lf%lf%lf%lf%lf", &x.x, &x.y, &y.x, &y.y, &d4.x, &d4.y, &d2.x, &d2.y);
        d1.x = d4.x, d1.y = d2.y;
        d3.x = d2.x, d3.y = d4.y;
        if ((x.x >= d2.x&&x.x <= d4.x) || (x.x >= d4.x&&x.x <= d2.x))
            if ((x.y >= d2.y&&x.y <= d4.y) || (x.y >= d4.y&&x.y <= d2.y))
            {
                printf("T\n");
                continue;
            }
        if ((y.x >= d2.x&&y.x <= d4.x) || (y.x >= d4.x&&y.x <= d2.x))
            if ((y.y >= d2.y&&y.y <= d4.y) || (y.y >= d4.y&&y.y <= d2.y))
            {
                printf("T\n");
                continue;
            }
        if (intersect_in(x, y, d1, d2) || intersect_in(x, y, d3, d2) || intersect_in(x, y, d3, d4) || intersect_in(x, y, d1, d4))
            printf("T\n");
        else printf("F\n");
    }
    return 0;
}

相關推薦

POJ - 1410 - Intersection計算幾何

You are to write a program that has to decide whether a given line segment intersects a given rectangle. An example: line: start point: (4,9) end

Intersection 計算幾何

Output For each test case in the input file, the output file should contain a line consisting either of the letter "T" if the line segment intersects

HDU 5120 Intersection 計算幾何

題意: 求兩個一模一樣,但位置不一定相同的圓環的環相交面積 分析: 其實就是圓交面積++−− ans=大圓交−2∗一大一小圓交+小圓交 寫圓交函數的時候,注意有三種情況,相含,相交,相離 把重合當成相含了, 一直沒考慮相含卡

HDU 5120 Intersection 計算幾何2014ICPC 北京站現場賽

Intersection Time Limit: 4000/4000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others) Total Submission(s): 1253    Accepte

HDU 5120 Intersection計算幾何

Description 給出兩個圓環的內外半徑和圓心座標,問這兩個圓環的相交面積 Input 第一行為一整數T表示用例組數,每組用例第一行兩個整數r和R分別表示圓環內外半徑,第二行和第三行為兩個圓

HDU 5120 Intersection計算幾何+容斥

Matt is a big fan of logo design. Recently he falls in love with logo made up by rings. The following figures are some famous examples you may know.  A ri

HDU 6055 17多校 Regular polygon計算幾何

ati sort 結果 stream int tom tle eight idt Problem Description On a two-dimensional plane, give you n integer points. Your task is to figur

hdu 6127 : Hard challenge (2017 多校第七場 1008計算幾何

for %d logs opera log val r+ ++ show 題目鏈接 題意:二維平面上有n個點(沒有重疊,都不在原點,任意兩點連線不過原點),每個點有一個權值,用一條過原點的直線把他們劃分成兩部分,使兩部分的權值和的乘積最大。輸出最大的乘積。 極角排序後,將原

Codeforces 849B Tell Your World 計算幾何

continue int ++i 滿足 light size sizeof tar tell 題目鏈接 Tell Your World 題意 給出N個點(i, xi),問是否存在兩條平行的直線,使得每一個點恰好在兩條直線的其中一條上。 每條直線必須穿過至少一個點。 考

POJ 2318 TOYS 計算幾何叉積的運用

line sync ted all names pri char ems sizeof Calculate the number of toys that land in each bin of a partitioned toy box. Mom and dad have

51 Nod 1298 圓與三角形計算幾何

tput body bits truct 大於 簡單 以及 else c++ 題目鏈接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1298 題目: 1298 圓與三角形

POJ - 1039 Pipe計算幾何

std algo double 透明 pac down queue == 是否 http://poj.org/problem?id=1039 題意 有一寬度為1的折線管道,上面頂點為(xi,yi),所對應的下面頂點為(xi,yi-1),假設管道都是不透明的,不反射的,光

Thinking-Bear magic 計算幾何

return name code 數加 重復 ack tps href tac ---- 點我 ---- 題目大意: 給你一個正n邊形及邊長 a和一個正整數L, 求正多邊形的面積s,若s大於L,則連接相鄰兩邊的中點,形成新的正多邊形,重復這個操作直至s小於L:如圖:

【HDOJ5538】House Building計算幾何

for fine tdi ++ spa memset 其余 building while 題意:給定一個n*m的方陣,第i行第j列的高度為a[i][j],問除了下底面之外其余五面的總表面積 n<=50,0<=a[i][j]<=1000 思路:隊友寫的,抱大

Newcoder 110 B.簡單多邊形計算幾何

Description 為了讓所有選手都感到開心, N o w

Codeforces 1079D Barcelonian Distance計算幾何

pen clu sin ces img 鏈接 sta show 直線 題目鏈接:Barcelonian Distance 題意:給定方格坐標,方格坐標上有兩個點A,B和一條直線。規定:直線上沿直線走,否則沿方格走。求A到B的最短距離。 題解:通過直線到達的:A、B兩點都

2018.10.03 bzoj1610:Line連線遊戲計算幾何

傳送門 計算幾何入門題,直接算出所有直線的斜率看有多少種不同就行了。 注意處理斜率不存在的情況。 程式碼: #include<bits/stdc++.h> #define N 205 usi

2018.10.03 bzoj3707: 圈地計算幾何

傳送門 計算幾何好題。 本蒟蒻表示不看題解只會O(n3)O(n^3)O(n3)。 正解是先考慮把直線按照斜率從小到大排序,然後把點按座標排序。 這樣每次列舉到直線(a,b)(a,b)(a,b)時,離直

51Nod 1265 四點共面計算幾何

基準時間限制:1 秒 空間限制:131072 KB 分值: 0 難度:基礎題 給出三維空間上的四個點(點與點的位置均不相同),判斷這4個點是否在同一個平面內(4點共線也算共面)。如果共面,輸出"Yes",否則輸出"No"。 Input 第1行:一個數T,表示輸入的測