【问题标题】:Why is this postscript code not drawing anything?为什么这个后记代码没有绘制任何东西?
【发布时间】:2020-02-03 01:28:15
【问题描述】:
%!PS-Adobe-3.0 EPSF-3.0
%%Title: L-System
%%Creator: (me)
%%BoundingBox: 0 0 500 500 
%%EndComments

%
% %% BeginProlog ... %% EndProlog are document structuring comments
% to give definitions of new "resources" (operators, fonts, etc.)


%
% %% BeginResource ... %% EndResource encloses a resource
% as a set of operators (procset).
%

%%BeginProlog
%%BeginResource: procset (graphisme tortue) 1.0 0

% --------- definition of operations for turtle graphics
%
% The coordinate system follows the movements and turns of the turtle,
% so that the tourtue is always at the origin with the next rotates
% towards the X axis.
%
% Operations:
% T: move advances the turtle
% T: draw advances the turtle by drawing a line between the start and finish points
% T: turn turns the turtle's nose

/T: move% d T: move -
% advances the turtle by d (number --- positive, negative or 0)
{
    0 translate % put the origin at this new position
} def
/T:draw % d T: draw -
% advance the turtle by d drawing a line
{
    newpath 
    0 0 moveto
    0 lineto
    currentpoint 
    stroke 
    translate 
} def

/T:turn % angle T: turn -
% turns the turtle's nose by the angle (in degrees)
{
    rotate
} def

/T:init % x y angle T: init -
% initial state of the turtle
{   
    3 1 roll translate
    rotate 
} def

%%EndResource

%%BeginResource: procset (random rule) 1.0 0

realtime srand % random seed --- during tests, use a fixed value (e.g., 30 rand) if necessary for repeatability

/L: rnd % [op1 op2 ...] L.rnd -
% chooses an operator at random and executes it
% op1, op2 etc are names (start with /)
{
    rand % random number between 0 and 2 ^ 31-1
    1 index length % length of the array
    mod % random number between 0 and len-1
    get
    cvx % conversion to executable
    exec % execute
} def

%%EndResource


% ---------------- BEGIN

/L:d 1.5 def
/L:a 60 def
/L
{
    dup 0 eq 
    {
     L:d T:draw
     pop
    }{
        1 sub
        dup L
        L:a T:turn
        dup R
        L:a T:turn
        L:a T:turn
        dup R
        L:a neg T:turn
        dup L
        L:a neg T:turn
        L:a neg T:turn
        dup L
        L
        L:a neg T:turn
        dup R
        L:a T:turn
    }ifelse 
}def
/R
{
    dup 0 eq 
    {
     L:d T:draw
     pop
    }{
        1 sub
        L:a neg T:turn
        dup L
        L:a T:turn
        dup R
        dup R
        L:a T:turn
        L:a T:turn
        dup R
        L:a T:turn
        dup L
        L:a neg T:turn
        L:a neg T:turn
        dup L
        L:a neg T:turn
        R
    }ifelse 
}def
/omega
{
    L
} def

%%EndResource
%%EndProlog
500 500
90
T:init

3
omega
%%EOF

为什么它不起作用?我尝试使用 mac OS 的默认 pdf 查看器运行它,它说它无法将此 eps 文件转换为 pdf(通常可以)。

这段代码分为三部分,第一部分是定义海龟操作的地方。我们希望这个文件允许我们在屏幕上绘制东西。

/T: move% d T: move -
% advances the turtle by d (number --- positive, negative or 0)
{
    0 translate % put the origin at this new position
} def
/T:draw % d T: draw -
% advance the turtle by d drawing a line
{
    newpath 
    0 0 moveto
    0 lineto
    currentpoint 
    stroke 
    translate 
} def

/T:turn % angle T: turn -
% turns the turtle's nose by the angle (in degrees)
{
    rotate
} def

/T:init % x y angle T: init -
% initial state of the turtle
{   
    3 1 roll translate
    rotate 
} def

%%EndResource

%%BeginResource: procset (random rule) 1.0 0

realtime srand % random seed --- during tests, use a fixed value (e.g., 30 rand) if necessary for repeatability

/L: rnd % [op1 op2 ...] L.rnd -
% chooses an operator at random and executes it
% op1, op2 etc are names (start with /)
{
    rand % random number between 0 and 2 ^ 31-1
    1 index length % length of the array
    mod % random number between 0 and len-1
    get
    cvx % conversion to executable
    exec % execute
} def

%%EndResource

这个文件的第二部分是我指定要绘制的内容的地方。这是基于指定 L-System 的规则。

/L:d 1.5 def
/L:a 60 def
/L
{
    dup 0 eq 
    {
     L:d T:draw
     pop
    }{
        1 sub
        dup L
        L:a T:turn
        dup R
        L:a T:turn
        L:a T:turn
        dup R
        L:a neg T:turn
        dup L
        L:a neg T:turn
        L:a neg T:turn
        dup L
        L
        L:a neg T:turn
        dup R
        L:a T:turn
    }ifelse 
}def
/R
{
    dup 0 eq 
    {
     L:d T:draw
     pop
    }{
        1 sub
        L:a neg T:turn
        dup L
        L:a T:turn
        dup R
        dup R
        L:a T:turn
        L:a T:turn
        dup R
        L:a T:turn
        dup L
        L:a neg T:turn
        L:a neg T:turn
        dup L
        L:a neg T:turn
        R
    }ifelse 
}def
/omega
{
    L
} def

文件的第三部分是我们要求绘制的地方

500 500
90
T:init

3
omega
%%EOF

通常应该画一个六角形。

【问题讨论】:

    标签: postscript l-systems


    【解决方案1】:
    /T: move 
    

    这里似乎有一个额外的空间导致解释器尝试执行未定义的move。同样的问题更远:

    /L: rnd 
    

    第一次定义这些名称时不应有空格(以后尝试调用它们时也不应有空格)。

    我看到的另一个可能的问题是realtime。根据我的经验,解释器(如 ghostscript)通常不会从该运算符中给出有用的结果。在某些情况下,例如 Unix 系统,您可以阅读 /dev/urandom 以获得良好的种子。但当然你不能在禁止文件操作符的 EPS 中这样做。

    如果你有兴趣,我前段时间写了一些 PS 代码来做 L-Systems link。更多内容请关注codegolf

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-10
      • 2017-12-20
      • 1970-01-01
      • 1970-01-01
      • 2022-11-15
      • 2021-04-02
      相关资源
      最近更新 更多