SWIFT – formula for selecting each pixel in the bitmap

I’m looking for an algorithm, I’m currently programming fast but pseudo code or any similar “C series” syntax will work.

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 choose the same two, and always pick them in the end.

I used it in a fractal generator before, so it was not only rendered line by line, but slowly built in a random manner, but a long time ago, in a Java applet, I no longer had the code.

I don’t believe it uses any pseudo-random number generator, the main reason I like it is that it does not make the rendering time take longer than the straight line by line method. Any shuffling algorithm I have seen will make the rendering take longer Time to process so many values, unless I am missing something.

Edit: I used the shuffle array method. I will shuffle the cards once when the application loads, and it won’t take that anyway. Long time. This is the code of my “Reseller” course.

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 return(result)
}
//############## ########################################
}

init is called once, it calls shuffle, but if you want, you can call shuffle again when you need it.
Whenever you need a “card”, please call Deal. When the “deck” is complete, It loops to the beginning.

If you have enough memory space to store all pixel positions, you can Play them randomly:

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;ifor (i=0;i {
j = random(sz); // pseudo-randomness with uniform distribution
swap(pixel[i], pixel[j]);
}

In this way, you can ensure that each pixel is used once, and most likely all pixels are shuffled…

I’m looking for an algorithm, I’m programming fast now but pseudo code or any similar “C series” syntax will do.

Imagine a large number of A 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 choose the same two, and always pick them in the end.

I I used it in the fractal generator before, so it was not only rendered line by line, but slowly built in a random manner, but a long time ago, in Java applets, I no longer have the code.

I I don’t believe it uses any pseudo-random number generator. The main reason I like it is that it doesn’t make the rendering time longer than the straight line-by-line method. Any shuffling algorithm I have seen will make the rendering take longer. Handling so many values, unless I missed something.

Edit: I used the shuffle array method. I shuffle the cards once when the application loads, and it won’t take that long anyway. .This is the code of my “Reseller” course.

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 return(result)
}
//################ ######################################
}

init is called once, it calls shuffle, but if you want, you can call shuffle again when needed.< br>Whenever you need a “card”, please call Deal. When the “deck” is completed, it loops to the beginning.

If you have enough Memory space to store all pixel positions, you can play them randomly:

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;ifor (i=0;i {
j = random(sz); // pseudo-randomness with uniform distribution
swap(pixel[i],pixel[j]);
}

In this way, You can ensure that each pixel is used once, and it is likely that all pixels are shuffled…

Leave a Comment

Your email address will not be published.