ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
From: Mojca Miklavec <mojca.miklavec.lists@gmail.com>
Subject: Re: Metapost -- What am I doing wrong
Date: Mon, 26 Dec 2005 00:02:59 +0100	[thread overview]
Message-ID: <6faad9f00512251502s560cd871i541b9453ab906e79@mail.gmail.com> (raw)
In-Reply-To: <CF326809-45FA-4829-81E2-60D4D422BA2A@cox.net>

David Arnold wrote:
> All,
>
> Can someone tell me what I am doing wrong here?

\useMPgraphic is meant to include whole figures inside text, not for
including portions of other metapost code inside metapost.

What you can do is to define new functions inside \startMPinclusions
... \stopMPinclusions or in an external file.

I don't doubt that implementing support for your way of thinking in
metafun is doable, but I don't know if it is worth the effort or not.

Here's an approximate transliteration into working code (I don't claim
that it's most elegant and I didn't know how to pass a function as an
argument, but I hope you can get an idea how to proceed. With slightly
more arguments/parameters to the defined functions, you can get rid of
hardcoding "fives" in your code for example.)

\setupcolors[state=start]

\definecolor[gridlines][s=0.7]

\startMPinclusions
	color gridlines; gridlines=\MPcolor{gridlines};

	% draw the grid 2n x 2n with unit length u
	def draw_axis(expr n, u) =

		% draw the grid
		for i=-5 upto 5:
			draw ((i,-5)--(i,5)) scaled u;
			draw ((-5,i)--(5,i)) scaled u;
		endfor;

		%draw axes
		drawdblarrow (-5.2u,0)--(5.2u,0);
		drawdblarrow (0,-5.2u)--(0,5.2u);

		%label axes
		label.rt(btex $x$ etex, (5.2u,0));
		label.top(btex $y$ etex, (0,5.2u));
		label.bot(btex $5$ etex, (5u,0));
		label.lft(btex $5$ etex, (0,5u));
	enddef;

	def scale_and_draw_f(expr p, u) =

		%scale function path
		%p:=p scaled u;

		%draw function path
		drawdblarrow (p scaled u) withcolor blue;

	enddef;

	% I don't know how to give the function as an argument,
	% so I cheated and copied it here once more
	vardef create_function_path_f =

		%define clipping path
		path cpath;
		cpath:=(-5,-5)--(5,-5)--(5,5)--(-5,5)--cycle;

		%define function
		vardef f(expr x)=
			x*x
		enddef;

		%create function path
		path p; p:=(-5,f(-5));
		for x=-5 step .1 until 5:
			p:=p--(x,f(x));
		endfor;
		p:=p--(5,f(5));

		%clip function path to clipping path
		p:=p cutbefore cpath;
		p:=reverse p;
		p:=p cutbefore cpath;

		p
	enddef;

\stopMPinclusions

\starttext

\startMPpage

	%define function
	vardef f(expr x)=
		x*x
	enddef;

	path p;

	p := create_function_path_f;

	%initialize scale
	numeric u; 10u=2in;

	draw_axis(n, u);

	scale_and_draw_f(p, u);

	%draw plotted points
	for x=-2 step 1 until 2:
		drawdot ((x,f(x)) scaled u) withpen pencircle scaled 4pt withcolor blue;
	endfor;

\stopMPpage

\stoptext


> %Ouput=pdf
>
> \setupcolors[state=start]
>
> \definecolor[gridlines][s=0.7]
>
> \startMPinclusions
>    color gridlines; gridlines=\MPcolor{gridlines}
> \stopMPinclusions
>
>
> \startMPgraphic{create function path f 5-by-5}
>
>    %define clipping path
>    path cpath;
>    cpath:=(-5,-5)--(5,-5)--(5,5)--(-5,5)--cycle;
>
>    %create function path
>    path p; p:=(-5,f(-5));
>    for x=-5 step .1 until 5:
>      p:=p--(x,f(x));
>    endfor;
>    p:=p--(5,f(5));
>
>    %clip function path to clipping path
>    p:=p cutbefore cpath;
>    p:=reverse p;
>    p:=p cutbefore cpath;
>
> \stopMPgraphic
>
> \startMPgraphic{scale and draw f 5-by-5}
>
>    %scale function path
>    p:=p scaled u;
>
>    %draw function path
>    drawdblarrow p withcolor blue;
>
> \stopMPgraphic
>
> \startMPgraphic{draw axis 5-by-5}
>
>    %draw the grid
>    for k=-5u step 1u until 5u:
>      draw (k,-5u)--(k,5u) withcolor gridlines;
>      draw (-5u,k)--(5u,k) withcolor gridlines;
>    endfor;
>
>    %draw axes
>    drawdblarrow (-5.2u,0)--(5.2u,0);
>    drawdblarrow (0,-5.2u)--(0,5.2u);
>
>    %label axes
>    label.rt(btex $x$ etex, (5.2u,0));
>    label.top(btex $y$ etex, (0,5.2u));
>    label.bot(btex $5$ etex, (5u,0));
>    label.lft(btex $5$ etex, (0,5u));
>
> \stopMPgraphic
>
> \starttext
>
> \startMPpage
>
>    %define function
>    vardef f(expr x)=
>      x*x
>    enddef;
>
>    \useMPgraphic{create function path f 5-by-5}
>
>    %initialize scale
>    numeric u; 10u=2in;
>
>    \useMPgraphic{draw axis 5-by-5}
>
>    \useMPgraphic{scale and draw f 5-by-5}
>
>    %draw plotted points
>    for x=-2 step 1 until 2:
>      drawdot ((x,f(x)) scaled u) withpen pencircle scaled 4pt withcolor
>      blue;
>    endfor;
>
> \stopMPpage
>
> \stoptext

  reply	other threads:[~2005-12-25 23:02 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-12-25 18:08 David Arnold
2005-12-25 23:02 ` Mojca Miklavec [this message]
2005-12-25 23:48   ` David Arnold

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=6faad9f00512251502s560cd871i541b9453ab906e79@mail.gmail.com \
    --to=mojca.miklavec.lists@gmail.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).