On 07/03/2010 10:03 AM, Taco Hoekwater wrote:

If this is too complicated, it might help if I can find out if a given
point lies inside a given cycled path.

Even this is fairly tricky. Some important questions are:

I thought this was a neat thing to try to write a macro for. Attached
is a test file that defines a macro inside() that takes two arguments:
a point and a cyclic path. It returns true if the point lies inside
that path, in most cases. There are lots of problems with it;
what happens to a point actually on the curve is basically undefined,
and self-intersects sometimes make it fail in interesting ways.

Anyway, perhaps it helps.

Best wishes,
Taco
def inside(expr j, p) =
  begingroup
  save n;
  hide(
  save done, L, P, t, bottom, top;
  boolean done;
  path L,P;
  numeric t[];
  pair bottom, top;
  bottom = llcorner p;
  top = urcorner p;
  P := p;
  n := 0;
  if (xpart j <= xpart bottom) or (xpart j >= xpart top) 
    or (ypart j <= ypart bottom) or (ypart j >= ypart top):
  else:
    L := j--(xpart j, ((ypart bottom)-100));
    done := false;
    forever:
     (t[n],whatever) = L intersectiontimes P;
      if (t[n]<0) and (n>0) :  % find upward match
        P := reverse P;
        numeric t[];
        L := j--(xpart j, ((ypart bottom)-100));
        forever:
          (t[n],whatever) = L intersectiontimes P;
          exitif t[n]<0;
          L := subpath (t[n]+epsilon, length L) of L;
          n := n + 1;
        endfor;
        done := true;
      fi
      exitif done;
      L := subpath (t[n]+epsilon,length L) of L;
      n := n + 1;
    endfor; 
  fi )
if (n>1) and (not odd n): true else: false fi
endgroup
enddef;

beginfig(1);
path P;
% P:= (fullcircle scaled 20) shifted (20,20);
% P:= (0,10){down}..(10,10){up} .. (30,10){down} .. (20,10){up}..cycle;
% P := unitsquare scaled 10 rotated 30 shifted (20,10);
% P := (10,10){down}..(30,10){up} .. (0,10){down} .. (20,10){up}..cycle;
P := (0,10){down}..(10,0){right}..(30,5){up}..
    (20,10){up}..(30,15){up}..(20,20){left}..cycle;
draw P withcolor red;

nx := 100;
ny := 100;
pair endgrid;
endgrid := (30,30);
pickup pencircle xscaled (xpart endgrid/nx) yscaled (ypart endgrid/ny);
pair J;

for k = 1 upto nx:
 for l = 1 upto ny:
  J := (k/nx*xpart endgrid,l/ny*ypart endgrid);
  if inside(J,P):
     drawdot J withcolor green;
  else:
     drawdot J withcolor blue;
  fi
 endfor;
endfor;
currentpicture := currentpicture scaled 10;

endfig;
end.



___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the 
Wiki!

maillist : [email protected] / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________

Reply via email to