ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* METAPOST subpath rounding issue
@ 2020-05-07 19:28 Gerben Wierda
  2020-05-07 22:46 ` ntg
  0 siblings, 1 reply; 6+ messages in thread
From: Gerben Wierda @ 2020-05-07 19:28 UTC (permalink / raw)
  To: mailing list for ConTeXt users


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

I have a METAPOST algorithm that splits a path at a certain time in two, does something with both ends (not the ends where they were split) and then rejoins them.

In very rare cases this crashes, because the subpath doesn’t work as expected.

      firstPart := subpath (0,halfWayTime) of workingConn;
      secondPart := subpath (halfWayTime,pathTimeLen) of workingConn;

may sometimes result in something like this:

metapost log    > >> Path at line 0:
metapost log    > (273,-427)..controls (259.50666666666666,-427) and (246.01333333333335,-427)
metapost log    >  ..(232.52000000000001,-427)
metapost log    > 
metapost log    > >> Path at line 0:
metapost log    > (232.51999999999998,-427)..controls (161.68000000000001,-427) and (90.840000000
metapost log    > 000003,-427)
metapost log    >  ..(20,-427)

As can be seen in these (rare) cases the two calls to subpath result in a different point resulting from both. so, when I later try to rejoin them with & it fails:

metapost log    > ! Paths don't touch; '&' will be changed to '..'.
metapost log    > <to be read again> 

Which means subpath doesn’t always exactly do what I expect it to do (and many explanations, but not the official manual) state. Again, this is rare.

I’ve done this to work around it but I wondered if there was a better (reliable) solution

      save cutFirstPart; path cutFirstPart; cutFirstPart := firstPart maxcutbefore fromPicOutline;
      save cutSecondPart; path cutSecondPart; cutSecondPart := secondPart maxcutafter toPicOutline;
      if ((xpart point 0 of cutSecondPart) <> (xpart point infinity of cutFirstPart))
        or ((ypart point 0 of cutSecondPart) <> (ypart point infinity of cutFirstPart)):
        resultConn := cutFirstPart--cutSecondPart;
      else:
        resultConn := cutFirstPart & cutSecondPart;
      fi

G

[-- Attachment #1.2: Type: text/html, Size: 6149 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] 6+ messages in thread

* Re: METAPOST subpath rounding issue
  2020-05-07 19:28 METAPOST subpath rounding issue Gerben Wierda
@ 2020-05-07 22:46 ` ntg
  2020-05-08 12:58   ` Gerben Wierda
  0 siblings, 1 reply; 6+ messages in thread
From: ntg @ 2020-05-07 22:46 UTC (permalink / raw)
  To: mailing list for ConTeXt users



> On 7 May 2020, at 20:28, Gerben Wierda <gerben.wierda@rna.nl> wrote:
> 
> I have a METAPOST algorithm that splits a path at a certain time in two, does something with both ends (not the ends where they were split) and then rejoins them.
> 
> In very rare cases this crashes, because the subpath doesn’t work as expected.
> 
>       firstPart := subpath (0,halfWayTime) of workingConn;
>       secondPart := subpath (halfWayTime,pathTimeLen) of workingConn;
> 
> may sometimes result in something like this:
> 
> metapost log    > >> Path at line 0:
> metapost log    > (273,-427)..controls (259.50666666666666,-427) and (246.01333333333335,-427)
> metapost log    >  ..(232.52000000000001,-427)
> metapost log    > 
> metapost log    > >> Path at line 0:
> metapost log    > (232.51999999999998,-427)..controls (161.68000000000001,-427) and (90.840000000
> metapost log    > 000003,-427)
> metapost log    >  ..(20,-427)
> 
> As can be seen in these (rare) cases the two calls to subpath result in a different point resulting from both. so, when I later try to rejoin them with & it fails:
> 
> metapost log    > ! Paths don't touch; '&' will be changed to '..'.
> metapost log    > <to be read again> 
> 
> Which means subpath doesn’t always exactly do what I expect it to do (and many explanations, but not the official manual) state. Again, this is rare.
> 
> I’ve done this to work around it but I wondered if there was a better (reliable) solution
> 
>       save cutFirstPart; path cutFirstPart; cutFirstPart := firstPart maxcutbefore fromPicOutline;
>       save cutSecondPart; path cutSecondPart; cutSecondPart := secondPart maxcutafter toPicOutline;
>       if ((xpart point 0 of cutSecondPart) <> (xpart point infinity of cutFirstPart))
>         or ((ypart point 0 of cutSecondPart) <> (ypart point infinity of cutFirstPart)):
>         resultConn := cutFirstPart--cutSecondPart;
>       else:
>         resultConn := cutFirstPart & cutSecondPart;
>       fi

A crude test of 

  path pb;
  pb:=(5.5cm,0cm)--(5.5cm,0cm)--(10.5cm,0cm);
  draw pb;

gives no errors so why not just join using -- all the time and save the test?

--
Bruce Horrocks
Hampshire, UK

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

* Re: METAPOST subpath rounding issue
  2020-05-07 22:46 ` ntg
