标签: spiral

循环循环

一位朋友需要一种算法让他循环遍历NxM矩阵的元素(N和M是奇数).我提出了一个解决方案,但我想知道我的同事是否可以提出更好的解决方案.

我发布我的解决方案作为这个问题的答案.

示例输出:

对于3x3矩阵,输出应为:

(0,0)(1,0)(1,1)(0,1)(-1,1)(-1,0)( - 1,-1)(0,-1)(1,-1) )

3x3矩阵

此外,该算法应支持非平方矩阵,因此例如对于5x3矩阵,输出应为:

(0,0)(1,0)(1,1)(0,1)(-1,1)(-1,0)( - 1,-1)(0,-1)(1,-1) )(2,-1)(2,0)(2,1)(-2,1)(-2,0)( - 2,-1)

5x3矩阵

algorithm loops matrix spiral

148
推荐指数
8
解决办法
8万
查看次数

在螺旋上绘制等距点

我需要一种算法来计算螺旋路径上的点分布.

该算法的输入参数应为:

  • 循环宽度(距离最内圈的距离)
  • 固定点之间的距离
  • 绘制的点数

绘制的螺旋是阿基米德螺旋,所获得的点必须彼此等距.

该算法应打印出单点笛卡尔坐标的序列,例如:

第1点:(0.0)第2点:( ......,......)........点N(...,...)

编程语言并不重要,非常感谢!

编辑:

我已从此站点获取并修改此示例:

    //
//
// centerX-- X origin of the spiral.
// centerY-- Y origin of the spiral.
// radius--- Distance from origin to outer arm.
// sides---- Number of points or sides along the spiral's arm.
// coils---- Number of coils or full rotations. (Positive numbers spin clockwise, negative numbers spin counter-clockwise)
// rotation- Overall rotation of the spiral. ('0'=no rotation, '1'=360 degrees, '180/360'=180 degrees) …
Run Code Online (Sandbox Code Playgroud)

algorithm algebra spiral draw

24
推荐指数
3
解决办法
1万
查看次数

在扩展的圆形螺旋中迭代2d阵列

给定一个nn矩阵M,在行i和列j,我想遍历所有的相邻值以圆形螺旋.

这样做的目的是测试一些函数,f它取决于M,找到远离(i, j)f返回的半径True.所以,f看起来像这样:

def f(x, y):
    """do stuff with x and y, and return a bool"""
Run Code Online (Sandbox Code Playgroud)

并会像这样调用:

R = numpy.zeros(M.shape, dtype=numpy.int)
# for (i, j) in M
for (radius, (cx, cy)) in circle_around(i, j):
    if not f(M[i][j], M[cx][cy]):
       R[cx][cy] = radius - 1
       break
Run Code Online (Sandbox Code Playgroud)

其中circle_around是返回功能(一个迭代)以圆形螺旋指数.因此,对于每个点M,此代码将计算并存储从f返回的那个点开始的半径True.

如果有一种更有效的计算方式R,我也会对此持开放态度.


更新:

感谢所有提交答案的人.我编写了一个简短的函数来绘制circle_around迭代器的输出,以显示它们的作用.如果您更新答案或发布新答案,则可以使用此代码验证您的解决方案. …

python geometry loops matrix spiral

23
推荐指数
3
解决办法
8291
查看次数

Code Golf:复活节螺旋

什么比复活节代码高尔夫球场的Spiral更合适?
好吧,我猜几乎一切.

挑战

字符数最短的代码显示由星号('*')组成的漂亮的ASCII螺旋.

输入是一个数字,R它将是Spiral的x大小.另一个维度(y)总是如此R-2.该程序可以假定R总是奇数并且> = 5.

一些例子:

Input
    7
Output

*******
*     *
* *** *
*   * *
***** *                   

Input
    9
Output

*********
*       *
* ***** *
* *   * *
* *** * *
*     * *
******* *

Input
   11
Output

***********
*         *
* ******* *
* *     * *
* * *** * *
* *   * * *
* ***** * *
* …
Run Code Online (Sandbox Code Playgroud)

language-agnostic code-golf spiral rosetta-stone

22
推荐指数
3
解决办法
1602
查看次数

坐标(x,y)列表用螺旋算法排序

我有一个用螺旋算法排序的坐标列表.我需要从该区域的中间开始并"触摸"任何坐标.

为了简化这一点,表示(未排序的)坐标列表(x,y在后面的图像上标有"点").

此处提供CSV坐标列表.
X从左向右
增加Y从TOP增加到BOTTOM

未排序的坐标列表 每个坐标不与下一个坐标相邻,而是由1或2个骰子(在某些情况下为更多)进行干扰.

从该区域的中心开始,我需要通过螺旋运动触摸任何坐标:

螺旋方法

