Answer the question
In order to leave comments, you need to log in
How to scale an arbitrary polygon?
Essence: there is a certain arbitrary polygon, given by a set of points. It is required to shift all these points so that the size of the polygon increases.
On the Internet I saw the idea of moving the geometric center of a polygon or the center of gravity of a polygon to the point [0; 0], by multiplying all the angles by a certain number and returning the polygon to its place. But the centers of the polygon can be located outside the figure.
My idea is to build a bisector of the corners of a polygon and shift these corners by some distance, but the question is how to determine exactly which distance?
Or maybe there are simpler algorithms?
I implement everything in JS.
Answer the question
In order to leave comments, you need to log in
Well, the simplest thing is to use transformation matrices. And first move the center of the coordinate system to where you need to scale your polygon.
Scaling occurs relative to the selected point, the center. Vectors are drawn from the center to each of the vertices of the polygon. The length of each vector is multiplied by k. When multiplied by 0, the polygon collapses to that point. Or with a smooth increase in k, the polygon "grows" from this center. And this center can be located anywhere - the point is arbitrary.
You only need to decide where the center is.
If you place the center in the (0, 0)
task is simplified, because. the coordinates of the vertices coincide with the vectors in them from the center.
In JS, there could be something like this function that takes as input a coefficient k, an array of center coordinates, and an array of arrays of point coordinates:
function scale( k, center, points) {
return points.map( p => p.map( (x, i) => center[i] + k * (x - center[i])));
}
scale(
2, // в два раза
[3, 1], // относительно точки (3,1)
[ [0,0], [1,1], [3,1], [10,10] ] // этот четырёхугольник
)
// [ [-3, -1], [-1, 1], [3, 1], [17, 19] ]
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question