@ 2020-05-08 12:58   ` Gerben Wierda
  2020-05-09 21:53     ` Bruce Horrocks
  0 siblings, 1 reply; 6+ messages in thread
From: Gerben Wierda @ 2020-05-08 12:58 UTC (permalink / raw)
  To: mailing list for ConTeXt users


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



> On 8 May 2020, at 00:46, ntg@scorecrow.com wrote:
> 
> 
> 
>> On 7 May 2020, at 20:28, Gerben Wierda <gerben.wierda@rna.nl> wrote:
>> 
>> I have a METAPOST algorithm that splits a path at a certain time in two, does something with both ends (not the ends where they were split) and then rejoins them.
>> 
>> In very rare cases this crashes, because the subpath doesn’t work as expected.
>> 
>>      firstPart := subpath (0,halfWayTime) of workingConn;
>>      secondPart := subpath (halfWayTime,pathTimeLen) of workingConn;
>> 
>> may sometimes result in something like this:
>> 
>> metapost log    > >> Path at line 0:
>> metapost log    > (273,-427)..controls (259.50666666666666,-427) and (246.01333333333335,-427)
>> metapost log    >  ..(232.52000000000001,-427)
>> metapost log    > 
>> metapost log    > >> Path at line 0:
>> metapost log    > (232.51999999999998,-427)..controls (161.68000000000001,-427) and (90.840000000
>> metapost log    > 000003,-427)
>> metapost log    >  ..(20,-427)
>> 
>> As can be seen in these (rare) cases the two calls to subpath result in a different point resulting from both. so, when I later try to rejoin them with & it fails:
>> 
>> metapost log    > ! Paths don't touch; '&' will be changed to '..'.
>> metapost log    > <to be read again> 
>> 
>> Which means subpath doesn’t always exactly do what I expect it to do (and many explanations, but not the official manual) state. Again, this is rare.
>> 
>> I’ve done this to work around it but I wondered if there was a better (reliable) solution
>> 
>>      save cutFirstPart; path cutFirstPart; cutFirstPart := firstPart maxcutbefore fromPicOutline;
>>      save cutSecondPart; path cutSecondPart; cutSecondPart := secondPart maxcutafter toPicOutline;
>>      if ((xpart point 0 of cutSecondPart) <> (xpart point infinity of cutFirstPart))
>>        or ((ypart point 0 of cutSecondPart) <> (ypart point infinity of cutFirstPart)):
>>        resultConn := cutFirstPart--cutSecondPart;
>>      else:
>>        resultConn := cutFirstPart & cutSecondPart;
>>      fi
> 
> A crude test of 
> 
>  path pb;
>  pb:=(5.5cm,0cm)--(5.5cm,0cm)--(10.5cm,0cm);
>  draw pb;
> 
> gives no errors so why not just join using -- all the time and save the test?

Because the double exact points are also creating (different) problems in my algorithm as they make the path have 'no direction' at that point (direction is (0,0).

G

> 
> --
> Bruce Horrocks
> Hampshire, UK
> 
> ___________________________________________________________________________________
> 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: 18436 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] 6+ messages in thread

* Re: METAPOST subpath rounding issue
  2020-05-08 12:58   ` Gerben Wierda
@ 2020-05-09 21:53     ` Bruce Horrocks
  2020-05-09 22:01       ` Bruce Horrocks
  0 siblings, 1 reply; 6+ messages in thread
From: Bruce Horrocks @ 2020-05-09 21:53 UTC (permalink / raw)
  To: mailing list for ConTeXt users



> On 8 May 2020, at 13:58, Gerben Wierda <gerben.wierda@rna.nl> wrote:
> 
>> On 8 May 2020, at 00:46, ntg@scorecrow.com wrote:
>> 
>>> On 7 May 2020, at 20:28, Gerben Wierda <gerben.wierda@rna.nl> wrote:
>>> 
>>> I have a METAPOST algorithm that splits a path at a certain time in two, does something with both ends (not the ends where they were split) and then rejoins them.
>>> 
>>> In very rare cases this crashes, because the subpath doesn’t work as expected.
>>> 
>>>      firstPart := subpath (0,halfWayTime) of workingConn;
>>>      secondPart := subpath (halfWayTime,pathTimeLen) of workingConn;
>>> 
>>> may sometimes result in something like this:
>>> 
>>> metapost log    > >> Path at line 0:
>>> metapost log    > (273,-427)..controls (259.50666666666666,-427) and (246.01333333333335,-427)
>>> metapost log    >  ..(232.52000000000001,-427)
>>> metapost log    > 
>>> metapost log    > >> Path at line 0:
>>> metapost log    > (232.51999999999998,-427)..controls (161.68000000000001,-427) and (90.840000000
>>> metapost log    > 000003,-427)
>>> metapost log    >  ..(20,-427)
>>> 
>>> As can be seen in these (rare) cases the two calls to subpath result in a different point resulting from both. so, when I later try to rejoin them with & it fails:
>>> 
>>> metapost log    > ! Paths don't touch; '&' will be changed to '..'.
>>> metapost log    > <to be read again> 
>>> 
>>> Which means subpath doesn’t always exactly do what I expect it to do (and many explanations, but not the official manual) state. Again, this is rare.
>>> 
>>> I’ve done this to work around it but I wondered if there was a better (reliable) solution
>>> 
>>>      save cutFirstPart; path cutFirstPart; cutFirstPart := firstPart maxcutbefore fromPicOutline;
>>>      save cutSecondPart; path cutSecondPart; cutSecondPart := secondPart maxcutafter toPicOutline;
>>>      if ((xpart point 0 of cutSecondPart) <> (xpart point infinity of cutFirstPart))
>>>        or ((ypart point 0 of cutSecondPart) <> (ypart point infinity of cutFirstPart)):
>>>        resultConn := cutFirstPart--cutSecondPart;
>>>      else:
>>>        resultConn := cutFirstPart & cutSecondPart;
>>>      fi
>> 
>> A crude test of 
>> 
>>  path pb;
>>  pb:=(5.5cm,0cm)--(5.5cm,0cm)--(10.5cm,0cm);
>>  draw pb;
>> 
>> gives no errors so why not just join using -- all the time and save the test?
> 
> Because the double exact points are also creating (different) problems in my algorithm as they make the path have 'no direction' at that point (direction is (0,0).

You can save extracting the xparts and yparts by using direct subtraction of pairs and comparing with (0,0) like this:

\starttext
\startMPcode
path cutFirstPart,cutSecondPart;

cutFirstPart := (0,0) -- (232.52000000000001,-427);
cutSecondPart := (232.51999999999998,-427) -- (999,-427);
%cutFirstPart := (0,0) -- (232.52000000000001,-427);
%cutSecondPart := (232.52000000000001,-427) -- (999,-427);

if (point infinity of cutFirstPart) - (point 0 of cutSecondPart) = (0,0) :
  label("same", (1cm,1cm))
else :
  label("different", (1cm,1cm))
fi;
\stopMPcode
\stoptext

*Except* that the example doesn't work with floats that are so close. Changing 232.52_etc to 332.52_etc works as expected. I'm hoping that this is a "feature" of the parser reading in the example at a lower precision than the number of decimals provided. For your code, where the different values are created by calculation, the two pairs should be recognised as different.

--
Bruce Horrocks
Hampshire, UK

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

* Re: METAPOST subpath rounding issue
  2020-05-09 21:53     ` Bruce Horrocks
@ 2020-05-09 22:01       ` Bruce Horrocks
  2020-05-10 18:17         ` Gerben Wierda
  0 siblings, 1 reply; 6+ messages in thread
From: Bruce Horrocks @ 2020-05-09 22:01 UTC (permalink / raw)
  To: mailing list for ConTeXt users

> You can save extracting the xparts and yparts by using direct subtraction of pairs and comparing with (0,0) like this:

Aaaargh, no, ignore that, it's nonsense. It's amazing how you can stare at something for minutes but only see the flaw the moment you press send. Sorry for the noise.

--
Bruce Horrocks
Hampshire, UK

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

* Re: METAPOST subpath rounding issue
  2020-05-09 22:01       ` Bruce Horrocks
@ 2020-05-10 18:17         ` Gerben Wierda
  0 siblings, 0 replies; 6+ messages in thread
From: Gerben Wierda @ 2020-05-10 18:17 UTC (permalink / raw)
  To: mailing list for ConTeXt users


> On 10 May 2020, at 00:01, Bruce Horrocks <ntg@scorecrow.com> wrote:
> 
>> You can save extracting the xparts and yparts by using direct subtraction of pairs and comparing with (0,0) like this:
> 
> Aaaargh, no, ignore that, it's nonsense. It's amazing how you can stare at something for minutes but only see the flaw the moment you press send. Sorry for the noise.

Instead, I would like to thank you for thinking about it and trying to help. Wrestling with it (and making mistakes) is part of all that (I’ve probably made more than most).

G

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

end of thread, other threads:[~2020-05-10 18:17 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-07 19:28 METAPOST subpath rounding issue Gerben Wierda
2020-05-07 22:46 ` ntg
2020-05-08 12:58   ` Gerben Wierda
2020-05-09 21:53     ` Bruce Horrocks
2020-05-09 22:01       ` Bruce Horrocks
2020-05-10 18:17         ` Gerben Wierda

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