解析我已经起草这个PHP算法的每个坐标:

 //$missing is an associative array having as key the coordinate "x,y" to be touched
 $direction = 'top';
 $distance = 1;
 $next = '128,127';     //starting coordinate
 $sequence = array(
     $next;
 )
 unset($missing[$next]);
 reset($missing);
 $loopcount = 0;
 while ($missing) {
    for ($loop = 1; $loop <= 2; $loop++) {
        for ($d = 1; $d <= $distance; $d++) {
            list($x,$y) = explode(",", $next);
                if ($direction == 'top')    $next = ($x) . "," . ($y …
Run Code Online (Sandbox Code Playgroud)

php sorting spiral coordinates

15
推荐指数
1
解决办法
1435
查看次数

使用ggplot&coord_polar(Condegram)的螺旋条形图

我想在阿基米德螺旋上创建一个条形图,就像这里讨论的那样.

最终目标是这样的,但不那么压倒性.

这是一个示例数据帧:

    test <- structure(list(month = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
                                     1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12), 
           year = c(2015, 2015, 2015, 2015, 2015, 2015, 2015, 
                    2015, 2015, 2015, 2015, 2015, 2016, 2016, 
                    2016, 2016, 2016, 2016, 2016, 2016, 2016, 
                    2016, 2016, 2016), 
           value = c(49, 34, 35, 34, 50, 35, 48, 50, 44, 38, 42, 
                   43, 33,30, 42, 43, 58, 55, …
Run Code Online (Sandbox Code Playgroud)

r spiral ggplot2 polar-coordinates

12
推荐指数
1
解决办法
1017
查看次数

矩阵和算法"螺旋"

我想问一下是否有一些算法准备就绪,这让我做到了这一点:我有一个矩阵m(col)xn(行)和mxn元素.我希望从中心开始给这个元素定位并旋转为螺旋,例如,对于矩阵3x3我有9个元素如此定义:

 5  6  7
 4  9  8
 3  2  1 
Run Code Online (Sandbox Code Playgroud)

或者对于una矩阵4 x 3我有12个元素,定义如下:

  8   9  10  1
  7  12  11  2
  6   5   4  3
Run Code Online (Sandbox Code Playgroud)

或者,矩阵5x2我有10个如此定义的元素:

    3  4
    7  8
   10  9
    6  5 
    2  1
Run Code Online (Sandbox Code Playgroud)

我已经解决了基本上定义mxn元素的整数数组并手动加载值的问题,但是对我来说就像那个自动生成算法的矩阵一样.感谢谁能帮我找到一些东西,非常感谢.

UPDATE

这个代码,我想做的事情,但不是在delphi中; 只是我需要从1开始而不是从0开始.重要的是它对任何matrics mx n都有效.谁帮我在delphi中翻译呢?

(defun spiral (rows columns)  
  (do ((N (* rows columns))       
    (spiral (make-array (list rows columns) :initial-element nil))      
    (dx 1) (dy 0) (x 0) (y 0)    
   (i 0 (1+ i)))     
 ((= i N) spiral)   
 (setf (aref spiral y x) i) …
Run Code Online (Sandbox Code Playgroud)

delphi algorithm matrix spiral delphi-xe2

8
推荐指数
1
解决办法
2132
查看次数

找到矩形平铺螺旋的第n个元素?

获取矩形平铺螺旋的第n个元素的算法是什么?

这是n:

[ 20  ][ 21  ][ 22  ][ 23  ][ 24  ]
[ 19  ][  6  ][  7  ][  8  ][  9  ]
[ 18  ][  5  ][  0  ][  1  ][ 10  ]
[ 17  ][  4  ][  3  ][  2  ][ 11  ]
[ 16  ][ 15  ][ 14  ][ 13  ][ 12  ]
Run Code Online (Sandbox Code Playgroud)

以下是相应的坐标n:

[-2,2 ][-1,2 ][ 0,2 ][ 1,2 ][ 2,2 ]
[-2,1 ][-1,1 ][ 0,1 ][ 1,1 ][ 2,1 ] …
Run Code Online (Sandbox Code Playgroud)

algorithm spiral

8
推荐指数
2
解决办法
3769
查看次数

求解均匀分布/均匀间隙螺旋点的算法?

首先,只是为了直观地了解我所追求的内容,这是我发现的最接近的结果(但不完全是我所追求的):

在此输入图像描述

以下是整个网站的参考:http://www.mathematische-basteleien.de/spiral.htm

但是,它并没有完全解决我所追求的问题.我想存储一个非常具体的螺旋算法的点数组.

  • 积分均匀分布
  • 360度周期有一个均匀的差距

如果我没弄错的话,前两点将是:

  • point [0] = new Point(0,0);
  • point [1] = new Point(1,0);

但是从哪里开始呢?

我想提供的唯一论据是:

  • 我希望解决的点数(数组的长度).
  • 每个点之间的距离(像素间隙).
  • 周期之间的距离.

对我来说,几乎听起来我必须计算" 螺旋周长 "(如果有这样一个术语),以便沿着螺旋线绘制均匀分布的点.

2*PI*半径可靠地使用这种计算你觉得呢?

如果之前已经完成,请显示一些代码示例!

algorithm math pseudocode spiral actionscript-3

7
推荐指数
1
解决办法
3026
查看次数

在服务器端实现Paper.js螺旋栅格示例

我试图在服务器上绘制一个螺旋栅格示例(链接)(运行Node.js).但是,我遇到的问题是我的路径没有显示在导出的帧上,我只能看到下载的图像.可能是我身边的一个愚蠢的错误,但是,通过谷歌和文档查看的日子并没有帮助我解决问题.

我做了什么:

  • 添加paper.前缀
  • 将+/-更改为相应的add()subtract()
  • 尝试在图层中显示路径.

这是我的代码:

var paper = require('paper');
var fs = require('fs');
var drawer = {};

var canvas = new paper.Canvas(1000, 1000);
paper.setup(canvas);

var layer = paper.project.activeLayer;

drawer.drawSpiral = function(url, filename, fn) {   
    var url = 'http://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png';
    var raster = new paper.Raster(url);

    raster.onLoad = function() {
        raster.fitBounds(paper.view.bounds);

        var position = paper.view.center;
        var count = 0;
        var max = Math.min(raster.bounds.width, raster.bounds.height) * 0.5;

        var path = new paper.Path();
        path.fillColor = 'black';
        path.closed …
Run Code Online (Sandbox Code Playgroud)

javascript raster spiral node.js paperjs

7
推荐指数
1
解决办法
1685
查看次数