-1

I have polygon from class Polygon. How can I make it bigger or smaller. I'm looking for function like grow(int,int) for rectangle.I haven't got any idea how can I do that.

Thanks in advance!

@sorry for my stupid question without any details.

I'm making polygon using:

    private int[] xCoord = new int[50], yCoord= new int[50]; //Arrays containing the points of the polygon
    private int pointCt=0; //The number of points that have been input

I'm clicking mouse on a certain place, then draw a line

xCoord[pointCt] = arg0.getX();
yCoord[pointCt] = arg0.getY();
pointCt++;


    private void putLine(int x1,int y1,int x2,int y2)
    {
        Graphics g= getGraphics();
        g.drawLine(x1, y1, x2, y2);
        g.dispose();
    }

  polygon=new MyPolygon(xCoord,yCoord,pointCt);

I dont exactly know which line of my code should I post but I hope its enought. I just need an advice how to make bigger polygon having cusps in a table. Thanks!

PoorGuy
  • 29
  • 8
  • 3
    You have to learn math. Then you have to apply math. – RaminS Apr 25 '16 at 16:53
  • 2
    I'm voting to close this question as too broad. It's not even clear how a polygon is represented, which coordinate system is used, etc. – Alex Shesterov Apr 25 '16 at 16:56
  • 1
    Welcome to Stack Overflow! You seem to be asking for someone to write some code for you. Stack Overflow is a question and answer site, not a code-writing service. Please [see here](http://stackoverflow.com/help/how-to-ask) to learn how to write effective questions. –  Apr 25 '16 at 17:00

2 Answers2

1

These are operations that are best covered by linear algebra. You'd be well served by brushing up on translation, rotation, and scaling of a body. But...

If you have all the vertex data, and the object is at origin (0, 0, 0) you can just multiply all vertices by a number - 2.0 will make the object twice as big, 3.0 will make it three times as big, 0.5 will shrink it by half, etc. Make sure to do this to all components, so if you have a two-dimensional object, make sure that you multiply both the X and the Y values of each of its vertices by the same number.

If the object is not at origin, you can find the center of the object (by getting the average of all vertices), translate that to origin (by subtracting the center from each of the vertices' position), perform your scaling (by multiplying as in the previous paragraph), and add the center back to each vertex.

This is the best way to do what you're after - no need to much around with distances or matrices.

Knetic
  • 2,099
  • 1
  • 20
  • 34
-1

If you have access to the position data for the vertices then just calculate the distance between all the vertices and multiply it by whatever you want the scale to be. (Ex. 20% larger multiply all distances by 1.20). You can do this using the Distance Formula. Math.sqrt((x2-x1)^2 + (y2-y1)^2)

Once you get the new distance just add the difference between the old distance and new distance to x2 and y2 and you have your new vertex coordinates, and do this for every vertex of the polygon. If your polygon is at the origin (0, 0) you can just multiply the vertices by whatever you want the scale to be, but otherwise I'd recommend the scaling based on vertices distances.

Tyler
  • 21
  • 4
  • If he has access to the coordinates of the vertices it is am answer. Use the Distance Formula to get the distance between vertices, multiply that distance and modify the vertices. – Tyler Apr 25 '16 at 17:17