Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.postscript > #1986 > unrolled thread

How to rewrite this

Started byCecil Westerhof <Cecil@decebal.nl>
First post2014-08-19 19:02 +0200
Last post2014-09-02 11:58 +0100
Articles 10 — 6 participants

Back to article view | Back to comp.lang.postscript


Contents

  How to rewrite this Cecil Westerhof <Cecil@decebal.nl> - 2014-08-19 19:02 +0200
    Re: How to rewrite this jdaw1 <jdawiseman@gmail.com> - 2014-08-20 00:43 -0700
    Re: How to rewrite this Herbert Voss <Herbert.Voss@alumni.tu-berlin.de> - 2014-08-20 13:08 +0200
      Re: How to rewrite this Cecil Westerhof <Cecil@decebal.nl> - 2014-08-20 15:28 +0200
    Re: How to rewrite this tlvp <mPiOsUcB.EtLlLvEp@att.net> - 2014-08-30 01:47 -0400
    Re: How to rewrite this luser- -droog <mijoryx@yahoo.com> - 2014-08-29 23:43 -0700
      Re: How to rewrite this luser- -droog <mijoryx@yahoo.com> - 2014-08-30 01:23 -0700
        Re: How to rewrite this Terry White <TW382@aol.com> - 2014-09-01 09:43 +0100
          Re: How to rewrite this luser- -droog <mijoryx@yahoo.com> - 2014-09-01 18:20 -0700
            Re: How to rewrite this Terry White <TW382@aol.com> - 2014-09-02 11:58 +0100

#1986 — How to rewrite this

FromCecil Westerhof <Cecil@decebal.nl>
Date2014-08-19 19:02 +0200
SubjectHow to rewrite this
Message-ID<87fvgs5s5w.fsf@Equus.decebal.nl>
I use the following code to draw a star:
    15  15  moveto
    20   0  lineto
    25  15  lineto
    40  20  lineto
    25  25  lineto
    20  40  lineto
    15  25  lineto
     0  20  lineto
    closepath
    fill

I thought to rewrite this neatly like this:
    gsave
        20  20  translate
        4 {
            -5   -5  moveto
             0  -20  lineto
             5   -5  lineto
            90  rotate
        } repeat
        closepath
        fill
    grestore

But then only four triangles are filled and the square in the middle
is not. I could of-course fill the square also, but I was wondering if
there is a better way.

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

[toc] | [next] | [standalone]


#1987

Fromjdaw1 <jdawiseman@gmail.com>
Date2014-08-20 00:43 -0700
Message-ID<597c288b-6303-41d6-862c-ff4c6ab8e0de@googlegroups.com>
In reply to#1986
I don't have time to think through your code, though suspect that your path intersects itself. But if you want a general star-drawing routine there is one in lines 2849 to 2495 of 
http://www.jdawiseman.com/papers/placemat/placemat.ps 
and some (perhaps not quite up-to-date) documentation in 
http://www.jdawiseman.com/papers/placemat/postscript_routines.html#star 

You'll need to def DeBugLevel, or remove the lines referring to it.

[toc] | [prev] | [next] | [standalone]


#1988

FromHerbert Voss <Herbert.Voss@alumni.tu-berlin.de>
Date2014-08-20 13:08 +0200
Message-ID<c5jdtpFh05lU1@mid.uni-berlin.de>
In reply to#1986
Am 19.08.2014 19:02, schrieb Cecil Westerhof:

> I thought to rewrite this neatly like this:
>     gsave
>         20  20  translate
>         4 {
>             -5   -5  moveto
>              0  -20  lineto
>              5   -5  lineto
>             90  rotate
>         } repeat
>         closepath
>         fill
>     grestore
> 
> But then only four triangles are filled and the square in the middle
> is not. I could of-course fill the square also, but I was wondering if
> there is a better way.


  200  200  translate
  10 dup scale
  -5   -5  moveto
  4 {
     5  -15  rlineto
     5   15  rlineto
     90  rotate
  } repeat
  closepath
  fill


Herbert

[toc] | [prev] | [next] | [standalone]


#1989

FromCecil Westerhof <Cecil@decebal.nl>
Date2014-08-20 15:28 +0200
Message-ID<87wqa347e0.fsf@Equus.decebal.nl>
In reply to#1988
Op Wednesday 20 Aug 2014 13:08 CEST schreef Herbert Voss:

>> I thought to rewrite this neatly like this:
>> gsave
>> 20  20  translate
>> 4 {
>> -5   -5  moveto
>> 0  -20  lineto
>> 5   -5  lineto
>> 90  rotate
>> } repeat
>> closepath
>> fill
>> grestore
>>
>> But then only four triangles are filled and the square in the
>> middle is not. I could of-course fill the square also, but I was
>> wondering if there is a better way.
>
>
> 200  200  translate
> 10 dup scale
> -5   -5  moveto
> 4 {
> 5  -15  rlineto
> 5   15  rlineto
> 90  rotate
> } repeat
> closepath
> fill

How stupid of me, I put the moveto in the loop instead of before
it. >:-(

Thanks a bundle.

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

[toc] | [prev] | [next] | [standalone]


#1995

Fromtlvp <mPiOsUcB.EtLlLvEp@att.net>
Date2014-08-30 01:47 -0400
Message-ID<kum8megna1pm.9uj75p1hof5x.dlg@40tude.net>
In reply to#1986
On Tue, 19 Aug 2014 19:02:35 +0200, Cecil Westerhof wrote:

> I use the following code to draw a star:
> 
> ...
> 
> ... I was wondering if
> there is a better way.

The Blue Book (PostScript Language Tutorial and Cookbook) gives a detailed
analysis of a star-drawing and -filling routine in the Tutorial's Chapter 6
(More Graphics), Section 2 (Graphics State), found on pp. 50-53 of the 1985
edition (4th printing, October 1986, ISBN-10 0-201-10179-3). Better? YMMV.

HTH. Cheers, -- tlvp
-- 
Avant de repondre, jeter la poubelle, SVP.

[toc] | [prev] | [next] | [standalone]


#1996

Fromluser- -droog <mijoryx@yahoo.com>
Date2014-08-29 23:43 -0700
Message-ID<ee857523-758a-429e-80af-57cb11f9eef2@googlegroups.com>
In reply to#1986
Here's my take on drawing stars:


/n 5 def 
/r1 10 def 
/r2 80 def 
/da 360 n div def 

300 400 translate

r2 0 moveto
n {
    da 2 div rotate
    r1 0 lineto
    da 2 div rotate
    r2 0 lineto
}
repeat
closepath
stroke

[toc] | [prev] | [next] | [standalone]


#1997

Fromluser- -droog <mijoryx@yahoo.com>
Date2014-08-30 01:23 -0700
Message-ID<28d1764a-50d5-4782-b912-78e0adffe433@googlegroups.com>
In reply to#1996
wrapped in a proc:


/starpath { % n r1 r2 x y 
    matrix currentmatrix 6 1 roll                % [] n r1 r2 x y 
    translate                                    % [] n r1 r2
    10 dict begin {/r2 /r1 /n} {exch def}forall  % []

        /da 360 n div def 

        r2 0 moveto
        n {
            da 2 div rotate
            r1 0 lineto
            da 2 div rotate
            r2 0 lineto
        }
        repeat
        closepath

    end       % []
    setmatrix %
} def 

10 30 70 300 400 starpath fill

[toc] | [prev] | [next] | [standalone]


#2001

FromTerry White <TW382@aol.com>
Date2014-09-01 09:43 +0100
Message-ID<540431c6$0$19044$c3e8da3$cc4fe22d@news.astraweb.com>
In reply to#1997
On 30/08/2014 09:23, luser- -droog wrote:
> wrapped in a proc:
>
>
> /starpath { % n r1 r2 x y
>      matrix currentmatrix 6 1 roll                % [] n r1 r2 x y
>      translate                                    % [] n r1 r2
>      10 dict begin {/r2 /r1 /n} {exch def}forall  % []
>
>          /da 360 n div def
>
>          r2 0 moveto
>          n {
>              da 2 div rotate
>              r1 0 lineto
>              da 2 div rotate
>              r2 0 lineto
>          }
>          repeat
>          closepath
>
>      end       % []
>      setmatrix %
> } def
>
> 10 30 70 300 400 starpath fill
>

Good stuff,  I've just been having some fun playing with it and I added 
an extra parameter to specify the initial rotation of the star in degrees.

But the line I don't fully understand is this one:

10 dict begin {/r2 /r1 /n} {exch def}forall

(after remembering that the % []  stuff is all just comments!)

I can see it must be looping through what is left on the stack and 
assigning the values  to the names  r2 r1 n.  But what's the 
significance of the "10 dict" at the front?



[toc] | [prev] | [next] | [standalone]


#2003

Fromluser- -droog <mijoryx@yahoo.com>
Date2014-09-01 18:20 -0700
Message-ID<59473046-adc1-4200-89f8-8cc8e0559dd7@googlegroups.com>
In reply to#2001
On Monday, September 1, 2014 3:43:46 AM UTC-5, Terry White wrote:
> On 30/08/2014 09:23, luser- -droog wrote:
> 
> > wrapped in a proc:
> >
> >
> > /starpath { % n r1 r2 x y
> >      matrix currentmatrix 6 1 roll                % [] n r1 r2 x y
> >      translate                                    % [] n r1 r2
> >      10 dict begin {/r2 /r1 /n} {exch def}forall  % []
> >
> >          /da 360 n div def
> >
> >          r2 0 moveto
> >          n {
> >              da 2 div rotate
> >              r1 0 lineto
> >              da 2 div rotate
> >              r2 0 lineto
> >          }
> >          repeat
> >          closepath
> >
> >      end       % []
> >      setmatrix %
> > } def
> >
> > 10 30 70 300 400 starpath fill
> >
> > 

> Good stuff,  I've just been having some fun playing with it and I added 
> an extra parameter to specify the initial rotation of the star in degrees.
> 
> But the line I don't fully understand is this one:
> 
> 10 dict begin {/r2 /r1 /n} {exch def}forall
> 
> (after remembering that the % []  stuff is all just comments!)
> 
> I can see it must be looping through what is left on the stack and 
> assigning the values  to the names  r2 r1 n.  But what's the 
> significance of the "10 dict" at the front?

That's a rough way of simulating local variables. `10 dict` creates
a dict with capacity for 10 elements (rule of thumb: double the expected
size needed for speedy hash lookups). `begin` pushes the dictionary on 
the dict stack, so the local name space is in effect. Then it sets r2 r1
and n variables from the stack. The `end` at the end discards the 
dictionary before the procedure terminates.

It would probably work fine for simple uses without a local namespace.
But it could cause problems if other code uses the names r2 r1 or n 
for anything else, because the values will be different after calling 
starpath.

I deliberately placed the x y values last so they could be used by
translate immediately and not need names. IIRC this approach is described
in the Green Book.

It saves and restore the current transformation matrix because, since we're
constructing a path, we can't use `gsave ... grestore` to restore everything.
Because `grestore` would discard the path as well.

I omitted stack comments for the middle, because each line is a complete
'statement'; ie. the stack picture is the same after each line. Technically,
it should have `% []` comments all the way through. But I thought it looked
ugly. But that might have made it more difficult to read, since it now looks
like: oh yeah! and there was also this secret other thing on the stack that
we've been ignoring. Sigh. decisions.

[toc] | [prev] | [next] | [standalone]


#2005

FromTerry White <TW382@aol.com>
Date2014-09-02 11:58 +0100
Message-ID<5405a2e6$0$18152$c3e8da3$3a1a2348@news.astraweb.com>
In reply to#2003
> I omitted stack comments for the middle, because each line is a complete
> 'statement'; ie. the stack picture is the same after each line. Technically,
> it should have `% []` comments all the way through. But I thought it looked
> ugly. But that might have made it more difficult to read, since it now looks
> like: oh yeah! and there was also this secret other thing on the stack that
> we've been ignoring. Sigh. decisions.
>
Thanks for the explanation.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.postscript


csiph-web