สร้างภาพช่วงเวลาพิเศษ


10

สร้างโปรแกรมหรือฟังก์ชั่นที่รับอินพุตในรูปแบบของ

<width>, <height>, <color1>, <color2>

และสร้างการส่งออกดังกล่าวที่สลับแถวแรกระหว่างพิกเซลที่มีสี <color1>และมีระยะเวลาของ<color2> สำหรับแต่ละแถวที่เป็นไปตามระยะเวลาที่จะต้อง<height> <height> - <row number>โปรดทราบว่า<color1>เริ่มต้นแถวเสมอ

ตัวอย่างผลลัพธ์

อินพุต120, 6, #000000, #FFFF00:

ตัวอย่างผลลัพธ์

อินพุต80, 50, #000000, #FFFF00:

ตัวอย่างผลลัพธ์ 2

(ตัวอย่างเอาต์พุตถูกปรับสัดส่วนเพื่อความชัดเจน)

นี่คือดังนั้นรายการที่ถูกต้องสั้นที่สุดในไบต์ชนะ


นั่นเป็นเศษส่วนหรือไม่? มันมีมิติเศษส่วนหรือไม่?
PyRulez

คำตอบ:


6

JavaScript (ES6), 177 ไบต์

g=
(w,h,f,b)=>`<svg${s=` width=${w} height=`+h}><rect${s} fill=${b} />`+[...Array(h)].map((_,i)=>`<line stroke=${f} stroke-dasharray=${h-i} x1=0 y1=${i} x2=${w} y2=${i} />`).join``
Width: <input id=w value=80 type=number min=1><br>Height: <input id=h value=50 type=number min=1><br>Foreground: <input id=f value=#000000><br>Background: <input id=b value=#FFFF00><br><input type=button value=Go! onclick=o.innerHTML=g(+w.value,+h.value,f.value,b.value)><div id=o>

ส่งออกภาพ SVG ที่เข้ากันได้กับ HTML5


4

Excel VBA ขนาด 129 ไบต์

ฟังก์ชันหน้าต่าง VBE แบบไม่ระบุชื่อทันทีที่รับอินพุตจากช่วง[A1:D1]และเอาต์พุตไปยังช่วง[A2].Resize([B1],[A1])

Cells.RowHeight=48:For Each r in[A2].Resize([B1],[A1]):r.Interior.Color=IIf(int((r.Column-1)/([B1]-R.Row+2))Mod 2,[D1],[C1]):Next

เอาท์พุต

เอาต์พุตสำหรับอินพุต160, 90, 0xBDD7EE, 0xDDEBF7: เอาท์พุต


4

Tcl / Tk, 143

147 150 151

proc P {w h b c} {set p [image c photo -w $w -h $h]
time {incr y
set x 0
time {$p p -t $x $y [expr {[incr x]/($h-$y+1)%2?$c:$b}]} $w} $h
set p}

ส่งคืนรูปภาพที่สามารถทดสอบได้โดยวางไว้บนcanvasวิดเจ็ตการเขียนไฟล์อาจอยู่บนคลิปบอร์ด ฯลฯ

รหัสสำหรับการทดสอบ

