//多边形类
public class Polygon{
POINT[] ps;
public Polygon(POINT[] ps){
this.ps = ps;
}
public boolean isInPolygon(POINT p) {
double polygonArea=polygonArea();
double otherArea=triAngleArea(p,ps[0],ps[ps.length-1]);
for(int i=1;i<ps.length;i++){
otherArea+=triAngleArea(p,ps[i],ps[i-1]);
// 若面积之和大于原多边形的面积,证明点在多边形外
if((otherArea-polygonArea)>1){
return false;
}
}
//否则在多边形内
return true;
}
//计算多边形面积
private double polygonArea(){
double area=0;
for(int i=2;i<ps.length;i++){
area+=triAngleArea(ps[0],ps[i-1],ps[i]);
}
return area;
}
// 计算这三个点组成三角形面积
private double triAngleArea(POINT A, POINT B, POINT C) {
POINT ab,bc;
ab = new POINT(B.x - A.x,B.y - A.y);
bc = new POINT(C.x - B.x,C.y - B.y);
return Math.abs((ab.x * bc.y - ab.y * bc.x) / 2.0);
}
}
public class Polygon{
POINT[] ps;
public Polygon(POINT[] ps){
this.ps = ps;
}
public boolean isInPolygon(POINT p) {
double polygonArea=polygonArea();
double otherArea=triAngleArea(p,ps[0],ps[ps.length-1]);
for(int i=1;i<ps.length;i++){
otherArea+=triAngleArea(p,ps[i],ps[i-1]);
// 若面积之和大于原多边形的面积,证明点在多边形外
if((otherArea-polygonArea)>1){
return false;
}
}
//否则在多边形内
return true;
}
//计算多边形面积
private double polygonArea(){
double area=0;
for(int i=2;i<ps.length;i++){
area+=triAngleArea(ps[0],ps[i-1],ps[i]);
}
return area;
}
// 计算这三个点组成三角形面积
private double triAngleArea(POINT A, POINT B, POINT C) {
POINT ab,bc;
ab = new POINT(B.x - A.x,B.y - A.y);
bc = new POINT(C.x - B.x,C.y - B.y);
return Math.abs((ab.x * bc.y - ab.y * bc.x) / 2.0);
}
}






我是你大爷


