首页 > 试题广场 >

判断一个点是否在矩形内部

[编程题]判断一个点是否在矩形内部
  • 热度指数:1122 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
在二维坐标系中,所有的值是double类型,那么一个矩形可以由四个点来代表,(x1, y1)为最左的点,(x2, y2)为最上的点,(x3, y3)为最下的点,(x4, y4)为最右的点。给定4个点代表的矩形,再给定一个点(x, y),判断(x, y)是否在矩形中


输入描述:
输入有五行,每行两个整数。
对于前四行,第i行的浮点数表示()
最后一行两个浮点数表示(x, y)


输出描述:
若(x, y)在矩形中,输出"Yes"

否则输出"No"
示例1

输入

2.00 2.50
3.00 5.00
3.00 1.50
4.00 4.00
3.21 3.78

输出

Yes
示例2

输入

2.00 2.50
3.00 5.00
3.00 1.50
4.00 4.00
2333.33 2333333.33

输出

No

备注:
保证输入的顺序与题目描述相同
测试用例有问题

用例:
-1943121244.21 990857559.21
-984347432.21 1054456367.21
24557925.07 767104.07
983331737.07 64365912.07
427832327.00 526745069.00

对应输出应该为:

Yes

你的输出为:

No

在matlab里面画的图:


点很明显在矩形外面。应该输出No才对

编辑于 2019-12-24 14:39:55 回复(3)
#include <bits/stdc++.h>
using namespace std;

struct P{
    double x,y;
};

double F(P a, P b){
    return acos((a.x*b.x+a.y*b.y)/(sqrt(a.x*a.x+a.y*a.y)*sqrt(b.x*b.x+b.y*b.y)));
}

int main(){
    P p[5], d[4];
    for(int i=0;i<5;i++)
        cin>>p[i].x>>p[i].y;
    for(int i=0;i<4;i++){
        d[i].x = p[i].x - p[4].x;
        d[i].y = p[i].y - p[4].y;
    }
    double a = F(d[0], d[1]);
    double b = F(d[1], d[3]);
    double c = F(d[3], d[2]);
    double e = F(d[2], d[0]);
    if(abs((a+b+c+e) - 2*acos(-1)) < 1e-6)
        cout<<"Yes"<<endl;
    else
        cout<<"No"<<endl;
    return 0;
}

发表于 2020-03-12 02:56:30 回复(0)
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        double[] x = new double[4];
        double[] y = new double[4];
        for(int i=0;i<4;i++){
            x[i]=sc.nextDouble();
            y[i]=sc.nextDouble();
        }
        double x0=sc.nextDouble();
        double y0=sc.nextDouble();
        double[] k=new double[4];
        double[] b=new double[4];
        k[0]=(y[1]-y[0])/(x[1]-x[0]);
        b[0]=y[0]-k[0]*x[0];
        k[1]=(y[3]-y[1])/(x[3]-x[1]);
        b[1]=y[1]-k[1]*x[1];
        k[2]=(y[2]-y[0])/(x[2]-x[0]);
        b[2]=y[2]-k[2]*x[2];
        k[3]=(y[3]-y[2])/(x[3]-x[2]);
        b[3]=y[3]-k[3]*x[3];
        if(k[0]*x0+b[0]>y0&&k[1]*x0+b[1]>y0&&k[2]*x0+b[2]<y0&&k[3]*x0+b[3]<y0){
            System.out.println("Yes");
        }else{
            System.out.println("No");
        }
    }
}
高中数学
发表于 2020-10-05 10:46:28 回复(0)
最后一个测试用例有问题
#include <iostream>
using namespace std;

double crossProduct(double x1, double y1, double x2, double y2) {
    return x1*y2 - x2*y1;
}

int main() {
    double x1, y1, x2, y2, x3, y3, x4, y4, x, y;
    cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3 >> x4 >> y4 >> x >> y;
    
    if(crossProduct(x3-x1, y3-y1, x-x1, y-y1) > 0 &&
       crossProduct(x4-x3, y4-y3, x-x3, y-y3) > 0 &&
       crossProduct(x2-x4, y2-y4, x-x4, y-y4) > 0 &&
       crossProduct(x1-x2, y1-y2, x-x2, y-y2) > 0)
        cout << "Yes" << endl;
    else
        cout << "No" << endl;
    
    return 0;
}


编辑于 2020-01-08 17:36:43 回复(0)
#include <algorithm>
#include <string>
#include <vector>
using namespace std;

bool inStandardRec(double x1, double y1, double x2, double y2, double x, double y)
{
	return x > x1&&x < x2&&y<y1&&y>y2;
}
//顺时针
bool inRec(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4, double x, double y)
{
	float dis = sqrt((x3 - x4)*(x3 - x4) + (y3 - y4)*(y3 - y4));
	float cos = (x3 - x4) / dis;
	float sin = (y3 - y4) / dis;
	return inStandardRec(x1*cos + y1 * sin, y1*cos - x1 * sin, x3*cos + y3 * sin, y3*cos - x3 * sin,
		x*cos + y * sin, y*cos - x * sin);
}

int main()
{
	vector<pair<double, double>>vec(5);
	for (int i = 0; i < 5; i++)
	{
		scanf("%lf", &vec[i].first);
		scanf("%lf", &vec[i].second);
	}
	printf("%s", inRec(vec[0].first, vec[0].second, vec[1].first, vec[1].second,
		 vec[3].first, vec[3].second, vec[2].first, vec[2].second,
		vec[4].first, vec[4].second) ? "Yes" : "No");
}

编辑于 2019-08-08 13:47:15 回复(0)