Scaling a table

Hey folks,

Is there a Lua or Corona-built-in way to scale a table of values?

I have a table of vertices that I will use with display.newPolygon.

This table is normalized so that all values are between 0 and 1.

I’ll give you an example of a normalized set of vertices that make a star.

local vertices = { 0.5, 0, 0.63, 0.36, 1, 0.4, 0.73, 0.6, 0.81, 1, 0.5, 0.8, 0.19, 1, 0.27, 0.6, 0, 0.4, 0.37, 0.36 }

This set can now have it’s values multiplied by a given scalar (such as the width or height of a frame) so that it is set to that size.

How should I go about multiplying these values?

I’ve resorted to looping through the table and multiplying the values one by one (code below). If anyone has a better solution, please post it.

local scalar = 50 --lets make a star 50 pixels wide and high for i = 1, #vertices do vertices[i] = vertices[i] \* scalar; end

I suppose you could modify it so that every second vertex is multiplied by another scalar so you could have scalarX and scalarY for more control.

If you are looking to just change the size of the star you can use myStar.xScale and myStar.yScale.  Or I may just not be understanding what you need…

Thank you, that’s exactly what I needed.

I believe the docs need some updating; I didn’t know polygon had an x/yScale modifier.

Even ShapeObject docs doesn’t mention it, nor does the guide on Creating Shapes.

It seems like rect has a rect:scale(x,y) function but it doesn’t work for shapes.

I agree that the docs could be a little improved.  However on the ShapeObjects page under Properties it does state that it inherits properties of DisplayObject, which does show you all of the available functions.  Though I admit this is a little tedious to get to. 

Ah thanks JonPM. I guess I just had to keep following the parent. Knowing that will make my life much easier :slight_smile:

I’ve resorted to looping through the table and multiplying the values one by one (code below). If anyone has a better solution, please post it.

local scalar = 50 --lets make a star 50 pixels wide and high for i = 1, #vertices do vertices[i] = vertices[i] \* scalar; end

I suppose you could modify it so that every second vertex is multiplied by another scalar so you could have scalarX and scalarY for more control.

If you are looking to just change the size of the star you can use myStar.xScale and myStar.yScale.  Or I may just not be understanding what you need…

Thank you, that’s exactly what I needed.

I believe the docs need some updating; I didn’t know polygon had an x/yScale modifier.

Even ShapeObject docs doesn’t mention it, nor does the guide on Creating Shapes.

It seems like rect has a rect:scale(x,y) function but it doesn’t work for shapes.

I agree that the docs could be a little improved.  However on the ShapeObjects page under Properties it does state that it inherits properties of DisplayObject, which does show you all of the available functions.  Though I admit this is a little tedious to get to. 

Ah thanks JonPM. I guess I just had to keep following the parent. Knowing that will make my life much easier :slight_smile: