crazydraw2


//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




/$shape.i 



//(c) T.Frogley 2001

//codemonkey_uk@hotmail.com



#ifndef shape_i

#define shape_i



#define p_x 0

#define p_y 1

#define p_next 2

#define sizeof_p 3

#define p_blocktype "iip"



int centre_x;

int centre_y;



pointer NewPoint(int x, int y)

{

	pointer result;

	result = malloct(1,p_blocktype);

	if (result){

		result[p_x]=x;

		result[p_y]=y;

		result[p_next]=0;

	}

	return result;

}



DeletePoint(pointer p)

{

	pointer t;

	do{

	  t = p[p_next];

	  free(p);

	}while(p=t);

}



// ...

// a full implementation of shape.i

// can be found with PocketIFS



#endif shape_i






/$slist.i



//(c) T.Frogley 2001

//codemonkey_uk@hotmail.com



#ifndef SLIST_I

#define SLIST_I



//slist 'node' type

#define SLIST_ITR_DATA 0

#define SLIST_ITR_NEXT 1

#define SIZEOF_SLIST_ITR 2



//returns ptr to itr on success

pointer New_slist_itr(pointer d)

{

  pointer result;

  if (result=

    malloct(SIZEOF_SLIST_ITR,"pp"))

      result[SLIST_ITR_DATA]=d;

  return result;

}



#define SLIST_FIRST 0

#define SLIST_LAST 1

#define SIZEOF_SLIST 2



//returns ptr to slist on success

pointer New_slist()

{

  pointer result;

  result=malloct(2,"pp");

  return result;

}



//returns ptr to itr on success

pointer slist_Insert(

  pointer l, pointer d

)

{

  pointer itr;



  itr = New_slist_itr(d);

  if (!itr) return 0;



  itr[SLIST_ITR_NEXT]=l[SLIST_FIRST];

  l[SLIST_FIRST]=itr;



  return itr;

}



//returns ptr to itr on success

pointer slist_Append(

  pointer l, pointer d

)

{

  if (l[SLIST_LAST])

    return l[SLIST_LAST]

      =l[SLIST_LAST][SLIST_ITR_NEXT]

      =New_slist_itr(d);

    else

      return l[SLIST_FIRST]

        =l[SLIST_LAST]

        =New_slist_itr(d);

}



//removes the first item in the list

// and retuns its data

pointer slist_PopFront(

  pointer l

)

{

  pointer d,i;

  if (i=l[SLIST_FIRST]){

    d=i[SLIST_ITR_DATA];

    l[SLIST_FIRST]=i[SLIST_ITR_NEXT];

    free(i);

  }

  return d;

}



slist_Size(

  pointer l

)

{

  int s=0;

  pointer i;

  i=l[SLIST_FIRST];

  while(i){

    ++s;

     i=i[SLIST_ITR_NEXT];

  }

  return s;

}

#endif//slist_i

screenshot

Thad's Homepage (c) Wednesday 17th May 2006 T.Frogley
With thanks to notagoth.org
Valid HTML 4.01!
[ source ]