views:

118

answers:

4

I'm trying to figure out if I have points that make for example a square:

 *     *



 *     *

and let's say I know the center of this square. I want a formula that will make it for eample twice its size but from the center

 *               *

      *     *



      *     *

 *               *

Therefore the new shape is twice as large and from the center of the polygon. It has to work for any shape not just squares.

I'm looking more for the theory behind it more than the implementation.

A: 

If you want the shape twice as large, scale the distance of the coordinates to be sqrt(2) times further from the center.

In other words, let's say your point is at (x, y) and the center is (xcent, ycent). Your new point should be at

(xcent + sqrt(2)*(x - xcent),  ycent + sqrt(2)*(y - ycent))

This will scale the distances from the new 'origin', (xcent, ycent) in such a way that the area doubles. (Because sqrt(2)*sqrt(2) == 2).

Peter
+4  A: 

If you know the center point cp and a point v in the polygon you would like to scale by scale, then:

v2 = v - cp; // get a vector to v relative to the centerpoint
v2_scaled = v2 * scale; // scale the cp-relative-vector
v1_scaled = v2_scaled + cp; // translate the scaled vector back

This translate-scale-translate pattern can be performed on vectors of any dimension.

fbrereto
A: 

I'm not sure there's a clean way to do this for all types of objects. For relatively simple ones, you should be able to find the "center" as the average of all the X and Y values of the individual points. To double the size, you find the length and angle of a vector from the center to the point. Double the length of the vector, and retain the same angle to get the new point.

Edit: of course, "twice the size" is open to several interpretations (e.g., doubling the perimeter vs. doubling the area) These would change the multiplier used above, but the basic algorithm would remain essentially the same.

Jerry Coffin
A: 

To do what you want you need to perform three operations: translate the square so that its centroid coincides with the origin of the coordinate system, scale the resulting square, translate it back.

Dima