2013-04-14 41 views
9

在postscript中,roll运算符非常一般,难以可视化。你如何确保你朝着正确的方向滚动?你如何保持'卷'操作员直?

我希望得到一个坚实的手柄上roll,因为我希望能够使用变量

/f { % x y z 
    /z exch def 
    /y exch def 
    /x exch def 
    x dup mul 
    y dup mul 
    z dup mul add add % x^2+y^2+z^2 
} def 

转变功能为使用堆栈操作,功能更像

/f { % x y z 
    3 1 roll dup mul % y z x^2 
    3 1 roll dup mul % z x^2 y^2 
    3 1 roll dup mul % x^2 y^2 z^2 
    add add % x^2+y^2+z^2 
} def 

/f { % x y z 
    3 { 3 1 roll dup mul } repeat 
    2 { add } repeat  % x^2+y^2+z^2 
} bind def 

这些应该通过减少名称查找(哈希表搜索)。

随着roll我总是要测试它;我通常会在第一次尝试时发现错误!我用exch很好,但

+0

这个问题是为了激发答案(分享你的知识)。任何有待改进的建议都是热切期盼的。 –

回答

9

我很长时间没有滚动。我记得现在使用这些方法,这些都是等价的:

韵(-ish)

NJ

  • 积极Ĵ,以滚远

    7 8 9 3 1 roll 
    % 9 7 8

  • 负,拿回来(或 “negateeve,以再取回”)

    % 9 7 8 
    3 -1 roll 
    % 7 8 9


堆栈(的东西)

也许一个更好的办法来想它是一个物理堆栈 (例如书籍),所以堆栈的顶部字面上是“在顶部”。

然后积极的卷上升:

 for j number of times 
    pick up n books 
    put the top one on the bottom (shifting the substack "up") 
    put them back down

和负滚下山:

 for j number of times 
    pick up n books 
    put the bottom one on top (shifting the substack "down") 
    put them back down

横盘

但我通常想象堆栈横盘整理,道路对象 将作为一系列文字查看文件。所以我认为 积极的一面,因为它隐藏了第n个 事物背后的前j个事物;和负面卷作为钩起j事情开始 第n件事情。给与承担。

客场。

n j roll 

__ j > 0 __  move top j elements to the bottom of n 

n   TOS 
    -------------| 
|  j  | 
|  -----| 
|  |  | 
V  V  | 

a b c d e f g h 

^  |  | 
|  |-------| 
^   | 
-<-<-<-<-<- 
    move 

然后回来。

__ j < 0 __ move j elements from the bottom of n to the top 

n   TOS 
    -------------| 
|  j  | 
|-----  | 
|  |  | 
V  V  | 

a b c d e f g h 

|  |  ^
|-------|  | 
    |   ^
    ->->->->->- 
     move 

棉绒滚筒

又一方式是侧身图片它,并铺在顶部粘性轮(棉绒辊,也许)

(a) (b) (c) (d) (e) 5 3 roll 

      _______ 
     /  \ 
      | 3 | 
      |/| \ | 
      \_______/ 
(a) (b) (c) (d) (e)

然后一个正像卷轴逆时针旋转 就像弧形和旋转一样。

  _______ (e) 
    / /\ 
     | 3 --| (d) 
     |  \ | 
     \_______/ (c) 
(a) (b) 


    (e)__(d)__(c) 
    /\ | /\ 
    | 3 | 
    |  | 
    \_______/ 
    (a) (b) 


    (c)_______ 
    /\  \ 
(d) |-- 3 | 
    |/  | 
    \_______/ 
    (e) (a) (b) 


    _______ 
/  \ 
    | 3 | 
    |/| \ | 
    \_______/ 
(c) (d) (e) (a) (b)

和负滚去顺时针 喜欢的ARCn和负旋转。

 _______ 
/  \ 
    | 3 | 
    |/| \ | 
    \_______/ 
(a) (b) (c) (d) (e) 


    (a)_______ 
    /\  \ 
(b) |-- 3 | 
    |/  | 
    \_______/ 
    (c)  (d) (e) 


    (c)__(b)__(a) 
    /\ | /\ 
    | 3 | 
    |  | 
    \_______/ 
    (d) (e) 

     _______ (c) 
    / /\ 
     | 3 --| (b) 
     |  \ | 
     \_______/ (a) 
(d) (e) 

      _______ 
     /  \ 
      | 3 | 
      |/| \ | 
      \_______/ 
(d) (e) (a) (b) (c)

消除负

它不应该是不难看出,负轧辊是完全不必要的,因为如果j < 0,可以用n-J所取代。例如。

3 -1 roll % roll bottom 1 element from 3 to the top 
3 2 roll % roll top 2 elements behind the 3rd 

是一样的。

16 -4 roll % roll bottom 4 elements from 16 to the top 
16 12 roll % roll top 12 elements behind the 16th 

是一样的。


这导致了最终的,最终的简化视图(虽然上面的每一个都可以)。

卷只是一个大交换

你真的只是交换顶部Ĵ元素与下面的正Ĵ元素。

假设你有一个堆栈(其中$ TOS $标记堆栈的顶部)上的这个烂摊子,并要适当地订购吧:

g h i j k l m n o p q r s t u v w x y z a b c d e f $TOS$ 

数上升(下降)为ñj

g h i j k l m n o p q r s t u v w x y z a b c d e f 
26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 
|               | j = 6 . . . . 
| n = 26 . . . . . . . . . . . . . . . . . . . . . . . 

> 26 6 roll pstack 

a b c d e f g h i j k l m n o p q r s t u v w x y z 

Ĵ简单地定位从Ñ元件之间划分相对于最深元件行(它从下面计数)的负值。

t u v w x y z a b c d e f g h i j k l m n o p q r s 
26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 
. . . . j = -7 |              | 
. . . . . . . . . . . . . . . . . . . . . . . n = 26 | 

> 26 -7 roll pstack 

a b c d e f g h i j k l m n o p q r s t u v w x y z 

这里是一个方便的功能,让一个接口滚这是更紧密地类似于大交换视图。

% r0..rN s0..sM N M swap s0..sM r0..rN 
% a gentler interface to the power of roll 
/swap { 
    exch 1 index add exch 
    roll 
} def 
0 1 2 3 /a /b /c 4 3 swap pstack 

输出:

GPL Ghostscript 8.62 (2008-02-29) 
Copyright (C) 2008 Artifex Software, Inc. All rights reserved. 
This software comes with NO WARRANTY: see the file PUBLIC for details. 
3 
2 
1 
0 
/c 
/b 
/a 
GS<7>GS<7> 
+0

这是从我以前在[comp.lang.postscript](https://groups.google.com/d/msg/comp.lang.postscript/gCGb5UQAmac/TsQOBNVvFKQJ)中发布的资料中编译的 –

+0

如果您已阅读所有通过试图读取/ xpose函数[这里](http://stackoverflow.com/a/14600918/733077)做一个* flattened * 4x4矩阵的转置来测试你的理解。这个函数可以考虑将二维数组中的所有数字和'{aload pop roll} forall'中的所有数字都考虑进去,但是之后无处可以写下评论。 :) –

1

简单的说:如果你做一些圆形物体滚动,从你的距离可能比之前之后更大,除非你用别的东西比你的手(例如:一个魔法棒......),使物体滚动。

5 1 roll意味着堆栈顶部的对象将在距离堆栈顶部较远的位置。

10 11 12 13 14 15 16 17 18 19 20 5 1 roll 

10 11 12 13 14 15 20 16 17 18 19 

你看20已经较深,和最后五个元素改变位置相同。

5 in 5 1 roll意味着只有来自堆栈的五个第一个对象都可以改变位置。

-145模相同,因此很容易记住负数在使用此解决方案之前必须修改为正数。