Utilities

«*»= char * MyStrTok(char *s,char delim) while(*s != '' && *s != delim) s++; return(s); @

The following 3 functions implement the standard Bresenham algorithms extended with a pattern stencil. «*»= static void DrawCircle(int xmin,int ymin,int w) int x,y,d; WinPtr win=WinGetWindowPointer(WinGetDrawWindow());

x=0; y=w/2; d=1-w/2;

SetPoint(win,x+xmin+w/2,y+ymin+w/2); SetPoint(win,-x+xmin+w/2,-y+ymin+w/2); SetPoint(win,x+xmin+w/2,-y+ymin+w/2); SetPoint(win,-x+xmin+w/2,y+ymin+w/2); SetPoint(win,y+xmin+w/2,x+ymin+w/2); SetPoint(win,-y+xmin+w/2,-x+ymin+w/2); SetPoint(win,y+xmin+w/2,-x+ymin+w/2); SetPoint(win,-y+xmin+w/2,x+ymin+w/2);

while(y>x) if(d<0) d=d+2*x+3; x++; else d=d+2*(x-y)+5; x++; y–; SetPoint(win,x+xmin+w/2,y+ymin+w/2); SetPoint(win,-x+xmin+w/2,-y+ymin+w/2); SetPoint(win,x+xmin+w/2,-y+ymin+w/2); SetPoint(win,-x+xmin+w/2,y+ymin+w/2); SetPoint(win,y+xmin+w/2,x+ymin+w/2); SetPoint(win,-y+xmin+w/2,-x+ymin+w/2); SetPoint(win,y+xmin+w/2,-x+ymin+w/2); SetPoint(win,-y+xmin+w/2,x+ymin+w/2);

static void FillCircle(int xmin,int ymin,int w,CustomPatternType p) int x,y,d,i; WinPtr win=WinGetWindowPointer(WinGetDrawWindow());

x=0; y=w/2; d=1-w/2;

WinSetPattern(EmptyPattern);

while(y>x) if(d<0) d=d+2*x+3; x++; else d=d+2*(x-y)+5; x++; y–; for(i=-y+ymin+w/2;i<=y+ymin+w/2;i++) if(getpoint(x+xmin+w/2,i,p)) SetPoint(win,x+xmin+w/2,i); else UnsetPoint(win,x+xmin+w/2,i); if(getpoint(-x+xmin+w/2,i,p)) SetPoint(win,-x+xmin+w/2,i); else UnsetPoint(win,-x+xmin+w/2,i); for(i=-x+ymin+w/2;i<=x+ymin+w/2;i++) if(getpoint(y+xmin+w/2,i,p)) SetPoint(win,y+xmin+w/2,i); else UnsetPoint(win,y+xmin+w/2,i); if(getpoint(-y+xmin+w/2,i,p)) SetPoint(win,-y+xmin+w/2,i); else UnsetPoint(win,-y+xmin+w/2,i);

for(i=ymin;i<ymin+w;i++) if(getpoint(xmin+w/2,i,p)) SetPoint(win,xmin+w/2,i); else UnsetPoint(win,xmin+w/2,i);

void DrawLine(int xmin, int ymin, int xmax, int ymax, Word pattern) int x,y,dx,dy,sx,sy,e,i; Boolean changed; WinPtr win=WinGetWindowPointer(WinGetDrawWindow());

x=xmin; y=ymin;

dx=ABS(xmax-xmin); dy=ABS(ymax-ymin);

sx=sgn(xmax-xmin); sy=sgn(ymax-ymin);

if(dy>dx) int tmp;

tmp=dy; dy=dx; dx=tmp; changed=true; else changed=false;

e=2*dy-dx;

for(i=1;i<=dx;i++) if(pattern&(1«(iSetPoint(win,x,y); else UnsetPoint(win,x,y); while(e>=0) if(changed) x+=sx; else y+=sy; e-=2*dx; if(changed) y+=sy; else x+=sx; e+=2*dy; SetPoint(win,xmax,ymax); @

Determine if a point in the pattern array is set. The formula looks more complicated than it really is. The whole point is, that the pattern array consists of 4 words, so the y/2th entry has to be used and the comparison pattern has to be shifted if by 8 if y is odd ([[y«*»= static Boolean getpoint(int x, int y, CustomPatternType p) xy return((p[y/2]&(1«(7-x+((y @

«*»= int sgn(int x) if(x<0) return(-1); if(x>0) return(1);

return(0); @

[[SetPoint]] and [[GetPoint]] are a little critical, because they access the screen memory directly. This will probably break on color devices and maybe won't even work on OS3.3 «*»= static void SetPoint(WinPtr win,int x,int y) BytePtr line,p;

if(x<0 || x>=win->displayWidthV20) return; if(y<0 || y>=win->displayHeightV20) return;

y++; x++;

if(!RctPtInRectangle(x,y,&screenRect)) return;

line=win->displayAddrV20+y*(win->displayWidthV20/8); p=line+x/8; *p|=1«(7-x

static void UnsetPoint(WinPtr win,int x,int y) BytePtr line,p;

y++; x++;

if(!RctPtInRectangle(x,y,&screenRect)) return;

line=win->displayAddrV20+y*(win->displayWidthV20/8); p=line+x/8; *p&= (1«(7-x @