serifsadi
serifsadi

Reputation: 603

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

I use this codes for draw a polygon on a canvas. But I want to calculate the area of polygon. Of course give the measurements of each line. I see a lot of example on maps but ı don't convert/adapt on canvas. Can anyone showing a 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 current touch position to the list of points
                    pointsList.add(new Point((int) event.getX(), (int) event.getY()));

                    // Get canvas from surface
                    Canvas canvas = surfaceHolder.lockCanvas();

                    // Clear screen
                    canvas.drawColor(Color.WHITE);

                    // Iterate on the list
                    for (int i = 0; i < 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 with next point (if it 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);
                }
            }
            return false;
        }
    }
}

Upvotes: 1

Views: 1447

Answers (1)

Dave Morrissey
Dave Morrissey

Reputation: 4411

Here's a method for calculating the area of a polygon.

for (int i = 0; i < points.size(); i++) {
    float addX = points.get(i).x;
    float addY = points.get(i == points.size() - 1 ? 0 : i + 1).y;
    float subX = points.get(i == points.size() - 1 ? 0 : i + 1).x;
    float subY = points.get(i).y;
    total += (addX * addY * 0.5);
    total -= (subX * subY * 0.5);
}
return Math.abs(total);

Upvotes: 2

Related Questions