程序员代码面试指南刷题--23.判断一个点是否在矩形内部
程序员文章站
2024-02-27 15:16:03
...
题目描述
在二维坐标系中,所有的值是double类型,那么一个矩形可以由四个点来代表,(x1, y1)为最左的点,(x2, y2)为最上的点,(x3, y3)为最下的点,(x4, y4)为最右的点。给定4个点代表的矩形,再给定一个点(x, y),判断(x, y)是否在矩形中
输入描述:
输入有五行,每行两个整数。
对于前四行,第i行的浮点数表示(xi,yix_i, y_ixi,yi)
最后一行两个浮点数表示(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
解法一:报错(只有那一个例子出错我佛了)
思路: 哪个好心人帮忙看看
import java.io.*;
public class Main{
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] ss = br.readLine().trim().split(" ");
Double x1 = Double.parseDouble(ss[0]);
Double y1 = Double.parseDouble(ss[1]);
ss = br.readLine().trim().split(" ");
Double x2 = Double.parseDouble(ss[0]);
Double y2 = Double.parseDouble(ss[1]);
ss = br.readLine().trim().split(" ");
Double x3 = Double.parseDouble(ss[0]);
Double y3 = Double.parseDouble(ss[1]);
ss = br.readLine().trim().split(" ");
Double x4 = Double.parseDouble(ss[0]);
Double y4 = Double.parseDouble(ss[1]);
ss = br.readLine().trim().split(" ");
Double x = Double.parseDouble(ss[0]);
Double y = Double.parseDouble(ss[1]);
boolean flag = judge(x1,y1,x2,y2,x3,y3,x4,y4,x,y);
if(flag==true) System.out.print("Yes");
else System.out.print("No");
}
public static boolean judge(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4,double x,double y){
int flag = uporDown(x1,y1,x2,y2,x,y);
if(flag==1||flag==0) return false;
flag = uporDown(x1,y1,x3,y3,x,y);
if(flag==-1||flag==0) return false;
flag = uporDown(x4,y4,x3,y3,x,y);
if(flag==-1||flag==0) return false;
flag = uporDown(x4,y4,x2,y2,x,y);
if(flag==1||flag==0) return false;
return true;
}
public static int uporDown(double x1,double y1,double x2,double y2,double x,double y){
double k = (y2-y1)/(x2-x1);
double b = y1-k*x1;
if(y>(k*x+b)) return 1;
else if(y==(k*x+b)) return 0;
else return -1;
}
}
解法二:调整方向
import java.io.*;
public class Main{
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] ss = br.readLine().trim().split(" ");
Double x1 = Double.parseDouble(ss[0]);
Double y1 = Double.parseDouble(ss[1]);
ss = br.readLine().trim().split(" ");
Double x2 = Double.parseDouble(ss[0]);
Double y2 = Double.parseDouble(ss[1]);
ss = br.readLine().trim().split(" ");
Double x3 = Double.parseDouble(ss[0]);
Double y3 = Double.parseDouble(ss[1]);
ss = br.readLine().trim().split(" ");
Double x4 = Double.parseDouble(ss[0]);
Double y4 = Double.parseDouble(ss[1]);
ss = br.readLine().trim().split(" ");
Double x = Double.parseDouble(ss[0]);
Double y = Double.parseDouble(ss[1]);
boolean flag = judge(x1,y1,x2,y2,x3,y3,x4,y4,x,y);
if(flag==true) System.out.print("Yes");
else System.out.print("No");
}
public static boolean judge(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4,double x,double y){
if(y1==y2){
return isInside(x1,y1,x4,y4,x,y);
}
double l=Math.abs(y4-y3);
double k=Math.abs(x4-x3);
double s=Math.sqrt(k*k+l*l);
double sin=l/s;
double cos=k/s;
double x1r=x1*cos+y1*sin;
double y1r=-x1*sin+y1*cos;
double x4r=x4*cos+y4*sin;
double y4r=-x4*sin+y4*cos;
double xr=x*cos+y*sin;
double yr=-x*sin+y*cos;
return isInside(x1r,y1r,x4r,y4r,xr,yr);
}
public static boolean isInside(double x1,double y1,double x4,double y4,double x,double y){
if(x<=x1){
return false;
}else if(x>=x4){
return false;
}else if(y>=y1){
return false;
}else if(y<=y4){
return false;
}else{
return true;
}
}
public static int uporDown(double x1,double y1,double x2,double y2,double x,double y){
double k = (y2-y1)/(x2-x1);
double b = y1-k*x1;
if(y>(k*x+b)) return 1;
else if(y==(k*x+b)) return 0;
else return -1;
}
}
上一篇: 从n个数中删除m个数
下一篇: 不用加减乘除做加法——位运算的实践