グラデーション

 投稿者:しばっち  投稿日:2009年 5月10日(日)15時35分27秒
  N次多項式、補間式によるグラデーション画像の生成


RANDOMIZE
LET  XSIZE=640 !'画像サイズ
LET  YSIZE=400
CALL GINIT(XSIZE,YSIZE)
LET  TH=INT(RND*90)
LET  S=SIN(TH*PI/180)
LET  C=COS(TH*PI/180)
LET R1=INT(RND*256)
LET R2=INT(RND*256)
LET R3=INT(RND*256)
LET R4=INT(RND*256)
LET G1=INT(RND*256)
LET G2=INT(RND*256)
LET G3=INT(RND*256)
LET G4=INT(RND*256)
LET B1=INT(RND*256)
LET B2=INT(RND*256)
LET B3=INT(RND*256)
LET B4=INT(RND*256)
LET MODE=INT(RND*40)+1
SELECT CASE INT(RND*4)
CASE 0
   FOR YY=0 TO YSIZE-1
      FOR XX=0 TO XSIZE-1
         LET  T=(C*XX+S*(YSIZE-YY))/(XSIZE*C+YSIZE*S)
         LET RR=INTERPOLANT(MODE,R1,R2,T)
         LET GG=INTERPOLANT(MODE,G1,G2,T)
         LET BB=INTERPOLANT(MODE,B1,B2,T)
         CALL PSET(XX,YY,RR,GG,BB)
      NEXT XX
   NEXT YY
CASE 1
   LET  N=INT(RND*7)+3
   DIM X(N,N),Y(N),R(N),G(N),B(N)
   FOR I=1 TO N
      FOR J=1 TO N
         LET  T=(I-1)/(N-1)
         LET  X(I,J)=T^(N-J)
      NEXT J
   NEXT I
   MAT X=INV(X)
   FOR I=1 TO N
      LET  Y(I)=INT(RND*256)
   NEXT I
   MAT R=X*Y !'redの係数
   FOR I=1 TO N
      LET  Y(I)=INT(RND*256)
   NEXT I
   MAT G=X*Y !'greenの係数
   FOR I=1 TO N
      LET  Y(I)=INT(RND*256)
   NEXT I
   MAT B=X*Y !'blueの係数
   FOR YY=0 TO YSIZE-1
      FOR XX=0 TO XSIZE-1
         LET  T=(C*XX+S*(YSIZE-YY))/(XSIZE*C+YSIZE*S)
         LET RR=0
         LET GG=0
         LET BB=0
         FOR I=1 TO N  !'多項式の計算
            LET  RR=RR*T+R(I)
            LET  GG=GG*T+G(I)
            LET  BB=BB*T+B(I)
         NEXT I
         CALL PSET(XX,YY,RR,GG,BB)
      NEXT  XX
   NEXT  YY
CASE 2 !'四角形
   FOR YY=0 TO YSIZE-1
      FOR XX=0 TO XSIZE-1
         LET  RR=RECTCOL(0,0,XSIZE-1,YSIZE-1,R1,R2,R3,R4,XX,YY,MODE)
         LET  GG=RECTCOL(0,0,XSIZE-1,YSIZE-1,G1,G2,G3,G4,XX,YY,MODE)
         LET  BB=RECTCOL(0,0,XSIZE-1,YSIZE-1,B1,B2,B3,B4,XX,YY,MODE)
         CALL PSET(XX,YY,RR,GG,BB)
      NEXT  XX
   NEXT  YY
