シェルピンスキーの三角形

 投稿者:しばっち  投稿日:2013年 9月 2日(月)20時02分5秒
  RANDOMIZE
OPTION ARITHMETIC NATIVE
PUBLIC NUMERIC K,XX(4096),YY(4096),ZZ(4096)  !'4^LEV
OPTION ANGLE DEGREES
LET ZTH=0          ! z軸のまわりの回転角
LET XTH=0          ! x軸のまわりの回転角初期値
LET YTH=0          ! y軸のまわりの回転角初期値
DIM M(4,4),POINT(4),ROTX(4,4),ROTY(4,4)
MAT M=ROTATE(ZTH)
SET POINT STYLE 1
LET LMIN=1E+10
LET LMAX=-1E+10
INPUT  PROMPT "LEVEL(1<=LEV=<6)=":LEV
CALL RECURSIVE(LEV,0,0,0,128)
LET LL=128/2^LEV
FOR I=1 TO K
   LET MX=MX+XX(I)+2*LL*COS(0)
   LET MX=MX+XX(I)+2*LL*COS(120)
   LET MX=MX+XX(I)+2*LL*COS(240)
   LET MX=MX+XX(I)
   LET MY=MY+YY(I)
   LET MY=MY+YY(I)
   LET MY=MY+YY(I)
   LET MY=MY+YY(I)+2*LL
   LET MZ=MZ+ZZ(I)+2*LL*SIN(0)
   LET MZ=MZ+ZZ(I)+2*LL*SIN(120)
   LET MZ=MZ+ZZ(I)+2*LL*SIN(240)
   LET MZ=MZ+ZZ(I)
   LET XMIN=MIN(XMIN,XX(I))
   LET XMAX=MAX(XMAX,XX(I))
   LET YMIN=MIN(YMIN,YY(I))
   LET YMAX=MAX(YMAX,YY(I))
   LET ZMIN=MIN(ZMIN,ZZ(I))
   LET ZMAX=MAX(ZMAX,ZZ(I))
NEXT I
LET RANGE=MAX(XMAX-XMIN,MAX(YMAX-YMIN,ZMAX-ZMIN))
LET XDT=RND-.5
LET YDT=RND-.5
LET MX=MX/4/K
LET MY=MY/4/K
LET MZ=MZ/4/K
LOCATE VALUE NOWAIT(1),RANGE  0 TO 2,AT 1 : SCALE
LOCATE VALUE NOWAIT(2),RANGE -3 TO 5,AT 1 : SPEED
LOCATE VALUE NOWAIT(3),RANGE -RANGE TO RANGE,AT 0 : XMOVE
LOCATE VALUE NOWAIT(4),RANGE -RANGE TO RANGE,AT 0 : YMOVE
LOCATE VALUE NOWAIT(5),RANGE -RANGE TO RANGE,AT 0 : ZMOVE
DO
   LOCATE VALUE NOWAIT(1): SCALE
   LOCATE VALUE NOWAIT(2): SPEED
   LOCATE VALUE NOWAIT(3): XMOVE
   LOCATE VALUE NOWAIT(4): YMOVE
   LOCATE VALUE NOWAIT(5): ZMOVE
   MAT ROTX=IDN ! x軸のまわりの回転
   LET ROTX(2,2)=COS(XTH)
   LET ROTX(2,3)=SIN(XTH)
   LET ROTX(3,2)=-SIN(XTH)
   LET ROTX(3,3)=COS(XTH)
   MAT ROTY=IDN ! y軸のまわりの回転
   LET ROTY(1,1)=COS(YTH)
   LET ROTY(1,3)=-SIN(YTH)
   LET ROTY(3,1)=SIN(YTH)
   LET ROTY(3,3)=COS(YTH)
   MAT M=M *  ROTX * ROTY
   SET DRAW MODE HIDDEN
   CLEAR
   FOR I=1 TO K
      CALL  TETRAHEDRON(XX(I),YY(I),ZZ(I),LL)
   NEXT I
   IF FL=0 THEN
      SET WINDOW -LMAX,LMAX,-LMAX,LMAX
      LET WW=LMAX*2
   END IF
   LET FL=1
   SET DRAW MODE EXPLICIT
   MOUSE POLL X,Y,L,R
   IF R<>0 THEN STOP
   LET XTH=0
   LET YTH=0
   IF L<>0 THEN
      DO WHILE L<>0
         MOUSE POLL X,Y,L,R
      LOOP
      LET XDT=-(Y-Y0)/WW*5
      LET YDT= (X-X0)/WW*5
      LET XDT=MAX(-2,MIN(2,XDT))
      LET YDT=MAX(-2,MIN(2,YDT))
   ELSE
      LET XTH=XTH+XDT*SPEED
      LET YTH=YTH+YDT*SPEED
   END IF
   LET X0=X
   LET Y0=Y
LOOP

SUB PLOT(X,Y,Z)
   LET POINT(1)=X-MX+XMOVE
   LET POINT(2)=Y-MY+YMOVE
   LET POINT(3)=Z-MZ+ZMOVE
   MAT POINT=POINT*M
   IF FL=0 THEN
      LET LMIN=MIN(LMIN,POINT(1))
      LET LMAX=MAX(LMAX,POINT(1))
      LET LMIN=MIN(LMIN,POINT(2))
      LET LMAX=MAX(LMAX,POINT(2))
   ELSE
      PLOT LINES:POINT(1)*SCALE,POINT(2)*SCALE;
   END IF
END SUB

SUB PLOTLINE(X1,Y1,Z1,X2,Y2,Z2)
   PLOT LINES
   CALL PLOT(X1,Y1,Z1)
   CALL PLOT(X2,Y2,Z2)
   PLOT LINES
END SUB

SUB TETRAHEDRON(X,Y,Z,L) !'4面体
   CALL PLOT(X+2*L*COS(0),Y,Z+2*L*SIN(0))
   CALL PLOT(X+2*L*COS(120),Y,Z+2*L*SIN(120))
   CALL PLOT(X+2*L*COS(240),Y,Z+2*L*SIN(240))
   CALL PLOT(X+2*L*COS(0),Y,Z+2*L*SIN(0))
   CALL PLOTLINE(X+2*L*COS(0),Y,Z+2*L*SIN(0),X,Y+2*L,Z)
   CALL PLOTLINE(X+2*L*COS(120),Y,Z+2*L*SIN(120),X,Y+2*L,Z)
   CALL PLOTLINE(X+2*L*COS(240),Y,Z+2*L*SIN(240),X,Y+2*L,Z)
END SUB
END

EXTERNAL  SUB RECURSIVE(LEV,X,Y,Z,L) !'シェルピンスキー三角形
OPTION ARITHMETIC NATIVE
OPTION ANGLE DEGREES
IF LEV=0 THEN
   LET K=K+1
   LET XX(K)=X
   LET YY(K)=Y
   LET ZZ(K)=Z
ELSE
   CALL RECURSIVE(LEV-1,X,Y+L,Z,L/2)
   CALL RECURSIVE(LEV-1,X+L*COS(0),Y,Z+L*SIN(0),L/2)
   CALL RECURSIVE(LEV-1,X+L*COS(120),Y,Z+L*SIN(120),L/2)
   CALL RECURSIVE(LEV-1,X+L*COS(240),Y,Z+L*SIN(240),L/2)
END IF
END SUB
 

戻る