📄 hilbert.c
字号:
#define byte unsigned char
#define word unsigned short
#include <stdlib.h>
#include <stdio.h>
#include "metawndo.h" /* master MetaWINDOW include file */
/* special stack size declaration if using Turbo or Borland C++ */
#ifdef TurboC
extern unsigned _stklen = 14336U; /* stack size 14K */
#endif
void Waiting(int a);
int xa,ya, x,y, ox,oy, i,j,h,h0, penColr,cnt;
long maxColr, pnClrInc;
rect vR,R1,R2;
void a(int), b(int), c(int), d(int), Plt(void);
void plt_rect(void), quit(void), inc_pen(void);
void hilbert( int argc, char *argv[] )
{
int GrafixCard;
int count = 0;
mwEvent waitEvent;
GrafixCard = 2;
/* init the system */
// i = InitGraphics( GrafixCard );
/* switch to graphics page */
// SetDisplay(GrafPg0);
/* find number of colors supported */
maxColr = QueryColors();
if (maxColr > 65536) pnClrInc = 0x010305;
else if (maxColr > 256) pnClrInc = 0x0865;
else pnClrInc = 1;
/* set virtual coordinate system */
SetRect(&vR, 0,0,256,256); VirtualRect(&vR);
BackColor(0);
while( count++ < 1)
{
if(maxColr > 1)
RasterOp(zREPz);
else {
RasterOp(zXORz);
PenColor(White);
}
/* do that hilbert thing */
EraseRect(&vR);
for( cnt = 4; cnt > 0; cnt-- ) {
i=0; h0=256; h=h0; xa=h/2; ya=xa;
while (h > 4) {
i++;
h =h/2;
xa =xa + (h/2);
ya =ya + (h/2);
x =xa; y =ya;
ox =xa; oy= ya;
a(i);
}
/* stop if key pressed */
}
#ifndef CYCLE
PenColor(-1);
MoveTo( 0,vR.Ymax - 20 );
DrawString( "Press 'x' to exit" );
MoveTo( 0,vR.Ymax - 10 );
DrawString( "any other key to continue" );
while(!KeyEvent(1, &waitEvent));
if((waitEvent.eventChar == 'x') || (waitEvent.eventChar == 'X')) quit();
#else
#ifdef _MNT_
KeyEvent(0, &waitEvent);
if((waitEvent.eventChar == 'x') || (waitEvent.eventChar == 'X')) quit();
#endif
Waiting(150);
#endif
/* ye old bouncing box gizmo */
EraseRect(&vR);
RasterOp(zREPz);
for( cnt = 4; cnt > 0; cnt-- ) {
/* make pseudorandom number */
i = (rand() & 0x00f)+12;
if( cnt % 2) {
xa=i/2;
ya=i/3;
}
else {
xa=i/3;
ya=i/2;
}
SetRect(&R1, 0,0, i-6, i-6);
for( j = 256; j > 0; j-- ) {
plt_rect();
if((R1.Xmax > vR.Xmax) || (R1.Xmin < vR.Xmin)) /* flip x delta */
xa ^= -1;
if((R1.Ymax > vR.Ymax) || (R1.Ymin < vR.Ymin)) /* flip y delta */
ya ^= -1;
OffsetRect(&R1,xa,ya);
}
}
#ifndef CYCLE
PenColor(-1);
MoveTo( 0,vR.Ymax - 20 );
DrawString( "Press 'x' to exit" );
MoveTo( 0,vR.Ymax - 10 );
DrawString( "any other key to continue" );
while(!KeyEvent(1, &waitEvent));
if((waitEvent.eventChar == 'x') || (waitEvent.eventChar == 'X')) quit();
#else
#ifdef _MNT_
KeyEvent(0, &waitEvent);
if((waitEvent.eventChar == 'x') || (waitEvent.eventChar == 'X')) quit();
#endif
Waiting(150);
#endif
EraseRect(&vR); /* tunnel thing */
RasterOp(zREPz);
for( cnt = 16; cnt > 0; cnt-- ) {
SetRect(&R1,124,124,132,132);
SetRect(&R2,124,124,132,132);
PenColor(Red);
for( i = 0; i < 31; i++ ) {
inc_pen();
FrameRect(&R1);
InsetRect(&R1,-4,-4);
Waiting(1);
}
PenColor(Green);
for( i = 0; i < 31; i++ ) {
FrameRect(&R2);
InsetRect(&R2,-4,-4);
Waiting(1);
}
}
PenColor(White);
count = 10;
}
#ifndef CYCLE
PenColor(-1);
MoveTo( 0,vR.Ymax - 20 );
DrawString( "Press 'x' to exit" );
MoveTo( 0,vR.Ymax - 10 );
DrawString( "any other key to continue" );
while(!KeyEvent(1, &waitEvent));
if((waitEvent.eventChar == 'x') || (waitEvent.eventChar == 'X')) quit();
#else
#ifdef _MNT_
KeyEvent(0, &waitEvent);
if((waitEvent.eventChar == 'x') || (waitEvent.eventChar == 'X')) quit();
#endif
Waiting(150);
#endif
SetLocal();
}
void plt_rect()
{
inc_pen();
PaintOval(&R1);
return;
}
void Plt()
{
MoveTo(ox,oy); LineTo(x,y);
inc_pen();
ox=x; oy=y;
}
void inc_pen()
{
if( maxColr > 1 ) {
penColr += pnClrInc;
if (penColr > maxColr)
penColr = 1;
PenColor(penColr);
}
}
void a( int i )
{
if( i > 0 ) {
d(i-1); x=x-h; Plt();
a(i-1); y=y-h; Plt();
a(i-1); x=x+h; Plt();
b(i-1);
}
}
void b( int i )
{
if (i > 0) {
c(i-1); y=y+h; Plt();
b(i-1); x=x+h; Plt();
b(i-1); y=y-h; Plt();
a(i-1);
}
}
void c( int i )
{
if( i > 0 ) {
b(i-1); x=x+h; Plt();
c(i-1); y=y+h; Plt();
c(i-1); x=x-h; Plt();
d(i-1);
}
}
void d( int i )
{
if (i > 0) {
a(i-1); y=y-h; Plt();
d(i-1); x=x-h; Plt();
d(i-1); y=y+h; Plt();
c(i-1);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -