2

我目前正在为 Postscript 编译器编写徽标。我的 PS 输出代码似乎无效。任何想法可能是什么问题?或者 LOGO 的实际 PostScript 版本应该是什么样子?

LOGO输入码

PROC LDRAGON ( LEVEL )
    IF LEVEL == 0 THEN
    FORWARD 5 
    ELSE
    LDRAGON ( LEVEL - 1 )
    LEFT 90
    RDRAGON ( LEVEL - 1 )
    ENDIF 


PROC RDRAGON ( LEVEL )
    IF LEVEL == 0 THEN
    FORWARD 5 
    ELSE
    LDRAGON ( LEVEL - 1 ) 
    RIGHT 90
    RDRAGON ( LEVEL - 1 )
    ENDIF 

PROC MAIN (VOID)
   LDRAGON ( 11 )

我的编译器中的代码。

%!PS-Adobe-3.0
/Xpos { 300 } def showpage
/Ypos { 500 } def
/Heading { 0 } def
/Arg { 0 } def
/Right {
Heading exch add Trueheading
/Heading exch def
} def
/Left {
Heading exch sub Trueheading
/Heading exch def
} defп
/Trueheading {
360 mod dup
0 lt { 360 add } if
} def
/Forward {
dup Heading sin mul
exch Heading cos mul
2 copy Newposition
rlineto
} def
/Newposition {
Heading 180 gt Heading 360 lt
and { neg } if exch
Heading 90 gt Heading 270 lt
and { neg } if exch
Ypos add /Ypos exch def
Xpos add /Xpos exch def
} def
/LEVEL { 11 } def
/LDRAGON{
LEVEL
0
eq
{
5 FORWARD }{
LEVEL
1
1
sub
LDRAGON
90
LEFT
LEVEL
1
sub
RDRAGON
} ifelse
} def
/MAIN {
11
LDRAGON
} def
Xpos Ypos moveto
MAIN
stroke
showpage
4

1 回答 1

3

第一个问题是开头的评论行。该Adobe-3.0部分不是代码使用的 Postscript 版本,而是文件符合的 Document Structuring Conventions 版本。由于您根本没有使用任何 DSC 注释,因此第一行应该是%!PS或只是%!.

接下来,大多数行的左列中都有乱码。我猜这是 TAB 字符的编码,但它不是 ASCII 制表符。最安全的策略是始终使用空格进行缩进。

showpage运算符发出当前页面的输出。几乎可以肯定它应该在结尾,而不是开头。...哦,我看到它也在底部。顶部的那个应该被删除。

接下来我看到的(虽然在技术上不是问题)是加法是可交换的。所以exch add总是可以简化为add

Left: defnshould be的定义末尾有一个错字def

Heading 180 gt Heading 360 lt and总是假的。也许你打算or?...实际上我认为这部分根本没有必要。Postscript 的三角函数为所有象限产生适当的符号值。

这部分看起来它有太多1的s:

LEVEL
1
1
sub
LDRAGON

并且RDRAGON没有定义。虽然由于函数是相同的,但您可以重用相同的函数体。/RDRAGON /LDRAGON load def


如果函数LEVEL中的名称LDRAGON应该引用函数的参数,那么它必须显式定义。并且它需要定义一个本地命名空间,因此它不会覆盖同一变量的其他实例。

/LDRAGON{
    1 dict begin
    /LEVEL exch def
    %...
    end

现在我们有了本地字典,重新定义一个“全局”变量(如HeadingXposYpos)应该使用store而不是def.


Postscript 区分大小写,因此FORWARDForward2 个不同的名称。


更正的 Postscript 程序:

%!
%(debug.ps/db5.ps)run traceon stepon currentfile cvx debug
/Xpos { 300 } def
/Ypos { 500 } def
/Heading { 0 } def
/Arg { 0 } def
/Right {
    Heading add Trueheading
    /Heading exch store
} def
/Left {
    Heading exch sub Trueheading
    /Heading exch store
} def
/Trueheading {
    360 mod dup
    0 lt { 360 add } if
} def
/Forward {
    dup Heading sin mul
    exch Heading cos mul
    2 copy Newposition
    rlineto
} def
/Newposition {
    Heading 180 gt Heading 360 lt
    and { neg } if
    exch
    Heading 90 gt Heading 270 lt
    and { neg } if exch
    Ypos add /Ypos exch store
    Xpos add /Xpos exch store
} def
/LEVEL { 11 } def
/LDRAGON{
    1 dict begin
    /LEVEL exch def
    LEVEL
    0
    eq
    {
        5 Forward }{
        LEVEL
        1
        sub
        LDRAGON
        90
        Left
        LEVEL
        1
        sub
        RDRAGON
    } ifelse
    end
} def
/RDRAGON{
    1 dict begin
    /LEVEL exch def
    LEVEL
    0
    eq
    {
        5 Forward }{
        LEVEL
        1
        sub
        LDRAGON
        90
        Right
        LEVEL
        1
        sub
        RDRAGON
    } ifelse
    end
} def
/MAIN {
    11
    LDRAGON
} def
Xpos Ypos moveto
MAIN
stroke
showpage
于 2014-03-18T04:34:16.023 回答