ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* Using lmtx and new metafun
@ 2019-12-12 14:18 Otared Kavian
  0 siblings, 0 replies; only message in thread
From: Otared Kavian @ 2019-12-12 14:18 UTC (permalink / raw)
  To: mailing list for ConTeXt users

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
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2019-12-12 14:18 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-12-12 14:18 Using lmtx and new metafun Otared Kavian

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).