判断线段是否与矩形相交

输入格式:

xstart ystart xend yend xleft ytop xright ybottom
Note: The terms top left and bottom right do not imply any ordering of coordinates.


计算几何题对我来说,光是写对就要花很久,而代码还要做到既简洁又易懂真是难上加难……
注意点在于:线段与矩形不相交,这意味着线段不仅可以在矩形外,还可以在矩形内。

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

int main()
{
    int t, x1, y1, x2, y2, xl, yt, xr, yb;
    int a, b, c, f1, f2, f3, f4;
    cin >> t;
    while (t--)
    {
        scanf("%d%d%d%d%d%d%d%d", &x1, &y1, &x2, &y2, &xl, &yt, &xr, &yb);
        if (xl > xr) swap(xl, xr);
        if (yt < yb) swap(yt, yb);
        a = y1 - y2;
        b = x2 - x1;
        c = x1 * y2 - y1 * x2;
        f1 = a * xl + b * yb + c;
        f2 = a * xl + b * yt + c;
        f3 = a * xr + b * yb + c;
        f4 = a * xr + b * yt + c;
        if ((f1>0 && f2>0 && f3>0 && f4>0) || (f1<0 && f2<0 && f3<0 && f4<0))
            printf("F\n");
        else if ((x1 > xr && x2 > xr) || (x1 < xl && x2 < xl))
            printf("F\n");
        else if ((y1 > yt && y2 > yt) || (y1 < yb && y2 < yb))
            printf("F\n");
        else
            printf("T\n");
    }
    return 0;
}
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
判断线段矩形是否相交可以分为两种情况: 1. 线段的两个端点都在矩形内部: 这种情况比较简单,只需要判断线段的两个端点是否都在矩形内部即可。可以通过比较端点的坐标与矩形的边界坐标来实现。如果两个端点都在矩形内部,则线段矩形相交。 2. 线段的两个端点不都在矩形内部: 这种情况稍微复杂一些,需要判断线段是否矩形的四条边相交。可以将矩形的四条边分别表示成直线方程,然后判断线段是否与这些直线相交。如果线段矩形的任意一条相交,则线段矩形相交。 下面是一段示例代码,演示了如何判断线段矩形相交: ```c++ #include <iostream> using namespace std; struct Point { double x, y; }; struct Segment { Point start, end; }; struct Rectangle { Point leftTop, rightBottom; }; // 计算两点间的距离 double distance(Point p1, Point p2) { double dx = p1.x - p2.x; double dy = p1.y - p2.y; return sqrt(dx * dx + dy * dy); } // 判断是否矩形内部 bool inRectangle(Point p, Rectangle rect) { return p.x >= rect.leftTop.x && p.x <= rect.rightBottom.x && p.y >= rect.leftTop.y && p.y <= rect.rightBottom.y; } // 判断线段是否矩形相交 bool isIntersect(Segment seg, Rectangle rect) { // 判断线段两个端点是否都在矩形内部 if (inRectangle(seg.start, rect) && inRectangle(seg.end, rect)) { return true; } // 判断线段是否矩形的四条边相交 Point p1 = rect.leftTop; Point p2 = {rect.rightBottom.x, rect.leftTop.y}; Point p3 = rect.rightBottom; Point p4 = {rect.leftTop.x, rect.rightBottom.y}; Segment edge1 = {p1, p2}; Segment edge2 = {p2, p3}; Segment edge3 = {p3, p4}; Segment edge4 = {p4, p1}; Segment edges[] = {edge1, edge2, edge3, edge4}; for (int i = 0; i < 4; i++) { Segment edge = edges[i]; // 判断线段是否与边相交 double d1 = (seg.end.x - seg.start.x) * (edge.start.y - seg.start.y) - (seg.end.y - seg.start.y) * (edge.start.x - seg.start.x); double d2 = (seg.end.x - seg.start.x) * (edge.end.y - seg.start.y) - (seg.end.y - seg.start.y) * (edge.end.x - seg.start.x); if (d1 * d2 <= 0) { double d3 = (edge.end.x - edge.start.x) * (seg.start.y - edge.start.y) - (edge.end.y - edge.start.y) * (seg.start.x - edge.start.x); double d4 = (edge.end.x - edge.start.x) * (seg.end.y - edge.start.y) - (edge.end.y - edge.start.y) * (seg.end.x - edge.start.x); if (d3 * d4 <= 0) { return true; } } } return false; } int main() { Segment seg = {{1, 1}, {3, 3}}; Rectangle rect = {{2, 2}, {4, 4}}; if (isIntersect(seg, rect)) { cout << "线段矩形相交" << endl; } else { cout << "线段矩形相交" << endl; } return 0; } ```
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值