CASE 3 !'三角形
   LET R5=INT(RND*256)
   LET G5=INT(RND*256)
   LET B5=INT(RND*256)
   LET X1=0
   LET Y1=0
   LET X2=XSIZE-1
   LET Y2=0
   LET X3=X2
   LET Y3=YSIZE-1
   LET X4=0
   LET Y4=YSIZE-1
   LET X5=INT(XSIZE/2)
   LET Y5=INT(YSIZE/2)
   FOR YY=0 TO YSIZE-1
      FOR XX=0 TO XSIZE-1
         IF AREA3(X5,Y5,X1,Y1,X2,Y2,XX,YY)<>0 THEN
            LET  RR=TRIANGLECOL(X5,Y5,X1,Y1,X2,Y2,R5,R1,R2,XX,YY)
            LET  GG=TRIANGLECOL(X5,Y5,X1,Y1,X2,Y2,G5,G1,G2,XX,YY)
            LET  BB=TRIANGLECOL(X5,Y5,X1,Y1,X2,Y2,B5,B1,B2,XX,YY)
         ELSEIF AREA3(X5,Y5,X2,Y2,X3,Y3,XX,YY)<>0 THEN
            LET  RR=TRIANGLECOL(X5,Y5,X2,Y2,X3,Y3,R5,R2,R3,XX,YY)
            LET  GG=TRIANGLECOL(X5,Y5,X2,Y2,X3,Y3,G5,G2,G3,XX,YY)
            LET  BB=TRIANGLECOL(X5,Y5,X2,Y2,X3,Y3,B5,B2,B3,XX,YY)
         ELSEIF AREA3(X5,Y5,X3,Y3,X4,Y4,XX,YY)<>0 THEN
            LET  RR=TRIANGLECOL(X5,Y5,X3,Y3,X4,Y4,R5,R3,R4,XX,YY)
            LET  GG=TRIANGLECOL(X5,Y5,X3,Y3,X4,Y4,G5,G3,G4,XX,YY)
            LET  BB=TRIANGLECOL(X5,Y5,X3,Y3,X4,Y4,B5,B3,B4,XX,YY)
         ELSEIF AREA3(X5,Y5,X4,Y4,X1,Y1,XX,YY)<>0 THEN
            LET  RR=TRIANGLECOL(X5,Y5,X4,Y4,X1,Y1,R5,R4,R1,XX,YY)
            LET  GG=TRIANGLECOL(X5,Y5,X4,Y4,X1,Y1,G5,G4,G1,XX,YY)
            LET  BB=TRIANGLECOL(X5,Y5,X4,Y4,X1,Y1,B5,B4,B1,XX,YY)
         END IF
         CALL PSET(XX,YY,RR,GG,BB)
      NEXT  XX
   NEXT  YY
END SELECT
END

EXTERNAL SUB GINIT(XSIZE,YSIZE)
SET BITMAP SIZE XSIZE,YSIZE
SET COLOR MODE "NATIVE"
CLEAR
SET POINT STYLE 1
SET WINDOW 0,XSIZE-1,YSIZE-1,0
END SUB

EXTERNAL SUB PSET(X,Y,R,G,B)
LET  RR=MIN(255,MAX(0,INT(R)))
LET  GG=MIN(255,MAX(0,INT(G)))
LET  BB=MIN(255,MAX(0,INT(B)))
SET COLOR COLORINDEX(RR/255,GG/255,BB/255)
PLOT POINTS: X , Y
END SUB

EXTERNAL  FUNCTION RECTCOL(X1,Y1,X2,Y2,C1,C2,C3,C4,X,Y,MODE)
LET  P=(X-X1)/(X2-X1)
LET  Q=(Y-Y1)/(Y2-Y1)
LET S1=INTERPOLANT(MODE,C1,C2,P)
LET S2=INTERPOLANT(MODE,C3,C4,Q)
LET RECTCOL=INTERPOLANT(MODE,S1,S2,Q)
END FUNCTION

EXTERNAL  FUNCTION INTERPOLANT(MODE,A,B,T) !'補間式
LET T=MIN(1,MAX(0,T))
SELECT CASE MODE
CASE 1
   LET  V=(1-T)*A+B*T
CASE 2
   LET  V=(1-T)^2*A+B*T^2
CASE 3
   LET  V=(1-T)^3*A+B*T^3
CASE 4
   LET  V=(1-T)^4*A+B*T^4
CASE 5
   LET  V=(1-T)^5*A+B*T^5
CASE 6
   LET  V=(1-T^2)*A+B*T^2
CASE 7
   LET  V=(1-T^3)*A+B*T^3
CASE 8
   IF T=1 THEN LET  V=B ELSE  LET  V=SQR(1-T^2)*A+B*T^2
CASE 9
   IF T=1 THEN LET  V=B ELSE  LET  V=SQR(1-T^3)*A+B*T^3
CASE 10
   IF T=1 THEN LET  V=B ELSE  LET  V=SQR(1-T^2)^3*A+B*T
CASE 11
   LET  N=1/2
   LET  M=1/3
   IF T=1 THEN LET  V=B  ELSE LET  V=(1-T^M)^N*A+B*T^M
CASE 12
   LET  V=A*COS(PI/2*T)+B*SIN(PI/2*T)
CASE 13
   LET  V=A*COS(PI/2*T)^2+B*SIN(PI/2*T)^3
CASE 14
   LET  V=A*COS(PI/2*T^2)+B*SIN(PI/2*T^2)
CASE 15
   LET  V=A*(B/A)^T
CASE 16
   LET  V=A*EXP(T*LOG(B/A))
CASE 17
   LET  V=A+(B-A)*TAN(PI/4*T)
CASE 18
   LET  V=A+(B-A)*SIN(PI/2*T)
CASE 19
   LET  V=A+(B-A)*LOG(T+1)/LOG(2)
CASE 20
   LET  V=A+(B-A)*LOG(2*T+1)/LOG(3)
CASE 21
   LET  V=A+(B-A)*LOG((EXP(1)-1)*T+1)
CASE 22
   LET  V=A+(B-A)*(2^T-1)
CASE 23
   LET  V=A+(B-A)*ATN(T)*4/PI
CASE 24
   IF T=0 THEN LET  V=A ELSE  LET  V=A+(B-A)*LOG(10*T)/LOG(10)
CASE 25
   LET  V=A+(B-A)*SQR(T)
CASE 26
   LET  V=A+(B-A)*T^(1/5)
CASE 27
   LET V=A+(B-A)*T^10
CASE 28
   LET  V=A+(B-A)*ATN(T)*4/PI
CASE 29
   LET  V=A+(B-A)*ASIN(T)*2/PI
CASE 30
   LET  V=A+(B-A)*SIN(PI/2*T)^COS(PI/2*T)
CASE 31
   LET V=A+T-1+ABS(B-A)^T
CASE 32
   LET V=A+(B-A)*(1-(1-T)^T)
CASE 33
   LET V=A+(B-A)*(1-(1-T)/(1+T^2))
CASE 34
   LET V=A+(B-A)*(T*T*T+T*T+T)/(2*T*T+1)
CASE 35
   IF T=1 THEN LET V=B ELSE LET V=A+(B-A)*SIN(PI*(1-T))/((1-T)*PI)
CASE 36
   LET V=A+(B-A)*(T^3+3*T^2+2*T)/(T^2+2*T+3)
CASE 37
   LET V=A+(B-A)*T^3/(T^2+T-1)
CASE 38
   LET V=A+(B-A)*(4^T-1)/(2^T+1)
CASE 39
   LET V=A+(B-A)*(T+T^2+T^3+T^4+T^5+T^6+T^7)/7
CASE 40
   LET V=A+(B-A)*(5^T-1)/4
END SELECT
LET INTERPOLANT=V
END FUNCTION

EXTERNAL  FUNCTION AREA3(X1, Y1, X2, Y2, X3, Y3, PX, PY)
LET  T = TRIANGLE(X1, Y1, X2, Y2, X3, Y3)
LET  A = TRIANGLE(X1, Y1, X2, Y2, PX, PY)
LET  B = TRIANGLE(X2, Y2, X3, Y3, PX, PY)
LET  C = TRIANGLE(X1, Y1, X3, Y3, PX, PY)
IF ABS(A + B + C - T) < 1 THEN LET  AREA3 = -1 ELSE LET  AREA3 = 0
END FUNCTION

EXTERNAL  FUNCTION TRIANGLE(X1, Y1, X2, Y2, X3, Y3) !'三角形の面積
LET  TRIANGLE = ABS(X1 * Y2 + X2 * Y3 + X3 * Y1 - X2 * Y1 - X3 * Y2 - Y3 * X1) / 2
END FUNCTION
 

Re: グラデーション

 投稿者:しばっち  投稿日:2009年 5月10日(日)15時36分51秒
  > No.356[元記事へ]

続き


EXTERNAL  FUNCTION TRIANGLECOL(OX,OY,AX,AY,BX,BY,C1,C2,C3,X,Y)
LET  A = AX - OX
LET  B = BX - OX
LET  C = AY - OY
LET  D = BY - OY
LET  PX = X - OX
LET  PY = Y - OY
LET  DET = A * D - B * C
IF DET = 0 THEN
   EXIT FUNCTION
END IF
LET  S = (D * PX - B * PY) / DET
IF S < 0 THEN
   EXIT FUNCTION
END IF
LET  T = (A * PY - C * PX) / DET
IF T < 0 THEN
   EXIT FUNCTION
END IF
IF S + T <= 1 THEN
   LET  TRIANGLECOL=(1-S)*(1-T)*C1+S*C2+T*C3
END IF
END FUNCTION
 

戻る