Allegro.cc - Online Community

Allegro.cc Forums » Allegro.cc Comments » How can I scale an object from the array of coordinates?

This thread is locked; no one can reply to it. rss feed Print
How can I scale an object from the array of coordinates?
NathanJ
Member #18,585
October 2020

I would like to scale up a polygon defined on cords below. How can I do that?

float cords[8] = {
x1,y1,
x2, y2,
x3, y3,
x4, y4
};

Eric Johnson
Member #14,841
January 2013
avatar

I suggest you read-up on linear transformations, but basically you just multiply each element of your array by a scaling matrix.

But I suppose if all you're trying to do is scale the points (or vertices) of a polygon, you could simply iterate through the array and multiply it by a scaling factor...

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

If you scale the data by itself, it will induce a translation. You need to center it on the origin first, then scale it, and THEN you can translate it to its correct position.

#SelectExpand
1float cords[8] = { 2x1,y1, 3x2, y2, 4x3, y3, 5x4, y4 6}; 7const float cx = (x1 + x2 + x3 + x4)/4.0; 8const float cy = (y1 + y2 + y3 + y4)/4.0; 9 10ALLEGRO_TRANSFORM t; 11al_identity_transform(&t); 12al_translate_transform(&t , -cx , -cy); 13al_scale_transform(&t , scalex , scaley); 14al_translate_transform(&t , posx , posy); 15 16al_transform_coordinates(&t , &cords[0] , &cords[1]); 17al_transform_coordinates(&t , &cords[2] , &cords[3]); 18al_transform_coordinates(&t , &cords[4] , &cords[5]); 19al_transform_coordinates(&t , &cords[6] , &cords[7]); 20 21/// Our polygon is now centered on posx , posy at a scale of scalex,scaley.

Go to: