Polygon with absolute coords

Hej,

as the topic states, whenever I draw a display.polygon it creates it’s origin to its self calculated bbcenter and repositions that into origin. This is absolutely useless if you calculated the absolute vertices coords already.

Does anybody know how to workaround that or deactivate it all together?

Thanks,

Phil

I’m not sure, but take a look at this example.  I think it may fix the issue to your satisfaction:

case 1 - ideal case; no fix required

case 2 - alt case; no fix required

case 3 - non-ideal behavior

case 4 - possible solution technique

local function test( marker, case ) -- IDEAL CASE if( case == 1 ) then local vertices = { 0, 0, 100, 0, 100, 100, 0, 100 } local o = display.newPolygon( marker.x, marker.y, vertices ) o.strokeWidth = 2 o:setFillColor(0,0,0,0) o:setStrokeColor( 1, 0, 0 ) --display.setDefault("isAnchorClamped", false) --o.anchorX = breachX/o.contentWidth --o.anchorY = breachY/o.contentHeight --print( breachX, breachY, o.anchorX, o.anchorY, breachX/o.contentWidth, breachY/o.contentHeight ) o.anchorX = 0 o.anchorY = 0 -- ALTERNATE IDEAL CASE elseif( case == 2 ) then local vertices = { -20, -50, 100, -10, 100, 100, -10, 100 } local o = display.newPolygon( marker.x, marker.y, vertices ) o.strokeWidth = 2 o:setFillColor(0,0,0,0) o:setStrokeColor( 1, 0, 0 ) o.anchorX = 0 o.anchorY = 0 -- NON-IDEAL CASE elseif( case == 3 ) then local vertices = { 0, 0, 100, -10, 100, 100, -10, 100 } local o = display.newPolygon( marker.x, marker.y, vertices ) o.strokeWidth = 2 o:setFillColor(0,0,0,0) o:setStrokeColor( 1, 0, 0 ) o.anchorX = 0 o.anchorY = 0 -- POSSIBLE FIX elseif( case == 4 ) then local vertices = { 0, 0, 100, -10, 100, 100, -10, 100 } -- 1. Find points where x or y are \< 0 local breachX = 0 local breachY = 0 for i = 1, #vertices, 2 do if( vertices[i] \< breachX ) then breachX = vertices[i] end if( vertices[i+1] \< breachY ) then breachY = vertices[i+1] end end -- 2 Draw polygon local o = display.newPolygon( marker.x, marker.y, vertices ) o.strokeWidth = 2 o:setFillColor(0,0,0,0) o:setStrokeColor( 1, 0, 0 ) o.anchorX = 0 o.anchorY = 0 -- 3 Adjust position of polygon ONLY if breach is left or above minimum vertex if( breachX \< vertices[1] ) then o.x = o.x + breachX end if( breachY \< vertices[2] ) then o.y = o.y + breachY end end end local marker = display.newCircle( display.contentCenterX, display.contentCenterY, 10 ) test( marker, 4)

This may not be a perfect solution, but I think it is 9/10 of the way there.

I think the last trick is to figure out how to deal with vertex lists where  < vertex[0],  vertex[1] > is not <0,0>

I think this should be as simple as making the polygon position < vertex[0],  vertex[1] >, then adjusting if there is a breach.

Hey philipfuchs!

If I understand your question correctly, then this might help you.

The vertices below are taken directly from Corona doc’s display.newPolygon page and creating a polygon using them results in the behaviour that you described.

local vertices = { 0,-110, 27,-35, 105,-35, 43,16, 65,90, 0,45, -65,90, -43,15, -105,-35, -27,-35, } local polygon = display.newPolygon( 240, 160, vertices ) local function offset\_xy( t ) local table\_sort = table.sort local coordinatesX, coordinatesY = {}, {} local minX, maxX, minY, maxY local function compare( a, b ) return a \> b end for i = 1, #t\*0.5 do coordinatesY[i] = t[i\*2] end for i = 1, #t\*0.5 do coordinatesX[i] = t[i\*2-1] end table\_sort( coordinatesX, compare ) maxX = coordinatesX[1] minX = coordinatesX[#coordinatesX] table\_sort( coordinatesY, compare ) maxY = coordinatesY[1] minY = coordinatesY[#coordinatesY] local offset\_x = (minX+maxX)\*0.5 local offset\_y = (minY+maxY)\*0.5 return offset\_x, offset\_y end local offset\_x, offset\_y = offset\_xy(vertices) polygon.x = polygon.x+offset\_x polygon.y = polygon.y+offset\_y

The function above takes the vertices and then separates them into x and y tables. The tables are then sorted from largest to smallest. This way, we can find out the vertices’ max and min x and y coordinates. Finally, the function finds the averages for the x and y coordinates. If these averages are not equal to zero, then we have the problem that you described. However, you can offset the polygon’s x and y coordinates by simply adding the offset values that the function returns and now your polygon should be located where you wanted it.

I hope this helps.

Keep in mind that if you rotate your polygon, then this method will not work unless you calculate the rotated vertices and run them through this function again.

The vertices are taken directly from Corona Doc’s newPolygon page. 

@XeduR @Spyric,

I like this solution much better. It is grounded in better principles than mine.

Sweet

Checking back in.

I just wanted to say thanks to ‘XeduR @Spyric’ for the much better solution which I am now using in my own game.

