From 2a0b26f2fd5ad2f5a56d416f7b6a1d9bb558d43e Mon Sep 17 00:00:00 2001 From: MrZ626 <1046101471@qq.com> Date: Sun, 8 Aug 2021 03:08:35 +0800 Subject: [PATCH] =?UTF-8?q?GC=E6=8B=93=E5=B1=95=E6=A8=A1=E5=9D=97=E8=B0=83?= =?UTF-8?q?=E6=95=B4=EF=BC=8C=E5=9C=86=E8=A7=92=E6=AD=A3=E5=A4=9A=E8=BE=B9?= =?UTF-8?q?=E5=BD=A2=E6=88=90=E4=B8=BA=E7=BB=98=E5=9B=BE=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Zframework/gcExtend.lua | 112 +++++++++++++++++++--------------------- 1 file changed, 52 insertions(+), 60 deletions(-) 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)