フラグメントシェーダー 正多角形

 投稿者:しばっち  投稿日:2022年 2月11日(金)14時24分7秒
  フラグメントシェーダー 正多角形

正多角形を定義しています。
http://math.sakura.ne.jp/?action=common_download_main&upload_id=2052


DIM P(2),COL(3)
ASK BITMAP SIZE XSIZE,YSIZE
SET COLOR MODE "NATIVE"
SET POINT STYLE 1
SET WINDOW -XSIZE/MIN(XSIZE,YSIZE),XSIZE/MIN(XSIZE,YSIZE),-YSIZE/MIN(XSIZE,YSIZE),YSIZE/MIN(XSIZE,YSIZE)
FOR YY=0 TO YSIZE-1
   FOR XX=0 TO XSIZE-1
      LET X=(XX*2-XSIZE)/MIN(XSIZE,YSIZE)
      LET Y=(YY*2-YSIZE)/MIN(XSIZE,YSIZE)
      CALL VEC2(P,X,Y)
      IF DISTANCE(P)<0 THEN
         CALL VEC3(COL,1,0,0)
      ELSE
         CALL VEC3(COL,0,0,0)
      END IF
      CALL SETCOLOR(COL(1),COL(2),COL(3))
      PLOT POINTS:X,Y
   NEXT XX
NEXT YY
END

EXTERNAL  FUNCTION POLYGON(P(),N)
FOR K=1 TO INT(N/2)
   LET S=S+ABS(P(1)*SIN(2*K*PI/N)-ABS(P(2))*COS(2*K*PI/N))
   LET E=E+SIN(2*K*PI/N)
NEXT K
LET S=S+.5*ABS(P(2)*SIN(N*PI/2))-.5*P(1)*COT(INT(N/2)*PI/N)
LET E=E-.5*COT(INT(N/2)*PI/N)
LET POLYGON=S-E
END FUNCTION

EXTERNAL  FUNCTION DISTANCE(P())
CALL ROTATE(P,PI/6)
LET DISTANCE=POLYGON(P,3)

!CALL ROTATE(P,PI/4)
!LET DISTANCE=POLYGON(P,4)

!CALL ROTATE(P,PI/5*1.5)
!LET DISTANCE=POLYGON(P,5)

!LET DISTANCE=POLYGON(P,6)

!CALL ROTATE(P,PI/14)
!LET DISTANCE=POLYGON(P,7)

!CALL ROTATE(P,PI/8)
!LET DISTANCE=POLYGON(P,8)
END FUNCTION

EXTERNAL  SUB ROTATE(A(),T)
DIM M(2,2)
LET S=SIN(T)
LET C=COS(T)
LET M(1,1)=C
LET M(1,2)=S
LET M(2,1)=-S
LET M(2,2)=C
MAT A=A*M
END SUB

EXTERNAL  SUB SETCOLOR(R,G,B)
SET COLOR COLORINDEX(CLAMP(R,0,1),CLAMP(G,0,1),CLAMP(B,0,1))
END SUB

EXTERNAL  SUB VEC2(A(),X,Y)
LET A(1)=X
LET A(2)=Y
END SUB

EXTERNAL  SUB VEC3(A(),X,Y,Z)
LET A(1)=X
LET A(2)=Y
LET A(3)=Z
END SUB

EXTERNAL  FUNCTION CLAMP(X,A,B)
LET CLAMP=MIN(B,MAX(X,A))
END FUNCTION
 

戻る