任意軸回転

 投稿者:しばっち  投稿日:2017年 2月11日(土)18時32分24秒
  立方体を任意軸回転させ平行投影で表示します

OPTION ARITHMETIC NATIVE
OPTION ANGLE DEGREES
DIM M(4,4),POINT(4),ROTX(4,4),ROTY(4,4),ROTZ(4,4)
SET POINT STYLE 1
LET LMIN=1E+10
LET LMAX=-1E+10
LET L=100
LOCATE VALUE NOWAIT(1),RANGE -1 TO 1,AT 0:X0
LOCATE VALUE NOWAIT(2),RANGE -1 TO 1,AT 1:Y0
LOCATE VALUE NOWAIT(3),RANGE -1 TO 1,AT 0:Z0
LOCATE VALUE NOWAIT(4),RANGE -180 TO 180,AT 0:THETA
LOCATE VALUE NOWAIT(5),RANGE -100 TO 100,AT 0:XMOVE
DO
   LOCATE VALUE NOWAIT(1):X0
   LOCATE VALUE NOWAIT(2):Y0
   LOCATE VALUE NOWAIT(3):Z0
   LOCATE VALUE NOWAIT(4):THETA
   LOCATE VALUE NOWAIT(5):XMOVE
   SET DRAW MODE HIDDEN
   CLEAR
   CALL CUBE(XMOVE,YMOVE,ZMOVE,L)
   IF FL=0 THEN
      SET WINDOW LMIN*2,LMAX*2,LMIN*2,LMAX*2
   END IF
   LET FL=1
   SET DRAW MODE EXPLICIT
LOOP

SUB PLOT(X,Y,Z)
   CALL ROTATE(X,Y,Z,X0,Y0,Z0,THETA,XX,YY,ZZ)
   IF FL=0 THEN
      LET LMIN=MIN(LMIN,XX)
      LET LMAX=MAX(LMAX,XX)
      LET LMIN=MIN(LMIN,YY)
      LET LMAX=MAX(LMAX,YY)
   ELSE
      PLOT LINES:XX,YY;
   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 CUBE(X,Y,Z,L)
   CALL PLOT(X-L/2,Y+L/2,Z-L/2)
   CALL PLOT(X+L/2,Y+L/2,Z-L/2)
   CALL PLOT(X+L/2,Y+L/2,Z+L/2)
   CALL PLOT(X-L/2,Y+L/2,Z+L/2)
   CALL PLOT(X-L/2,Y+L/2,Z-L/2)
   PLOT LINES
   CALL PLOT(X-L/2,Y-L/2,Z-L/2)
   CALL PLOT(X+L/2,Y-L/2,Z-L/2)
   CALL PLOT(X+L/2,Y-L/2,Z+L/2)
   CALL PLOT(X-L/2,Y-L/2,Z+L/2)
   CALL PLOT(X-L/2,Y-L/2,Z-L/2)
   PLOT LINES
   CALL PLOTLINE(X-L/2,Y+L/2,Z-L/2,X-L/2,Y-L/2,Z-L/2)
   CALL PLOTLINE(X+L/2,Y+L/2,Z-L/2,X+L/2,Y-L/2,Z-L/2)
   CALL PLOTLINE(X+L/2,Y+L/2,Z+L/2,X+L/2,Y-L/2,Z+L/2)
   CALL PLOTLINE(X-L/2,Y+L/2,Z+L/2,X-L/2,Y-L/2,Z+L/2)
END SUB

SUB ROTATE(XX,YY,ZZ,X0,Y0,Z0,TH,NX,NY,NZ) !'ロドリゲスの回転公式
!'原点と点(X0,Y0,Z0)を通る回転軸
!'点 P(XX,YY,ZZ) TH度回転 P'(NX,NY,NZ)
   DIM A(3,3)
   LET S=SQR(X0*X0+Y0*Y0+Z0*Z0)
   IF S>0 THEN
      LET X=X0/S
      LET Y=Y0/S
      LET Z=Z0/S
      LET A(1,1)=X*X*(1-COS(TH))+COS(TH)
      LET A(1,2)=X*Y*(1-COS(TH))+Z*SIN(TH)
      LET A(1,3)=X*Z*(1-COS(TH))-Y*SIN(TH)
      LET A(2,1)=Y*X*(1-COS(TH))-Z*SIN(TH)
      LET A(2,2)=Y*Y*(1-COS(TH))+COS(TH)
      LET A(2,3)=Y*Z*(1-COS(TH))+X*SIN(TH)
      LET A(3,1)=Z*X*(1-COS(TH))+Y*SIN(TH)
      LET A(3,2)=Z*Y*(1-COS(TH))-X*SIN(TH)
      LET A(3,3)=Z*Z*(1-COS(TH))+COS(TH)
      LET NX=XX*A(1,1)+YY*A(1,2)+ZZ*A(1,3)
      LET NY=XX*A(2,1)+YY*A(2,2)+ZZ*A(2,3)
      LET NZ=XX*A(3,1)+YY*A(3,2)+ZZ*A(3,3)
   END IF
END SUB
END
 

戻る