ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* Fuzzy lines
@ 2021-05-11 10:58 Henning Hraban Ramm
  2021-05-11 11:30 ` Mikael Sundqvist
  0 siblings, 1 reply; 23+ messages in thread
From: Henning Hraban Ramm @ 2021-05-11 10:58 UTC (permalink / raw)
  To: mailing list for ConTeXt users

Hi MetaFans,

is it possible to get lines with Metapost that look like they were drawn with a pencil or brush?

Some vector graphics apps (in my case: Affinity Designer) can use such naturally looking brushes for vector graphics (and I don’t know how they do it), but I’d like to automate/parametrize some designs, and that would finally be a reason to learn Metapost...

Hraban
___________________________________________________________________________________
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] 23+ messages in thread

* Re: Fuzzy lines
  2021-05-11 10:58 Fuzzy lines Henning Hraban Ramm
@ 2021-05-11 11:30 ` Mikael Sundqvist
  2021-05-11 12:33   ` Henning Hraban Ramm
  2021-05-11 12:56   ` Jairo A. del Rio
  0 siblings, 2 replies; 23+ messages in thread
From: Mikael Sundqvist @ 2021-05-11 11:30 UTC (permalink / raw)
  To: mailing list for ConTeXt users

Hi!

Not really what you ask for, perhaps, but this could be interesting
for you: https://tex.stackexchange.com/q/39296/52406

/Mikael

On Tue, May 11, 2021 at 12:58 PM Henning Hraban Ramm <texml@fiee.net> wrote:
>
> Hi MetaFans,
>
> is it possible to get lines with Metapost that look like they were drawn with a pencil or brush?
>
> Some vector graphics apps (in my case: Affinity Designer) can use such naturally looking brushes for vector graphics (and I don’t know how they do it), but I’d like to automate/parametrize some designs, and that would finally be a reason to learn Metapost...
>
> Hraban
> ___________________________________________________________________________________
> 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
> ___________________________________________________________________________________
___________________________________________________________________________________
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] 23+ messages in thread

* Re: Fuzzy lines
  2021-05-11 11:30 ` Mikael Sundqvist
@ 2021-05-11 12:33   ` Henning Hraban Ramm
  2021-05-11 12:56   ` Jairo A. del Rio
  1 sibling, 0 replies; 23+ messages in thread
From: Henning Hraban Ramm @ 2021-05-11 12:33 UTC (permalink / raw)
  To: mailing list for ConTeXt users

Hi Mikael,

thank you, there are some interesting ideas.

We have already seen a lot of randomization in paths; duplicating a randomized path with varying saturation enhances the effect.

With PGF/TikZ I could maybe use a brush decoration on a path.

Is it possible to use a irregular "pen" in MetaPost? With transparency?
The MF manual says in 1.11, pens are limited, but not how.

Or are properties (MF manual 8.10) the way to go?

Hraban


> Am 11.05.2021 um 13:30 schrieb Mikael Sundqvist <mickep@gmail.com>:
> 
> Not really what you ask for, perhaps, but this could be interesting
> for you: https://tex.stackexchange.com/q/39296/52406
> 
> /Mikael
> 
> On Tue, May 11, 2021 at 12:58 PM Henning Hraban Ramm <texml@fiee.net> wrote:
>> 
>> is it possible to get lines with Metapost that look like they were drawn with a pencil or brush?
>> 
>> Some vector graphics apps (in my case: Affinity Designer) can use such naturally looking brushes for vector graphics (and I don’t know how they do it), but I’d like to automate/parametrize some designs, and that would finally be a reason to learn Metapost...
>> 
>> Hraban

___________________________________________________________________________________
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] 23+ messages in thread

* Re: Fuzzy lines
  2021-05-11 11:30 ` Mikael Sundqvist
  2021-05-11 12:33   ` Henning Hraban Ramm
@ 2021-05-11 12:56   ` Jairo A. del Rio
  2021-05-11 16:02     ` Aditya Mahajan
  2021-05-12 17:55     ` Henning Hraban Ramm
  1 sibling, 2 replies; 23+ messages in thread
From: Jairo A. del Rio @ 2021-05-11 12:56 UTC (permalink / raw)
  To: mailing list for ConTeXt users


