在位图中选取每个像素而不重复的公式 [英] formula to pick every pixel in a bitmap without repeating

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

问题描述

我正在寻找一种算法,我现在正在 swift 中编程,但伪代码或任何相当相似的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天全站免登陆