position polygon accurately in Corona SDK? (relative to known vertices - issue is it creates it own centre -


question: how can 1 position polygon relative 1 of it's known vertice points?

in other words how calculate auto generated center of polygon relative 1 of known vertices (i.e. used in path)?

e.g. image placing specific shape on map make polygon, want position on map, can't accurately without knowing it's corona engine created centre is. extract api: "the local origin @ center of polygon , anchor point initialized local origin."

ps wondering if should using line , appending points create polygon, perhaps can't add background color in case(?)

the center calculated corona center of bounding box of polygon.

i assume have table points of polygon stored that:

local polygon = {x1,y1,x2,y2,...,xn,yn} 

1) find bounding box of original points, loop thru points; smallest x , smallest y values give coordinates of top-left point; largest x , y values bottom-right point;

local minx = -math.huge local miny = -math.huge local maxx = math.huge local maxy = math.huge  i=1, #polygon, 2     local px = polygon[i]     local py = polygon[i+1]     if px > maxx maxx = px end     if py > maxy maxy = py end     if px < minx minx = py end     if py < miny miny = py end end 

2) find center of bounding box:

local centerx = (maxx - minx)/2 local centery = (maxy - miny)/2 

3) add center point top-left point

local offsetx = centerx + minx local offsety = centery + miny     

4) add offset corona polygon place in same position original polygon.

should work bot have not tested it. let me know.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -