ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
From: David Arnold <darnold@northcoast.com>
Cc: ntg-context@ntg.nl
Subject: Re: scaling Metapost graphics
Date: Tue, 28 Nov 2000 13:18:53 -0800	[thread overview]
Message-ID: <3.0.3.32.20001128131853.00b9ff84@mail.northcoast.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 1536 bytes --]

Johannes,

Hans will help you with the Context way, if there is one. What I like to do
is get the graphics the right size before I use them. I always use a
scaling factor in my MP code. For example, let's say I want a figure that
is 3 inches wide.

%initialize scale
numeric u; 5u=3in;

%initialize points
pair A, B, C, D, E;
A:=origin;
B:=(3u,0);
C:=(5u,0);
D:=(5u,3u);
E:=(0,5u);

%draw frame
draw A--B--C--D--B--E--cycle;

Note that this is exactly 3inches wide. Now, suppose I change my mind and I
want it to be 4inches wide. This is all I have to do:

%initialize scale
numeric u; 5u=4in;

%initialize points
pair A, B, C, D, E;
A:=origin;
B:=(3u,0);
C:=(5u,0);
D:=(5u,3u);
E:=(0,5u);

%draw frame
draw A--B--C--D--B--E--cycle;

Notice that nothing changes save my scaling factor. So, good advance
planning is a savior. I've attached some more code where you can see this
in action.

At 10:06 PM 11/28/00 +0100, you wrote:
>Hi all,
>In the beginner's book it says that \useexternalfigure can go with the
>option height=, width=. Obviously, \useMPgraphics does not provide
>such an option (at least it would print out the option as plain
>text). So what is the ConTeXt way to include MP graphics and scale
>them?
>
>Groetjes
>
>
>Johannes
>-- 
>Johannes Hüsing <hannes@ruhrau.de>   /"\  ASCII-Ribbon Campaign
>                                     \ /  against HTML
>                                      X   in e-mail and news
>                                     / \
>
>

