java知道四个点坐标,怎么判断一个点是不是在这个矩形区域内(矩形可能是斜着放的,有一定的斜度)
java知道四个点坐标,怎么判断一个点是不是在这个矩形区域内(矩形可能是斜着放的,有一定的斜度)其实知道思路,但是数学很差不会,所以不能写出那个方法,求救...
java知道四个点坐标,怎么判断一个点是不是在这个矩形区域内(矩形可能是斜着放的,有一定的斜度)其实知道思路,但是数学很差不会,所以不能写出那个方法,求救
展开
2013-07-28
展开全部
写法不是很规范,但是思路都在,不限于矩形
class Quar{
public static boolean isInside(Point left,Point top,Point right,Point buttom,Point pointToCheck){
Line line1=Line.getLine(left, top),
line2=Line.getLine(top, right),
line3=Line.getLine(right, buttom),
line4=Line.getLine(buttom, left);
if(Line.getValue(line1, pointToCheck)>=0 && Line.getValue(line2, pointToCheck)>=0
&& Line.getValue(line3, pointToCheck)<=0 && Line.getValue(line4, pointToCheck)<=0){
return true;
}else return false;
}
public static void main(String[] args) {
System.out.println(isInside(new Point(0,1), new Point(1,8), new Point(2,7), new Point(1,1), new Point(1.2,7)));
}
}
class Point{
double x;
double y;
Point(double x,double y){
this.x=x;
this.y=y;
}
}
class Line{
double a;
double b;
double c;
Line(double a,double b,double c){
this.a=a;
this.b=b;
this.c=c;
}
static Line getLine(Point p1,Point p2){
//cy=ax+b
double a=(p1.y-p2.y)/(p1.x-p2.x),b,c;
if(Double.isNaN(a))throw new NumberFormatException("输入的两点有重合");
else if(Double.isInfinite(a)){
a=1;
b=p1.x;
c=0;
}else{
b=(p1.x*p2.y-p2.x*p1.y)/(p1.x-p2.x);
c=1;
}
return new Line(a,b,c);
}
static double getValue(Line l,Point p){
return l.a*p.x+l.b-l.c*p.y;
}
}
class Quar{
public static boolean isInside(Point left,Point top,Point right,Point buttom,Point pointToCheck){
Line line1=Line.getLine(left, top),
line2=Line.getLine(top, right),
line3=Line.getLine(right, buttom),
line4=Line.getLine(buttom, left);
if(Line.getValue(line1, pointToCheck)>=0 && Line.getValue(line2, pointToCheck)>=0
&& Line.getValue(line3, pointToCheck)<=0 && Line.getValue(line4, pointToCheck)<=0){
return true;
}else return false;
}
public static void main(String[] args) {
System.out.println(isInside(new Point(0,1), new Point(1,8), new Point(2,7), new Point(1,1), new Point(1.2,7)));
}
}
class Point{
double x;
double y;
Point(double x,double y){
this.x=x;
this.y=y;
}
}
class Line{
double a;
double b;
double c;
Line(double a,double b,double c){
this.a=a;
this.b=b;
this.c=c;
}
static Line getLine(Point p1,Point p2){
//cy=ax+b
double a=(p1.y-p2.y)/(p1.x-p2.x),b,c;
if(Double.isNaN(a))throw new NumberFormatException("输入的两点有重合");
else if(Double.isInfinite(a)){
a=1;
b=p1.x;
c=0;
}else{
b=(p1.x*p2.y-p2.x*p1.y)/(p1.x-p2.x);
c=1;
}
return new Line(a,b,c);
}
static double getValue(Line l,Point p){
return l.a*p.x+l.b-l.c*p.y;
}
}
2013-07-28
展开全部
很简单,我等下写给你
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询