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


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

I like some feedback on my tetrafoil function

Started byCecil Westerhof <Cecil@decebal.nl>
First post2013-08-01 15:15 +0200
Last post2013-10-29 02:30 -0700
Articles 4 — 2 participants

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


Contents

  I like some feedback on my tetrafoil function Cecil Westerhof <Cecil@decebal.nl> - 2013-08-01 15:15 +0200
    Re: I like some feedback on my tetrafoil function luser- -droog <mijoryx@yahoo.com> - 2013-08-01 20:40 -0700
      Re: I like some feedback on my tetrafoil function Cecil Westerhof <Cecil@decebal.nl> - 2013-08-02 11:06 +0200
        Re: I like some feedback on my tetrafoil function luser- -droog <mijoryx@yahoo.com> - 2013-10-29 02:30 -0700

#1554 — I like some feedback on my tetrafoil function

FromCecil Westerhof <Cecil@decebal.nl>
Date2013-08-01 15:15 +0200
SubjectI like some feedback on my tetrafoil function
Message-ID<87haf98sc1.fsf@Aspire.decebal.nl>
At closer inspection I saw that two figures where the ‘same’:
tetrafoil and “clover” I was asking about in a previous post.

I now made a function to draw both:
    % centerX centerY width [[[outerBox] innerCircle] innerCross]  drawClover  -
    /drawTetrafoil {
        10 dict begin
            dup type /booleantype eq {
                /innerCross exch def
                dup type /booleantype eq {
                    /innerCircle exch def
                    dup type /booleantype eq {
                        /outerBox exch def
                    } {
                        /outerBox false def
                    } ifelse
                } {
                    /innerCircle false def
                    /outerBox    false def
                } ifelse
            } {
                /innerCross  false def
                /innerCircle false def
                /outerBox    false def
            } ifelse
    
            /width    exch def
            /centerY  exch def
            /centerX  exch def
    
            /circleRadius width 4 div def
    
            gsave
                newpath
                centerX  centerY  translate
                4 {
                    innerCross {
                        0  0  moveto
                        0  circleRadius  rlineto
                    } if
                    circleRadius neg  circleRadius  circleRadius  0  270  arc
                    90 rotate
                } repeat
                stroke
                innerCircle {
                    0  0  circleRadius  0  360  arc
                    stroke
                } if
                outerBox {
                    width -.5 mul dup
                    width dup
                    rectstroke
                } if
            grestore
        end
    } def

I have a few questions about this.

Is the manner I work with optional parameters the way to do it, or are
there better ways? (I think it the most likely I want to draw the
inner cross, and the least likely I want to draw the outer box.)

Something is wrong in my function. If I call it with:
    235 100 100 true true false drawTetrafoil

The inner circle is correct at North, but in West, South and East the
inner ‘arms’ of the tetrafoil go through the inner circle. What is
going wrong?

I want to rotate the tetrafoil, but in such a way that it stays in the
outer box. What is the best way to get this done?

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

[toc] | [next] | [standalone]


#1563

Fromluser- -droog <mijoryx@yahoo.com>
Date2013-08-01 20:40 -0700
Message-ID<67f824cb-26ed-4370-b521-4848c329ab0a@googlegroups.com>
In reply to#1554
On Thursday, August 1, 2013 8:15:58 AM UTC-5, Cecil Westerhof wrote:
> At closer inspection I saw that two figures where the ‘same’:
> 
> tetrafoil and “clover” I was asking about in a previous post.
> 
> I now made a function to draw both:
<snip> 
> 
> I have a few questions about this.
> 
> Is the manner I work with optional parameters the way to do it, or are
> there better ways? (I think it the most likely I want to draw the
> inner cross, and the least likely I want to draw the outer box.)
> 

I think the way you're doing it is as good as any, from the technical side.
But from the programming side, you're encoding lots of power into the 
function, but you're not really abstracting away any of the details.

I would tend to make a fully-general function, with all parameters
required. And then make a few convenience functions that accept
just a few necessary parameters. Optional parameters are just weird;
no matter how you do it. (IMHO)

> 
> Something is wrong in my function. If I call it with:
> 
>     235 100 100 true true false drawTetrafoil
> 
> 
> 
> The inner circle is correct at North, but in West, South and East the
> 
> inner ‘arms’ of the tetrafoil go through the inner circle. What is
> 
> going wrong?
> 

It's the mitered line join. Either use `setlinejoin` or `setmiterlimit`

Adding

  2 setlinejoin

before the call, fixes the output.

> 
> I want to rotate the tetrafoil, but in such a way that it stays in the
> 
> outer box. What is the best way to get this done?
> 

You can use `pathbbox` for this. It returns bounding-box coordinates for
the current path in the current user space. Make sure to `flattenpath` 
first, to tighten-up against the curves (otherwise, it uses the outer
control points from the curve segments, which you don't want here).

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


#1564

FromCecil Westerhof <Cecil@decebal.nl>
Date2013-08-02 11:06 +0200
Message-ID<87y58k7986.fsf@Aspire.decebal.nl>
In reply to#1563
Op vrijdag 2 aug 2013 05:40 CEST schreef luser:

>> I have a few questions about this.
>>
>> Is the manner I work with optional parameters the way to do it, or
>> are there better ways? (I think it the most likely I want to draw
>> the inner cross, and the least likely I want to draw the outer
>> box.)
>>
>
> I think the way you're doing it is as good as any, from the
> technical side. But from the programming side, you're encoding lots
> of power into the function, but you're not really abstracting away
> any of the details.
>
> I would tend to make a fully-general function, with all parameters
> required. And then make a few convenience functions that accept
> just a few necessary parameters. Optional parameters are just weird;
> no matter how you do it. (IMHO)

For the convenience functions I kept outerbox optional. But one
optional parameter is not to bad I think. I also made a filled
version:
    % centerX centerY width outerBox innerCircle innerCross  drawTetrafoil  -
    /drawTetrafoil {
        gsave
            newpath
            drawTetrafoilRaw
            stroke
        grestore
    } def
    
    % centerX centerY width [outerBox]  drawTetrafoilAll  -
    /drawTetrafoilAll {
        dup type /booleantype ne {
            false
        } if
        true true
        drawTetrafoil
    } def
    
    % centerX centerY width [outerBox]  drawTetrafoilCrossed  -
    /drawTetrafoilCrossed {
        dup type /booleantype ne {
            false
        } if
        false true
        drawTetrafoil
    } def
    
    % centerX centerY width [outerBox]  drawTetrafoilEmpty  -
    /drawTetrafoilEmpty {
        dup type /booleantype ne {
            false
        } if
        false false
        drawTetrafoil
    } def
    
    % centerX centerY width [outerBox]  drawTetrafoilFilled  -
    /drawTetrafoilFilled {
        dup type /booleantype ne {
            false
        } if
        false false
        gsave
            newpath
            drawTetrafoilRaw
            fill
        grestore
    } def
    
    % centerX centerY width [outerBox]  drawTetrafoilInnerCircle  -
    /drawTetrafoilInnerCircle {
        dup type /booleantype ne {
            false
        } if
        true false
        drawTetrafoil
    } def
    
    % centerX centerY width outerBox innerCircle innerCross  drawTetrafoilRaw  -
    /drawTetrafoilRaw {
        10 dict begin
            /innerCross exch def
            /innerCircle exch def
            /outerBox exch def
            /width    exch def
            /centerY  exch def
            /centerX  exch def
    
            /circleRadius width 4 div def
    
            centerX  centerY  translate
            outerBox {
                width -.5 mul dup
                width dup
                rectstroke
            } if
            innerCircle {
                0  0  circleRadius  0  360  arc
                stroke
            } if
            2 setlinejoin
            4 {
                innerCross {
                    0  0  moveto
                    0  circleRadius  rlineto
                } if
                circleRadius neg  circleRadius  circleRadius  0  270  arc
                90 rotate
            } repeat
            closepath
        end
    } def


>> Something is wrong in my function. If I call it with:
>>
>> 235 100 100 true true false drawTetrafoil
>>
>>
>>
>> The inner circle is correct at North, but in West, South and East
>> the
>>
>> inner ‘arms’ of the tetrafoil go through the inner circle. What is
>>
>> going wrong?
>>
>
> It's the mitered line join. Either use `setlinejoin` or
> `setmiterlimit`
>
> Adding
>
> 2 setlinejoin
>
> before the call, fixes the output.

Done.


>> I want to rotate the tetrafoil, but in such a way that it stays in
>> the
>>
>> outer box. What is the best way to get this done?
>>
>
> You can use `pathbbox` for this. It returns bounding-box coordinates
> for the current path in the current user space. Make sure to
> `flattenpath` first, to tighten-up against the curves (otherwise, it
> uses the outer control points from the curve segments, which you
> don't want here).

Maybe I understand you wrong, But I already know the bounding box (I
draw him). What I mean is the following: Now the first ‘leaf’ is in
the North-East corner. I want him for example also be at North. That
would be simple I think. I need to divide circleRadius by ‘2 sqrt’.
But what about the interleaving rotations?

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

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


#1717

Fromluser- -droog <mijoryx@yahoo.com>
Date2013-10-29 02:30 -0700
Message-ID<f35aca47-52c9-4ded-ac65-155110a3b4aa@googlegroups.com>
In reply to#1564
On Friday, August 2, 2013 4:06:17 AM UTC-5, Cecil Westerhof wrote:
> Op vrijdag 2 aug 2013 05:40 CEST schreef luser:
> 
> 
> 
> 
> >> I want to rotate the tetrafoil, but in such a way that it stays in
> >> the
> >> outer box. What is the best way to get this done?
> >>
> >
> > You can use `pathbbox` for this. It returns bounding-box coordinates
> > for the current path in the current user space. Make sure to
> > `flattenpath` first, to tighten-up against the curves (otherwise, it
> > uses the outer control points from the curve segments, which you
> > don't want here).
> 
> 
> Maybe I understand you wrong, But I already know the bounding box (I
> draw him). What I mean is the following: Now the first ‘leaf’ is in
> the North-East corner. I want him for example also be at North. That
> would be simple I think. I need to divide circleRadius by ‘2 sqrt’.
> 
> But what about the interleaving rotations?
> 

As long as the center is correct, `90 rotate` will change the 
orientation, but keep inside the box. ... Oh. NOW I see.
Yes `2 sqrt div dup scale` should do it. 

[toc] | [prev] | [standalone]


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


csiph-web