set w 960
set h 540
pack [canvas .c -w $w -he $h -highlightt 0]
.c cr image 0 0 -image [P $w $h #00ffff #ffbf32] -anchor nw

ป้อนคำอธิบายรูปภาพที่นี่


3

Mathematica ขนาด 52 ไบต์

Image@Table[If[Mod[j,2i]>i,#4,#3],{i,-#2,-1},{j,#}]&

ตัวอย่างอินพุต:

Image@Table[If[Mod[j,2i]>i,#4,#3],{i,-#2,-1},{j,#}]&[120,6,Black,Yellow]

3

C (gcc) , 155 143 ไบต์

พิมพ์ Portable PixMap (.ppm) ไปยัง stdout

x,y,z;f(w,h,r,g,b,R,G,B){for(y=~0,printf("P3 %d %d 255",w,h);y++<~-h;)for(x=~0;x++<~-w;)z=(x/(h-y)%2),printf("\n%d %d %d ",z?R:r,z?G:g,z?B:b);}

ลองออนไลน์!

เอาท์พุต

  • การป้อนข้อมูล: 80, 50, #ad00df, #f17e18

ป้อนคำอธิบายรูปภาพที่นี่ (รูปภาพที่ถูกแปลงและปรับขนาด)


อะไรคือข้อดีของการเขียน~0แทน -1?
sergiol

@sergiol มันดูเท่ห์กว่า และ - ทฤษฎี - ถ้าคุณต้องการตัวแปรที่มีเพียง1วินาทีจะพกพามากกว่า~0 -1
Jonathan Frech

2
ภาพนี้ฆ่าดวงตาของฉันเมื่อฉันเลื่อนผ่านมัน +1
HyperNeutrino


1

Java 8, 321 + 42 = 363 ไบต์

import java.awt.*;import java.awt.image.*;
(w,h,a,b)->{BufferedImage i=new BufferedImage(w,h,1);Graphics2D g=(Graphics2D)i.getGraphics();g.setColor(new Color(a));g.fillRect(0,0,w,h);g.setColor(new Color(b));for(int j=h;j>0;g.drawLine(0,h-j,w,h-j--))g.setStroke(new BasicStroke(1,0,0,1,new float[]{j},j));javax.imageio.ImageIO.write(i,"png",new java.io.File("a"));}

ส่งออกไปยังไฟล์ PNG ที่ชื่อa(ไม่มีนามสกุล)

โค้ดล้อมรอบที่ใช้เรียกแลมบ์ดา: ลองออนไลน์!

Ungolfed

(w,h,a,b)->{
    BufferedImage i=new BufferedImage(w,h,1);
    Graphics2D g=(Graphics2D)i.getGraphics();
    g.setColor(new Color(a));
    g.fillRect(0,0,w,h);
    g.setColor(new Color(b));
    for(int j=h;j>0;g.drawLine(0,h-j,w,h-j--))
        g.setStroke(new BasicStroke(1,0,0,1,new float[]{j},j));
    javax.imageio.ImageIO.write(i,"png",new java.io.File("a"));
}

ผลลัพธ์

อินพุต: 80, 50, 0xFF00FF, 0xFFFF00.

เอาท์พุทตัวอย่าง


1

SOGL V0.12 , 83 ไบต์

..¹Cbe¹"εģνļ▲I-℮eīÆoHι⅛ļJd⁾)⁶f°┌²‘→e∫Ab∫Ƨ01a∙H∑bmWrcwιFHea-¹"S⁸▒Μ¬NrηA¤Y_⅓ļpPboā⁵‘→

ลองที่นี่!

SOGL ถูกสร้างขึ้นในการประมวลผล .. ดังนั้นฉันจึงใช้ประโยชน์จากการใช้- ดำเนินการเป็น JavaScript ได้อย่างง่ายดายสามารถเป็นจำนวนไบต์น้อย (อย่างน้อย 30) ถ้า SOGL ได้อะไรทำสำหรับกราฟิก

สตริงที่ไม่บีบอัด:

..¹Cbe¹"__processing0.hidden=0;cp.size(ŗ,ŗ)”→e∫Ab∫Ƨ01a∙H∑bmWrcwιFHea-¹"cp.stroke(ŗ);cp.point(ŗ,ŗ)”→

คำอธิบาย:

..¹C       save the first 2 inputs as an array on variable C
be¹        save next input - width - on B, next input - height - on E, and wrap both in an array
"__processing0.hidden=0;cp.size(ŗ,ŗ)”→   
           execute "__processing0.hidden=0;cp.size(ŗ,ŗ)" as JavaScript - show the canvas and set the size to the corresponding items in the array

e∫A        repeat height times, saving 1-indexed counter on A
  b∫       repeat width times, pushing 1-indexed counter
    Ƨ01    push "01"
    a∙     multiply vertically A times             - ["01", "01", "01",...]
    H      rotate anticlockwise                    - ["000...","111..."]
    ∑      join together                           - "000..111.."
    bm     mold to the length of width             - "00011100011100"
    W      get the counterth letter of that string - "1"
    r      convert to number                       - 1
    cw     get that item from the variable C
    ι      remove the array from the stack
    FH     push current loops counter minus 1
    ea-    push e-a - height-outerLoopsCounter
    ¹      wrap the 3 items in an array - [itemInC (color), counter-1 (x position), height-outerLoopsCounter (y position)]

    "cp.stroke(ŗ);cp.point(ŗ,ŗ)”→ 
           execute "cp.stroke(ŗ);cp.point(ŗ,ŗ)" as JavaScript, replacing ŗ with the corresponding item from the array

1

SmileBASIC ขนาด 84 ไบต์

DEF C W,H,C,D
FOR J=1TO H
FOR I=0TO W-1N=1AND I/J
GPSET I,H-J,C*!N+D*N
NEXT
NEXT
END

สีของพิกเซลที่(x,height-y)เป็น[color1,color2][x/y & 1]


0

Python 2 , 123 101 ไบต์

w,h,c,C=input()
print'P3',w,h,255
for i in range(h):
 for j in range(w):print`[c,C][j/(h-i)%2]`[1:-1]

ลองออนไลน์!

พิมพ์ .ppm เพื่อการส่งออกในขณะที่โจนาธานตอบ


ดูเหมือนกับคำตอบ C ของฉัน ... 105 ไบต์ถ้าคุณเปลี่ยนรูปแบบการป้อนสีของคุณ (เนื่องจากคำถามไม่ได้กำหนดรูปแบบสีอย่างจริงจัง)
Jonathan Frech

@JonathanFrech ขอบคุณ :) ใช่ฉันได้รับแรงบันดาลใจจากคำตอบของคุณเมื่อมองหาวิธีที่จะส่งออกภาพ
ยกเลิก

เพิ่งออกมาจากความอยากรู้; คุณใช้โปรแกรมใดในการเปิดไฟล์. ppm ของคุณ
Jonathan Frech

'%d '*3%'%d %d %d'%จะสั้นกว่า
Jonathan Frech

@JonathanFrech นี้งานหรือผมเคยใช้ GIMP
TFeld

0

JavaScript (ES6) + HTML5, 138 ไบต์

(w,h,a,b)=>{with(c.width=w,c.getContext`2d`)for(c.height=y=h;~--y;)for(x=w;~--x;)fillStyle=x/(h-y)%2<1?a:b,fillRect(x,y,1,1)}
<canvas id=c>

f=
(w,h,a,b)=>{with(c.width=w,c.getContext`2d`)for(c.height=y=h;~--y;)for(x=w;~--x;)fillStyle=x/(h-y)%2<1?a:b,fillRect(x,y,1,1)}
#c{display:block
<input id=w value=80>
<input id=h value=50>
<input id=a value=#000>
<input id=b value=#FF0>
<input value=go type=button onclick=f(w.value,h.value,a.value,b.value)>
<canvas id=c>

<canvas id=c>ผลที่ฟังก์ชั่นโดยการดำเนินงานใน

เริ่มต้นจากด้านล่างขวาของผ้าใบซ้อนforiterates ห่วงไปทางซ้ายและขึ้นและตรวจสอบเงื่อนไขสำหรับพิกเซลที่x/(h-y)%2<1 x,yถ้าเงื่อนไขเป็นtrueมันสีที่พิกเซลพร้อมอื่นcolor1color2

โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.