Pixel Poking Acak


20

Tugas Anda sederhana: tulis sebuah program yang akan menggantikan piksel acak dalam persegi panjang 16px * 8px hitam (lebar dengan tinggi) dengan piksel putih.

Lubang-lubang harus acak seragam, dan Anda harus menampilkan gambar 16px kali 8 px dengan piksel putih dimasukkan.

Ganti hanya 1 piksel per kolom (16 piksel yang diganti total)

Anda tidak mengambil input apa pun, dan Anda tidak dapat mengandalkan gambar yang disimpan di tempat lain di komputer.

Ini adalah sehingga program dengan bytecount terpendek menang!


1
Haruskah output berubah ketika menjalankan program beberapa kali?
Quentin

@ Quentin ya itu harus
GracefulLemming

Jawaban:


7

MATL , 15 14 13 byte

8tE2$r&S1=3YG

Contoh (dengan kompiler MATL berjalan di MATLAB):

masukkan deskripsi gambar di sini

Atau coba di MATL Online! (Jika tidak berjalan pertama kali, tekan "Jalankan" lagi atau segarkan halaman). Perhatikan bahwa gambar tersebut diskalakan oleh penerjemah online untuk visualisasi yang lebih baik.

Ini adalah port jawaban Octave / MATLAB saya (lihat penjelasan di sana). Berikut adalah pernyataan yang setara:

MATL        Octave / MATLAB
----        ---------------
8tE         8,16
2$r         rand(...)
&S          [~,out]=sort(...)
1=          ...==1 
3YG         imshow(...)

6

Pyth - 16 15 byte

Output gambar menjadi o.png.

.wCm.S+255mZ7y8

Contoh gambar:


3

Oktaf / MATLAB, 48 37 35 byte

[~,z]=sort(rand(8,16));imshow(z==1)

Contoh (pada Oktaf):

masukkan deskripsi gambar di sini

Penjelasan

           rand(8,16)                  % 8×16 matrix of random values with uniform
                                       % distribution in (0,1)
[~,z]=sort(          );                % Sort each column, and for each output the
                                       % indices of the sorting. This gives an 8×16
                                       % matrix where each column contains a random
                                       % permutation of the values 1,2,...,8 
                              z==1     % Test equality with 1. This makes all values 
                                       % except 1 equal to 0
                       imshow(    )    % Show image, with grey colormap

3

C, 85 100 byte

main(c){char a[138]="P5 16 8 1 ";for(srand(time(0)),c=17;--c;a[9+c+(rand()&112)]=1);write(1,a,138);}

Menulis file gambar PGM ke stdout (panggil saja prog >out.pgm).

Tidak digabungkan dan dijelaskan:

main(c) {
    // A buffer large enough to contain the whole image,
    // pre-filled with the PGM header.
    // This is a binary greyscale (P5) image with only two levels (1),
    // Because a binary bitmap would require pixel packing.
    char a[138] = "P5 16 8 1 ";

    // c iterates from 16 to 1 over the columns
    for(
        srand(time(0)), c = 17;
        --c;

        // (rand() % 8) * 16 is equivalent to (rand() & 7) << 4
        // Since all bits are equally random, this is equivalent
        // to rand() & (7 << 4), that is rand() & 112.
        // This picks a pixel from the first column, which is then
        // offset to the correct column by c - 1 + 10
        // (10 is the length of the header).
        a[9 + c + (rand() & 112)] = 1
    )
        ; // Empty for body

    // Write the whole buffer to stdout
    write(1,a,138);
}