[-- Attachment #2: pretest.mp --]
[-- Type: text/plain, Size: 14085 bytes --]

input mp-tool;

%prologues:=0;

%pi
numeric pi;
pi:=3.14159;

%cos function for radian input
def cos(expr x)=
 cosd(180/pi*x)
enddef;

%sin function for radian input
def sin(expr x)=
 sind(180/pi*x)
enddef;

%xtick marks
def xtick(expr p)=
 draw ((0,-2pt)--(0,2pt)) shifted p;
enddef;

%ytick marks
def ytick(expr p)=
 draw ((-2pt,0)--(2pt,0)) shifted p;
enddef;

%opendot
vardef open(expr pos)=
 path p;
 p:=fullcircle scaled 3pt;
 p:=p shifted pos;
 fill p withcolor white;
 draw p withcolor red;
enddef;

beginfig(1);

%Define function to be drawn
def f(expr x)=
 x*(x+2)*(x-2)
enddef;

%Declare and initialize viewing window
numeric xmin, xmax, ymin, ymax;
xmin=-5;
xmax=5;
ymin=-5;
ymax=5;

%Declare and initialize scaling variables
numeric ux, uy;
(xmax-xmin)*ux=2in;
(ymax-ymin)*uy=2in;

%Declare and initialize clipping boundary
path q;
q:=(xmin,ymin)--(xmin,ymax)--(xmax,ymax)--(xmax,ymin)--cycle;
q:=q xscaled ux yscaled uy;

%Declare and initialize number of plotted points
numeric N;
N:=200;

%Declare and calculate plotting increment delta x
numeric dx;
dx:=(xmax-xmin)/N;

%Declare and create function path
path p;
p:=(xmin,f(xmin));
for x=xmin step dx until xmax:
 p:=p--(x,f(x));
endfor;
p:=p--(xmax,f(xmax));

%Scale function path
p:=p xscaled ux yscaled uy;

%Clip function path
draw p;
clip currentpicture to q;

%Save and clear the function path
picture pic;
pic:=currentpicture;
clearit;

%Draw and label coordinate axes
drawdblarrow (1.1xmin*ux,0)--(1.1xmax*ux,0);
label.rt(btex $x$ etex,(1.1xmax*ux,0));
drawdblarrow (0,1.1ymin*uy)--(0,1.1ymax*uy);
label.top(btex $y$ etex,(0,1.1ymax*uy));

%Superimpose function plot
draw pic withcolor blue;

%xtick marks
for k=xmin step 1 until xmax:
 xtick((k*ux,0))
endfor;

%label xticks
label.bot(btex $-5$ etex, (xmin*ux,0));
label.bot(btex $5$ etex, (xmax*ux,0));

endfig;

beginfig(2);

%Define function to be drawn
def f(expr x)=
 x*(x+2)*(x-2)
enddef;

%Declare and initialize viewing window
numeric xmin, xmax, ymin, ymax;
xmin=-5;
xmax=5;
ymin=-5;
ymax=5;

%Declare and initialize scaling variables
numeric ux, uy;
(xmax-xmin)*ux=2in;
(ymax-ymin)*uy=2in;

%Declare and initialize clipping boundary
path q;
q:=(xmin,ymin)--(xmin,ymax)--(xmax,ymax)--(xmax,ymin)--cycle;
q:=q xscaled ux yscaled uy;

%Declare and initialize number of plotted points
numeric N;
N:=200;

%Declare and calculate plotting increment delta x
numeric dx;
dx:=(xmax-xmin)/N;

%Declare and create function path
path p;
p:=(xmin,f(xmin));
for x=xmin step dx until xmax:
 p:=p--(x,f(x));
endfor;
p:=p--(xmax,f(xmax));

%Scale function path
p:=p xscaled ux yscaled uy;

%Clip function path
draw p withcolor blue;

%Highlight parts of the path that are above the axis
p:=(-2,f(-2));
for x = -2 step dx until 0:
 p:=p--(x,f(x));
endfor;
p:=p--(0,f(0));
p:=p xscaled ux yscaled uy;
draw p withcolor red withpen pencircle scaled 1pt;

p:=(2,f(2));
for x = 2 step dx until xmax:
 p:=p--(x,f(x));
endfor;
p:=p--(xmax,f(xmax));
p:=p xscaled ux yscaled uy;
draw p withcolor red withpen pencircle scaled 1pt;

%Draw open circles
open((-2*ux,0));
open((0,0));
open((2*ux,0));

clip currentpicture to q;

%Save and clear the function path
picture pic;
pic:=currentpicture;
clearit;

%Draw and label coordinate axes
drawdblarrow (1.1xmin*ux,0)--(1.1xmax*ux,0);
label.rt(btex $x$ etex,(1.1xmax*ux,0));
drawdblarrow (0,1.1ymin*uy)--(0,1.1ymax*uy);
label.top(btex $y$ etex,(0,1.1ymax*uy));

%Superimpose function plot
draw pic;

%xtick marks
for k=xmin step 1 until xmax:
 xtick((k*ux,0))
endfor;

%label xticks
label.bot(btex $-5$ etex, (xmin*ux,0));
label.bot(btex $5$ etex, (xmax*ux,0));

endfig;

beginfig(3);

%Define function to be drawn
def f(expr x)=
 exp(x)
enddef;

%Declare and initialize viewing window
numeric xmin, xmax, ymin, ymax;
xmin=-5;
xmax=5;
ymin=-5;
ymax=5;

%Declare and initialize scaling variables
numeric ux, uy;
(xmax-xmin)*ux=2in;
(ymax-ymin)*uy=2in;

%Declare and initialize clipping boundary
path q;
q:=(xmin,ymin)--(xmin,ymax)--(xmax,ymax)--(xmax,ymin)--cycle;
q:=q xscaled ux yscaled uy;

%Declare and initialize number of plotted points
numeric N;
N:=200;

%Declare and calculate plotting increment delta x
numeric dx;
dx:=(xmax-xmin)/N;

%Declare and create function path
path p;
p:=(xmin,f(xmin));
for x=xmin step dx until xmax:
 p:=p--(x,f(x));
endfor;
p:=p--(xmax,f(xmax));

%Scale function path
p:=p xscaled ux yscaled uy;

%Clip function path
draw p withcolor blue;

%reflect p about x-axis
draw (p reflectedabout ((0,0),(1,0))) withcolor red;

clip currentpicture to q;

%Save and clear the function path
picture pic;
pic:=currentpicture;
clearit;

%Draw and label coordinate axes
drawdblarrow (1.1xmin*ux,0)--(1.1xmax*ux,0);
label.rt(btex $x$ etex,(1.1xmax*ux,0));
drawdblarrow (0,1.1ymin*uy)--(0,1.1ymax*uy);
label.top(btex $y$ etex,(0,1.1ymax*uy));

%Superimpose function plot
draw pic;

%xtick marks
for k=xmin step 1 until xmax:
 xtick((k*ux,0))
endfor;

%label xticks
label.bot(btex $-5$ etex, (xmin*ux,0));
label.bot(btex $5$ etex, (xmax*ux,0));

%label first curve
label.rt(btex $y=e^x$ etex, (2ux,5uy));

%label second curve
label.rt(btex $y=e^{-x}$ etex, (2ux,-5uy));

endfig;

beginfig(4);

%Define function to be drawn
def f(expr x)=
 exp(x)
enddef;

%Declare and initialize viewing window
numeric xmin, xmax, ymin, ymax;
xmin=-5;
xmax=5;
ymin=-5;
ymax=5;

%Declare and initialize scaling variables
numeric ux, uy;
(xmax-xmin)*ux=2in;
(ymax-ymin)*uy=2in;

%Declare and initialize clipping boundary
path q;
q:=(xmin,ymin)--(xmin,ymax)--(xmax,ymax)--(xmax,ymin)--cycle;
q:=q xscaled ux yscaled uy;

%Declare and initialize number of plotted points
numeric N;
N:=200;

%Declare and calculate plotting increment delta x
numeric dx;
dx:=(xmax-xmin)/N;

%Declare and create function path
path p;
p:=(xmin,f(xmin));
for x=xmin step dx until xmax:
 p:=p--(x,f(x));
endfor;
p:=p--(xmax,f(xmax));

%Scale function path
p:=p xscaled ux yscaled uy;

%Clip function path
draw p reflectedabout ((0,0),(1,0)) shifted (0,1uy) withcolor blue;

clip currentpicture to q;

%Save and clear the function path
picture pic;
pic:=currentpicture;
clearit;

%Draw and label coordinate axes
drawdblarrow (1.1xmin*ux,0)--(1.1xmax*ux,0);
label.rt(btex $x$ etex,(1.1xmax*ux,0));
drawdblarrow (0,1.1ymin*uy)--(0,1.1ymax*uy);
label.top(btex $y$ etex,(0,1.1ymax*uy));

%Superimpose function plot
draw pic;

%xtick marks
for k=xmin step 1 until xmax:
 xtick((k*ux,0))
endfor;

%label xticks
label.bot(btex $-5$ etex, (xmin*ux,0));
label.bot(btex $5$ etex, (xmax*ux,0));

%ytick marks
for k=xmin step 1 until xmax:
 ytick((0,k*uy))
endfor;

%label xticks
label.lft(btex $-5$ etex, (0,ymin*uy));
label.lft(btex $5$ etex, (0,ymax*uy));

%draw in the asymptote
draw (xmin*ux,1uy)--(xmax*ux,1uy) withcolor red dashed evenly;

endfig;

beginfig(5);

%Define function to be drawn
def f(expr x)=
 ln(x-2)
enddef;

%Declare and initialize viewing window
numeric xmin, xmax, ymin, ymax;
xmin=-5;
xmax=5;
ymin=-5;
ymax=5;

%Declare and initialize scaling variables
numeric ux, uy;
(xmax-xmin)*ux=2in;
(ymax-ymin)*uy=2in;

%Declare and initialize clipping boundary
path q;
q:=(xmin,ymin)--(xmin,ymax)--(xmax,ymax)--(xmax,ymin)--cycle;
q:=q xscaled ux yscaled uy;

%Declare and initialize number of plotted points
numeric N;
N:=200;

%Declare and calculate plotting increment delta x
numeric dx;
dx:=(xmax-xmin)/N;

%Declare and create function path
path p;
p:=(2.01,f(2.01));
for x=2.01 step dx until xmax:
 p:=p--(x,f(x));
endfor;
p:=p--(xmax,f(xmax));

%Scale function path
p:=p xscaled ux yscaled uy;

%draw the function
draw p withcolor blue;

%clip
clip currentpicture to q;

%Save and clear the function path
picture pic;
pic:=currentpicture;
clearit;

%Draw and label coordinate axes
drawdblarrow (1.1xmin*ux,0)--(1.1xmax*ux,0);
label.rt(btex $x$ etex,(1.1xmax*ux,0));
drawdblarrow (0,1.1ymin*uy)--(0,1.1ymax*uy);
label.top(btex $y$ etex,(0,1.1ymax*uy));

%Superimpose function plot
draw pic;

%xtick marks
for k=xmin step 1 until xmax:
 xtick((k*ux,0))
endfor;

%label xticks
label.bot(btex $-5$ etex, (xmin*ux,0));
label.bot(btex $5$ etex, (xmax*ux,0));

%draw the asymptote
draw (2ux,ymin*uy)--(2ux,ymax*uy) dashed evenly withcolor red;

%plot key point
drawdot (3ux,0) withpen pencircle scaled 2pt;

%label key point
pic:=thelabel.ulft(btex $(3,0)$ etex, (3ux,0));
unfill bbox pic;
draw pic;

endfig;

beginfig(6);

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

%draw axes
path xaxis,yaxis;
xaxis:=(-5u,0)--(5u,0);
yaxis:=(0,-5u)--(0,5u);
drawdblarrow xaxis scaled 1.1;
drawdblarrow yaxis scaled 1.1;
label.rt(btex $x$ etex, (1.1*u*xmax,0));
label.top(btex $y$ etex, (0,1.1*u*ymax));

%tickmarks
for k=-5u step 1u until 5u:
 xtick((k,0));
 ytick((0,k));
endfor;

%define and plot ellipse
path p;
p:=fullcircle xscaled 2u yscaled 6u;
draw p shifted (1u,-2u) withcolor blue;

%Label the center
drawdot (1u,-2u) withpen pencircle scaled 2pt;

%direction of motion
drawarrow (2u,-2u)--((2u,-2u)+10*down) withpen pencircle scaled 1pt withcolor red;

endfig;

beginfig(7);

%Define function to be drawn
def f(expr x)=
 sqrt(x)
enddef;

%Declare and initialize viewing window
numeric xmin, xmax, ymin, ymax;
xmin=-5;
xmax=5;
ymin=-5;
ymax=5;

%Declare and initialize scaling variables
numeric ux, uy;
(xmax-xmin)*ux=2in;
(ymax-ymin)*uy=2in;

%Declare and initialize clipping boundary
path q;
q:=(xmin,ymin)--(xmin,ymax)--(xmax,ymax)--(xmax,ymin)--cycle;
q:=q xscaled ux yscaled uy;

%Declare and initialize number of plotted points
numeric N;
N:=200;

%Declare and calculate plotting increment delta x
numeric dx;
dx:=(xmax-xmin)/N;

%Declare and create function path
path p;
p:=(0,f(0));
for x=0 step dx until xmax:
 p:=p--(x,f(x));
endfor;
p:=p--(xmax,f(xmax));

%Scale function path
p:=p xscaled ux yscaled uy;

%draw the function
draw p withcolor blue;

%reflect the function across x-axis
draw p reflectedabout ((0,0),(1,0)) withcolor red;

%clip
clip currentpicture to q;

%Save and clear the function path
picture pic;
pic:=currentpicture;
clearit;

%Draw and label coordinate axes
drawdblarrow (1.1xmin*ux,0)--(1.1xmax*ux,0);
label.rt(btex $x$ etex,(1.1xmax*ux,0));
drawdblarrow (0,1.1ymin*uy)--(0,1.1ymax*uy);
label.top(btex $y$ etex,(0,1.1ymax*uy));

%Superimpose function plot
draw pic;

%label the graphs
label.top(btex $y=\sqrt{x}$ etex, (5ux,3uy));
label.bot(btex $y=-\sqrt{x}$ etex, (5ux,-3uy));

%xtick marks
for k=xmin step 1 until xmax:
 xtick((k*ux,0))
endfor;

%label xticks
label.bot(btex $-5$ etex, (xmin*ux,0));
label.bot(btex $5$ etex, (xmax*ux,0));

%ytick marks
for k=xmin step 1 until xmax:
 ytick((0,k*ux))
endfor;

%label xticks
label.lft(btex $-5$ etex, (0,ymin*ux));
label.lft(btex $5$ etex, (0,ymax*ux));

endfig;

beginfig(8);

%Define function to be drawn
def f(expr x)=
 -sqrt(x+2)
enddef;

%Declare and initialize viewing window
numeric xmin, xmax, ymin, ymax;
xmin=-5;
xmax=5;
ymin=-5;
ymax=5;

%Declare and initialize scaling variables
numeric ux, uy;
(xmax-xmin)*ux=2in;
(ymax-ymin)*uy=2in;

%Declare and initialize clipping boundary
path q;
q:=(xmin,ymin)--(xmin,ymax)--(xmax,ymax)--(xmax,ymin)--cycle;
q:=q xscaled ux yscaled uy;

%Declare and initialize number of plotted points
numeric N;
N:=200;

%Declare and calculate plotting increment delta x
numeric dx;
dx:=(xmax-xmin)/N;

%Declare and create function path
path p;
p:=(-2,f(-2));
for x=-2 step dx until xmax:
 p:=p--(x,f(x));
endfor;
p:=p--(xmax,f(xmax));

%Scale function path
p:=p xscaled ux yscaled uy;

%draw the function
draw p withcolor blue;

%clip
clip currentpicture to q;

%Save and clear the function path
picture pic;
pic:=currentpicture;
clearit;

%Draw and label coordinate axes
drawdblarrow (1.1xmin*ux,0)--(1.1xmax*ux,0);
label.rt(btex $x$ etex,(1.1xmax*ux,0));
drawdblarrow (0,1.1ymin*uy)--(0,1.1ymax*uy);
label.top(btex $y$ etex,(0,1.1ymax*uy));

%Superimpose function plot
draw pic;

%xtick marks
for k=xmin step 1 until xmax:
 xtick((k*ux,0))
endfor;

%label xticks
label.bot(btex $-5$ etex, (xmin*ux,0));
label.bot(btex $5$ etex, (xmax*ux,0));

%ytick marks
for k=xmin step 1 until xmax:
 ytick((0,k*ux))
endfor;

%label xticks
label.lft(btex $-5$ etex, (0,ymin*ux));
label.lft(btex $5$ etex, (0,ymax*ux));

endfig;

beginfig(9);

%Define function to be drawn
def f(expr x)=
 -sqrt(x+2)
enddef;

%Declare and initialize viewing window
numeric xmin, xmax, ymin, ymax;
xmin=-5;
xmax=5;
ymin=-5;
ymax=5;

%Declare and initialize scaling variables
numeric ux, uy;
(xmax-xmin)*ux=2in;
(ymax-ymin)*uy=2in;

%Declare and initialize clipping boundary
path q;
q:=(xmin,ymin)--(xmin,ymax)--(xmax,ymax)--(xmax,ymin)--cycle;
q:=q xscaled ux yscaled uy;

%Declare and initialize number of plotted points
numeric N;
N:=200;

%Declare and calculate plotting increment delta x
numeric dx;
dx:=(xmax-xmin)/N;

%Declare and create function path
path p;
p:=(-2,f(-2));
for x=-2 step dx until xmax:
 p:=p--(x,f(x));
endfor;
p:=p--(xmax,f(xmax));

%Scale function path
p:=p xscaled ux yscaled uy;

%draw the function
draw p withcolor blue;

%draw the line y=x
draw (-5u,-5u)--(5u,5u) withcolor black;

%draw the reflection
draw p reflectedabout ((0,0),(1,1)) withcolor red;

%clip
clip currentpicture to q;

%Save and clear the function path
picture pic;
pic:=currentpicture;
clearit;

%Draw and label coordinate axes
drawdblarrow (1.1xmin*ux,0)--(1.1xmax*ux,0);
label.rt(btex $x$ etex,(1.1xmax*ux,0));
drawdblarrow (0,1.1ymin*uy)--(0,1.1ymax*uy);
label.top(btex $y$ etex,(0,1.1ymax*uy));

%Superimpose function plot
draw pic;

%xtick marks
for k=xmin step 1 until xmax:
 xtick((k*ux,0))
endfor;

%label xticks
label.bot(btex $-5$ etex, (xmin*ux,0));
label.bot(btex $5$ etex, (xmax*ux,0));

%ytick marks
for k=xmin step 1 until xmax:
 ytick((0,k*ux))
endfor;

%label xticks
label.lft(btex $-5$ etex, (0,ymin*ux));
label.lft(btex $5$ etex, (0,ymax*ux));

%Label f
label.bot(btex $f$ etex, (5ux,-3uy));

%label f^-1
label.lft(btex $f^{-1}$ etex, (-3ux,5uy));

%label y=x
label.urt(btex $y=x$ etex, (5u,5u));

endfig;

beginfig(10);

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

%axes
drawdblarrow (-3.1u,0)--(7.1u,0);
drawdblarrow (0,-5.1u)--(0,5.1u);

endfig;

end

[-- Attachment #3: Type: text/plain, Size: 292 bytes --]

-
David Arnold
College of the Redwoods
Mathematics Department
7351 Tompkins Hill Rd
Eureka, CA 95501
(707) 476-4222 Office
(707) 476-4424 Fax
http://online.redwoods.cc.ca.us/instruct/darnold/

Ordinary Differential Equations Using MATLAB, 2/e
http://www.prenhall.com/books/esm_0130113816.html

             reply	other threads:[~2000-11-28 21:18 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-11-28 21:18 David Arnold [this message]
2000-11-28 22:01 ` Johannes H?sing
2000-11-29  8:59 ` Hans Hagen
  -- strict thread matches above, loose matches on Subject: below --
2003-03-20  6:26 reusing metapost graphics Matthew Huggett
2003-03-20  8:53 ` scaling " Patrick Gundlach
2000-11-28 21:06 scaling Metapost graphics Johannes H?sing

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.3.32.20001128131853.00b9ff84@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).