无需重复就可以选择位图中每个像素的公式 [英] formula to pick every pixel in a bitmap without repeating

查看:100
本文介绍了无需重复就可以选择位图中每个像素的公式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种算法,我现在正在快速编程,但是伪代码或任何类似的 C家族语法都可以。

I'm looking for an algorithm, I am programming in swift now but pseudocode or any reasonably similar "C family" syntax will do.

想象一个大列表值,例如位图中的像素。您想一次以视觉上随机的顺序选择一个,一次又一次不要选择相同的一个,而总是最终全部都选择一次。

Imagine a large list of values, such as pixels in a bitmap. You want to pick each one in a visually random order, one at a time, and never pick the same one twice, and always end up picking them all.

之前它是在Fractal生成器中使用的,因此它不仅可以逐行渲染,而且可以以随机方式缓慢地构建它,但是那是很久以前的Java小程序了,我不再有代码了。

I used it before in a Fractal generator so that it was not just rendering line by line, but built it up slowly in a stochastic way, but that was long ago, in a Java applet, and I no longer have the code.

我不认为它使用了任何伪随机数生成器,而我最喜欢的是它没有使渲染时间比逐行方法花费的时间更长。我看过的任何改组算法都会使渲染花费大量的时间来处理,除非我遗漏了一些东西。

I do not believe it used any pseudo-random number generator, and the main thing I liked about it is that it did not make the rendering time take longer than the just line by line approach. Any of the shuffling algorithms I looked at would make the rendering take longer with such a large number of values to deal with, unless I'm missing something.

编辑:我使用改组数组方法。当应用加载时,我会随机播放一次,而且不会花那么长时间。这是我的经销商类的代码。

I used the shuffling an array approach. I shuffle once when the app loads, and it does not take that long anyway. Here is the code for my "Dealer" class.

import Foundation
import Cocoa
import Quartz

class Dealer: NSObject
{
  //########################################################
  var deck = [(CGFloat,CGFloat)]()
  var count = 0
  //########################################################
  init(_ w:Int, _ h:Int)
  {
    super.init()
    deck.reserveCapacity((w*h)+1)
    for y in 0...h
    {
      for x in 0...w
      {
        deck.append((CGFloat(x),CGFloat(y)))
      }
    }
    self.shuffle()
  }
  //########################################################
  func shuffle()
  {
    var j:Int = 0
    let total:Int = deck.count-1
    for i:Int in 0...total
    {
      j = Int(arc4random_uniform(UInt32(total)))
      deck.swapAt(i, j)
    }
  }
  //########################################################
  func deal() -> (CGFloat,CGFloat)
  {
    let result = deck[count]
    let total:Int = deck.count-1
    if(count<total) { count=count+1 } else { count=0 }
    return(result)
  }
  //########################################################
}

该init一次被调用,它调用shuffle,但是如果需要,可以根据需要再次调用shuffle。
每次您需要卡时,您都叫Deal。

The init is called once, and it calls shuffle, but if you want you can call shuffle again if needed. Each time you need a "card" you call Deal. It loops to the beginning when the "deck" is done.

推荐答案

,如果您有足够的内存空间来存储所有像素位置,则会循环到开始处。您可以将它们洗牌:

if you got enough memory space to store all the pixel positions you can shuffle them:

const int xs=640;            // image resolution
const int ys=480;
color pixel[sz];             // image data
const int sz=xs*ys;          // image size 
int adr[sz],i,j;
for (i=0;i<sz;i++) adr[i]=i; // ordered positions
for (i=0;i<sz;i++)           // shuffle them
 {
 j = random(sz);             // pseudo-randomness with uniform distribution 
 swap(pixel[i],pixel[j]);
 }

这样,您可以确保每个像素使用一次,而且很可能全部使用他们被洗了……

this way you got guaranteed that each pixel is used once and most likely all of them are shuffled ...

这篇关于无需重复就可以选择位图中每个像素的公式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