| Bytes | Lang | Time | Link |
|---|---|---|---|
| 030 | TinyVG | 250809T175358Z | Adam |
| 075 | HTML | 250530T003417Z | Miro |
| 071 | R | 240805T091155Z | Glory2Uk |
| 125 | C GCC | 220505T213421Z | matteo_c |
| 262 | Pure bash with utf8 | 240226T115357Z | F. Hauri |
| 029 | MATL | 170731T224007Z | Luis Men |
| 144 | Applesoft BASIC | 220518T030354Z | Ohentis |
| 080 | Perl 5 + p0513 | 170801T120524Z | Dom Hast |
| 069 | Desmos | 220505T201941Z | naffetS |
| 083 | APL dzaima/APL | 200926T133013Z | Razetime |
| 055 | GFA BASIC Atari ST | 170801T102838Z | Arnauld |
| 080 | PostScript | 170803T151543Z | Thomas F |
| 170 | Python 2 | 180108T151123Z | Gáb |
| 094 | Tcl/Tk | 170801T073737Z | sergiol |
| 108 | PowerShell | 171015T222410Z | Tessella |
| 068 | GWBasic | 171015T181748Z | Anonymou |
| 042 | SmileBASIC | 171015T145730Z | 12Me21 |
| 079 | Excel VBA | 170801T134758Z | Taylor R |
| 373 | C++ with OpenGL | 170824T235941Z | ATaco |
| 083 | Mathematica | 170731T235216Z | ZaMoC |
| 313 | Excel VBA | 170802T165336Z | Greedo |
| 097 | GIF | 170801T014312Z | adrian |
| 147 | Bash + core utils | 170806T150148Z | Andrew D |
| nan | HTML + CSS 215+80 bytes | 170802T093911Z | Khaled.K |
| nan | JavaScript + HTML | 170731T223450Z | user7234 |
| 497 | Batch File | 170804T041118Z | Matheus |
| 075 | R | 170804T080154Z | R.Jagdhu |
| 074 | Processing | 170804T043457Z | Rapha |
| 089 | Python 2 | 170802T005627Z | Karl Nap |
| 187 | Postscript | 170801T052633Z | ceilingc |
| 160 | Bitmap | 170801T055214Z | Hand-E-F |
| 195 | C# | 170803T185324Z | video_er |
| 064 | SVG HTML5 | 170801T001347Z | Neil |
| 096 | GraphicsMagick ? | 170801T102635Z | tsh |
| 101 | PNG | 170802T101153Z | Anu Shib |
| nan | Java 8 | 170801T012015Z | Justin M |
| 227 | Braindraw | 170801T031034Z | benzene |
| 112 | R | 170801T224307Z | Gregor T |
| 109 | R | 170802T090039Z | plannapu |
| 073 | Octave | 170801T110236Z | bracco23 |
| nan | JavaScript ES6 in HTML4? | 170801T110445Z | tsh |
| 070 | BBC BASIC | 170801T235816Z | Level Ri |
| 140 | R | 170801T174820Z | Alex Axt |
| 112 | Haskell | 170801T170805Z | nimi |
| 085 | Processing | 170801T165941Z | Cody |
| 079 | Red | 170801T153903Z | Geeky I |
| 143 | Tikz | 170801T150127Z | Wheat Wi |
| 058 | LibreLogo | 170801T135358Z | Grant Mi |
| 394 | C++ | 170801T023441Z | HatsuPoi |
| 043 | x8616 Machine Code for DOS | 170801T115539Z | Cody Gra |
| 190 | Python 3 | 170801T113325Z | Andrew D |
| 209 | C# | 170801T090313Z | Jan Ivan |
| 058 | TIBASIC TI84 Plus CSE only | 170801T053746Z | Scott Mi |
| nan | HTML + JavaScript ES6 | 170801T015430Z | darrylye |
| nan | HTML + CSS | 170801T012605Z | darrylye |
| nan | Ruby + ruby2d | 170801T014150Z | Value In |
| 138 | Löve2D | 170801T001621Z | ATaco |
TinyVG, 30 bytes
TinyVG is a vector graphics format designed to be “golfier” than SVG. It uses a binary format rather than an XML dialect. I wrote this answer to see how it compares in terms of size on a simple image. (According to this meta answer, direct image answers are allowed as long as they show non-trivial effort in golfing.)
Output of xxd -ps -u:
725601502020021F00FFFF02000000002020020101060D14060D06061400
Converted to SVG:
<svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 32 32"><rect style="fill:#FF0000;stroke:none;" x="0" y="0" width="32" height="32"/><rect style="fill:#FFFFFF;stroke:none;" x="6" y="13" width="20" height="6"/><rect style="fill:#FFFFFF;stroke:none;" x="13" y="6" width="6" height="20"/></svg>
Explanation
# Header
72 56 01 # This is a TinyVG version 1 image.
50 # Coordinates are specified with 8-bit precision
# and 0 fractional bits,
# colors are encoded as 16-bit RGB (rrrrrggggggbbbbb).
20 20 # The size is 32×32.
02 # The image uses two colors:
1F 00 # RGB code for red
FF FF # RGB code for white
# Drawing commands
02 # Fill rectangles
00 # One rectangle
00 # First color in the palette (red)
00 00 20 20 # x y width height
02 # Fill rectangles
01 # Two rectangles
01 # Second color in the palette (white)
06 0D 14 06 # x y width height
0D 06 06 14 # x y width height
00 # End of file
```
HTML, 75 bytes
<svg viewBox=0,0,6,6 style=background:red><path d=M3,1v4M1,3h4 stroke=#fff>
R, 71 bytes
par(mar=1:4*0+.059,bg="red",cex=107)
frame();text(.5,.5,"+",co="white")
Just found this challenge and since the Swiss National Day was just four days ago, I am adding my answer in R to the existing four answers in R (75 bytes, 109 bytes, 112& 125 bytes and 140 bytes).
This program outputs a white + sign on the empty red plot. It might be possible to reduce this code further by 4 bytes by using an older 3.x version of R, where the background color bg=2 was exactly red as required (apparently, this isn't the case anymore).
C (GCC), 133 130 125 bytes
#define a(x)labs(i x 32-15.5)<
i;main(c){for(puts("P6 32 32 1");i<1024;++i)printf("\1%c%c",c=a(%)10&a(/)10&(a(%)3|a(/)3),c);}
Outputs a PPM image.
Pure bash with utf-8, 262
printf -vv %32s;w=${v:19} d=${w:6} e=${d:1} r=$'\e[31;47m' m=$'\e[0m\n' c=${e:3}
c=${c// /${r} $m} b=${c:14} g=${c// /${v// /█}} h=${c// /${w// /█}$e${w// /█}}
i=$r${e// /█}${d// /▀}$e${d// /▀}${e// /█}$m
echo "$g$h$i${b// /${e// /█}$w$d${e// /█}}${i//▀/▄}$h$g"
Pure bash, without utf-8, 283
printf -vf %14s;f=("$f" "${f:2}") w=$'\e[47m' r=${w/7/1}
for v in 6:rrr 7:rwr 6:www 7:rwr 6:rrr;do l=$r$f c=r s=${v%:*} m=${v#*:}
for((h=0;h<3;h++)){ [ $c != ${m:h:1} ]&&c=${m:h:1}&&l+=${!c};l+=${f[h&1]};}
[ $c = r ]||l+=$r;l+=$f;printf -vo %${s}s;printf "${o// /$l$'\e[0m\n'}";done
Will render:
MATL, 29 bytes
2Y66t&Y"OJQht3$)6thYaQK7hBoYG
Try it at MATL Online!
Explanation
2Y6 % Push predefined literal: 3×3 matrix [0 1 0; 1 1 1; 0 1 0]
6t&Y" % Repeat each entry 6 times in the two dimensions. Gives an 18×18 matrix
OJQh % Push [0 1j+1]. Used as an index, this means [0 1 ... end+1]. Indexing
% is 1-based and modular, so 0 is the same as end
t3$) % Apply that index in the two dimensions. This extends by 1 entry in
% each dimension, so that the arms have length 7. Gives a 20×20 matrix
6thYa % Pad with 6 zeros in the two dimensions. Gives a 32×32 matrix
Q % Add 1. The matrix now contains values 1 (will become red) and 2 (white)
K7hBo % Convert array [4 7] to binary. Gives [1 0 0; 1 1 1], to be used as
% colormap. First row is red, second is white
YG % Show image using colormap
Applesoft BASIC, 144 bytes
0GR
1COLOR=1
2X=0
3VLIN0,31ATX
4X=X+1
5IFX<32GOTO3
6COLOR=15
7X=12
8VLIN6,25ATX
9X=X+1
10IFX<19GOTO8
11Y=12
12HLIN6,25ATY
13Y=Y+1
14IFY<19GOTO12
Perl 5 + -p0513, 80 bytes
print"P6 32 32 1",$_=($}="A..")x192,$\=($}x13 .1x18 .$}x13)x7,($}x6,1x60,$}x6)x6
Outputs a PBM file.
Perl 5 + -p0513 -M5.10.0, 82 bytes
say"P6 32 32 1",map{($|--||"A..")x$_}205,@a=((18,26)x6,18),20,(54,14)x6,0,6,@a,205
Also outputs a PBM file. This is a slightly more interesting approach, but I can't get it to beat the above.
Perl 5 + -Mutf8 -p0513 -M5.10.0, 149 bytes
say"[101;97m",$\=($l=$"x32 ."
")x3,$_=(($s=$"x13).($}='█')x6 ."$s
")x3,$l=($"x=6).($a='▄'x7).$}x6 .$a.$",("
",$",$}x20,$")x2,"
",$l=~y;▄;▀;r
Output is to the terminal via ANSI escape codes and looks like this:
Proportions should be to scale based on one character space being two vertical 'pixels'.
You can preview the output by copying from TIO and pasting in here (this shows the control characters when hovered over the element).
Desmos, 90 69 bytes
c=hsv(0,[1,0,0],1)
[0,6,13]<=x<=[32,26,19]\{[0,13,6]<=y<=[32,19,26]\}
-21 bytes thanks to Aiden Chow
APL (dzaima/APL), 83 bytes
P5.size←2⌿32
g←P5.G
a←13 6 19 26
P5.draw←{r←g.rect
g.bg'f00'
g.stroke←⍬
r a
r 2⌽⌽a}
32x32 canvas:
GFA BASIC (Atari ST), 62 55 bytes
A manually edited listing in .LST format. All lines end with CR, including the last one.
DEFF 2
PB 0,0,31,31
DEFF 0
PB 13,6,18,25
PB 6,13,25,18
Expanded and commented:
DEFFILL 2 ! set fill color: default system color #2 is red (&H700)
PBOX 0,0,31,31 ! draw the red square
DEFFILL 0 ! set fill color: default system color #0 is white (&H777)
PBOX 13,6,18,25 ! draw a vertical white rectangle
PBOX 6,13,25,18 ! draw a horizontal white rectangle
###Output
The output is 32x32.
NB: This is indeed using the 'white' color, although it's gray-looking on the emulator (and on the real thing as well, unless you turn the brightness quite high).
PostScript, 85 80 bytes
Code:
1 0 0 setrgbcolor
0 0 32 32 rectfill
1 setgray
[13 6 6 20 6 13 20 6] rectfill
Result:
Python 2, 170 bytes
Uses Pillow 2.8.0 with PIL 1.1.7 for the old (and deprecated) offset method.
from PIL.ImageDraw import*
i=Image.new('RGB',(20,20),-1)
Draw(i).rectangle((3,3,18,18),'red',-1)
k=Image.new('RGB',(32,32),'red')
k.paste(i.offset(9),(6,6))
k.show()
Tcl/Tk, 94
If it runs on the interactive shell, one can abbreviate canvas to can and grid to gri
gri [can .c -bg red -w 32 -he 32]
lmap R {{8 15 28 21} {15 8 21 28}} {.c cr r $R -f #FFF -w 0}
Tcl/Tk, 98
If it runs on the interactive shell, one can abbreviate canvas to can
grid [can .c -bg red -w 32 -he 32]
.c cr r 8 15 28 21 -f #FFF -w 0
.c cr r 15 8 21 28 -f #FFF -w 0
Tcl/Tk, 101
pack [canvas .c -bg red -w 32 -he 32]
.c cr r 8 15 28 21 -f #FFF -w 0
.c cr r 15 8 21 28 -f #FFF -w 0
PowerShell, 108 bytes
$1=($r='2 0 0 ')*192
$2=($r*13+($w='2 '*3)*6+$r*13)*7
$3=($r*6+$w*20+$r*6)*6
"P3 32 32 2 $1$2$3$2$1"|sc .ppm
Outputs a PPM file called .ppm (checked with IrfanView). It sets '2' as the maximum value for each channel, so $r is 2 0 0 for red, $w is 2 2 2 for white, and the rest is string multiplication and a file header; $1 is a horizontal red line for the very top and bottom of the flag, $2 is wide red, narrow white, wide red for the top and bottom parts of the cross, 3 is the centre narrow red, wide white, narrow red. sc is set-content and used because > makes a 2-byte+BOM Unicode file which doesn't work.
GW-Basic, 68 bytes (tokenised)
0 SCREEN 9:PALETTE 9,36:FOR I=0 TO 3:LINE(X,Y)-(31-X,31-Y),Y+9,BF
↪ :Y=I*7 MOD 14+6:SWAP X,Y:NEXT:LOCATE 4
To get it down to 68 bytes you have to manually edit the file GW-Basic saves to remove all spaces and the trailing end of file and null characters. The PALETTE statement is necessary because GW-Basic has no VGA support and the reds in the default EGA palette are too dark (4) and too washed out (12). The final LOCATE statement ensures that GW-Basic's Ok isn't printed on top of the flag.
SmileBASIC, 42 bytes
GCLS #RED
GFILL 6,13,25,18GFILL 13,6,18,25
Alternatively -1<<16 or -8<<16 can be used instead of #RED for the same length.
Excel VBA, 86 85 79 Bytes
Code
Anonymous VBE immediate window function that takes no input and outputs the Swiss flag onto the range [A1:F32] of the ActiveSheet object. This works by first making the cells square, then drawing the red region and finally drawing in the white cross.
Cells.RowHeight=48:[A1:AF32].Interior.Color=255:[G14:Z19,N7:S26].Interior.Color=-1
Note that arrangements which that remove the step of coloring the "cross" of the Swiss flag after putting in the red background are actually longer than the above configuration, as they require at least 16 cell addresses, which coordinate to the 8 distinct regions of the flag
-1 Byte thanks to @Greedo's for RowHeight=48 over ColumnWidth=2
-6 Bytes thanks to @Greedo's for -1 instead rgbWhite
Output
C++ with OpenGL, 373 Bytes.
This is my first time really working with C++ and OpenGL on a challenge, so let me know what I can improve.
#include<GL/glut.h>
#define v glVertex2d
#define r(w,x,y,z) v(w,x);v(w,z);v(y,z);v(y,x);
void d(){glScaled(1/16.0f,1/16.0f,1);glBegin(GL_QUADS);glColor3f(1,0,0);r(-16,-16,16,16)glColor3f(1,1,1);r(-3,-10,3,10)r(-10,-3,10,3)glEnd();glFlush();}int main(int c,char**a){glutInit(&c,a);glutInitWindowSize(320,320);glutCreateWindow("");glutDisplayFunc(d);glutMainLoop();return 0;}
Compiled with the MinGW version of g++ on Windows with FreeGlut.
Creates a 320 x 320 screen displaying the Flag.

Whenever a redraw happens, it spawns a smaller flag inside itself. This is because I never clear the screen, nor reset the matrix.

However, a redraw does not happen automatically.
Ungolfed Code
#include <GL/glut.h>
#define rect(w,x,y,z) glVertex2d(w,x);glVertex2d(w,z);glVertex2d(y,z);glVertex2d(y,x);
void display(){
glClear();
glScaled(1/16.0f,1/16.0f,1);
glBegin(GL_QUADS);
glColor3f(1,0,0);
rect(-16,-16,16,16)
glColor3f(1,1,1);
rect(-3,-10,3,10)
rect(-10,-3,10,3)
glEnd();
glFlush();
}
int main(int argc, char **argv){
glutInit(&argc, argv);
glutInitWindowSize(320,320);
glutCreateWindow("Swiss Flag");
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
Mathematica, 83 bytes
R=Rectangle;Graphics[{Red,{0,0}~R~{32,32},White,{6,13}~R~{26,19},{13,6}~R~{19,26}}]
next one is from @ASCII-only (I thought it was a joke but it works!)
Mathematica, 23 bytes
"CH"~CountryData~"Flag"
-10 bytes from @Jonathan Frech
Excel VBA, 315 313 bytes
2 bytes saved thanks to Taylor Scott!
Golfed
Sub m
Set r=[A1:AF32]
r.RowHeight=48
r.NumberFormat=";"
Set p=r.FormatConditions.AddColorScale(2).ColorScaleCriteria
s 1,255,p
s 2,-1,p
r.Formula=Replace("=IFS(OR(ROW()<6,ROW()>26,|<6,|>26),0,OR(AND(ROW()>13,ROW()<19),AND(|>13,|<19)),1,1,0)", "|", "COLUMN()")
End Sub
Sub s(n,v,p)
p(n).FormatColor.Color=v
End Sub
And commented
Sub m
Set r=[A1:AF32] 'Evaluates range reference to return 32 square range object
r.RowHeight=48 'set row height to make grid cells square - assumes default font
r.NumberFormat=";" 'make text in cells invisible
Set p=r.FormatConditions.AddColorScale(2).ColorScaleCriteria
'add 2-colour scale conditional formatting
s 1,255,p 'where lowest value is red
s 2,-1,p 'highest is white
r.Formula=Replace("=IFS(OR(ROW()<6,ROW()>26,|<6,|>26),0,OR(AND(ROW()>13,ROW()<19),AND(|>13,|<19)),1,1,0)", "|", "COLUMN()") ''#apply grid of 1s and 0s is flag shape
End Sub
Sub s(n,v,p) 'exploits ByRef default (thanks Gaffi)
p(n).FormatColor.Color=v 'set colour for 2-colour scale conditional formatting
End Sub
I wanted to do something that didn't involve looping
Assumes font is the default Calibri size 11
GIF, 97 Bytes
GIF87a ð ÿ ÿÿÿ, @„©Ëí£œ´Ú;ƒÞXmÞ!ŸŠcyŒj¨,àZê<Cô
:8nïtï‹é‚BJ1tì$1ËKSvb=_ÔªõŠe ;
Cheap and byte-heavy, but worth it.
EDIT: Some characters aren't in the code above, here's a pastebin for the GIF's hexdump.
EDIT: compressed down to 97 bytes with online tool.
Bash + core utils, 147 bytes
echo H4sICBkuh1kAA2ZsYWcueHBtAFNUiAjwNeIyNlIAIiMFQ64ihWQFZTc3AyDgKoewQYCLq4gAGIIKysFg+CooxwKGngI6BNQgUoAFDA0FXABVWXDvSwQAAA|base64 -d|gunzip>1.xpm
Decodes base64-encoded GZipped image data, unGZips it and writes into 1.xpm.
HTML + CSS 215+80 bytes
CSS
table{ all:unset } th{ background:red }
#a{ height: 6 } #b{ height: 7 }
#c{ width: 6 } #d{ width: 7 }
HTML
<table>
<tr id=a> <th colspan=5>
<tr id=b> <th id=c> <th id=d> <td id=c> <th id=d> <th id=c>
<tr id=a> <th id=c> <td id=d> <td id=c> <td id=d> <th id=c>
<tr id=b> <th id=c> <th id=d> <td id=c> <th id=d> <th id=c>
<tr id=a> <th colspan=5>
Inaccurate Version 223+40 179+40 139+40 bytes
- saved 44 bytes, thanks to @manatwork
- saved 40 bytes, thanks to @SteveBennett
CSS
table{ all:unset } th{ background:red }
HTML
<table>
<tr> <th colspan=5>
<tr> <th colspan=2> <td> <th colspan=2>
<tr> <th> <td> <td> <td> <th>
<tr> <th colspan=2> <td> <th colspan=2>
<tr> <th colspan=5>
JavaScript + HTML, 99 + 13 = 112 bytes
Saved 2 bytes thanks to user2428118
Saved 2 bytes thanks to Matheus Avellar
with(c.getContext`2d`)fillStyle='red',fillRect(0,0,32,32),clearRect(6,13,20,6),clearRect(13,6,6,20)
<canvas id=c>
Pure JavaScript, 194 129 bytes
with(document.write`<canvas id=c>`,c.getContext`2d`)fillStyle='red',fillRect(0,0,32,32),clearRect(6,13,20,6),clearRect(13,6,6,20)
Batch File, 584 497 bytes
EDIT: -87 bytes! Replaced rem with hh as it is a no-op when no arguments are passed, removed a bunch of quotation marks and some other stuff.
I'm sure this can be compressed some more, but for the moment it's the best I got.
@!! 2>nul||cmd/q/v/c%0&&exit/b
for /f "tokens=1,2delims=#" %%a in ('"prompt #$H#&for %%b in (1)do hh"')do set K=%%a
set s=aaaaaa
set g=%s%%s%b
:d
set/ai+=1
set t=%i%
if %t%==5 set t=1
if %t%==4 set t=2
if %t%==1 for /l %%a in (1,1,6)do call:c 44 %g%%g%%s%&echo(
if %t%==2 for /l %%a in (1,1,7)do call:c 44 %g%&call:c ff %s%&call:c 44 %g%&echo(
if %t%==3 for /l %%a in (1,1,6)do call:c 44 %s%&call:c ff %g%%s%c&call:c 44 %s%&echo(
if %t%==6 exit
goto d
:c
<nul set/p.=%K%>%~2&findstr/a:%1 . %~2 nul
This code produces the following output (tested on Windows version 10.0.15063):
As a negative side-effect, it creates 4 different files on the same directory with some weird names and gibberish in them. You're free to delete them.
Also, in order for this to look right, CMD's properties must be changed (if not already set correctly):
This code requires that the font chosen has a 1:1 width to height ratio.
R , 75 bytes
x=matrix(2,32,32);x[7:26,14:19]=NA;x[14:19,7:26]=NA;image(x,c=2,ax=F,as=1)
Results in:
Processing, 74 bytes
size(32,32);background(-65536);noStroke();rect(13,6,6,20);rect(6,13,20,6);
I have honestly no idea if that's legal here but here it is.
Outputs:
Explanation:
size(32,32); //Set the window size to 32x32
background(-65536); //Draw the background as red. This is a hack that works because the color datatype uses an int but does not use it as a number. The int equivalent to #FF0000 is -65536 apparently. and it saves me a byte.
noStroke(); //Disable the stroke when drawing shapes like rect
rect(13,6,6,20); //Draws a rectangle of size 6x20, starting the top-left corner at position (13, 6)
rect(6,13,20,6); //Draws a rectangle of size 20x6, starting the top-left corner at position (6, 13)
Python 2, 92 91 89 bytes
r,w=' \0\0',' '*3
A,B=192*r,(13*r+6*w+13*r)*7
print"P6 "+"32 "*3+A+B+(6*r+20*w+6*r)*6+B+A
Output as binary ppm, usage:
python golf_swiss.py > swiss.ppm
Increasing the color depth allowed me to use space for maximum value
Postscript, 301 286 187 bytes
/m{moveto}def
/s{setrgbcolor}def
/r{rlineto}def
/c{closepath fill}def
1 0 0 s
0 0 m
0 320 r
320 0 r
0 -320 r
c
1 1 1 s
130 60 m
60 0 r
0 200 r
-60 0 r
c
60 130 m
0 60 r
200 0 r
0 -60 r c
Thanks to the anonymous editor who suggested a 99 byte reduction!
Bitmap, 160 bytes
Here's a bitmap with 1-bit colour depth, using a 24-bit colour definition. The last six bytes of the second line is the palette and following that is the pixel array. Every four bytes contains a whole row of pixels.
42 4D A0 00 00 00 00 00 00 00 20 00 00 00 0C 00
00 00 20 00 20 00 01 00 01 00 00 00 FF FF FF FF
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 07 E0 00 00 07 E0 00
00 07 E0 00 00 07 E0 00 00 07 E0 00 00 07 E0 00
00 07 E0 00 03 FF FF C0 03 FF FF C0 03 FF FF C0
03 FF FF C0 03 FF FF C0 03 FF FF C0 00 07 E0 00
00 07 E0 00 00 07 E0 00 00 07 E0 00 00 07 E0 00
00 07 E0 00 00 07 E0 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
I tried to truncate the trailing zeroes, but image viewers require there are enough pixels defined for the entire canvas.
C#, 195 bytes
Hi, this is my first code golf and I'm a C# programmer, so I thought I might submit this entry that I quickly made. It correctly outputs a 32 x 32 bitmap image called "s" in the directory of the executable.
var b=new Bitmap(32,32);var g=Graphics.FromImage(b);g.Clear(Color.Red);g.FillRectangle(Brushes.White,new Rectangle(13,0,6,32));g.FillRectangle(Brushes.White,new Rectangle(0,13,32,6));b.Save("s");
Explanation:
C# offers an extensive GDI+ image manipulation APIs built into the default .NET framework. The System.Drawing namespace contains two such very powerful wrapper classes: Bitmap and Graphics. I merely used those two classes to generate a Swiss flag bitmap.
Ungolfed:
// Creates a new 32 x 32 bitmap object.
var b = new Bitmap(32, 32);
// Creates a new graphics object from the bitmap image.
var g = Graphics.FromImage(b);
// Clears the bitmap with a red(ARGB: FFFF0000) fill color.
g.Clear(Color.Red);
// Draws the vertical rectangle with a white(ARGB: FFFFFFFF) color.
g.FillRectangle(Brushes.White, new Rectangle(13, 0, 6, 32));
// Draws the horizontal rectangle with a white(ARGB: FFFFFFFF) color.
g.FillRectangle(Brushes.White, new Rectangle(0, 13, 32, 6));
// Save the bitmap to a file named: "s."
b.Save("s");
SVG (HTML5), 94 83 82 64 bytes
<svg><path d=M0,0V32H32V0M6,13h7V6h6v7h7v6H19v6H13V19H6 fill=red
Saved 2 bytes thanks to @ThePirateBay. Saved 1 byte thanks to @ATaco. Saved 18 bytes thanks to @CraigAyre pointing out that a 32×32 image was acceptable. As pointed out by @Shaggy, this assumes your default background is white; add 22 bytes for a flag that works on any background:
<svg><path d=M0,0V32H32V0 fill=red /><path d=M6,13h7V6h6v7h7v6H19v6H13V19H6 fill=#FFF
GraphicsMagick (?), 96 bytes
gm convert -size 32x32 xc:red -fill #fff -draw "rectangle 13,6 18,25 rectangle 6,13 25,18" 1.bmp
Not sure how to golf with GM (should this be considered as a language?). Also not sure how to count bytes here...
With GM installed, type the given code in windows cmd, you will get an image with name 1.bmp. You may want change double quote to single quote if you are using bash (it should work, but i had not tested this).
Thanks to Digital Trauma. use only one -draw save 8 bytes
PNG - 101 bytes :p
âPNG
IHDR I¥Ë∑PLTEˇˇˇˇA4IDAT◊c`¿ÿ"òˇˇ?Äõ ¬º)’ «√IENDÆB`Ç
Explanation :
Ctrl+C -> Ctrl+V
Java 8, 246 + 42 = 288 bytes
Thanks to @Aaron for -64 bytes.
-4 bytes by outputting to file a without extension.
import java.awt.*;import java.awt.image.*;
x->{BufferedImage i=new BufferedImage(32,32,1);Graphics g=i.getGraphics();g.setColor(Color.RED);g.fillRect(0,0,32,32);g.setColor(Color.WHITE);g.fillRect(13,6,6,20);g.fillRect(6,13,20,6);javax.imageio.ImageIO.write(i,"png",new java.io.File("a"));}
A lamba expression that can be assigned to a functional interface method that throws an exception. Creates an image file named a (an image file) in the directory running this file.
Surrounding code used to run it: Try it online!
Ungolfed
x->{
BufferedImage i=new BufferedImage(32,32,1);
Graphics g=i.getGraphics();
g.setColor(Color.RED);
g.fillRect(0,0,32,32);
g.setColor(Color.WHITE);
g.fillRect(13,6,6,20);
g.fillRect(6,13,20,6);
javax.imageio.ImageIO.write(i,"png",new java.io.File("a"));
}
Result
Grid provided by the IntelliJ IDEA image preview (apparently).
Braindraw, 227 bytes (Non-competing) (Updated to actually work)
Ungolfed version (241 bytes)
r<<^++++++++[-^++++v]^[->>[>]>++++++++[-<++++>]<[<]<]>>[[-vv[v]-[^]^]>]vv<[<]>>>>>>>>>>>>>>vvvvvvg<<^^++++++[->>[>]>+++++[-<++++>]<[<]<]>>[[-vv[v]b-g-[^]^]>]<vv[<]><<<<<<<vvvvvvv<<^^++++++[-vv[v]++++++++++++++[^]^]vv[[->>[>]b[+]-g[+]-[<]<]v]
Lightly golfed version (227 bytes)
r<<^++++++++[-^++++v]^[->>[>]>++++++++[-<++++>]<[<]<]>>[[-vv[v]-[^]^]>]vv<[<]g>>>>>>>>>>>>vvvv++++++[->>[>]>+++++[-<++++>]<[<]<]>>[[-vv[v]b-g-[^]^]>]<vv[<]<<<<<<vvvvv<<++++++[-vv[v]++++++++++++++[^]^]vv[[->>[>]b[+]-g[+]-[<]<]v]
Braindraw is a programming language of my own design. As of 3:08 pm today, I do have a working interpreter and have checked that the program runs properly. However, the interpreter did not exist before this competition began, so I am not competing.
Braindraw is the same as Brainf***, except that it operates on three two-dimensional arrays, rather than one one-dimensional array. These arrays are called r, g, and b, and act as the red green and blue color channels. The operator r moves the pointer to the red channel, etc etc. Up is ^, down is v.
R, 112 bytes
par(bg=2,mar=0*1:4)
plot(0:32,0:32,'n',xaxs="i",yaxs="i")
rect(c(6,13),c(13,6),c(26,19),c(19,26),c="white",b=NA)
Though, to guarantee the aspect ratio, we need 5 more bytes for a total of 117:
par(bg=2,mar=0*1:4)
plot(0:32,0:32,'n',xaxs="i",yaxs="i",as=1)
rect(c(6,13),c(13,6),c(26,19),c(19,26),c="white",b=NA)
With lots of inspiration from Alex Axthelm's answer, and thanks to Rift for saving a few more bytes.
R, 109 bytes
par(mar=0*1:4,bg=2,xaxs="i",yaxs="i")
frame()
rect(a<-c(6,13)/32,rev(a),b<-c(26,19)/32,rev(b),c="white",b=NA)
Uses frame to avoid presetting a plot, but default plotting region is on interval [0,1] instead of [0,32]. Also uses the fact that rect is vectorized. The default size of the plotting window is 7in x 7in. It outputs the following:
R, 125 bytes
png()
par(mar=0*1:4,bg=2,xaxs="i",yaxs="i")
frame()
rect(a<-c(6,13)/32,rev(a),b<-c(26,19)/32,rev(b),c="white",b=NA)
dev.off()
Default for png is a square of 480x480 pixels. Output is the following png file:
Octave, 105 bytes 73 bytes
Note: line feeds added for clarity, not included in byte count
x(32,32,3)=0;
x(:,:,1)=1;
x(7:26,14:19,:)=1;
x(14:19,7:26,:)=1;
imshow(x*255)
EDIT: thanks to flawr for saving quite a lot of bytes!
Here is the result on octave-online.net, thanks to sanchises
JavaScript (ES6) in HTML4(?), 178 176 bytes
document.write('<table cellspacing=0>'+[...Array(32)].map((_,y,p)=>p.map((_,x)=>`<td bgcolor=${x>5&&x<26&&y>12&&y<19||y>5&&y<26&&x>12&&x<19?'#fff':'red'}>`).join``).join`<tr>`)
Yes! We do not need <canvas> nor <svg> or even <img>!
Thanks to Justin Mariner, save 2 bytes.
BBC BASIC, 70 bytes
V.19;16,255|25,97,64;64;25,4,12;26;25,99,40;12;25,4,26;12;25,99,12;40;
Download interpreter at http://www.bbcbasic.co.uk/bbcwin/download.html
Program runs in default mode for this interpreter (white background, black foreground.) 2x2 units = 1 pixel. Flag is 64x64 units = 32 x 32 pixels.
The VDU or V. command sends characters to the screen controller. These include a number of machine specific control characters for graphics. Number followed by , sends 1 byte, by ; sends 2 bytes. Number followed by | sends "sufficient" bytes to finish a multibyte command.
Ungolfed
VDU 19,0,16,255| :REM Reprogram colour 0 (foreground) to #0000FF red (BBC BASIC is little endian.)
VDU 25,97,64;64; :REM Cursor is already at 0,0. Move to 64,64 and draw a rectangle in foreground colour (red.)
VDU 25,4,12;26; :REM Move to 12,26
VDU 25,99,40;12; :REM and draw a rectangle in backgorund colour (white) 40 units wide x 12 units high,
VDU 25,4,26;12; :REM Then move to 26,12
VDU 25,99,12;40; :REM and draw a vertical rectangle of simiar dimensions.
R, 171 140 bytes.
x = rep(c(6,13,19,26,19,13),e=2)
l=c(0,32)
par(bg="red",mar=0*1:4)
plot(0,0,'n',l,l,xaxs="i",yaxs="i")
polygon(x,c(x,x)[4:15],c="white",b=0)
(somewhat) ungolfed:
x <- rep(c(6,13,19,26,19,13),each=2)
y <- c(x,x)[4:15] # index by 3 positions
par(bg="red",mar=c(0,0,0,0)) # prepare the plot space
plot(0,type='n', xaxs="i",yaxs="i",xlim=c(0,32),ylim=c(0,32))
polygon(x,y,col="white",border=0)
Thanks to @gregor for saving some bytes
Haskell, 112 bytes
import Graphics.Gloss
(#)=rectangleSolid
main=display(InWindow""(32,32)(0,0))red$color white$pictures[6#20,20#6]
A full program that uses the Gloss library and opens a window of size 32x32 with red background and two white rectangles (size 6x20 and size 20x6) placed at the center of the window.
Processing, 85 bytes
size(32,32);background(#FF0000);fill(255);noStroke();rect(6,13,20,6);rect(13,6,6,20);
A very straightforward implementation. Open the window to the minimum size, draw a red background, set the fill to white and shapes to not have borders, and draw two rectangles.
Red, 79 Bytes
Code:
view[base 32x32 red draw[fill-pen white pen off box 6x13 26x19 box 13x6 19x26]]
Result:
Explanation:
view [ ; activate view engine
base 32x32 red ; draw a 32 by 32 red box as base
draw [ ; draw on top of the base
fill-pen white ; fill color is white
pen off ; turn off outline, alternatively could use white
box 6x13 26x19 ; draw a horizontal rectangle, specified as 2 corners XxY
box 13x6 19x26 ; draw vertical rectangle
]]
Tikz, 145 143 bytes
\documentclass[tikz]{standalone}\begin{document}\tikz{\def~{)rectangle(}\fill[red](,~33,33);\fill[white](7,14~27,20)(14,7~20,27)}\end{document}
Here it is "ungolfed"
\documentclass[tikz]{standalone}
\begin{document}
\tikz{
\def~{)rectangle(}
\fill[red](,~33,33);
\fill[white](7,14~27,20)(14,7~20,27)
}\end{document}
Here it is ungolfed a little more
\documentclass[tikz]{standalone}
\begin{document}
\tikz{
\fill[red](1,1)rectangle(33,33);
\fill[white](7,14)rectangle(27,20)(14,7)rectangle(20,27)
}\end{document}
This makes 3 rectangles a big red one and two smaller white ones for the plus.
Here's what it looks like. It doesn't really look any different than the others.
LibreLogo, 58 bytes
Code:
pc [3]fc [5]square 25 fc [3]rectangle[4,15]rectangle[15,4]
Result:
Explanation:
pc [3] ; Pen Color = #FFFFFF
fc [5] ; Fill Color = #FF0000
square 25 ; Square = 25 pt
fc [3] ; Fill Color = #FFFFFF
rectangle [4, 15] ; Rectangle = 4 x 15 pt
rectangle [15, 4] ; Rectangle = 15 x 4 pt
C++, SFML, 406 399 394 bytes
-2 bytes thanks to pdpi
-10 bytes thanks to Zacharý
SFML ( Simple and Fast Multimedia Library ) is a C++ library written to ease the developpement of video games and multimedia programs
The code :
#include<SFML\Graphics.hpp>
using namespace sf;int main(){for(RenderWindow t(VideoMode(128,128),"");t.isOpen();){t.clear();RectangleShape r({128.f,128.f});r.setFillColor(Color::Red);t.draw(r);r.setFillColor(Color::White);r.setPosition({24.f,52.f});r.setSize({80.f,24.f});t.draw(r);r.rotate(90);r.setPosition({76.f,24.f});t.draw(r);t.display();for(Event e;t.pollEvent(e);)if(!e.type)t.close();}}
The flag get displayed in a 128x128 window. Your OS have to be able to display a 128x128 window ( my Win8.1 can't display with smaller width )
WARNING : you may want to add these 2 lines of code : t.setFramerateLimit(60);t.setVerticalSyncEnabled(true);, so your CPU won't heat when you run the code. I did not put them in the original code for golfing reasons.
x86-16 Machine Code for DOS, 43 bytes
; Draw 32x32 red square
B8 0C 28 mov ax, 0x0C28 ; AL == color red, AH == set pixel function
B9 00 1F mov cx, 31
DrawBox:
BA 00 1F mov dx, 31
DrawRow:
CD 10 int 0x10
4A dec dx
75 FB jnz DrawRow
E2 F6 loop DrawBox
; Draw the interior white cross
B0 0F mov al, 0x0F ; high byte already set
B1 06 mov cl, 6 ; high byte already 0
DrawCross:
B2 0D mov dl, 13 ; high byte already 0
DrawCrossInner:
CD 10 int 0x10 ; plot CX, DX
87 D1 xchg dx, cx
CD 10 int 0x10 ; plot DX, CX
87 D1 xchg dx, cx
42 inc dx
80 FA 12 cmp dl, 13+6
75 F2 jne DrawCrossInner
41 inc cx
80 F9 19 cmp cl, 6+(32-6-6)
75 EA jne DrawCross
C3 ret
The above code is designed to be assembled as a COM file and then run under DOS. It invokes the ROM BIOS video services to plot the individual pixels of the flag, forming a 32×32 representation of the Swiss flag in the upper-left corner of the screen.
The code assumes that the video mode is already set to mode 0x13, which also means that it requires a VGA display. Other video modes could be used, but requiring VGA gives you two things: (1) square pixels, and (2) a default palette that includes a true 100% red (0xFF0000) (meaning you don't have to waste bytes changing the palette colors). Before running this program, you will therefore need to switch your display to mode 0x13; the following code is all you need to do that:
mov ax, 0x13
int 0x10
The code makes no other assumptions, and should run on any x86-compatible hardware under a DOS-compatible operating system.
However, the program terminates immediately after drawing the flag, so the DOS prompt will normally be re-printed at the top-left corner of the screen, covering up the top few lines of the flag. Therefore, if you want to marvel at the true output, you need to hang/pause the system before it RETurns. Here's a screenshot of what that looks like, running in a FreeDOS virtual machine:
It might be possible to golf this down further by writing pixel data directly to the video buffer, especially since I'm already assuming mode 0x13. I haven't tried that yet. INT 0x10 is already a pretty short instruction, but if I can use the one-byte string instructions to write pixel data directly to memory, then that could result in a significant code savings.
Python 3, 190 bytes
Save output as image.xpm and open with your favorite text editor.
r=range;print('! XPM2\n32 32 2 1\nr c #FF0000\nw c #FFFFFF\n');d=[bytearray(b'r'*32)for _ in r(32)]
for i in r(20):
for j in r(6):d[i+6][j+13]=d[j+13][i+6]=119
print(b'\n'.join(d).decode())
Output:
! XPM2
32 32 2 1
r c #FF0000
w c #FFFFFF
rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
rrrrrrrrrrrrrwwwwwwrrrrrrrrrrrrr
rrrrrrrrrrrrrwwwwwwrrrrrrrrrrrrr
rrrrrrrrrrrrrwwwwwwrrrrrrrrrrrrr
rrrrrrrrrrrrrwwwwwwrrrrrrrrrrrrr
rrrrrrrrrrrrrwwwwwwrrrrrrrrrrrrr
rrrrrrrrrrrrrwwwwwwrrrrrrrrrrrrr
rrrrrrrrrrrrrwwwwwwrrrrrrrrrrrrr
rrrrrrwwwwwwwwwwwwwwwwwwwwrrrrrr
rrrrrrwwwwwwwwwwwwwwwwwwwwrrrrrr
rrrrrrwwwwwwwwwwwwwwwwwwwwrrrrrr
rrrrrrwwwwwwwwwwwwwwwwwwwwrrrrrr
rrrrrrwwwwwwwwwwwwwwwwwwwwrrrrrr
rrrrrrwwwwwwwwwwwwwwwwwwwwrrrrrr
rrrrrrrrrrrrrwwwwwwrrrrrrrrrrrrr
rrrrrrrrrrrrrwwwwwwrrrrrrrrrrrrr
rrrrrrrrrrrrrwwwwwwrrrrrrrrrrrrr
rrrrrrrrrrrrrwwwwwwrrrrrrrrrrrrr
rrrrrrrrrrrrrwwwwwwrrrrrrrrrrrrr
rrrrrrrrrrrrrwwwwwwrrrrrrrrrrrrr
rrrrrrrrrrrrrwwwwwwrrrrrrrrrrrrr
rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
C# 265/266 209 bytes
using System;using c=System.Console;class P{static void Main(){for(int i=0,j;i<32;i++){for(j=0;j<32;j++){if((6<i&&i<26&&j>13&&j<19)||(6<j&&j<26&&i>13&&i<19))c.BackgroundColor=ConsoleColor.White;else c.BackgroundColor=ConsoleColor.Red;c.Write(" ");}c.WriteLine();}}}
I wrote "space" characters into console while changing Background Color. But if you think it is too tall and you count single character as 8 x 16 pixels, you simply double values near "j" like this (+ 1 byte and + c.ReadKey(); so it stops):
using System;using c=System.Console;class P{static void Main(){for(int i=0,j;i<32;i++){for(j=0;j<64;j++){if((6<i&&i<26&&j>26&&j<38)||(12<j&&j<52&&i>13&&i<19))c.BackgroundColor=ConsoleColor.White;else c.BackgroundColor=ConsoleColor.Red;c.Write(" ");}c.WriteLine();} c.ReadKey(); }}
56 bytes saved thanks to TheLethalCoder
namespace System{using static Console;class P{static void Main(){for(int i=0,j;i<32;i++,WriteLine())for(j=0;j<32;Write(""))BackgroundColor=(ConsoleColor)((6<i&i<26&j>13&j<19)|(j++<26&i>13&i<19&6<j)?15:12);}}}
TI-BASIC (TI-84 Plus C(S)E only), 58 bytes
:For(A,0,31
:For(B,0,31
:Pxl-On(A,B,RED
:End
:End
:For(A,6,25
:For(B,13,18
:Pxl-Off(A,B
:Pxl-Off(B,A
:End
:End
HTML + JavaScript (ES6), 13 + 112 = 125 bytes
C=c.getContext`2d`
C[s='fillStyle']='red'
C[r='fillRect'](0,0,32,32)
C[s]='#fff'
C[r](6,13,20,6)
C[r](13,6,6,20)
<canvas id=c>
HTML + JavaScript (ES6), 13 + 114 = 127 bytes
with(c.getContext`2d`)fillStyle='red',fillRect(0,0,32,32),fillStyle='#fff',fillRect(6,13,20,6),fillRect(13,6,6,20)
<canvas id=c>
HTML + CSS, 15 + 117 bytes = 132 bytes
Using a flexbox wrapper with an inset box-shadow.
b{display:flex;box-shadow:red 0 0 0 6px inset;padding:2px;width:28px;flex-wrap:wrap}a{border:solid red 4px;margin:3px
<b><a><a><a><a>
HTML + CSS, 18 + 139 137 122 bytes = 140 bytes
Previous answer using border and an intermediate flexbox wrapper with negative margin.
i,b,a{display:inline-flex}i{border:solid red 6px}b{margin:-4px;width:28px;flex-wrap:wrap}a{border:solid red 4px;margin:3px
<i><b><a><a><a><a>
Ruby + ruby2d, 106+8 = 114 bytes
Uses the -rruby2d flag.
[[32,32],[6,20,13,6],[20,6,6,13]].map{|w,h,x,y|Rectangle.new width:w,height:h,x:x,y:y}[0].color='red'
show
Image output:
Löve2D, 139 138 Bytes
l=love g=l.graphics c=g.setColor r=g.rectangle f="fill"w=255 l.draw=loadstring"c(w,0,0)r(f,0,0,32,32)c(w,w,w)r(f,13,6,6,20)r(f,6,13,20,6)"
Saved one byte thanks to ThePirateBay
Ungolfed
function love.draw()
love.graphics.setColor(255,0,0)
love.graphics.rectangle("fill",0,0,32,32)
love.graphics.setColor(255,255,255)
love.graphics.rectangle("fill",13,6,6,20)
love.graphics.rectangle("fill",6,13,20,6)
end


























