代码之家  ›  专栏  ›  技术社区  ›  Victor Ciobanu

如何处理随机值以获得xy坐标?

  •  0
  • Victor Ciobanu  · 技术社区  · 7 年前

    所以我试着用PIL绘制一些RGB值来创建一个图像。

    我用于绘图的代码如下所示:

    from PIL import Image, ImageDraw
    
    class DrawDisplay(object):
        def __init__(self, size_x, size_y):
            self.size_x = size_x                                       #Length of the image
            self.size_y = size_y                                       #Width of the image
            self.img = Image.new("RGB", (self.size_x, self.size_y))    #Creates the drawing canvas
        def plot(self, xy, colour):
            d = ImageDraw.Draw(self.img)
            d.point(xy, colour)                                        #xy is co-ordinate in form of tuple, colour is rgb value in form of tuple
        def write(self, filename):
            self.img.save(filename)                                    #Saves image with the variable filename as the name
    

    我知道上面的代码没有问题,因为我已经用以下工具对其进行了测试:

    dd = DrawDisplay(64, 64)                                           #Utilises the drawing class
    for i in range(64):
        for j in range(64):
            r = i * 4    #Pretty colours
            g = j * 4
            b = 0
            dd.plot((i, j),(r, g, b))                                  #Uses method from earlier with i and j as x and y
    dd.write("testname.png")                                           #Saves the image as testname
    

    如果我点击testname,我会得到一个漂亮的图像。我的文件夹中的png

    Like this

    现在假设我有一个无序数组,每个数字直到64^2或4096:

    my_list = shuffle(range(4096))
    

    现在我需要帮助,如何处理 my_list 使用公式/函数获取xy坐标元组?

    以下是查看此问题的另一种方式:

    想象一个4x4网格。如果我在这个函数中加1,我的输出将是(1,1)(或者(0,0),如果你想让这个问题更简单。)

      1 2 3 4    #Visualised
    1 1 - - -
    2 - - - -
    3 - - - -
    4 - - - -
    

    16作为输入输出(4,4)(或(3,3)):

      1 2 3 4
    1 - - - -
    2 - - - -
    3 - - - -
    4 - - - 16
    

    和7个输出(3,2)(或(2,1)):

      1 2 3 4
    1 - - - -
    2 - - 7 -
    3 - - - -
    4 - - - -
    

    我的公式/函数是什么?

    3 回复  |  直到 7 年前
        1
  •  0
  •   shriyan    7 年前

    如果您有各种网格大小,那么这会有所帮助。这不是最优雅的,但很管用

    def fun(val, grid):
        pos = []
        for val in lst:
            x = val//grid[1]
            r = val%grid[1]
            if(r==0):
                pos.append([x, grid[1]])
            else:
                pos.append([x+1, val-(x*grid[1]])
       return pos
    
        2
  •  0
  •   kittel    7 年前

    我认为应该这样做:

    def idx2point(idx):
       return (idx/64,idx%64)
    

    如果要从1而不是0开始计数,可以将1添加到每个坐标。

        3
  •  0
  •   Olivier CAYROL    7 年前

    我还有一个计算坐标的函数:

    from math import floor, sqrt
    
    def f(num):
        a = floor(sqrt(num))
        if a**2 == num:
            return (a, a)
        b = (num - a**2 + 1) // 2
        if num % 2 == a % 2:
            return (b, a+1)
        else:
            return (a+1, b)
    

    这将产生以下结果:

    f(1)
    >>> (1, 1)
    f(9)
    >>> (3, 3)
    f(10)
    >>> (4, 1)
    f(11)
    >>> (1, 4)
    f(12)
    >>> (4, 2)