* Code that run in Metapost but not in starMPcode enviroment
@ 2015-02-01 11:29 DesdeChaves
2015-02-01 13:35 ` Hans Hagen
0 siblings, 1 reply; 5+ messages in thread
From: DesdeChaves @ 2015-02-01 11:29 UTC (permalink / raw)
To: mailing list for ConTeXt users
[-- Attachment #1.1: Type: text/plain, Size: 1871 bytes --]
I'm trying to draw some random arrows using Metapost. For that I use a
piece of code from fpast that in found at stackexchange
<http://tex.stackexchange.com/questions/179745/generating-random-arrows-in-tikz/180111#180111>
Unfortunately this code does not work in context (request for a solution
<http://tex.stackexchange.com/questions/225864/metapost-error-related-with-context>)
because the freelabel() function. If I disable it the result is the
expected.
What is wrong with this code?
\starttext
\startMPcode
numeric u, nb_vectors, wd;
u := 1cm; % unit length
nb_vectors := 20; wd = .5;
boolean intersect; pair random_origin; color random_color; path candidate,
random_vector[];
%
vardef bb(expr p) =
save orig, extr; pair orig, extr; orig = point 0 of p; extr = point 1 of
p;
pair v,w; v = wd*unitvector(extr-orig)*u; w = v rotated 90;
(orig - v + w) -- (extr + v + w) -- (extr + v - w) -- (orig - v - w) --
cycle
enddef;
%
pickup pencircle scaled 2bp;
i = 1;
forever:
exitunless (i <= nb_vectors);
intersect := false;
random_length := u*(2+uniformdeviate(1));
random_angle := uniformdeviate(359);
random_origin := u*(uniformdeviate(wd*nb_vectors),
uniformdeviate(wd*nb_vectors));
random_color := (uniformdeviate(1), uniformdeviate(1),
uniformdeviate(1));
candidate := random_origin -- (random_origin +
random_length*dir(random_angle));
j := 1;
forever:
exitif (j = i) or (intersect = true);
if bb(random_vector[j]) intersectiontimes bb(candidate) <> (-1, -1):
intersect := true;
fi;
j := j+1;
endfor;
if intersect = false:
random_vector[i] = candidate;
drawarrow candidate withcolor random_color;
freelabel(btex $v$ etex, point .5 of candidate, point .5 of candidate
+ dir (random_angle + 90));
i := i+1;
fi;
endfor;
\stopMPcode
\stoptext
[-- Attachment #1.2: Type: text/html, Size: 2832 bytes --]
[-- Attachment #2: Type: text/plain, Size: 485 bytes --]
___________________________________________________________________________________
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://tex.aanhet.net
archive : http://foundry.supelec.fr/projects/contextrev/
wiki : http://contextgarden.net
___________________________________________________________________________________
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Code that run in Metapost but not in starMPcode enviroment
2015-02-01 11:29 Code that run in Metapost but not in starMPcode enviroment DesdeChaves
@ 2015-02-01 13:35 ` Hans Hagen
0 siblings, 0 replies; 5+ messages in thread
From: Hans Hagen @ 2015-02-01 13:35 UTC (permalink / raw)
To: ntg-context
On 2/1/2015 12:29 PM, DesdeChaves wrote:
>
> I'm trying to draw some random arrows using Metapost. For that I use a
> piece of code from fpast that in found at stackexchange
> <http://tex.stackexchange.com/questions/179745/generating-random-arrows-in-tikz/180111#180111>
>
> Unfortunately this code does not work in context (request for a solution
> <http://tex.stackexchange.com/questions/225864/metapost-error-related-with-context>)
> because the freelabel() function. If I disable it the result is the
> expected.
> What is wrong with this code?
>
>
> \starttext
>
> \startMPcode
> numeric u, nb_vectors, wd;
> u := 1cm; % unit length
> nb_vectors := 20; wd = .5;
> boolean intersect; pair random_origin; color random_color; path
> candidate, random_vector[];
> %
> vardef bb(expr p) =
> save orig, extr; pair orig, extr; orig = point 0 of p; extr = point 1
> of p;
> pair v,w; v = wd*unitvector(extr-orig)*u; w = v rotated 90;
> (orig - v + w) -- (extr + v + w) -- (extr + v - w) -- (orig - v - w)
> -- cycle
> enddef;
> %
>
> pickup pencircle scaled 2bp;
numeric i ;
i = 1 ;
> i = 1;
or
i := 1 ; % never assume variable is not set global (such is mp)
> forever:
> exitunless (i <= nb_vectors);
> intersect := false;
> random_length := u*(2+uniformdeviate(1));
> random_angle := uniformdeviate(359);
> random_origin := u*(uniformdeviate(wd*nb_vectors),
> uniformdeviate(wd*nb_vectors));
> random_color := (uniformdeviate(1), uniformdeviate(1),
> uniformdeviate(1));
> candidate := random_origin -- (random_origin +
> random_length*dir(random_angle));
> j := 1;
> forever:
> exitif (j = i) or (intersect = true);
> if bb(random_vector[j]) intersectiontimes bb(candidate) <> (-1, -1):
> intersect := true;
> fi;
> j := j+1;
> endfor;
> if intersect = false:
> random_vector[i] = candidate;
> drawarrow candidate withcolor random_color;
> freelabel(btex $v$ etex, point .5 of candidate, point .5 of
> candidate + dir (random_angle + 90));
> i := i+1;
> fi;
> endfor;
> \stopMPcode
>
>
> \stoptext
>
>
> ___________________________________________________________________________________
> 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://tex.aanhet.net
> archive : http://foundry.supelec.fr/projects/contextrev/
> wiki : http://contextgarden.net
> ___________________________________________________________________________________
>
--
-----------------------------------------------------------------
Hans Hagen | PRAGMA ADE
Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
tel: 038 477 53 69 | voip: 087 875 68 74 | www.pragma-ade.com
| www.pragma-pod.nl
-----------------------------------------------------------------
___________________________________________________________________________________
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://tex.aanhet.net
archive : http://foundry.supelec.fr/projects/contextrev/
wiki : http://contextgarden.net
___________________________________________________________________________________
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Code that run in Metapost but not in starMPcode enviroment
2015-02-01 14:31 Akira Kakuto
@ 2015-02-01 23:54 ` Hans Hagen
0 siblings, 0 replies; 5+ messages in thread
From: Hans Hagen @ 2015-02-01 23:54 UTC (permalink / raw)
To: ntg-context
On 2/1/2015 3:31 PM, Akira Kakuto wrote:
>> Interestingly it seems to work in mkii, probably because
>> mkii calls external metafun:
>
> Sorry, I didn't notice explanation by Hans.
The problem with variables in metapost is that they are global by
default (unless 'save'd in a group or vardef). This means that one can
best explicitly declare variables that are used. Of course one can also
use a dedicated instance.
Hans
-----------------------------------------------------------------
Hans Hagen | PRAGMA ADE
Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
tel: 038 477 53 69 | voip: 087 875 68 74 | www.pragma-ade.com
| www.pragma-pod.nl
-----------------------------------------------------------------
___________________________________________________________________________________
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://tex.aanhet.net
archive : http://foundry.supelec.fr/projects/contextrev/
wiki : http://contextgarden.net
___________________________________________________________________________________
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Code that run in Metapost but not in starMPcode enviroment
@ 2015-02-01 14:31 Akira Kakuto
2015-02-01 23:54 ` Hans Hagen
0 siblings, 1 reply; 5+ messages in thread
From: Akira Kakuto @ 2015-02-01 14:31 UTC (permalink / raw)
To: ntg-context
> Interestingly it seems to work in mkii, probably because
> mkii calls external metafun:
Sorry, I didn't notice explanation by Hans.
Best,
Akira
___________________________________________________________________________________
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://tex.aanhet.net
archive : http://foundry.supelec.fr/projects/contextrev/
wiki : http://contextgarden.net
___________________________________________________________________________________
^ permalink raw reply [flat|nested] 5+ messages in thread
* Code that run in Metapost but not in starMPcode enviroment
@ 2015-02-01 14:26 Akira Kakuto
0 siblings, 0 replies; 5+ messages in thread
From: Akira Kakuto @ 2015-02-01 14:26 UTC (permalink / raw)
To: ntg-context
[-- Attachment #1: Type: text/plain, Size: 206 bytes --]
> What is wrong with this code?
Interestingly it seems to work in mkii, probably because
mkii calls external metafun:
context --pdftex --passon=--shell-escape test.tex
test.pdf is attached.
Best,
Akira
[-- Attachment #2: test.pdf --]
[-- Type: application/pdf, Size: 19708 bytes --]
[-- Attachment #3: Type: text/plain, Size: 485 bytes --]
___________________________________________________________________________________
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://tex.aanhet.net
archive : http://foundry.supelec.fr/projects/contextrev/
wiki : http://contextgarden.net
___________________________________________________________________________________
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2015-02-01 23:54 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-02-01 11:29 Code that run in Metapost but not in starMPcode enviroment DesdeChaves
2015-02-01 13:35 ` Hans Hagen
2015-02-01 14:26 Akira Kakuto
2015-02-01 14:31 Akira Kakuto
2015-02-01 23:54 ` Hans Hagen
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).