0

I am trying to create a pentagon shaped polygon which can be resized by dragging one of its vertices. I tried some code from this question on SO but later realised that the code is specifically written for a rectangle shape. I tried modifying the code in the onDraw() method but later refactored it to the way it was earlier after realising that it is of no use.

Any help?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
localhost
  • 107
  • 1
  • 11
  • Squares and circles are fairly simple to resize. Polygons like this will most likely require some kind of matrix transformation. If you are working with a bunch of custom shapes, it may be easier to start working with SVG. – Tank May 15 '19 at 19:09
  • @Tank but I don't think that an SVG image (pentagon) can be resized by dragging on one of its vertices. I mean it will be a non resizable image – localhost May 15 '19 at 19:20
  • Are you trying to resize the entire pentagon shape as a whole, or just move each vertice/point around? – Tank May 15 '19 at 19:26
  • @Tank not the entire shape but the vertice/point. So, if go with an SVG image will I be able to move one of its vertice/point to reshape the pentagon? – localhost May 15 '19 at 19:27

1 Answers1

0

So here's a code I just wrote to draw a pentagon in the canvas. You pretty much have the vertices which you can edit on the onTouchEvent.

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int width = getWidth();
        int heigth = getHeight();

        // not required, but just to move it to the middle
        float centerX = width / 2.f;
        float centerY = heigth / 2.f;

        // vertices
        float p1x = 100.f + centerX;
        float p1y = 0.f + centerY;
        float p2x = 0.f + centerX;
        float p2y = -80.f + centerY;
        float p3x = -100.f + centerX;
        float p3y = 0.f + centerY;
        float p4x = -80.f + centerX;
        float p4y = 80f + centerY;
        float p5x = 80.f + centerX;
        float p5y = 80.f + centerY;

        Path path = new Path();
        path.moveTo(p1x, p1y);
        path.lineTo(p2x, p2y);
        path.lineTo(p3x, p3y);
        path.lineTo(p4x, p4y);
        path.lineTo(p5x, p5y);
        path.lineTo(p1x, p1y);

        Paint paint = new Paint();
        paint.setColor(Color.BLUE);
        canvas.drawPath(path, paint);
    }

Israel dela Cruz
  • 794
  • 1
  • 5
  • 11