Here is a small video of the above code in use (combined with my own) dynamically generating a path with polygons:

https://www.youtube.com/watch?v=-12yLOS43Gc&feature=youtu.be

-Ed

I’m not sure, but take a look at this example.  I think it may fix the issue to your satisfaction:

case 1 - ideal case; no fix required

case 2 - alt case; no fix required

case 3 - non-ideal behavior

case 4 - possible solution technique

local function test( marker, case ) -- IDEAL CASE if( case == 1 ) then local vertices = { 0, 0, 100, 0, 100, 100, 0, 100 } local o = display.newPolygon( marker.x, marker.y, vertices ) o.strokeWidth = 2 o:setFillColor(0,0,0,0) o:setStrokeColor( 1, 0, 0 ) --display.setDefault("isAnchorClamped", false) --o.anchorX = breachX/o.contentWidth --o.anchorY = breachY/o.contentHeight --print( breachX, breachY, o.anchorX, o.anchorY, breachX/o.contentWidth, breachY/o.contentHeight ) o.anchorX = 0 o.anchorY = 0 -- ALTERNATE IDEAL CASE elseif( case == 2 ) then local vertices = { -20, -50, 100, -10, 100, 100, -10, 100 } local o = display.newPolygon( marker.x, marker.y, vertices ) o.strokeWidth = 2 o:setFillColor(0,0,0,0) o:setStrokeColor( 1, 0, 0 ) o.anchorX = 0 o.anchorY = 0 -- NON-IDEAL CASE elseif( case == 3 ) then local vertices = { 0, 0, 100, -10, 100, 100, -10, 100 } local o = display.newPolygon( marker.x, marker.y, vertices ) o.strokeWidth = 2 o:setFillColor(0,0,0,0) o:setStrokeColor( 1, 0, 0 ) o.anchorX = 0 o.anchorY = 0 -- POSSIBLE FIX elseif( case == 4 ) then local vertices = { 0, 0, 100, -10, 100, 100, -10, 100 } -- 1. Find points where x or y are \< 0 local breachX = 0 local breachY = 0 for i = 1, #vertices, 2 do if( vertices[i] \< breachX ) then breachX = vertices[i] end if( vertices[i+1] \< breachY ) then breachY = vertices[i+1] end end -- 2 Draw polygon local o = display.newPolygon( marker.x, marker.y, vertices ) o.strokeWidth = 2 o:setFillColor(0,0,0,0) o:setStrokeColor( 1, 0, 0 ) o.anchorX = 0 o.anchorY = 0 -- 3 Adjust position of polygon ONLY if breach is left or above minimum vertex if( breachX \< vertices[1] ) then o.x = o.x + breachX end if( breachY \< vertices[2] ) then o.y = o.y + breachY end end end local marker = display.newCircle( display.contentCenterX, display.contentCenterY, 10 ) test( marker, 4)

This may not be a perfect solution, but I think it is 9/10 of the way there.

I think the last trick is to figure out how to deal with vertex lists where  < vertex[0],  vertex[1] > is not <0,0>

I think this should be as simple as making the polygon position < vertex[0],  vertex[1] >, then adjusting if there is a breach.

Hey philipfuchs!

If I understand your question correctly, then this might help you.

The vertices below are taken directly from Corona doc’s display.newPolygon page and creating a polygon using them results in the behaviour that you described.

local vertices = { 0,-110, 27,-35, 105,-35, 43,16, 65,90, 0,45, -65,90, -43,15, -105,-35, -27,-35, } local polygon = display.newPolygon( 240, 160, vertices ) local function offset\_xy( t ) local table\_sort = table.sort local coordinatesX, coordinatesY = {}, {} local minX, maxX, minY, maxY local function compare( a, b ) return a \> b end for i = 1, #t\*0.5 do coordinatesY[i] = t[i\*2] end for i = 1, #t\*0.5 do coordinatesX[i] = t[i\*2-1] end table\_sort( coordinatesX, compare ) maxX = coordinatesX[1] minX = coordinatesX[#coordinatesX] table\_sort( coordinatesY, compare ) maxY = coordinatesY[1] minY = coordinatesY[#coordinatesY] local offset\_x = (minX+maxX)\*0.5 local offset\_y = (minY+maxY)\*0.5 return offset\_x, offset\_y end local offset\_x, offset\_y = offset\_xy(vertices) polygon.x = polygon.x+offset\_x polygon.y = polygon.y+offset\_y

The function above takes the vertices and then separates them into x and y tables. The tables are then sorted from largest to smallest. This way, we can find out the vertices’ max and min x and y coordinates. Finally, the function finds the averages for the x and y coordinates. If these averages are not equal to zero, then we have the problem that you described. However, you can offset the polygon’s x and y coordinates by simply adding the offset values that the function returns and now your polygon should be located where you wanted it.

I hope this helps.

Keep in mind that if you rotate your polygon, then this method will not work unless you calculate the rotated vertices and run them through this function again.

The vertices are taken directly from Corona Doc’s newPolygon page. 

@XeduR @Spyric,

I like this solution much better. It is grounded in better principles than mine.

Sweet

Checking back in.

I just wanted to say thanks to ‘XeduR @Spyric’ for the much better solution which I am now using in my own game.

Here is a small video of the above code in use (combined with my own) dynamically generating a path with polygons:

https://www.youtube.com/watch?v=-12yLOS43Gc&feature=youtu.be

-Ed