//crazydraw2
//(c) T.Frogley 2001
// http://thad.notagoth.org/
#include "shape.i"
#include "slist.i"
//4 way reflection
point(int x,int y,int ox,int oy,int c)
{
int x2,y2,ox2,oy2;
ox2 = 160-ox;
oy2 = 160-oy;
x2 = 160-x;
y2 = 160-y;
line( c, x, y, ox, oy );
line( c, x, y2, ox,oy2 );
line( c, x2, y ,ox2, oy );
line( c, x2, y2, ox2, oy2 );
}
//current item to draw
#define CUR 0
//end (location to insert new lines)
#define END 1
//first point in line
#define HEAD 2
#define COL 3
pointer segment(pointer l)
{
pointer nxt;
nxt=l[CUR][p_next];
if (nxt){
point(
l[CUR][p_x], l[CUR][p_y],
nxt[p_x], nxt[p_y],
l[COL]
);
point(
l[CUR][p_y], l[CUR][p_x],
nxt[p_y], nxt[p_x],
l[COL]
);
}
return nxt;
}
addline(pointer linelist, pointer line)
{
//add finished line to list
if(!slist_Append(linelist, line)){
puts("could not insert line!\n");
DeletePoint( line[HEAD] );
free(line);
}
return slist_Size(linelist);
}
main()
{
int x,y,e;
int ox,oy;
pointer nxt,l,i,p;
pointer line,linelist;
int linecount;
puts(
"crazydraw2\n"+
"(c) T.Frogley 2001\n"+
"codemonkey_uk@hotmail.com\n"
);
linelist=New_slist();
graph_on();
rect(0,0,0,160,160,0);
while(1){
e=event(0);
if (e==2){ //pen down
if (line){
if (line[HEAD][p_next]){
linecount=addline( linelist, line);
}
}
else{
//create new line
x = penx();y = peny();
line = malloct(4,"pppi");
line[HEAD] = line[CUR] =
line[END] = NewPoint(x,y);
}
}
else if (e==3){ //pen up
if (line) if (line[HEAD][p_next]){
linecount=addline( linelist, line);
line=0;
}
}
else if (e==4){ //pen move
if (line==0)
puts(
"penmove before pendown!\n"
);
else{
ox = x; oy = y;
x = penx();y = peny();
point(x,y,ox,oy,1);
point(y,x,oy,ox,1);
if (line[END]){
line[END][p_next]=
NewPoint(x,y);
if (line[END][p_next])
line[END]=line[END][p_next];
}
}
}
else{//any other / no event
//x = penx();y = peny();
if (i = linelist[SLIST_FIRST]){
l = i[SLIST_ITR_DATA];
if (l[CUR]){
nxt = segment(l);
if (linecount > 2){
//this is the deletion phase
//and this is the first segment,
if (!l[COL])
if (l[CUR]==l[HEAD]){
//remove(first) segment
l[HEAD] = nxt;
free(l[CUR]);
//if last segment, remove
if (!nxt){
free(
slist_PopFront(linelist)
);
linecount=slist_Size(linelist);
}
}
}
l[CUR] = nxt;
}
else{
//return to start of line
l[CUR]=l[HEAD];
//flip black/white
l[COL]=!l[COL];
}
while(i = i[SLIST_ITR_NEXT]){
l = i[SLIST_ITR_DATA];
if (l[CUR]){
nxt = segment(l);
l[CUR] = nxt;
}
else{
//return to start of line
l[CUR]=l[HEAD];
//flip black/white
l[COL]=!l[COL];
}
}//while
}
}
}
}
//end
|