diff --git a/Zframework/gcExtend.lua b/Zframework/gcExtend.lua index a289269f..c1679335 100644 --- a/Zframework/gcExtend.lua +++ b/Zframework/gcExtend.lua @@ -32,6 +32,55 @@ function GC.shadedPrint(str,x,y,mode,d,clr1,clr2) setColor(clr2 or COLOR.Z) printf(str,x,y,w,mode) end +function GC.regularPolygon(mode,x,y,R,segments,r,phase) + local X,Y={},{} + local ang=phase or 0 + local angStep=6.283185307179586/segments + for i=1,segments do + X[i]=x+R*math.cos(ang) + Y[i]=y+R*math.sin(ang) + ang=ang+angStep + end + X[segments+1]=x+R*math.cos(ang) + Y[segments+1]=y+R*math.sin(ang) + local halfAng=6.283185307179586/segments/2 + local erasedLen=r*math.tan(halfAng) + if mode=='line'then + erasedLen=erasedLen+1--Fix 1px cover + for i=1,segments do + --Line + local x1,y1,x2,y2=X[i],Y[i],X[i+1],Y[i+1] + local dir=math.atan2(y2-y1,x2-x1) + gc.line(x1+erasedLen*math.cos(dir),y1+erasedLen*math.sin(dir),x2-erasedLen*math.cos(dir),y2-erasedLen*math.sin(dir)) + + --Arc + ang=ang+angStep + local R2=R-r/math.cos(halfAng) + local arcCX,arcCY=x+R2*math.cos(ang),y+R2*math.sin(ang) + gc.arc('line','open',arcCX,arcCY,r,ang-halfAng,ang+halfAng) + end + elseif mode=='fill'then + local L={} + for i=1,segments do + --Line + local x1,y1,x2,y2=X[i],Y[i],X[i+1],Y[i+1] + local dir=math.atan2(y2-y1,x2-x1) + table.insert(L,x1+erasedLen*math.cos(dir)) + table.insert(L,y1+erasedLen*math.sin(dir)) + table.insert(L,x2-erasedLen*math.cos(dir)) + table.insert(L,y2-erasedLen*math.sin(dir)) + + --Arc + ang=ang+angStep + local R2=R-r/math.cos(halfAng) + local arcCX,arcCY=x+R2*math.cos(ang),y+R2*math.sin(ang) + gc.arc('fill','open',arcCX,arcCY,r,ang-halfAng,ang+halfAng) + end + gc.polygon('fill',L) + else + error("Draw mode should be 'line' or 'fill'") + end +end do--function GC.DO(L) local cmds={ origin="origin", @@ -62,67 +111,7 @@ do--function GC.DO(L) fElps=function(...)gc.ellipse('fill',...)end, dElps=function(...)gc.ellipse('line',...)end, fPoly=function(...)gc.polygon('fill',...)end, - dPoly=function(...)gc.polygon('line',...)end, - fRPol=function(x,y,R,segments,r,phase) - local X,Y={},{} - local ang=phase or 0 - local angStep=6.283185307179586/segments - for i=1,segments do - X[i]=x+R*math.cos(ang) - Y[i]=y+R*math.sin(ang) - ang=ang+angStep - end - X[segments+1]=x+R*math.cos(ang) - Y[segments+1]=y+R*math.sin(ang) - - local L={} - local halfAng=6.283185307179586/segments/2 - local erasedLen=r*math.tan(halfAng) - for i=1,segments do - --Line - local x1,y1,x2,y2=X[i],Y[i],X[i+1],Y[i+1] - local dir=math.atan2(y2-y1,x2-x1) - table.insert(L,x1+erasedLen*math.cos(dir)) - table.insert(L,y1+erasedLen*math.sin(dir)) - table.insert(L,x2-erasedLen*math.cos(dir)) - table.insert(L,y2-erasedLen*math.sin(dir)) - - --Arc - ang=ang+angStep - local R2=R-r/math.cos(halfAng) - local arcCX,arcCY=x+R2*math.cos(ang),y+R2*math.sin(ang) - gc.arc('fill','open',arcCX,arcCY,r,ang-halfAng,ang+halfAng) - end - gc.polygon('fill',L) - end, - dRPol=function(x,y,R,segments,r,phase) - local X,Y={},{} - local ang=phase or 0 - local angStep=6.283185307179586/segments - for i=1,segments do - X[i]=x+R*math.cos(ang) - Y[i]=y+R*math.sin(ang) - ang=ang+angStep - end - X[segments+1]=x+R*math.cos(ang) - Y[segments+1]=y+R*math.sin(ang) - - local halfAng=6.283185307179586/segments/2 - local erasedLen=r*math.tan(halfAng) - for i=1,segments do - --Line - local x1,y1,x2,y2=X[i],Y[i],X[i+1],Y[i+1] - local dir=math.atan2(y2-y1,x2-x1) - gc.line(x1+erasedLen*math.cos(dir),y1+erasedLen*math.sin(dir),x2-erasedLen*math.cos(dir),y2-erasedLen*math.sin(dir)) - - --Arc - ang=ang+angStep - local R2=R-r/math.cos(halfAng) - local arcCX,arcCY=x+R2*math.cos(ang),y+R2*math.sin(ang) - gc.arc('line','open',arcCX,arcCY,r,ang-halfAng,ang+halfAng) - end - end, dPie=function(...)gc.arc('line',...)end, dArc=function(...)gc.arc('line','open',...)end, @@ -130,6 +119,9 @@ do--function GC.DO(L) fPie=function(...)gc.arc('fill',...)end, fArc=function(...)gc.arc('fill','open',...)end, fBow=function(...)gc.arc('fill','closed',...)end, + + fRPol=function(...)GC.regularPolygon('fill',...)end, + dRPol=function(...)GC.regularPolygon('line',...)end, } local sizeLimit=gc.getSystemLimits().texturesize function GC.DO(L)