Friday, 15 May 2015

Android calculate the area of polygon using canvas drawing(not maps) -



Android calculate the area of polygon using canvas drawing(not maps) -

i utilize codes draw polygon on canvas. want calculate area of polygon. of course of study give measurements of each line. see lot of illustration on maps ı don't convert/adapt on canvas. can showing way or method ?

thanks in advance.

import java.util.arraylist; import java.util.list; import android.app.activity; import android.content.context; import android.graphics.canvas; import android.graphics.color; import android.graphics.paint; import android.graphics.paint.style; import android.graphics.point; import android.os.bundle; import android.view.motionevent; import android.view.surfaceholder; import android.view.surfaceview; public class mainactivity extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(new drawingview(mainactivity.this)); } class drawingview extends surfaceview { private surfaceholder surfaceholder; private final paint paint = new paint(paint.anti_alias_flag); private list<point> pointslist = new arraylist<point>(); public drawingview(context context) { super(context); surfaceholder = getholder(); paint.setcolor(color.black); paint.setstyle(style.fill); } @override public boolean ontouchevent(motionevent event) { if (event.getaction() == motionevent.action_down) { if (surfaceholder.getsurface().isvalid()) { // add together current touch position list of points pointslist.add(new point((int) event.getx(), (int) event.gety())); // canvas surface canvas canvas = surfaceholder.lockcanvas(); // clear screen canvas.drawcolor(color.white); // iterate on list (int = 0; < pointslist.size(); i++) { point current = pointslist.get(i); point first = pointslist.get(0); // draw points canvas.drawcircle(current.x, current.y, 5, paint); // draw line next point (if exists) if (i + 1 < pointslist.size()) { point next = pointslist.get(i + 1); canvas.drawline(current.x, current.y, next.x, next.y, paint); canvas.drawline(next.x, next.y, first.x, first.y, paint); c } } // release canvas surfaceholder.unlockcanvasandpost(canvas); } } homecoming false; } } }

here's method calculating area of polygon.

for (int = 0; < points.size(); i++) { float addx = points.get(i).x; float addy = points.get(i == points.size() - 1 ? 0 : + 1).y; float subx = points.get(i == points.size() - 1 ? 0 : + 1).x; float suby = points.get(i).y; total += (addx * addy * 0.5); total -= (subx * suby * 0.5); } homecoming math.abs(total);

android canvas polygon area

No comments:

Post a Comment