Pembaruan:

  • OP telah mengklarifikasi bahwa output harus berubah dengan setiap eksekusi, kehilangan 15 byte menjadi srand(time(0))( :()

3

Memproses, 74 73 byte

fill(0);rect(0,0,15,7);stroke(-1);for(int i=0;i<16;)point(i++,random(8));

Output sampel:

masukkan deskripsi gambar di sini

Penjelasan

fill(0);               //sets the fill colour to black
rect(0,0,15,7);        //draws a 16*8 black rectangle
stroke(-1);            //set stroke colour to white
for(int i=0;i<16;)     // for-loop with 16 iterations
 point(i++,random(8)); //  draw a point at x-coordinate i and a random y coordinate
                       //  the colour of the point is white since that is the stroke colour

2

Ruby, 61 byte

puts'P116 8';puts Array.new(16){[*[1]*7,0].shuffle}.transpose

Ini adalah program lengkap yang menampilkan gambar dalam format netpbm ke stdout.

Output sampel:

puts'P116 8';   # output the netpbm header (P1 for black and white, 16x8)
puts            # then output the data as follows:
Array.new(16){  # make a 16-element array and for each element,
[*[1]*7,0]      # generate the array [1,1,1,1,1,1,1,0] (1=black 0=white)
.shuffle}       # shuffle the array
.transpose      # transpose the rows/columns of the 2d array (so that each column
                # has one white pixel)

2

Befunge, 90 byte

Ini menghasilkan file PBM yang ditulis ke stdout.

>030#v_\2*>>?1v
:v9\$<^!:-1\<+<
|>p1+:78+`!
>"P",1..8.28*8*v
.1-\88+%9p:!#@_>1-::88+%9g:!!

Cobalah online!

Penjelasan

Tiga baris teratas membentuk generator angka acak, menyimpan 16 angka 3-bit acak (yaitu dalam kisaran 0-7) pada baris kesepuluh dari playfield. Baris empat menuliskan header PBM, dan baris terakhir kemudian menghasilkan piksel gambar. Hal ini dilakukan dengan menghitung 16 angka acak karena pikselnya adalah output - ketika angka yang sesuai dengan kolom tertentu mencapai nol, kita menghasilkan 1 daripada 0.

Output sampel (diperbesar):

Output sampel


1

Mathematica, 77 60 byte

Image[{RandomInteger@7+1,#}->1&~Array~16~SparseArray~{8,16}]

Output Sampel

masukkan deskripsi gambar di sini

Penjelasan

{RandomInteger@7+1,#}->1&~Array~16

Buat aturan penggantian untuk setiap kolom; ganti posisi yang dipilih secara acak dengan 1.

... ~SparseArray~{8,16}

Buat SparseArraydengan ukuran 8x16 dari aturan penggantian. Latar belakang secara 0default. (8x16 karena Mathematica menghitung baris terlebih dahulu)

Image[ ... ]

Konversikan SparseArraymenjadi Imageobjek.

Versi 77 byte

ReplacePixelValue[Image@(a=Array)[0&~a~16&,8],{#,RandomInteger@7+1}->1&~a~16]

1

HTML + JavaScript, 148 byte

c=O.getContext`2d`;for(x=16;x--;){r=Math.random()*8|0;for(y=8;y--;)c.fillStyle=y-r?'#000':'#fff',c.fillRect(x,y,1,1)}
<canvas id=O width=16 height=8>


0

R, 76 byte

a=matrix(0,8,16);for(i in 1:16)a[sample(1:8,1),i]=1;png::writePNG(a,'a.png')

Menggunakan paket pnguntuk menghasilkan file.
Contoh output:

masukkan deskripsi gambar di sini


0

QBasic, 59 byte

RANDOMIZE TIMER
SCREEN 9
FOR x=0TO 15
PSET(x,RND*8-.5)
NEXT

Cukup mudah. Ini -.5diperlukan karena PSETdengan argumen non-integer gunakan round-to-terdekat bukan floor atau truncate (dan -.5lebih pendek dari INT()).

Gambar yang dimaksud ditampilkan di sudut kiri atas jendela output. Contoh (terpangkas):16 titik acak


0

Java, ( Apakah itu penting Bytes, AKA 244 + 18 import = 262)

import java.awt.*;static void l(){new Frame(){public void paint(Graphics g){int x=50;int i=16;g.setColor(Color.BLACK);g.fillRect(x,x,16,8);g.setColor(Color.WHITE);for(;i>0;i--){int y=(int)(Math.random()*8);g.drawLine(x+i,x+y,x+i,x+y);setVisible(1>0);}}}.show();}

Tenang karena sistem koordinat termasuk panel bingkai jendela ... Jadi Anda perlu buffer setidaknya 26 byte atau tidak ada yang muncul, maka x=50sedikit.


Saya tahu ini sudah lama, tetapi Anda dapat golf ke 238 byte: import java.awt.*;v->{new Frame(){public void paint(Graphics g){int x=50,i=16,y;g.setColor(Color.BLACK);g.fillRect(x,x,i,8);for(g.setColor(Color.WHITE);i>0;g.drawLine(x+i,x+y,x+i--,x+y),setVisible(1>0))y=(int)(Math.random()*8);}}.show();}(Perubahan dilakukan: statis dihapus; Java 8 lambda; beberapa ints dihapus; i=16digunakan kembali; masukkan barang-barang di dalam for-loop untuk menghapus kurung dan ;)
Kevin Cruijssen

0

Postscript (65 byte)

0 0íkà0ícà8íc0 8ícííB1à1íù16{0 randàíj0.5í©ík1 0íÖíß1 0í≠}íÉ

Versi tidak disatukan:

0 0 moveto
16 0 lineto
16 8 lineto
0 8 lineto
closepath
fill
1 1 1 setrgbcolor
16{0 rand 16 mod 0.5 sub moveto 1 0 rlineto stroke 1 0 translate}repeat

0

SmileBASIC, 30 byte

FOR I=0TO 15GPSET I,RND(8)NEXT

0

Chip-8, 12 byte

0xA201 'Load sprite at address 201 (which is the second byte of this instruction)
0xC007 'Set register 0 to a random number from 0 to 7 (rnd & 0x7)
0xD101 'Draw sprite. x = register 1, y = register 0, height = 1
0x7101 'Add 1 to register 1
0x3110 'If register 1 is not 16...
0x1202 'Jump to second instruction

Menarik gambar di layar.


0

Tcl / Tk, 163

Dua pendekatan yang berbeda menghasilkan byte yang sama:

grid [canvas .c -w 16 -he 8 -bg #000 -highlightt 0]
.c cr i 8 4 -i [set p [image c photo -w 16 -h 8]]
set x 0
time {$p p #FFF -t $x [expr int(rand()*8)];incr x} 16

grid [canvas .c -w 16 -he 8 -bg #000 -highlightt 0]
.c cr i 8 4 -i [set p [image c photo -w 16 -h 8]]
time {$p p #FFF -t [expr [incr x]-1] [expr int(rand()*8)]} 16

masukkan deskripsi gambar di sini


0

VBA Excel, 86 105 byte

menggunakan jendela langsung

Cells.RowHeight=42:[a1:p8].interior.color=0:for x=0to 15:[a1].offset(rnd*7,x).interior.color=vbwhite:next

Solusi ini sayangnya tidak membuat sel-sel kuadrat aktivasi, seperti yang diperlukan untuk solusi pixel art Excel VBA - Solusi yang lebih valid adalahCells.RowHeight=42:[A1:P8].Interior.Color=0:For x=0To 15:[A1].Offset(Rnd*7,x).Interior.Color=-1:Next
Taylor Scott

ah ya saya lupa. Saya ingat saya menyesuaikannya secara manual. Terima kasih :)
remoel

(dan Anda dapat menggunakan -1bukannya vbWhite- penjelasan lengkap mengapa di sini )
Taylor Scott
Dengan menggunakan situs kami, Anda mengakui telah membaca dan memahami Kebijakan Cookie dan Kebijakan Privasi kami.
Licensed under cc by-sa 3.0 with attribution required.