Newsgroups: comp.lang.c,comp.graphics.algorithms,sci.image.processing
Path: cantaloupe.srv.cs.cmu.edu!das-news2.harvard.edu!news2.near.net!news.mathworks.com!news.duke.edu!news-feed-1.peachnet.edu!gatech!newsfeed.pitt.edu!uunet!mr.net!msc.edu!network.com!news
From: collim@ferrari.network.com (Mike Collins)
Subject: Re: Sorting points to follow an edge? [longish]
Message-ID: <D5Ht7n.7KH@network.com>
Sender: Mike Collins
Organization: Network Systems Corporation
References: <3ifv8v$b4f@usenet.rpi.edu> <3iismj$ic5@roundup.crhc.uiuc.edu> <3its3q$l8u@t6.mscf.uky.edu>
Date: Wed, 15 Mar 1995 17:42:58 GMT
Lines: 119
Xref: glinda.oz.cs.cmu.edu comp.lang.c:131716 comp.graphics.algorithms:14515 sci.image.processing:13325

In article <3its3q$l8u@t6.mscf.uky.edu> cutts@mscf.uky.edu (Matthew Daniell Cutts) writes:
>>>>>that they are ordered from top to bottom to follow the edge that they
>>>>>represent.  I basically have two vectors of floating point numbers, one
>>>>>x and one y, whose index corresponds to a given point location on the
>>>>>edge of an object.  Originally, they are sorted first by increasing x
>
>I did a similar task, except that I linked binary pixel elements in an
>image array.  (( CHOP ))
-----------------------------

I missed the beginning of this thread, but here's a stand-alone program
written to run under PC DOS, which draws a circle on the screen, then
follows the edge pixel by pixel, either deleting or adding a pixel on the
edge at random intervals. The original circle consequently starts to look
pretty mangy after a while, but no matter how irregular it gets, the edge-
following routine continues to work.

It's a long time since I wrote it, and it was as part of a larger project,
so there may be some extraneous junk in the listing thet doesn't get used.
But it works, anyway. (My compiler is Mix's POWER-C).

#include <stdio.h>
#include <graphics.h>
#include <stdlib.h>
#include <time.h>

#define TRUE 1

FILE *fp;
double drand();
unsigned prob=30;
int direct[16][4] = { 0, 1, 8, 1,
                      0, 1, 8, 2,
                     -1, 0, 9, 3,
                     -1, 0, 9, 4,
                      0,-1,10, 5,
                      0,-1,10, 6,
                      1, 0,11, 7,
                      1, 0,11, 0,
                      1,-1,12, 0,
                      1, 1,13, 2,
                     -1, 1,14, 4,
                     -1,-1,15, 6,
                     -1, 0, 9, 5,
                      0,-1,10, 7,
                      1, 0,11, 1,
                      0, 1, 8, 3};


main()
{ int x,y,colour,square,gp1;
  struct vconfig screen_data;
  char ch,string[10];
  clrscrn();
  setvmode(CGA_320);
  getvconfig(&screen_data);
  srand((unsigned) time(NULL));
  plots("Input probability factor");
  poscurs(1,0);
  prob=atoi(gets(string));
  clrscrn2(0);
  for(gp1=30;gp1>25;gp1--)
    { move_to(160,100); circle(gp1,1); }   /* draw the initial form */
/*  move_to(160,100);
  pen_color(1);   fill(1);   */
  edge();
  setvmode(DEFAULTMODE);
}

edge()
{ int xr,y,direction,die_num,burst_num;
  xr=160;    /* scan vertically down till the first non-zero pixel */
  y=0;
  while(!(readdot(y,xr)))
    y++;
  direction=8;          /* follow the outside edge */
  die_num=drand(prob);    /* set the pixel to die    */
  burst_num=drand(prob);  /* set pixel to be born - to burst */
  while(1)
   { xr+=direct[direction][0];
     y+=direct[direction][1];
     if(readdot(y,xr))
       { writedot(y,xr,(rotate(readdot(y,xr))));
         if(die_num--) direction=direct[direction][2];
         else { writedot(y,xr,0);       /* kill the pixel. */
                die_num=drand(prob);      /* set the next pixel to die */
                direction=direct[direction][3];
              }
       }
     else if(burst_num--) direction=direct[direction][3];
          else { writedot(y,xr,between(1,3,2+(outside(1,28,drand(prob)))));
                 burst_num=drand(prob);
                 direction=direct[direction][2];
               }
   }
}

rotate(dot)
int dot;
{ if(dot==1) return 3;
  return (dot-1);
}

outside(lolim,hilim,value)
int lolim,hilim;
double value;
{ if(value<lolim) return -1;
  if(value>hilim) return 1;
  return 0;
}

between(lo,hi,val)
int lo,hi,val;
{ if(val<=lo) return lo;
  if(val>=hi) return hi;
  return val;
}


