ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
From: David Arnold <darnold@northcoast.com>
Cc: ntg-context@ntg.nl
Subject: Draw grid
Date: Fri, 02 Jan 2004 10:34:48 -0800	[thread overview]
Message-ID: <3.0.5.32.20040102103448.00984d20@mail.northcoast.com> (raw)

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;

             reply	other threads:[~2004-01-02 18:34 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-01-02 18:34 David Arnold [this message]
2004-01-03 21:13 ` Hans Hagen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3.0.5.32.20040102103448.00984d20@mail.northcoast.com \
    --to=darnold@northcoast.com \
    --cc=ntg-context@ntg.nl \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).