android中计算多边形的面积

时间:2021-09-30 10:18:28

List<PointXY> list :保存的 是点的坐标

public float caculate(List<PointXY>list){ float temp=0; for (int i=0;i<list.size();i++){ if (i<list.size()-1){ PointXY p1 = list.get(i); PointXY p2 = list.get(i+1); temp += p1.getX() * p2.getY() - p2.getX() * p1.getY(); }else { PointXY pn = list.get(i); PointXY p0 = list.get(0); temp += pn.getX() * p0.getY() - p0.getX() * pn.getY(); } } temp=temp/2; List<Float> area = getScreenSizeOfDevice(); temp=(temp/area.get(0))*area.get(1)*2.54f; Log.d(TAG, "画图的面积 : "+temp); return temp; } private List<Float> getScreenSizeOfDevice(){ List<Float> list02 = new ArrayList<Float>(); Point point = new Point(); getWindowManager().getDefaultDisplay().getRealSize(point); DisplayMetrics dm = getResources().getDisplayMetrics(); float area = point.x * point.y;//得到像素面积, Log.d(TAG, "屏幕的总像素面积: "+area); list02.clear(); list02.add(area); double x = Math.pow(point.x / dm.xdpi, 2); double y = Math.pow(point.y/ dm.ydpi,2); double screenInches = Math.sqrt(x+y);//得到尺寸大小 list02.add((float)(x * y));//得到手机屏幕的物理面积 Log.d(TAG, "屏幕尺寸大小: "+screenInches); return list02; }}


public class PointXY {    public float X;    public float Y;    public PointXY(){        this.X=0;        this.Y=0;    }    public PointXY(float x, float y) {        X = x;        Y = y;    }    public float getX() {        return X;    }    public void setX(float x) {        X = x;    }    public float getY() {        return Y;    }    public void setY(float y) {        Y = y;    }}