ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* Draw grid
@ 2004-01-02 18:34 David Arnold
  2004-01-03 21:13 ` Hans Hagen
  0 siblings, 1 reply; 2+ messages in thread
From: David Arnold @ 2004-01-02 18:34 UTC (permalink / raw)
  Cc: ntg-context

Hans,

This sample code will give you an idea of my last feature request for
mp-tool. Often, I will design my schematic on graph paper, then start to
code. But there are times when I like to work interactively, write a little
code, test to see what I get, adjust, ... When I am in that mode, a grid is
"very" helpful. Once I am through with adjustments, I simply comment out
the line "  drawgrid(0,0,20,10,1u,1u);".

My drawgrid routine was stolen from the schema.mp file and adjusted for a
quick fix (I am writing for a deadline again), but it works. But it would
be nice if something like this were in mp-tool. Turn on your numbered grid,
work interactively, turn it off when done. Very nice.

Happy New Year!


input mp-tool

def drawgrid(expr xo,yo,nx,ny,sx,sy)=
  begingroup
    save cpen,result;
    pen cpen;
    picture result;

    cpen:=pencircle scaled 1pt;
    result:=nullpicture;

    for x=0 upto nx:
      for y=0 upto ny:
	addto result doublepath (x*sx,y*sy) withpen cpen;
      endfor
    endfor

    defaultfont:="cmr7";
    defaultscale:=7pt/fontsize defaultfont;
    for x=0 upto nx:
      addto result also thelabel.bot(decimal(x),(x*sx,0)) withpen cpen;
    endfor
    for y=0 upto ny:
      addto result also thelabel.lft(decimal(y),(0,y*sy)) withpen cpen;
    endfor;
    
    draw result;
  endgroup
enddef;


beginfig(6);

  %initialize scale
  numeric u; 1u=0.25in;

  drawgrid(0,0,20,10,1u,1u);

  %draw line for measurement later
  draw (0,0)--(10u,0);
  
  %draw base for pivot
  path base;
  base:=unitsquare xyscaled (4u,1u);
  base:=base shifted (8u,-2u);
  stripe_path_a(withcolor black)(draw)
  base withcolor white;
  draw (8u,-1u)--(12u,-1u);

  %draw pivot
  path tri;
  tri:=(-1,-1)--(0,1)--(1,-1)--cycle;
  tri:=tri scaled u;
  tri:=tri shifted (10u,0);
  fill tri withcolor 0.85white;

  %initialize pendulum
  path pend;
  pend:=(9.5,-0.5)--(10.5,-0.5)--(10.5,10.5)--(9.5,10.5)--cycle;
  pend:=pend scaled u;
  draw pend;
  
  %draw pivot point
  drawdot(10u,0) withpen pencircle scaled 6pt;

  %draw spring block left
  path left;
  left:=(2,9)--(3,9)--(3,11)--(2,11)--cycle;
  left:=left scaled u;
  stripe_path_a(withcolor black)(draw)
  left withcolor white;
  draw (3u,9u)--(3u,11u);

  %draw spring block right
  path right;
  right:=(17,9)--(18,9)--(18,11)--(17,11)--cycle;
  right:=right scaled u;
  stripe_path_a(withcolor black)(draw)
  right withcolor white;
  draw (17u,9u)--(17u,11u);

  %spring
  path spring, link;
  link:=(0,0)--(0.25,0.5)--(0.75,-0.5)--(1,0);
  spring:=link;
  for k=1 upto 2:
    spring:=spring--(link shifted (k,0));
  endfor
  spring:=spring scaled u;

  %left spring
  draw (3u,10u)--(5u,10u);
  draw spring shifted (5u,10u);
  draw (8u,10u)--(10u,10u);

  %right spring
  draw (10u,10u)--(12u,10u);
  draw spring shifted (12u,10u);
  draw (15u,10u)--(17u,10u);

  %draw spring contact point
  drawdot(10u,10u) withpen pencircle scaled 6pt;
  
  %label springs
  label(btex $k$ etex, (6.5u,11u));
  label(btex $k$ etex, (13.5u,11u));

  %label some measurement lengths
  pair A, B, C, D, E;
  A:=(1u,0);
  B:=(1u,10u);
  C:=(5u,0);
  D:=(5u,5u);
  E:=(10u,5u);
  drawdblarrow A--B;
  label.lft(btex $L$ etex, 0.5[A,B]);
  drawdblarrow C--D;
  draw (0,10u)--(2u,10u);
  label.lft(btex $L/2$ etex, 0.5[C,D]);
  draw (4u,5u)--(6u,5u);
  drawdot(E) withpen pencircle scaled 6pt;
  label.top(btex cm etex,E shifted (0,5));

endfig;

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Draw grid
  2004-01-02 18:34 Draw grid David Arnold
@ 2004-01-03 21:13 ` Hans Hagen
  0 siblings, 0 replies; 2+ messages in thread
From: Hans Hagen @ 2004-01-03 21:13 UTC (permalink / raw)
  Cc: ntg-context

Hi David,

I added to mp-tool:

% showgrid(-5,10,1cm,-10,10,1cm);

def showgrid (expr MinX, MaxX, DeltaX, MinY, MaxY, DeltaY)=
   begingroup
     save defaultfont, defaultscale, size ;
     string  defaultfont  ; defaultfont  := "cmtt10"; % i.e. infofont
     numeric defaultscale ; defaultscale := 8pt / fontsize defaultfont;
     numeric size         ; size         := 2pt ;
     for x=MinX upto MaxX :
       for y=MinY upto MaxY :
         draw (x*DeltaX, y*DeltaY)
           withpen pencircle scaled
           if (x mod 5 = 0) and (y mod 5 = 0) :
             1.5size withcolor .50white
           else :
                size withcolor .75white
           fi ;
       endfor ;
     endfor ;
     for x=MinX upto MaxX:
       label.bot(decimal x, (x*DeltaX,-size));
     endfor ;
     for y=MinY upto MaxY:
       label.lft(decimal y, (-size,y*DeltaY)) ;
     endfor ;
   endgroup
enddef;

I also send you a supp-pdf that does not fail on you example (peculiar 
sequence of settings catched)

Hans  

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2004-01-03 21:13 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-01-02 18:34 Draw grid David Arnold
2004-01-03 21:13 ` Hans Hagen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).