在二维坐标系中,所有的值是double类型,那么一个矩形可以由四个点来代表,(x1, y1)为最左的点,(x2, y2)为最上的点,(x3, y3)为最下的点,(x4, y4)为最右的点。给定4个点代表的矩形,再给定一个点(x, y),判断(x, y)是否在矩形中
输入有五行,每行两个整数。对于前四行,第i行的浮点数表示()
最后一行两个浮点数表示(x, y)
若(x, y)在矩形中,输出"Yes"
否则输出"No"
2.00 2.50 3.00 5.00 3.00 1.50 4.00 4.00 3.21 3.78
Yes
2.00 2.50 3.00 5.00 3.00 1.50 4.00 4.00 2333.33 2333333.33
No
保证输入的顺序与题目描述相同
#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; }
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"); } } }高中数学
#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; }
#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"); }