Почему этот постскрипт код ничего не рисует? - PullRequest
1 голос
/ 03 февраля 2020
%!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

Почему это не работает? Я пытаюсь запустить его с помощью программы просмотра PDF по умолчанию из ОС ma c, и он говорит, что не смог преобразовать этот файл 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-системы.

/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

Обычно предполагается, что нарисовать гексамазу.

1 Ответ

1 голос
/ 03 февраля 2020
/T: move 

Похоже, здесь дополнительный пробел заставляет интерпретатор попытаться выполнить move, который не определен. Та же проблема еще ниже:

/L: rnd 

Не должно быть пробела при определении этих имен в первый раз (и не должно быть пробела позже при попытке их вызвать).

Другая возможная проблема, которую я вижу это realtime. По моему опыту интерпретаторы (такие как ghostscript) обычно не дают полезного результата от этого оператора. В некоторых ситуациях, таких как Unix системы, вы можете прочитать из /dev/urandom, чтобы получить хорошее начальное значение. Но, конечно, вы не можете сделать это в EPS, где файловые операторы запрещены.

Если вам интересно, я написал некоторый PS-код для L-Systems, когда-нибудь go ссылка . И еще на Codegolf .

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...