java知道四个点坐标,怎么判断一个点是不是在这个矩形区域内(矩形可能是斜着放的,有一定的斜度)

java知道四个点坐标,怎么判断一个点是不是在这个矩形区域内(矩形可能是斜着放的,有一定的斜度),第1张

写法不是很规范,但是思路都在,不限于矩形
class Quar{
public static boolean isInside(Point left,Point top,Point right,Point buttom,Point pointToCheck){
Line line1=LinegetLine(left, top),
line2=LinegetLine(top, right),
line3=LinegetLine(right, buttom),
line4=LinegetLine(buttom, left);
if(LinegetValue(line1, pointToCheck)>=0 && LinegetValue(line2, pointToCheck)>=0
&& LinegetValue(line3, pointToCheck)<=0 && LinegetValue(line4, pointToCheck)<=0){
return true;
}else return false;
}
public static void main(String[] args) {
Systemoutprintln(isInside(new Point(0,1), new Point(1,8), new Point(2,7), new Point(1,1), new Point(12,7)));
}
}
class Point{
double x;
double y;
Point(double x,double y){
thisx=x;
thisy=y;
}
}
class Line{
double a;
double b;
double c;
Line(double a,double b,double c){
thisa=a;
thisb=b;
thisc=c;
}
static Line getLine(Point p1,Point p2){
//cy=ax+b
double a=(p1y-p2y)/(p1x-p2x),b,c;
if(DoubleisNaN(a))throw new NumberFormatException("输入的两点有重合");
else if(DoubleisInfinite(a)){
a=1;
b=p1x;
c=0;
}else{
b=(p1xp2y-p2xp1y)/(p1x-p2x);
c=1;
}
return new Line(a,b,c);
}
static double getValue(Line l,Point p){
return lapx+lb-lcpy;
}
}

只要实现这个的话,不考虑效率问题,可以new一个image,然后把这个不规则的四边fill上去,当然要指定个颜色,然后获取你要判断坐标的点。然后判断下颜色是不是和填充的一样
BufferedImage 有getRGB(int x, int y) 下面方法可以获取r、g、b的值
public static int [] getRGB(BufferedImage image, int x, int y) {
int [] rgb = null ;
if (image != null && x < imagegetWidth() && y < imagegetHeight()) {
rgb = new int [ 3 ];
int pixel = imagegetRGB(x,y);
rgb[ 0 ] = (pixel & 0xff0000 ) >> 16 ;
rgb[ 1 ] = (pixel & 0xff00 ) >> 8 ;
rgb[ 2 ] = (pixel & 0xff );
}
return rgb;
}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/13377639.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-07-24
下一篇 2023-07-24

发表评论

登录后才能评论

评论列表(0条)

保存