ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
From: Otared Kavian <otared@gmail.com>
To: mailing list for ConTeXt users <ntg-context@ntg.nl>
Subject: Using lmtx and new metafun
Date: Thu, 12 Dec 2019 15:18:21 +0100	[thread overview]
Message-ID: <D7F2FA1A-27DC-4A6B-AB6C-3B3CC9559E9A@gmail.com> (raw)

Hi Aditya,

Following your explanations in
	https://adityam.github.io/context-blog/post/new-metafun-interface/
I wrote an environment in order to display the graph of a function on a grid which can be adapted to each case.

Assuming that I have an MPinstance named GridAxis (for example) which can dispaly a grid and axes, how can I call it and write a line such as
	draw function (1,"x","x*x/4 - 2.5",xmin,xmax,.001) scaled sx  ;
without changing the part whichh is included in \startMPdefinitions, in oder to draw the above function on that grid ? 

Thanks for your help: Otared
PS: Here is my definition of GridAxis (I guess many parts of it could be better written…)

\defineMPinstance[GridAxis]  
	[
		format=metafun,
		extensions=yes,
		initializations=yes,
		method=double,
	]

\startMPdefinitions{GridAxis}

def GridAxisPlot = applyparameters "GridAxisPlot" "do_GridAxisPlot" enddef ;

presetparameters "GridAxisPlot" [
	xmin = -2.5,  xmax = 2.5,
	ymin = -2.5,  ymax = 2.5,
	
	dx   = 1,     dy   = 1,
	sx   = 5mm,   sy   = 5mm,
	scale = 0.5,

	grid = true,
	xaxisnumbers = true,
	yaxisnumbers = true,
	showmyorigin = true,

	style = "\switchtobodyfont[8pt]",
	];

vardef do_GridAxisPlot =
	image (
	pushparameters "GridAxisPlot";

	newnumeric xmin, xmax, ymin, ymax;
	xmin := getparameter "xmin";
	xmax := getparameter "xmax";
	ymin := getparameter "ymin";
	ymax := getparameter "ymax";

	newnumeric sx, sy;
	sx := getparameter "sx";
	sy := getparameter "sy";

	newnumeric dx, dy;
	dx := getparameter "dx";
	dy := getparameter "dy";

	newpath xaxis, yaxis;
	
	xaxis := (xmin*sx, 0) -- (xmax*sx, 0)  ;
	yaxis := (0, ymin*sy) --  (0, ymax*sy) ;

	newpath xtick, ytick;
	xtick := (-0.1sx, 0) -- (0.1sx, 0);
	ytick := (0, -0.1sy) -- (0, 0.1sy);

	newstring style;
	style := getparameter "style";

	pickup pencircle scaled .1mm ;
	newboolean grid;
	grid  := getparameter "grid";
	
	newboolean xaxisnumbers ;
	xaxisnumbers := getparameter "xaxisnumbers" ;
	
	newboolean yaxisnumbers ;
	yaxisnumbers := getparameter "yaxisnumbers" ;

	for x = dx step dx until xmax :
		if grid :
			draw yaxis shifted (x*sx, 0) withcolor 0.75white ;
		fi
		draw ytick shifted (x*sx, 0);
		if xaxisnumbers :
			label.bot(style & decimal x, (x*sx, 0));
		fi
	endfor

	newboolean showmyorigin ;
	showmyorigin := getparameter "showmyorigin" ;

	if showmyorigin :
		label.lrt(style & "0", origin) ;
	fi

	for x = -dx step -dx until xmin :
		if grid :
			draw yaxis shifted (x*sx, 0) withcolor 0.75white;
		fi
		draw ytick shifted (x*sx, 0);
		if xaxisnumbers :
			label.bot(style & decimal x, (x*sx, 0));
		fi
	endfor

	for y = dy step dy until ymax :
		if grid :
			draw xaxis shifted (0, y*sy) withcolor 0.75white;
		fi
		draw xtick shifted (0, y*sy);
		if yaxisnumbers :
			label.lft(style & decimal y, (0, y*sy));
		fi
	endfor

	for y = -dy step -dy until ymin :
		if grid :
			draw xaxis shifted (0, y*sy) withcolor 0.75white;
		fi
		draw xtick shifted (0, y*sy);
		if yaxisnumbers :
			label.lft(style & decimal y, (0, y*sy));
		fi
	endfor


	drawarrow xaxis;
	drawarrow yaxis;

	label.rt( style & "$x$",  (xmax*sx, 0));
	label.top(style & "$y$", (0, ymax*sy));

	newpair p ;

	newnumeric scale;
	scale := getparameter "scale" ;

	popparameters;

	)
enddef;
\stopMPdefinitions
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

                 reply	other threads:[~2019-12-12 14:18 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=D7F2FA1A-27DC-4A6B-AB6C-3B3CC9559E9A@gmail.com \
    --to=otared@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).