[-- Attachment #1.1: Type: text/plain, Size: 2769 bytes --]

For simple pens, you may define your own (section 9.7 of MetaPost manual).
For complicated strokes, something along these lines could be useful as a
starting point (I'm not taking cyclic paths in consideration, btw). I did
something similar with duck footprints some weeks ago:

%Sorry for the ugly code...

\starttext

\startMPpage
%Unit size

numeric u; u := 1mm;

%In a picture you are able to include everything, kinda

picture Pluma;

Pluma := image(

for i = 1 upto 5:

for j = 1 upto 5:

draw (i,j) randomized 1;

endfor

endfor

);


%Simple curve

path Camino;

Camino := origin for i = 1 upto 30: .. (u*i, u*sind (12i mod 360)) endfor;


%Strokes

for i = 0 step 1/2 until length Camino:

draw Pluma rotated (90 + angle direction i of Camino)

shifted point i of Camino;

endfor

\stopMPpage

\stoptext


Not sure if that's what you need...

Regards,


Jairo

El mar, 11 de may. de 2021 a la(s) 06:31, Mikael Sundqvist (mickep@gmail.com)
escribió:

> Hi!
>
> Not really what you ask for, perhaps, but this could be interesting
> for you: https://tex.stackexchange.com/q/39296/52406
>
> /Mikael
>
> On Tue, May 11, 2021 at 12:58 PM Henning Hraban Ramm <texml@fiee.net>
> wrote:
> >
> > Hi MetaFans,
> >
> > is it possible to get lines with Metapost that look like they were drawn
> with a pencil or brush?
> >
> > Some vector graphics apps (in my case: Affinity Designer) can use such
> naturally looking brushes for vector graphics (and I don’t know how they do
> it), but I’d like to automate/parametrize some designs, and that would
> finally be a reason to learn Metapost...
> >
> > Hraban
> >
> ___________________________________________________________________________________
> > 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
> >
> ___________________________________________________________________________________
>
> ___________________________________________________________________________________
> 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
>
> ___________________________________________________________________________________
>

[-- Attachment #1.2: Type: text/html, Size: 5858 bytes --]

[-- Attachment #2: 163.pdf --]
[-- Type: application/pdf, Size: 25029 bytes --]

[-- Attachment #3: Type: text/plain, Size: 493 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://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: Fuzzy lines
  2021-05-11 12:56   ` Jairo A. del Rio
@ 2021-05-11 16:02     ` Aditya Mahajan
  2021-05-12 18:07       ` Henning Hraban Ramm
  2021-05-13 13:08       ` Henning Hraban Ramm
  2021-05-12 17:55     ` Henning Hraban Ramm
  1 sibling, 2 replies; 23+ messages in thread
From: Aditya Mahajan @ 2021-05-11 16:02 UTC (permalink / raw)
  To: mailing list for ConTeXt users



On Tue, 11 May 2021, Jairo A. del Rio wrote:

> For simple pens, you may define your own (section 9.7 of MetaPost manual).
> For complicated strokes, something along these lines could be useful as a
> starting point (I'm not taking cyclic paths in consideration, btw). I did
> something similar with duck footprints some weeks ago:
> 
> %Sorry for the ugly code...

Look at this old code on how hide such code behind a macro to have a clean interface:

https://github.com/adityam/mp-sketch/blob/master/mp-sketch.mp

Sometimes I use such effects in presentations but you have to resist the temptation to overdo it:

http://www.cim.mcgill.ca/~adityam/talks/2016-it-forum.pdf

Aditya

___________________________________________________________________________________
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] 23+ messages in thread

* Re: Fuzzy lines
  2021-05-11 12:56   ` Jairo A. del Rio
  2021-05-11 16:02     ` Aditya Mahajan
@ 2021-05-12 17:55     ` Henning Hraban Ramm
       [not found]       ` <CAKyqqaaosis=BjOD_mArY5VM+bzcj-ar4uMtnt8SRJ2WLL9ykw@mail.gmail.com>
  1 sibling, 1 reply; 23+ messages in thread
From: Henning Hraban Ramm @ 2021-05-12 17:55 UTC (permalink / raw)
  To: mailing list for ConTeXt users

Hi Jairo,

thank you for the suggestion.

> Am 11.05.2021 um 14:56 schrieb Jairo A. del Rio <jairoadelrio6@gmail.com>:
> 
> For simple pens, you may define your own (section 9.7 of MetaPost manual).

Unfortunately, pens don’t work as patterns but only as outlines.

> For complicated strokes, something along these lines could be useful as a starting point (I'm not taking cyclic paths in consideration, btw). I did something similar with duck footprints some weeks ago:
> 
> %Sorry for the ugly code...
> \starttext
> \startMPpage
> %Unit size
> numeric u; u := 1mm;
> 
> %In a picture you are able to include everything, kinda
> picture Pluma;
> Pluma := image(
> 	for i = 1 upto 5:
> 		for j = 1 upto 5:
> 			draw (i,j) randomized 1;
> 		endfor
> 	endfor
> );

Unfortunately, that’s the same pattern over and over.
The randomization is only applied once.
Good for footprints, bad for fuzzy patterns.

> %Simple curve
> path Camino;
> Camino := origin for i = 1 upto 30: .. (u*i, u*sind (12i mod 360)) endfor;
> 
> %Strokes
> for i = 0 step 1/2 until length Camino:
> 	draw Pluma rotated (90 + angle direction i of Camino)
> 		shifted point i of Camino;
> endfor

Since I have more or less straight lines but of different length, the "one pattern per 1/n path segment" doesn’t work for me. I could try to construct my paths differently.


Hraban

___________________________________________________________________________________
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] 23+ messages in thread

* Re: Fuzzy lines
  2021-05-11 16:02     ` Aditya Mahajan
@ 2021-05-12 18:07       ` Henning Hraban Ramm
  2021-05-13 13:08       ` Henning Hraban Ramm
  1 sibling, 0 replies; 23+ messages in thread
From: Henning Hraban Ramm @ 2021-05-12 18:07 UTC (permalink / raw)
  To: mailing list for ConTeXt users

Hi Aditya,
thanks for the code (that also Mikael pointed to)!

> Am 11.05.2021 um 18:02 schrieb Aditya Mahajan <adityam@umich.edu>:
> 
> Look at this old code on how hide such code behind a macro to have a clean interface:
> 
> https://github.com/adityam/mp-sketch/blob/master/mp-sketch.mp

I came up with this simple cover layout:


input mp-sketch;

beginfig(1)
  color darkblue ; darkblue := (40/255,67/255,117/255) ;

  pen pillpen ; pillpen := makepen ((-1bp,-0.5bp)--(1bp,-0.5bp)..(1bp,0.5bp)--(-1bp,0.5bp)..cycle) ;
  pickup pillpen xyscaled 3bp ;

  sketchypaths;
  sketch_amount := 5bp;

  % insert some extra points to add fuzzyness - automate?
  % path one ; one := (-3mm,249mm) -- (185mm,249mm) -- (185mm,300mm) ; % links-oben
  path onep ; onep := (-3mm,249mm) .. (25.5mm,249mm) .. (150mm,249mm) -- (185mm,249mm) -- (185mm,300mm) ; % links-oben
  % path two ; two := (25.5mm,-3mm) -- (25.5mm,167mm) -- (213mm,167mm) ; % unten-rechts
  path twop ; twop := (25.5mm,-3mm) .. (25.5mm,25.5mm) .. (25.5mm,120mm) -- (25.5mm,167mm) .. (120mm,167mm) -- (213mm,167mm) ; % unten-rechts

  % draw paths several times - automate?
  numeric max ; max := 5;
  for i=0 upto max :
    draw onep withcolor darkblue withtransparency ("multiply", 1/max) ;
    draw twop withcolor darkblue withtransparency ("multiply", 1/max) ;
  endfor

  naturalizepaths;

  % draw crop box (paper size)
  pickup pencircle xyscaled 0.5bp;
  draw (0,0)--(0,297mm)--(210mm,297mm)--(210mm,0)--cycle withcolor magenta;

endfig


This begins to look like I imagined, even if I’d like to add some noise (pattern, shading).
Jairo’s suggestion looks too regular.
(Finally, I’ll overlay the whole page with a transparent image for structure.)

As you see, both paths use only three points, but I added some more to add fuzzyness.
Also they get drawn several times with transparency to get a irregular width and some blur.

I’m quite sure this could be added to your macro, but I’m still too inexperienced with Metapost.

Hraban
___________________________________________________________________________________
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] 23+ messages in thread

* Fwd:  Fuzzy lines
       [not found]       ` <CAKyqqaaosis=BjOD_mArY5VM+bzcj-ar4uMtnt8SRJ2WLL9ykw@mail.gmail.com>
@ 2021-05-12 21:21         ` Jairo A. del Rio
  2021-05-13 13:23           ` Henning Hraban Ramm
  0 siblings, 1 reply; 23+ messages in thread
From: Jairo A. del Rio @ 2021-05-12 21:21 UTC (permalink / raw)
  To: mailing list for ConTeXt users


[-- Attachment #1.1: Type: text/plain, Size: 4182 bytes --]

---------- Forwarded message ---------
De: Jairo A. del Rio <jairoadelrio6@gmail.com>
Date: mié, 12 de may. de 2021 a la(s) 14:08
Subject: Re: [NTG-context] Fuzzy lines
To: mailing list for ConTeXt users <ntg-context@ntg.nl>




El mié, 12 de may. de 2021 a la(s) 13:03, Henning Hraban Ramm (
texml@fiee.net) escribió:

> Hi Jairo,
>
> thank you for the suggestion.
>
> > Am 11.05.2021 um 14:56 schrieb Jairo A. del Rio <jairoadelrio6@gmail.com
> >:
> >
> > For simple pens, you may define your own (section 9.7 of MetaPost
> manual).
>
> Unfortunately, pens don’t work as patterns but only as outlines.
>
> > For complicated strokes, something along these lines could be useful as
> a starting point (I'm not taking cyclic paths in consideration, btw). I did
> something similar with duck footprints some weeks ago:
> >
> > %Sorry for the ugly code...
> > \starttext
> > \startMPpage
> > %Unit size
> > numeric u; u := 1mm;
> >
> > %In a picture you are able to include everything, kinda
> > picture Pluma;
> > Pluma := image(
> >       for i = 1 upto 5:
> >               for j = 1 upto 5:
> >                       draw (i,j) randomized 1;
> >               endfor
> >       endfor
> > );
>
> Unfortunately, that’s the same pattern over and over.
> The randomization is only applied once.
> Good for footprints, bad for fuzzy patterns.
>

You only need to move "randomized" at the drawing stage:

\starttext

\startMPpage

numeric u; u := 1mm;

picture Pluma;

Pluma := image(

for i = 1 upto 5:

for j = 1 upto 5:

draw (i,j);

endfor

endfor

);

path Camino;

Camino := origin for i = 1 upto 30: .. (u*i, u*sind (12i mod 360)) endfor;

for i = 0 step 1/2 until length Camino:

draw Pluma randomized 1 rotated (90 + angle direction i of Camino)

shifted point i of Camino;

endfor

\stopMPpage

\stoptext

But "randomized" will deform paths. Maybe you want something like this?

\starttext

\startMPpage[align=]

begingroup;

save Rnd; let Rnd = uniformdeviate;


numeric u; u := 3mm;

picture Pluma;

Pluma := image(

for i = 1 upto 5:

for j = 1 upto 5:

fill fullcircle shifted (i,j); %try fulltriangle or another shape

endfor

endfor

);

path Camino;

Camino := origin for i = 1 upto 30: .. u*i*left endfor;

for i = 0 step 1/2 until length Camino:

draw image(

for x within Pluma:

fill pathpart x %Assuming our image only contains cyclic paths

scaled (1+Rnd 1)

shifted (1/2dir Rnd 360)

withcolor hsvtorgb(6round(Rnd 60),9/10,1); %Colors just because they look
cute

endfor

) rotated (90 + angle direction i of Camino)

shifted point i of Camino;

endfor

endgroup;

\stopMPpage

\stoptext

> %Simple curve
> > path Camino;
> > Camino := origin for i = 1 upto 30: .. (u*i, u*sind (12i mod 360))
> endfor;
> >
> > %Strokes
> > for i = 0 step 1/2 until length Camino:
> >       draw Pluma rotated (90 + angle direction i of Camino)
> >               shifted point i of Camino;
> > endfor
>
> Since I have more or less straight lines but of different length, the "one
> pattern per 1/n path segment" doesn’t work for me. I could try to construct
> my paths differently.
>

You only need to use fractions, i.e., "for 0 step 1/n until 1: ... endfor"
if you work with straight lines defined using only two points. In the
example above I made one from more than two points just for ease of
manipulation. With examples and feedback from the mailing list, you might
define your own function using the new Metafun interface for LMTX.


>
> Hraban
>
>
> ___________________________________________________________________________________
> 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
> ___________________________________________________________________________________
>
>

Jairo

[-- Attachment #1.2: Type: text/html, Size: 8977 bytes --]

[-- Attachment #2: Type: text/plain, Size: 493 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://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: Fuzzy lines
  2021-05-11 16:02     ` Aditya Mahajan
  2021-05-12 18:07       ` Henning Hraban Ramm
@ 2021-05-13 13:08       ` Henning Hraban Ramm
  2021-05-13 16:36         ` Aditya Mahajan
  1 sibling, 1 reply; 23+ messages in thread
From: Henning Hraban Ramm @ 2021-05-13 13:08 UTC (permalink / raw)
  To: mailing list for ConTeXt users


> Am 11.05.2021 um 18:02 schrieb Aditya Mahajan <adityam@umich.edu>:
> 
> Look at this old code on how hide such code behind a macro to have a clean interface:
> 
> https://github.com/adityam/mp-sketch/blob/master/mp-sketch.mp

Hi Aditya,
I’m trying to include my additions into your code, but it doesn’t work.


numeric sketch_amount; sketch_amount := 3bp;
numeric sketch_passes; sketch_passes := 5;

def sketchdraw expr p =
  do_sketchdraw(p if (path p): randomized sketch_amount fi)
enddef;

def do_sketchdraw(expr p) text t =
    normaldraw p t ;
enddef;


Now I want to draw that path several times.

If I understand it right, I can’t put that into "sketchdraw" because that leaves the end open for path additions like "withcolor".

But it also doesn’t work in "do_sketchdraw"; I thought it might like this:

def do_sketchdraw(expr p) text t =
    for i=0 upto sketch_passes :
      normaldraw p t ; 
	% withtransparency ("multiply", 1/sketch_passes) ; % and how can I add that without breaking other additions?
    endfor
enddef;

But that just blocks, I guess there’s an infinite loop.

I’d be happy about some advise.

Hraban
___________________________________________________________________________________
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] 23+ messages in thread

* Re: Fuzzy lines
  2021-05-12 21:21         ` Fwd: " Jairo A. del Rio
@ 2021-05-13 13:23           ` Henning Hraban Ramm
  0 siblings, 0 replies; 23+ messages in thread
From: Henning Hraban Ramm @ 2021-05-13 13:23 UTC (permalink / raw)
  To: mailing list for ConTeXt users


> Am 12.05.2021 um 23:21 schrieb Jairo A. del Rio <jairoadelrio6@gmail.com>:
>> Unfortunately, that’s the same pattern over and over.
>> The randomization is only applied once.
>> Good for footprints, bad for fuzzy patterns.
>> 
> You only need to move "randomized" at the drawing stage:
> 
> \starttext
> \startMPpage
> numeric u; u := 1mm;
> picture Pluma;
> Pluma := image(
> 	for i = 1 upto 5:
> 		for j = 1 upto 5:
> 			draw (i,j);
> 		endfor
> 	endfor
> );

I made it into:

  picture NoisePattern;
  NoisePattern := image(
    pickup pencircle xyscaled 0.5bp;
    numeric pmax ; pmax := 7 ;
  	for i = 1 upto pmax:
  		for j = 1 upto pmax:
        		draw (i - (pmax/2), j - (pmax/2)) 
			withcolor LineColor 
			withtransparency ("normal", (uniformdeviate 0.33));
  		endfor
  	endfor
  );

Now the noise is randomly transparent and also on (and not over) the path.


> path Camino;
> Camino := origin for i = 1 upto 30: .. (u*i, u*sind (12i mod 360)) endfor;
> for i = 0 step 1/2 until length Camino:
> 	draw Pluma randomized 1 rotated (90 + angle direction i of Camino)
> 		shifted point i of Camino;
> endfor

And that became:

  def noisify(expr p) =
    for i = 0 step 0.01 until length p:
    	draw NoisePattern randomized 1 shifted point i of p ;
    endfor
  enddef;

I still need to account for the length of the path in the loop step.


> But "randomized" will deform paths. Maybe you want something like this?
> 
> \starttext
> \startMPpage[align=]
> begingroup;
> save Rnd; let Rnd = uniformdeviate;
> 
> numeric u; u := 3mm;
> picture Pluma;
> Pluma := image(
> 	for i = 1 upto 5:
> 		for j = 1 upto 5:
> 			fill fullcircle shifted (i,j); %try fulltriangle or another shape
> 		endfor
> 	endfor
> );
> path Camino;
> Camino := origin for i = 1 upto 30: .. u*i*left endfor;
> for i = 0 step 1/2 until length Camino:
> 	draw image(
> 	for x within Pluma:
> 		fill pathpart x %Assuming our image only contains cyclic paths
> 		scaled (1+Rnd 1)
> 		shifted (1/2dir Rnd 360)
> 		withcolor hsvtorgb(6round(Rnd 60),9/10,1); %Colors just because they look cute
> 	endfor
> 	) rotated (90 + angle direction i of Camino)
> 	shifted point i of Camino;
> endfor
> endgroup;
> \stopMPpage
> \stoptext

>> Since I have more or less straight lines but of different length, the "one pattern per 1/n path segment" doesn’t work for me. I could try to construct my paths differently.
> You only need to use fractions, i.e., "for 0 step 1/n until 1: ... endfor" if you work with straight lines defined using only two points.

Yes, I got that. But a fixed number (e.g. step 0.01 like above) doesn’t account for paths of different length.
I guess I’d need an algorithm like for dashed lines to get uniform noise on paths of different length.
(I also looked at dashpattern in the MP manual, but they’re not usable for patterns like this.)

Hraban

___________________________________________________________________________________
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] 23+ messages in thread

* Re: Fuzzy lines
  2021-05-13 13:08       ` Henning Hraban Ramm
@ 2021-05-13 16:36         ` Aditya Mahajan
  2021-05-13 16:55           ` Henning Hraban Ramm
  2021-05-14 10:05           ` Fuzzy lines Hans Hagen
  0 siblings, 2 replies; 23+ messages in thread
From: Aditya Mahajan @ 2021-05-13 16:36 UTC (permalink / raw)
  To: mailing list for ConTeXt users

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

On Thu, 13 May 2021, Henning Hraban Ramm wrote:

> 
> > Am 11.05.2021 um 18:02 schrieb Aditya Mahajan <adityam@umich.edu>:
> > 
> > Look at this old code on how hide such code behind a macro to have a clean interface:
> > 
> > https://github.com/adityam/mp-sketch/blob/master/mp-sketch.mp
> 
> Hi Aditya,
> I’m trying to include my additions into your code, but it doesn’t work.
> 
> 
> numeric sketch_amount; sketch_amount := 3bp;
> numeric sketch_passes; sketch_passes := 5;
> 
> def sketchdraw expr p =
>   do_sketchdraw(p if (path p): randomized sketch_amount fi)
> enddef;
> 
> def do_sketchdraw(expr p) text t =
>     normaldraw p t ;
> enddef;
> 
> 
> Now I want to draw that path several times.
> 
> If I understand it right, I can’t put that into "sketchdraw" because that leaves the end open for path additions like "withcolor".
> 
> But it also doesn’t work in "do_sketchdraw"; I thought it might like this:

A slightly different approach is needed. See attached. I also added a bit a error protection in case someone sets sketch_passes to be negative by mistake. 

It may be interesting to rewrite these macros using the new key-value interface for metapost, so that something like this works:

\sketchdraw[amount=3bp, passes=5, transparency=yes] fullsquare scaled 1cm withcolor red;

[-- Attachment #2: Type: application/pdf, Size: 7309 bytes --]

[-- Attachment #3: Type: application/x-tex, Size: 1133 bytes --]

[-- Attachment #4: Type: text/plain, Size: 2975 bytes --]

%D \module
%D   [       file=mp-sketch.mp
%D        version=2021.05.13
%D          title=\CONTEXT\ \METAPOST\ graphics,
%D       subtitle=Sketch drawing,
%D         author=Aditya Mahajan,
%D           date=\currentdate,
%D      copyright={Aditya Mahajan}]

%D This metapost module is inspired by a TeX.SE question:
%D http://tex.stackexchange.com/q/39296/323
%D
%D I thought that it would be fun to implement a similar feature in MetaPost.
%D
%D To use this package in MetaPost:
%D
%D \starttyping
%D    input mp-sketch;
%D    
%D    beginfig(1)
%D      sketchypaths; % Make draw and fill sketchy
%D      ...
%D      naturalizepaths; % Restore the value of draw and fill
%D      ...
%D    endfig
%D \stoptyping
%D
%D The code is heavily inspired by Hans Hagen's Metafun macros.
%D
%D The macro \type{sketchypaths} is modeled after \type{visualizepaths} from
%D \filename{mp-tool}.

def sketchypaths =
    let draw = sketchdraw ;
    let fill = sketchfill ;
enddef ;

%D Check if \filename{mp-tool} is loaded
if not known context_tool :
  let normaldraw = draw;
  let normalfill = fill;

  def naturalizepaths =
      let fill = normalfill ;
      let draw = normaldraw ;
  enddef ;
fi

%D The variable \type{sketch_amount} determines the amount of randomness in the
%D drawing
numeric sketch_amount; sketch_amount := 3bp;

%D The variable \type{sketch_passes} determines the number of times the path
%D is drawn
numeric sketch_passes; sketch_passes := 1;

%D The macro \type{sketchdraw} draws the randomized path. The 
%D \type{expr} ... \type{text} trick is copied from the definition of 
%D \type{drawarrow}
def sketchdraw expr p =
   do_sketchdraw(p)
enddef;

def do_sketchdraw(expr p) text t =
  if (path p) :
      for i = 1 upto max(1,sketch_passes) :
        normaldraw p 
                   randomized sketch_amount 
                   withtransparency ("multiply", 1/max(1,sketch_passes))
                   t ;
      endfor;
  else :
      normaldraw p t;
  fi
enddef;

%D The macro \type{sketchfill} randomizes the path before filling it.
def sketchfill expr p =
  do_sketchfill(p)
enddef ;

def do_sketchfill(expr p) text t =
  if (path p) :
      for i = 1 upto max(1,sketch_passes) :
        normalfill p 
                   randomized sketch_amount 
                   withtransparency ("multiply", 1/max(1,sketch_passes))
                   t ;
      endfor;
  else :
      normalfill p t;
  fi
enddef;

endinput;

% Modified example from
% http://tex.loria.fr/prod-graph/zoonekynd/metapost/metapost.html

beginfig(1)
  pair A,B,C,O;
  A=(0,0); B=(3cm,0); C=(1cm,2cm);

  O - 1/2[B,C] = whatever * (B-C) rotated 90;
  O - 1/2[A,B] = whatever * (A-B) rotated 90;

  sketchypaths;
  sketch_amount := 5bp;
  draw A--B--C--cycle;

  draw O withpen pencircle scaled 4bp;

  sketch_amount := 2bp;
  draw fullcircle scaled 2abs(O-A) shifted O;
endfig

[-- Attachment #5: Type: text/plain, Size: 493 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://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: Fuzzy lines
  2021-05-13 16:36         ` Aditya Mahajan
@ 2021-05-13 16:55           ` Henning Hraban Ramm
  2021-05-13 17:17             ` Aditya Mahajan
  2021-05-14 10:05           ` Fuzzy lines Hans Hagen
  1 sibling, 1 reply; 23+ messages in thread
From: Henning Hraban Ramm @ 2021-05-13 16:55 UTC (permalink / raw)
  To: mailing list for ConTeXt users


> Am 13.05.2021 um 18:36 schrieb Aditya Mahajan <adityam@umich.edu>:
> 
>> Now I want to draw that path several times.
>> 
>> If I understand it right, I can’t put that into "sketchdraw" because that leaves the end open for path additions like "withcolor".
> 
> A slightly different approach is needed. See attached. I also added a bit a error protection in case someone sets sketch_passes to be negative by mistake. 

Thank you so much!


> It may be interesting to rewrite these macros using the new key-value interface for metapost, so that something like this works:
> 
> \sketchdraw[amount=3bp, passes=5, transparency=yes] fullsquare scaled 1cm withcolor red;

Yes, I just read the luametafun manual and will try a few things – but this is still over my head.


Hraban

___________________________________________________________________________________
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] 23+ messages in thread

* Re: Fuzzy lines
  2021-05-13 16:55           ` Henning Hraban Ramm
@ 2021-05-13 17:17             ` Aditya Mahajan
  2021-05-13 18:02               ` Henning Hraban Ramm
  0 siblings, 1 reply; 23+ messages in thread
From: Aditya Mahajan @ 2021-05-13 17:17 UTC (permalink / raw)
  To: mailing list for ConTeXt users

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

On Thu, 13 May 2021, Henning Hraban Ramm wrote:

> 
> > Am 13.05.2021 um 18:36 schrieb Aditya Mahajan <adityam@umich.edu>:
> > 
> >> Now I want to draw that path several times.
> >> 
> >> If I understand it right, I can’t put that into "sketchdraw" because that leaves the end open for path additions like "withcolor".
> > 
> > A slightly different approach is needed. See attached. I also added a bit a error protection in case someone sets sketch_passes to be negative by mistake. 
> 
> Thank you so much!

Here is a different effect ... more of a trembling hand drawing than a sketch. You can play around with sketch_amount to see some variation. 

In this, rather than using the default randomized path (which randomizes the points of a path), I split the path into 20 segments, and randomize each segment. 

Aditya

[-- Attachment #2: Type: text/plain, Size: 3680 bytes --]

%D \module
%D   [       file=mp-sketch.mp
%D        version=2021.05.13
%D          title=\CONTEXT\ \METAPOST\ graphics,
%D       subtitle=Sketch drawing,
%D         author=Aditya Mahajan,
%D           date=\currentdate,
%D      copyright={Aditya Mahajan}]

%D This metapost module is inspired by a TeX.SE question:
%D http://tex.stackexchange.com/q/39296/323
%D
%D I thought that it would be fun to implement a similar feature in MetaPost.
%D
%D To use this package in MetaPost:
%D
%D \starttyping
%D    input mp-sketch;
%D    
%D    beginfig(1)
%D      sketchypaths; % Make draw and fill sketchy
%D      ...
%D      naturalizepaths; % Restore the value of draw and fill
%D      ...
%D    endfig
%D \stoptyping
%D
%D The code is heavily inspired by Hans Hagen's Metafun macros.
%D
%D The macro \type{sketchypaths} is modeled after \type{visualizepaths} from
%D \filename{mp-tool}.

def sketchypaths =
    let draw = sketchdraw ;
    let fill = sketchfill ;
enddef ;

%D Check if \filename{mp-tool} is loaded
if not known context_tool :
  let normaldraw = draw;
  let normalfill = fill;

  def naturalizepaths =
      let fill = normalfill ;
      let draw = normaldraw ;
  enddef ;
fi

%D The variable \type{sketch_amount} determines the amount of randomness in the
%D drawing
numeric sketch_amount; sketch_amount := 0.75bp;

%D The variable \type{sketch_passes} determines the number of times the path
%D is drawn
numeric sketch_passes; sketch_passes := 1;

%D Based on \type{randomized}. Assumes p is path:
numeric sketch_segments; sketch_segments := 20;

primarydef p sketchrandomized s = (
    if path p :
        for t = 0 step 1/sketch_segments until 1-1/sketch_segments : 
            ((point       (t*arclength(p))                     on p) randomshifted s) .. controls
            ((postcontrol (t*arclength(p))                     on p) randomshifted s) and
            ((precontrol  ((t+1/sketch_segments)*arclength(p)) on p) randomshifted s) ..
        endfor
        if cycle p :
            cycle
        else :
            ((point arclength(p) on p) randomshifted s)
        fi
    else :
        p
    fi
) enddef ;


%D The macro \type{sketchdraw} draws the randomized path. The 
%D \type{expr} ... \type{text} trick is copied from the definition of 
%D \type{drawarrow}
def sketchdraw expr p =
   do_sketchdraw(p)
enddef;

def do_sketchdraw(expr p) text t =
  if (path p) :
      for i = 1 upto max(1,sketch_passes) :
        normaldraw p 
                   sketchrandomized sketch_amount 
                   withtransparency ("multiply", 1/max(1,sketch_passes))
                   t ;
      endfor;
  else :
      normaldraw p t;
  fi
enddef;

%D The macro \type{sketchfill} randomizes the path before filling it.
def sketchfill expr p =
  do_sketchfill(p)
enddef ;

def do_sketchfill(expr p) text t =
  if (path p) :
      for i = 1 upto max(1,sketch_passes) :
        normalfill p 
                   randomized sketch_amount 
                   withtransparency ("multiply", 1/max(1,sketch_passes))
                   t ;
      endfor;
  else :
      normalfill p t;
  fi
enddef;

endinput;

% Modified example from
% http://tex.loria.fr/prod-graph/zoonekynd/metapost/metapost.html

beginfig(1)
  pair A,B,C,O;
  A=(0,0); B=(3cm,0); C=(1cm,2cm);

  O - 1/2[B,C] = whatever * (B-C) rotated 90;
  O - 1/2[A,B] = whatever * (A-B) rotated 90;

  sketchypaths;
  sketch_amount := 5bp;
  draw A--B--C--cycle;

  draw O withpen pencircle scaled 4bp;

  sketch_amount := 2bp;
  draw fullcircle scaled 2abs(O-A) shifted O;
endfig

[-- Attachment #3: Type: application/x-tex, Size: 616 bytes --]

[-- Attachment #4: Type: application/pdf, Size: 8959 bytes --]

[-- Attachment #5: Type: text/plain, Size: 493 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://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: Fuzzy lines
  2021-05-13 17:17             ` Aditya Mahajan
@ 2021-05-13 18:02               ` Henning Hraban Ramm
  2021-05-13 18:26                 ` Thomas A. Schmitz
                                   ` (2 more replies)
  0 siblings, 3 replies; 23+ messages in thread
From: Henning Hraban Ramm @ 2021-05-13 18:02 UTC (permalink / raw)
  To: mailing list for ConTeXt users

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



> Am 13.05.2021 um 19:17 schrieb Aditya Mahajan <adityam@umich.edu>:
> 
> On Thu, 13 May 2021, Henning Hraban Ramm wrote:
> 
>> 
>>> Am 13.05.2021 um 18:36 schrieb Aditya Mahajan <adityam@umich.edu>:
>>> 
>>>> Now I want to draw that path several times.
>>>> 
>>>> If I understand it right, I can’t put that into "sketchdraw" because that leaves the end open for path additions like "withcolor".
>>> 
>>> A slightly different approach is needed. See attached. I also added a bit a error protection in case someone sets sketch_passes to be negative by mistake. 
>> 
>> Thank you so much!
> 
> Here is a different effect ... more of a trembling hand drawing than a sketch. You can play around with sketch_amount to see some variation. 
> 
> In this, rather than using the default randomized path (which randomizes the points of a path), I split the path into 20 segments, and randomize each segment. 

Thank you, that is what I was asking for!

Unfortunately, the deviation of edge points is too big – try my attached test file.

Strangely, if I include the MP code in a ConTeXt document, the transparency doesn’t work (it multiplies, but at 100%); I must add "withtransparency ("multiply", 0.2)" to my draw command. I’ll try to come up with a MWE. (Transparency of an image also doesn’t work in that document.)

My test file also includes the noise overlay after Jairo’s suggestions. Maybe you want to implement that, too?
At the moment it still doesn’t respect the pen size, and the number of patterns per path is fixed.

Hraban



[-- Attachment #2: sketchtest.mp --]
[-- Type: application/octet-stream, Size: 2227 bytes --]

input mp-sketch;

beginfig(1)
  color darkblue ; darkblue := (40/255,67/255,117/255) ;
  color beige ; beige := (248/255,245/255,236/255) ;
  %pickup pensquare xyscaled 4bp rotated 30 ;

  % bleed box, paper shading
  fill (-3mm,-3mm)--(-3mm,300mm)--(213mm,300mm)--(213mm,-3mm)--cycle withcolor beige;

  pen pillpen ; pillpen := makepen ((-1.5bp,-0.5bp)--(1.5bp,-0.5bp)..(1.5bp,0.5bp)--(-1.5bp,0.5bp)..cycle) ;
  pickup pillpen xyscaled 3bp ;

  sketchypaths;
  sketch_amount := 5bp;
  sketch_passes := 5;
  sketch_segments := 5;

  path one ; one := (-3mm,249mm) -- (185mm,249mm) -- (185mm,300mm) ; % links-oben
  path two ; two := (25.5mm,-3mm) -- (25.5mm,130mm) -- (213mm,130mm) ; % unten-rechts

  draw one withcolor darkblue;
  draw two withcolor darkblue;

  picture NoisePattern;
  NoisePattern := image(
    pickup pencircle xyscaled 0.5bp;
    numeric pmax ; pmax := 7 ;
  	for i = 1 upto pmax:
  		for j = 1 upto pmax:
        draw (i - (pmax/2), j - (pmax/2)) withcolor darkblue withtransparency ("normal", (uniformdeviate 0.33));
  		endfor
  	endfor
  );

  def noisify(expr p) =
    for i = 0 step 1/100 until length p:
    	draw NoisePattern randomized 1 shifted point i of p ;
    endfor
  enddef;

  noisify(one);
  noisify(two);

  naturalizepaths; % Restore the value of draw and fill

  % draw crop box (paper size)
  pickup pencircle xyscaled 0.5bp;
  draw (0,0)--(0,297mm)--(210mm,297mm)--(210mm,0)--cycle withcolor magenta;

  % begingroup;
  % save Rnd; let Rnd = uniformdeviate;
  %
  % numeric u; u := 3mm;
  % picture PlumaToo;
  % PlumaToo := image(
  % 	for i = 1 upto pmax:
  % 		for j = 1 upto pmax:
  %       fill fullcircle shifted (i - (pmax/2), j - (pmax/2)); %try fulltriangle or another shape
  % 		endfor
  % 	endfor
  % );
  %
  % for i = 0 step 1/20 until length one:
  % 	draw image(
  % 	for x within PlumaToo:
  % 		fill pathpart x %Assuming our image only contains cyclic paths
  % 		scaled (1+Rnd 1)
  % 		shifted (1/2dir Rnd 360)
  % 		withcolor hsvtorgb(6round(Rnd 60),9/10,1) %Colors just because they look cute
  %     withtransparency ("normal", Rnd 1);
  % 	endfor
  % 	) rotated (90 + angle direction i of one)
  % 	shifted point i of one;
  % endfor
  % endgroup;


endfig

[-- Attachment #3: Type: text/plain, Size: 493 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://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: Fuzzy lines
  2021-05-13 18:02               ` Henning Hraban Ramm
@ 2021-05-13 18:26                 ` Thomas A. Schmitz
  2021-05-13 19:42                   ` Henning Hraban Ramm
  2021-05-13 22:39                 ` Aditya Mahajan
  2021-09-03 16:04                 ` Henning Hraban Ramm via ntg-context
  2 siblings, 1 reply; 23+ messages in thread
From: Thomas A. Schmitz @ 2021-05-13 18:26 UTC (permalink / raw)
  To: mailing list for ConTeXt users, Henning Hraban Ramm

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

On 5/13/21 8:02 PM, Henning Hraban Ramm wrote:
> Unfortunately, the deviation of edge points is too big – try my attached test file.
> 
> Strangely, if I include the MP code in a ConTeXt document,

I find the discussion interesting. Could you send complete documents, 
like Aditya did? When I try to compile your files, I get all sorts of 
errors, so I suspect they are not complete. Would be appreciated if you 
could send a ConTeXt document. I'm still not quite sure what you're 
looking for...

Thomas


[-- Attachment #2: test.tex --]
[-- Type: text/x-tex, Size: 462 bytes --]

\setupbodyfont[20pt]

\showframe

\startuseMPgraphic{fuzzy}

path p ; p := (0,0) -- (0,OverlayHeight) -- (OverlayWidth,OverlayHeight) --
(OverlayWidth,0) -- cycle ;

pickup pencircle scaled .5pt ;

for i=1 upto 20 :
 draw p enlarged -5pt randomized 15pt withcolor blue ;
endfor ;

\stopuseMPgraphic

\defineoverlay [Fuzzy] [\useMPgraphic{fuzzy}]

\startTEXpage


\framed [height=20cm,width=12cm,frame=on,background=Fuzzy,align={middle,lohi}]
{Test}

\stopTEXpage

[-- Attachment #3: test.pdf --]
[-- Type: application/pdf, Size: 8593 bytes --]

[-- Attachment #4: Type: text/plain, Size: 493 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://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: Fuzzy lines
  2021-05-13 18:26                 ` Thomas A. Schmitz
@ 2021-05-13 19:42                   ` Henning Hraban Ramm
  2021-05-14 15:08                     ` Thomas A. Schmitz
  0 siblings, 1 reply; 23+ messages in thread
From: Henning Hraban Ramm @ 2021-05-13 19:42 UTC (permalink / raw)
  To: mailing list for ConTeXt users

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



> Am 13.05.2021 um 20:26 schrieb Thomas A. Schmitz <thomas.schmitz@uni-bonn.de>:
> 
> On 5/13/21 8:02 PM, Henning Hraban Ramm wrote:
>> Unfortunately, the deviation of edge points is too big – try my attached test file.
>> Strangely, if I include the MP code in a ConTeXt document,
> 
> I find the discussion interesting. Could you send complete documents, like Aditya did? When I try to compile your files, I get all sorts of errors, so I suspect they are not complete. Would be appreciated if you could send a ConTeXt document. I'm still not quite sure what you're looking for...

Hi Thomas,

for my test files you only also need Aditya’s latest mp-sketch.mp, now attached.

Find also attached my (somewhat simplified) attempt on a cover for a new workbook series.
In the original I’m using the commercial "Supernett Cn" font family that also looks quite sketchy.

Hraban



[-- Attachment #2: mp-sketch.mp --]
[-- Type: application/octet-stream, Size: 3816 bytes --]

%D \module
%D   [       file=mp-sketch.mp
%D        version=2021.05.13
%D          title=\CONTEXT\ \METAPOST\ graphics,
%D       subtitle=Sketch drawing,
%D         author=Aditya Mahajan,
%D           date=\currentdate,
%D      copyright={Aditya Mahajan}]

%D This metapost module is inspired by a TeX.SE question:
%D http://tex.stackexchange.com/q/39296/323
%D
%D I thought that it would be fun to implement a similar feature in MetaPost.
%D
%D To use this package in MetaPost:
%D
%D \starttyping
%D    input mp-sketch;
%D    
%D    beginfig(1)
%D      sketchypaths; % Make draw and fill sketchy
%D      ...
%D      naturalizepaths; % Restore the value of draw and fill
%D      ...
%D    endfig
%D \stoptyping
%D
%D The code is heavily inspired by Hans Hagen's Metafun macros.
%D
%D The macro \type{sketchypaths} is modeled after \type{visualizepaths} from
%D \filename{mp-tool}.

def sketchypaths =
    let draw = sketchdraw ;
    let fill = sketchfill ;
enddef ;

%D Check if \filename{mp-tool} is loaded
if not known context_tool :
  let normaldraw = draw;
  let normalfill = fill;

  def naturalizepaths =
      let fill = normalfill ;
      let draw = normaldraw ;
  enddef ;
fi

%D The variable \type{sketch_amount} determines the amount of randomness in the
%D drawing
numeric sketch_amount; sketch_amount := 0.75bp;

%D The variable \type{sketch_passes} determines the number of times the path
%D is drawn
numeric sketch_passes; sketch_passes := 1;

%D Based on \type{randomized}. Assumes p is path:
numeric sketch_segments; sketch_segments := 20;

primarydef p sketchrandomized s = (
    if path p :
        for t = 0 step 1/sketch_segments until 1-1/sketch_segments : 
            ((point       (t*arclength(p))                     on p) randomshifted s) .. controls
            ((postcontrol (t*arclength(p))                     on p) randomshifted s) and
            ((precontrol  ((t+1/sketch_segments)*arclength(p)) on p) randomshifted s) ..
        endfor
        if cycle p :
            cycle
        else :
            ((point arclength(p) on p) randomshifted s)
        fi
    else :
        p
    fi
) enddef ;


%D The macro \type{sketchdraw} draws the randomized path. The 
%D \type{expr} ... \type{text} trick is copied from the definition of 
%D \type{drawarrow}
def sketchdraw expr p =
   do_sketchdraw(p)
enddef;

def do_sketchdraw(expr p) text t =
  if (path p) :
      for i = 1 upto max(1,sketch_passes) :
        normaldraw p 
                   sketchrandomized sketch_amount 
                   withtransparency ("multiply", 1/max(1,sketch_passes))
                   t ;
      endfor;
  else :
      normaldraw p t;
  fi
enddef;

%D The macro \type{sketchfill} randomizes the path before filling it.
def sketchfill expr p =
  do_sketchfill(p)
enddef ;

def do_sketchfill(expr p) text t =
  if (path p) :
      for i = 1 upto max(1,sketch_passes) :
        normalfill p 
                   randomized sketch_amount 
                   withtransparency ("multiply", 1/max(1,sketch_passes))
                   t ;
      endfor;
  else :
      normalfill p t;
  fi
enddef;

endinput;

% Modified example from
% http://tex.loria.fr/prod-graph/zoonekynd/metapost/metapost.html

beginfig(1)
  pair A,B,C,O;
  A=(0,0); B=(3cm,0); C=(1cm,2cm);

  O - 1/2[B,C] = whatever * (B-C) rotated 90;
  O - 1/2[A,B] = whatever * (A-B) rotated 90;

  sketchypaths;
  sketch_amount := 5bp;
  draw A--B--C--cycle;

  draw O withpen pencircle scaled 4bp;

  sketch_amount := 2bp;
  draw fullcircle scaled 2abs(O-A) shifted O;
endfig

[-- Attachment #3: sketchtest.mp --]
[-- Type: application/octet-stream, Size: 2227 bytes --]

input mp-sketch;

beginfig(1)
  color darkblue ; darkblue := (40/255,67/255,117/255) ;
  color beige ; beige := (248/255,245/255,236/255) ;
  %pickup pensquare xyscaled 4bp rotated 30 ;

  % bleed box, paper shading
  fill (-3mm,-3mm)--(-3mm,300mm)--(213mm,300mm)--(213mm,-3mm)--cycle withcolor beige;

  pen pillpen ; pillpen := makepen ((-1.5bp,-0.5bp)--(1.5bp,-0.5bp)..(1.5bp,0.5bp)--(-1.5bp,0.5bp)..cycle) ;
  pickup pillpen xyscaled 3bp ;

  sketchypaths;
  sketch_amount := 5bp;
  sketch_passes := 5;
  sketch_segments := 5;

  path one ; one := (-3mm,249mm) -- (185mm,249mm) -- (185mm,300mm) ; % links-oben
  path two ; two := (25.5mm,-3mm) -- (25.5mm,130mm) -- (213mm,130mm) ; % unten-rechts

  draw one withcolor darkblue;
  draw two withcolor darkblue;

  picture NoisePattern;
  NoisePattern := image(
    pickup pencircle xyscaled 0.5bp;
    numeric pmax ; pmax := 7 ;
  	for i = 1 upto pmax:
  		for j = 1 upto pmax:
        draw (i - (pmax/2), j - (pmax/2)) withcolor darkblue withtransparency ("normal", (uniformdeviate 0.33));
  		endfor
  	endfor
  );

  def noisify(expr p) =
    for i = 0 step 1/100 until length p:
    	draw NoisePattern randomized 1 shifted point i of p ;
    endfor
  enddef;

  noisify(one);
  noisify(two);

  naturalizepaths; % Restore the value of draw and fill

  % draw crop box (paper size)
  pickup pencircle xyscaled 0.5bp;
  draw (0,0)--(0,297mm)--(210mm,297mm)--(210mm,0)--cycle withcolor magenta;

  % begingroup;
  % save Rnd; let Rnd = uniformdeviate;
  %
  % numeric u; u := 3mm;
  % picture PlumaToo;
  % PlumaToo := image(
  % 	for i = 1 upto pmax:
  % 		for j = 1 upto pmax:
  %       fill fullcircle shifted (i - (pmax/2), j - (pmax/2)); %try fulltriangle or another shape
  % 		endfor
  % 	endfor
  % );
  %
  % for i = 0 step 1/20 until length one:
  % 	draw image(
  % 	for x within PlumaToo:
  % 		fill pathpart x %Assuming our image only contains cyclic paths
  % 		scaled (1+Rnd 1)
  % 		shifted (1/2dir Rnd 360)
  % 		withcolor hsvtorgb(6round(Rnd 60),9/10,1) %Colors just because they look cute
  %     withtransparency ("normal", Rnd 1);
  % 	endfor
  % 	) rotated (90 + angle direction i of one)
  % 	shifted point i of one;
  % endfor
  % endgroup;


endfig

[-- Attachment #4: covertest.tex --]
[-- Type: application/octet-stream, Size: 4990 bytes --]


\mainlanguage[de]

\setvariables[cover][
  issue={0},
  author={Autor Name},
  title={Toller Titel},
  subtitle={Aussagekräftiger Untertitel}
]

\definemeasure[Bleed][3mm]
\definemeasure[FrontOffset][210mm]

\definepapersize[Cover][% A3
  width=420mm,
  height=297mm]
\definepapersize[CoverPlus][
  width=430mm,
  height=307mm]
\setuppapersize[Cover][CoverPlus]

\definemeasure[MaxHeight][{\dimexpr\paperheight + 2\measure{Bleed}\relax}]
\definemeasure[MaxWidth][{\dimexpr\paperwidth + 2\measure{Bleed}\relax}]

\setuplayout[
  marking=on,
  location=middle,
  top=0mm,
  header=0mm,
]
\setupalign[flushleft,hanging]

\setupbodyfont[ss,24pt]

\setuppagenumbering[state=stop]

\definecolor[cmykblack][c=0,m=0,y=0,k=1]
\definecolor[LineColor][c=.9,m=.8,y=0,k=.3,t=1,a=2]
\setupcolors[
  pagecolormodel=auto,
  rgb=no,cmyk=yes,spot=no,
  state=start,
  overprint=yes,
  intent={Uncoated FOGRA29 (ISO 12647-2:2004)},
  textcolor=LineColor,
]
\setupbackend[
  format=PDF/X-4,
]
\definetransparency[tmultiply][a=2,t=1]
\definetransparency[tpaper][a=1,t=.05]

\definelayer[whole][
  width=\paperwidth,
  height=\paperheight
]

\startuniqueMPgraphic{fuzzylines}
  input mp-sketch; % by Aditya Mahajan

  pen pillpen ; pillpen := makepen ((-1.5bp,-0.5bp)--(1.5bp,-0.5bp)..(1.5bp,0.5bp)--(-1.5bp,0.5bp)..cycle) ;
  pickup pillpen xyscaled 3bp ;

  sketchypaths; % Make draw and fill sketchy
  sketch_amount := 4bp;
  sketch_passes := 5;
  sketch_segments := 10;

  path one   ; one   := (25.5mm,300mm) -- (25.5mm,249mm) -- (210mm,249mm) -- (395mm,249mm) -- (395mm,300mm) ; % links-oben
  % split horizontal and vertical parts to adapt settings:
  path one_h ; one_h := (25.5mm,249mm) -- (210mm,249mm) -- (395mm,249mm) ;
  path one_v ; one_v := (25.5mm,300mm) -- (25.5mm,249mm) ;
  path two   ; two   := (25.5mm+210mm,-3mm) -- (25.5mm+210mm,130mm) -- (213mm+210mm,130mm) ; % unten-rechts
  path three ; three := (-3mm,130mm) -- (210mm-25.5mm,130mm) -- (210mm-25.5mm,-3mm); % links-unten

  draw one withcolor "LineColor" withtransparency ("multiply", 0.2);
  draw two withcolor "LineColor" withtransparency ("multiply", 0.2);
  draw three withcolor "LineColor" withtransparency ("multiply", 0.2);

  picture NoisePattern;
  NoisePattern := image(
    pickup pencircle xyscaled 0.5bp;
    numeric pmax ; pmax := 7 ;
    numeric x ; numeric y ;
  	for i = 1 upto pmax:
  		for j = 1 upto pmax:
        % try to get more dots in the center
        x := (i - (pmax/2)) * (uniformdeviate 2.5); % we use a wide pen
        y := (j - (pmax/2)) * (uniformdeviate 1.25);
        draw (x, y) withcolor "LineColor" withtransparency ("normal", (uniformdeviate 0.33));
  		endfor
  	endfor
  );

  numeric noise_steps ; noise_steps := 100;

  def noisify(expr p) =
    for i = 0 step 1/noise_steps until length p:
    	draw NoisePattern randomized 1 shifted point i of p ;
    endfor
  enddef;

  noisify(one_h);
  noisify(two);
  noisify(three);
  noise_steps := 30;
  noisify(one_v);
  noisify(one_v shifted (369.5mm,0));

  naturalizepaths;
\stopuniqueMPgraphic

\defineoverlay[fuzzylines][\uniqueMPgraphic{fuzzylines}]

\setupbackgrounds[page][background={fuzzylines,whole},state=start]


\starttext %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\startlayout [page]

\definemeasure[FrontTextstart][{\dimexpr\measure{FrontOffset}+36mm\relax}]

\setlayerframed[whole][
  hoffset=-\measure{Bleed},
  voffset=-\measure{Bleed}-8mm,
  offset=overlay,
  frame=off,
  width=\measure{MaxWidth},
  height=\measure{MaxHeight},
]{\starttransparent[tpaper]% funktioniert nicht!
%\externalfigure[paper][width=\measure{MaxWidth},height=\measure{MaxHeight},rotation=90]%
\stoptransparent}

\setlayerframed[whole][
  x=\measure{FrontTextstart},
  y=60mm,
  width=150mm,
  align=flushleft,
  frame=off,
  offset=overlay,
]{\getvariable{cover}{author}}

\setlayerframed[whole][
  x=\measure{FrontTextstart}-2mm,
  y=77mm,
  height=65mm,
  width=150mm,
  align={flushleft,hanging,lohi},
  frame=off,
  offset=overlay,
]{\definedfont[SansItalic*default at 96bp]\getvariable{cover}{title}}

\setlayerframed[whole][
  x=\measure{FrontTextstart},
  y=144mm,
  width=150mm,
  align=flushleft,
  frame=off,
  offset=overlay,
]{\getvariable{cover}{subtitle}}

\setlayerframed[whole][
  x=\measure{FrontTextstart}+20mm,
  y=264.5mm,
  width=150mm,
  align=flushleft,
  frame=off,
  offset=overlay,
]{\definedfont[SansItalic*default at 24bp]Workbook \getvariable{cover}{issue}}

\setlayerframed[whole][
  x=\measure{FrontTextstart},
  y=258mm,
  offset=overlay,
  %frame=off,
  width=16mm,
  height=16mm,
]{\startMPcode
  %input img/dvhlogo.mp;
  %draw DVHlogo_sw xysized (16mm,16mm);
  %redraw currentpicture withpen pencircle scaled 0.5bp withcolor "LineColor";
\stopMPcode}

\flushlayer[whole]

TEST

\stoplayout

\stoptext

[-- Attachment #5: Type: text/plain, Size: 493 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://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: Fuzzy lines
  2021-05-13 18:02               ` Henning Hraban Ramm
  2021-05-13 18:26                 ` Thomas A. Schmitz
@ 2021-05-13 22:39                 ` Aditya Mahajan
  2021-05-14  9:43                   ` Hans Hagen
  2021-05-14 20:33                   ` Henning Hraban Ramm
  2021-09-03 16:04                 ` Henning Hraban Ramm via ntg-context
  2 siblings, 2 replies; 23+ messages in thread
From: Aditya Mahajan @ 2021-05-13 22:39 UTC (permalink / raw)
  To: mailing list for ConTeXt users

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

On Thu, 13 May 2021, Henning Hraban Ramm wrote:

> My test file also includes the noise overlay after Jairo’s suggestions. Maybe you want to implement that, too?
> At the moment it still doesn’t respect the pen size, and the number of patterns per path is fixed.

I am not sure if simply adding random noise to the line give an impression of hand-drawn lines. For a different approach, see:

https://m.habr.com/en/post/454376/

which uses the code here:

https://github.com/jemmybutton/fiziko

Aditya

[-- Attachment #2: Type: text/plain, Size: 493 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://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: Fuzzy lines
  2021-05-13 22:39                 ` Aditya Mahajan
@ 2021-05-14  9:43                   ` Hans Hagen
  2021-05-14 20:33                   ` Henning Hraban Ramm
  1 sibling, 0 replies; 23+ messages in thread
From: Hans Hagen @ 2021-05-14  9:43 UTC (permalink / raw)
  To: mailing list for ConTeXt users, Aditya Mahajan

On 5/14/2021 12:39 AM, Aditya Mahajan wrote:

> https://github.com/jemmybutton/fiziko
That's amazing mp code, right?

Hans

-----------------------------------------------------------------
                                           Hans Hagen | PRAGMA ADE
               Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
        tel: 038 477 53 69 | www.pragma-ade.nl | 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://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: Fuzzy lines
  2021-05-13 16:36         ` Aditya Mahajan
  2021-05-13 16:55           ` Henning Hraban Ramm
@ 2021-05-14 10:05           ` Hans Hagen
  1 sibling, 0 replies; 23+ messages in thread
From: Hans Hagen @ 2021-05-14 10:05 UTC (permalink / raw)
  To: mailing list for ConTeXt users, Aditya Mahajan

On 5/13/2021 6:36 PM, Aditya Mahajan wrote:
> On Thu, 13 May 2021, Henning Hraban Ramm wrote:
> 
>>
>>> Am 11.05.2021 um 18:02 schrieb Aditya Mahajan <adityam@umich.edu>:
>>>
>>> Look at this old code on how hide such code behind a macro to have a clean interface:
>>>
>>> https://github.com/adityam/mp-sketch/blob/master/mp-sketch.mp
>>
>> Hi Aditya,
>> I’m trying to include my additions into your code, but it doesn’t work.
>>
>>
>> numeric sketch_amount; sketch_amount := 3bp;
>> numeric sketch_passes; sketch_passes := 5;
>>
>> def sketchdraw expr p =
>>    do_sketchdraw(p if (path p): randomized sketch_amount fi)
>> enddef;
>>
>> def do_sketchdraw(expr p) text t =
>>      normaldraw p t ;
>> enddef;
>>
>>
>> Now I want to draw that path several times.
>>
>> If I understand it right, I can’t put that into "sketchdraw" because that leaves the end open for path additions like "withcolor".
>>
>> But it also doesn’t work in "do_sketchdraw"; I thought it might like this:
> 
> A slightly different approach is needed. See attached. I also added a bit a error protection in case someone sets sketch_passes to be negative by mistake.
> 
> It may be interesting to rewrite these macros using the new key-value interface for metapost, so that something like this works:
> 
> \sketchdraw[amount=3bp, passes=5, transparency=yes] fullsquare scaled 1cm withcolor red;
one resembles present-weird-001.pdf (in the doc tree) ... it was from 
the times one could rely on acrobat to jump around a large page ... i 
used that one for a presentation about mathml (decades ago) ... in times 
that the main question one got was "why use something else than latex 
for math" or "why use tex if you don't do math" which put context in a 
weird spot (but also permitted to come up with weird presentation styles 
that didn't look too tex)

Hans


-----------------------------------------------------------------
                                           Hans Hagen | PRAGMA ADE
               Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
        tel: 038 477 53 69 | www.pragma-ade.nl | 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://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: Fuzzy lines
  2021-05-13 19:42                   ` Henning Hraban Ramm
@ 2021-05-14 15:08                     ` Thomas A. Schmitz
  0 siblings, 0 replies; 23+ messages in thread
From: Thomas A. Schmitz @ 2021-05-14 15:08 UTC (permalink / raw)
  To: mailing list for ConTeXt users


> On 13. May 2021, at 21:42, Henning Hraban Ramm <texml@fiee.net> wrote:
> 
> Hi Thomas,
> 
> for my test files you only also need Aditya’s latest mp-sketch.mp, now attached.
> 
> Find also attached my (somewhat simplified) attempt on a cover for a new workbook series.
> In the original I’m using the commercial "Supernett Cn" font family that also looks quite sketchy.
> 
> Hraban

Perfect, thank you, that allowed me to play a bit with it. Would be interested to see what you really want to achieve one day…

All best

Thomas
___________________________________________________________________________________
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] 23+ messages in thread

* Re: Fuzzy lines
  2021-05-13 22:39                 ` Aditya Mahajan
  2021-05-14  9:43                   ` Hans Hagen
@ 2021-05-14 20:33                   ` Henning Hraban Ramm
  1 sibling, 0 replies; 23+ messages in thread
From: Henning Hraban Ramm @ 2021-05-14 20:33 UTC (permalink / raw)
  To: mailing list for ConTeXt users


> Am 14.05.2021 um 00:39 schrieb Aditya Mahajan <adityam@umich.edu>:
> 
> I am not sure if simply adding random noise to the line give an impression of hand-drawn lines. For a different approach, see:
> 
> https://m.habr.com/en/post/454376/
> 
> which uses the code here:
> 
> https://github.com/jemmybutton/fiziko

Just WOW

HR
___________________________________________________________________________________
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] 23+ messages in thread

* Re: Fuzzy lines
  2021-05-13 18:02               ` Henning Hraban Ramm
  2021-05-13 18:26                 ` Thomas A. Schmitz
  2021-05-13 22:39                 ` Aditya Mahajan
@ 2021-09-03 16:04                 ` Henning Hraban Ramm via ntg-context
  2022-01-21 18:00                   ` Iterating over MP paths (was: Fuzzy lines) Henning Hraban Ramm via ntg-context
  2 siblings, 1 reply; 23+ messages in thread
From: Henning Hraban Ramm via ntg-context @ 2021-09-03 16:04 UTC (permalink / raw)
  To: mailing list for ConTeXt users; +Cc: Henning Hraban Ramm

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


> Am 13.05.2021 um 20:02 schrieb Henning Hraban Ramm <texml@fiee.net>:
> 
> Thank you, that is what I was asking for!
> 
> Unfortunately, the deviation of edge points is too big – try my attached test file.

Hi Aditya et al.,

coming back to this: I found "fill" was not working, since do_sketchfill didn’t use sketchrandomized. That was an easy fix.

But I couldn’t fix the edge points issue: the less segments I use, the more some of the edge points deviate (see attachment with 10 segments; I used the setup from your theorems article).
Lower left and upper right are good, what happens with the others?

I just can’t understand the loop that splits the original path segments:

        for t = 0 step 1/sketch_segments until 1-1/sketch_segments :
            ((point       (t*arclength(p))                     on p) randomshifted s) .. controls
            ((postcontrol (t*arclength(p))                     on p) randomshifted s) and
            ((precontrol  ((t+1/sketch_segments)*arclength(p)) on p) randomshifted s) ..
        endfor

Would it make sense to check if a point is an edge point (without curve controls)?


Next question: Can I apply this to \framed and TABLE lines? (Probably only via backgrounds?)


Hraban



[-- Attachment #2: sketchtest2.pdf --]
[-- Type: application/pdf, Size: 12857 bytes --]

[-- Attachment #3: mp-sketch.mp --]
[-- Type: application/octet-stream, Size: 3812 bytes --]

%D \module
%D   [       file=mp-sketch.mp
%D        version=2021.05.13
%D          title=\CONTEXT\ \METAPOST\ graphics,
%D       subtitle=Sketch drawing,
%D         author=Aditya Mahajan,
%D           date=\currentdate,
%D      copyright={Aditya Mahajan}]

%D This metapost module is inspired by a TeX.SE question:
%D http://tex.stackexchange.com/q/39296/323
%D
%D I thought that it would be fun to implement a similar feature in MetaPost.
%D
%D To use this package in MetaPost:
%D
%D \starttyping
%D    input mp-sketch;
%D
%D    beginfig(1)
%D      sketchypaths; % Make draw and fill sketchy
%D      ...
%D      naturalizepaths; % Restore the value of draw and fill
%D      ...
%D    endfig
%D \stoptyping
%D
%D The code is heavily inspired by Hans Hagen's Metafun macros.
%D
%D The macro \type{sketchypaths} is modeled after \type{visualizepaths} from
%D \filename{mp-tool}.

def sketchypaths =
    let draw = sketchdraw ;
    let fill = sketchfill ;
enddef ;

%D Check if \filename{mp-tool} is loaded
if not known context_tool :
  let normaldraw = draw;
  let normalfill = fill;

  def naturalizepaths =
      let fill = normalfill ;
      let draw = normaldraw ;
  enddef ;
fi

%D The variable \type{sketch_amount} determines the amount of randomness in the
%D drawing
numeric sketch_amount; sketch_amount := 0.75bp;

%D The variable \type{sketch_passes} determines the number of times the path
%D is drawn
numeric sketch_passes; sketch_passes := 1;

%D Based on \type{randomized}. Assumes p is path:
numeric sketch_segments; sketch_segments := 20;

primarydef p sketchrandomized s = (
    if path p :
        for t = 0 step 1/sketch_segments until 1-1/sketch_segments :
            ((point       (t*arclength(p))                     on p) randomshifted s) .. controls
            ((postcontrol (t*arclength(p))                     on p) randomshifted s) and
            ((precontrol  ((t+1/sketch_segments)*arclength(p)) on p) randomshifted s) ..
        endfor
        if cycle p :
            cycle
        else :
            ((point arclength(p) on p) randomshifted s)
        fi
    else :
        p
    fi
) enddef ;


%D The macro \type{sketchdraw} draws the randomized path. The
%D \type{expr} ... \type{text} trick is copied from the definition of
%D \type{drawarrow}
def sketchdraw expr p =
   do_sketchdraw(p)
enddef;

def do_sketchdraw(expr p) text t =
  if (path p) :
      for i = 1 upto max(1,sketch_passes) :
        normaldraw p
                   sketchrandomized sketch_amount
                   withtransparency ("multiply", 1/max(1,sketch_passes))
                   t ;
      endfor;
  else :
      normaldraw p t;
  fi
enddef;

%D The macro \type{sketchfill} randomizes the path before filling it.
def sketchfill expr p =
  do_sketchfill(p)
enddef ;

def do_sketchfill(expr p) text t =
  if (path p) :
      for i = 1 upto max(1,sketch_passes) :
        normalfill p
                   sketchrandomized sketch_amount 
                   withtransparency ("multiply", 1/max(1,sketch_passes))
                   t ;
      endfor;
  else :
      normalfill p t;
  fi
enddef;

endinput;

% Modified example from
% http://tex.loria.fr/prod-graph/zoonekynd/metapost/metapost.html

beginfig(1)
  pair A,B,C,O;
  A=(0,0); B=(3cm,0); C=(1cm,2cm);

  O - 1/2[B,C] = whatever * (B-C) rotated 90;
  O - 1/2[A,B] = whatever * (A-B) rotated 90;

  sketchypaths;
  sketch_amount := 5bp;
  draw A--B--C--cycle;

  draw O withpen pencircle scaled 4bp;

  sketch_amount := 2bp;
  draw fullcircle scaled 2abs(O-A) shifted O;
endfig

[-- Attachment #4: Type: text/plain, Size: 493 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://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: Iterating over MP paths (was: Fuzzy lines)
  2021-09-03 16:04                 ` Henning Hraban Ramm via ntg-context
@ 2022-01-21 18:00                   ` Henning Hraban Ramm via ntg-context
  0 siblings, 0 replies; 23+ messages in thread
From: Henning Hraban Ramm via ntg-context @ 2022-01-21 18:00 UTC (permalink / raw)
  To: ntg-context; +Cc: Henning Hraban Ramm

Am 03.09.21 um 18:04 schrieb Henning Hraban Ramm via ntg-context:
> Would it make sense to check if a point is an edge point (without curve controls)?

In a private reply to my old message, Hans explained to me why this was 
not possible like I thought (check if the point has control points), 
since every point has control points.

But I didn’t keep at it last fall, and the MP foo is still weak in this 
one, so, let’s try again:


If I iterate over the points of a path like in

beginfig(1);
path p;
p = (0,0) .. (2cm,4cm) -- (4cm,5cm) .. (5cm,6cm) .. (8cm,7cm);
draw p withpen pencircle scaled 2pt withcolor .7 white;
for t=0 upto length p:
   drawdot point t of p withpen pencircle scaled 4pt withcolor red;
endfor
endfig;

[2]
How can I check if the point is an edge?
(I want to randomize only the non-edge points.)

1. First or last point: if t=0 or t=length p.
    ... if the path is not closed: if not cycle p.
OK

2. Would it make sense to check curl or tension?
    Can I check if the connection is specified as -- ?
??

[2]
Other question on the examples from 
https://tex.stackexchange.com/questions/288259/how-to-draw-dots-equally-spaced-along-a-path, 
in the answer by Thruston:

I want to split the path between (edge) points in segments of the same 
length. So disregarding the "edge" issue for now, how do I get at the 
path segment between points (in the for loop above) so that I can use it 
as a new path q in:

for t=0 step s until arclength q:
   drawdot point arctime t of q of q withpen pencircle scaled 4pt 
withcolor red;
endfor

Hraban
___________________________________________________________________________________
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] 23+ messages in thread

end of thread, other threads:[~2022-01-21 18:00 UTC | newest]

Thread overview: 23+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-11 10:58 Fuzzy lines Henning Hraban Ramm
2021-05-11 11:30 ` Mikael Sundqvist
2021-05-11 12:33   ` Henning Hraban Ramm
2021-05-11 12:56   ` Jairo A. del Rio
2021-05-11 16:02     ` Aditya Mahajan
2021-05-12 18:07       ` Henning Hraban Ramm
2021-05-13 13:08       ` Henning Hraban Ramm
2021-05-13 16:36         ` Aditya Mahajan
2021-05-13 16:55           ` Henning Hraban Ramm
2021-05-13 17:17             ` Aditya Mahajan
2021-05-13 18:02               ` Henning Hraban Ramm
2021-05-13 18:26                 ` Thomas A. Schmitz
2021-05-13 19:42                   ` Henning Hraban Ramm
2021-05-14 15:08                     ` Thomas A. Schmitz
2021-05-13 22:39                 ` Aditya Mahajan
2021-05-14  9:43                   ` Hans Hagen
2021-05-14 20:33                   ` Henning Hraban Ramm
2021-09-03 16:04                 ` Henning Hraban Ramm via ntg-context
2022-01-21 18:00                   ` Iterating over MP paths (was: Fuzzy lines) Henning Hraban Ramm via ntg-context
2021-05-14 10:05           ` Fuzzy lines Hans Hagen
2021-05-12 17:55     ` Henning Hraban Ramm
     [not found]       ` <CAKyqqaaosis=BjOD_mArY5VM+bzcj-ar4uMtnt8SRJ2WLL9ykw@mail.gmail.com>
2021-05-12 21:21         ` Fwd: " Jairo A. del Rio
2021-05-13 13:23           ` Henning Hraban Ramm

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