Yin and yang

From Rosetta Code
Jump to: navigation, search
Task
Yin and yang
You are encouraged to solve this task according to the task description, using any language you may know.

Create a function that given a variable representing size, generates a Yin and yang also known as a Taijitu symbol scaled to that size.

Generate and display the symbol generated for two different (small) sizes.

Contents

[edit] Ada

Library: GtkAda

Uses the Cairo component of GtkAda to create and save as png

YinYangAda.png
with Glib; use Glib;
with Cairo; use Cairo;
with Cairo.Png; use Cairo.Png;
with Cairo.Image_Surface; use Cairo.Image_Surface;
 
procedure YinYang is
subtype Dub is Glib.Gdouble;
 
procedure Draw (C : Cairo_Context; x : Dub; y : Dub; r : Dub) is begin
Arc (C, x, y, r + 1.0, 1.571, 7.854);
Set_Source_Rgb (C, 0.0, 0.0, 0.0); Fill (C);
Arc_Negative (C, x, y - r / 2.0, r / 2.0, 1.571, 4.712);
Arc (C, x, y + r / 2.0, r / 2.0, 1.571, 4.712);
Arc_Negative (C, x, y, r, 4.712, 1.571);
Set_Source_Rgb (C, 1.0, 1.0, 1.0); Fill (C);
Arc (C, x, y - r / 2.0, r / 5.0, 1.571, 7.854);
Set_Source_Rgb (C, 0.0, 0.0, 0.0); Fill (C);
Arc (C, x, y + r / 2.0, r / 5.0, 1.571, 7.854);
Set_Source_Rgb (C, 1.0, 1.0, 1.0); Fill (C);
end Draw;
 
Surface : Cairo_Surface;
Context : Cairo_Context;
Status : Cairo_Status;
begin
Surface := Create (Cairo_Format_ARGB32, 200, 200);
Context := Create (Surface);
Draw (Context, 120.0, 120.0, 75.0);
Draw (Context, 35.0, 35.0, 30.0);
Status := Write_To_Png (Surface, "YinYangAda.png");
pragma Assert (Status = Cairo_Status_Success);
end YinYang;

[edit] ALGOL 68

Works with: ALGOL 68 version Revision 1 - With Currying extensions to language.
Works with: ALGOL 68G version Any - tested with release 1.18.0-9h.tiny.
INT scale x=2, scale y=1;
CHAR black="#", white=".", clear=" ";
 
PROC print yin yang = (REAL radius)VOID:(
 
PROC in circle = (REAL centre x, centre y, radius, x, y)BOOL:
(x-centre x)**2+(y-centre y)**2 <= radius**2;
 
PROC (REAL, REAL)BOOL
in big circle = in circle(0, 0, radius, , ),
in white semi circle = in circle(0, +radius/2, radius/2, , ),
in small black circle = in circle(0, +radius/2, radius/6, , ),
in black semi circle = in circle(0, -radius/2, radius/2, , ),
in small white circle = in circle(0, -radius/2, radius/6, , );
 
FOR sy FROM +ROUND(radius * scale y) BY -1 TO -ROUND(radius * scale y) DO
FOR sx FROM -ROUND(radius * scale x) TO +ROUND(radius * scale x) DO
REAL x=sx/scale x, y=sy/scale y;
print(
IF in big circle(x, y) THEN
IF in white semi circle(x, y) THEN
IF in small black circle(x, y) THEN black ELSE white FI
ELIF in black semi circle(x, y) THEN
IF in small white circle(x, y) THEN white ELSE black FI
ELIF x < 0 THEN white ELSE black FI
ELSE
clear
FI
)
OD;
print(new line)
OD
);
 
main:(
print yin yang(17);
print yin yang(8)
)

Output:

                                  .                                  
                       ....................###                       
                  ...........................######                  
               ................................#######               
             ....................................#######             
          ........................................#########          
         .......................#####..............#########         
       .......................#########............###########       
      .......................###########...........############      
    .........................###########...........##############    
    ..........................#########............##############    
   .............................#####..............###############   
  ................................................#################  
 ................................................################### 
 ..............................................##################### 
 ............................................####################### 
 ..........................................######################### 
...................................##################################
 .........................########################################## 
 .......................############################################ 
 .....................############################################## 
 ...................################################################ 
  .................################################################  
   ...............##############.....#############################   
    ..............############.........##########################    
    ..............###########...........#########################    
      ............###########...........#######################      
       ...........############.........#######################       
         .........##############.....#######################         
          .........########################################          
             .......####################################             
               .......################################               
                  ......###########################                  
                       ...####################                       
                                  #                                  
                .                
         .............##         
      .................####      
    ...........###......#####    
   ...........#####......#####   
  .............###......#######  
 ......................######### 
 .....................########## 
.................################
 ..........##################### 
 .........###################### 
  .......######...#############  
   .....######.....###########   
    .....######...###########    
      ....#################      
         ..#############         
                #                

[edit] Applesoft BASIC

0 GOTO 6
1Y=R:D=1-R:X=0:FORC=0TO1STEP0:M=D>=0:Y=Y-M:D=D-Y*2*M:D=D+X*2+3:HPLOTXC-X,YC+YTOXC+X,YC+Y:HPLOTXC-Y,YC+XTOXC+Y,YC+X:HPLOTXC-X,YC-YTOXC+X,YC-Y:HPLOTXC-Y,YC-XTOXC+Y,YC-X:X=X+1:C=X>=Y:NEXTC:RETURN
2Y=R:D=1-R:X=0:FORC=0TO1STEP0:M=D>=0:Y=Y-M:D=D-Y*2*M:D=D+X*2+3:HPLOTXC-X,YC+Y:HPLOTXC+X,YC+Y:HPLOTXC-Y,YC+X:HPLOTXC+Y,YC+X:HPLOTXC-X,YC-Y:HPLOTXC+X,YC-Y:HPLOTXC-Y,YC-X:HPLOTXC+Y,YC-X:X=X+1:C=X>=Y:NEXTC:RETURN
3Y=R:D=1-R:X=0:FORC=0TO1STEP0:M=D>=0:Y=Y-M:D=D-Y*2*M:D=D+X*2+3:HPLOTXC,YC+YTOXC+X,YC+Y:HPLOTXC,YC+XTOXC+Y,YC+X:HPLOTXC,YC-YTOXC+X,YC-Y:HPLOTXC,YC-XTOXC+Y,YC-X:X=X+1:C=X>=Y:NEXTC:RETURN
 
6 HGR2 : HCOLOR = 3 : HPLOT 0,0 : CALL 62454
7 XC = 60 : YC = 60 : R = 30 : GOSUB 100YINYANG
8 XC = 180 : YC = 80 : R = 60 : GOSUB 100YINYANG
9 END
 
100 YP = YC : S = R
110 HCOLOR = 0: GOSUB 3FILLHALFCIRCLE
120 HCOLOR = 3:YC = YP - S / 2 : R = S / 2 : GOSUB 1FILLCIRCLE
130 HCOLOR = 0
140 YC = YP + S / 2 : GOSUB 1FILLCIRCLE
150 YC = YP - S / 2 : R = S / 6 : GOSUB 1FILLCIRCLE
160 HCOLOR = 3
170 YC = YP + S / 2 : GOSUB 1FILLCIRCLE
180 HCOLOR = 0 : YC = YP : R = S : GOSUB 2CIRCLE
190 RETURN

[edit] Asymptote

The resulting EPS, converted to SVG
unitsize(1 inch);
 
fill(scale(6)*unitsquare, invisible);
 
picture yinyang(pair center, real radius) {
picture p;
fill(p, unitcircle, white);
fill(p, arc(0, S, N) -- cycle, black);
fill(p, circle(N/2, 1/2), white);
fill(p, circle(S/2, 1/2), black);
fill(p, circle(N/2, 1/5), black);
fill(p, circle(S/2, 1/5), white);
draw(p, unitcircle, linewidth((1/32) * inch) + gray(0.5));
return shift(center) * scale(radius) * p;
}
 
add(yinyang((1 + 1/4, 4 + 3/4), 1));
add(yinyang((3 + 3/4, 2 + 1/4), 2));

[edit] AutoHotkey

Yin-yang-ahk.png

Requires the GDI+ Standard Library by tic: http://www.autohotkey.com/forum/viewtopic.php?t=32238

Yin_and_Yang(50,  50, A_ScriptDir "\YinYang1.png")
Yin_and_Yang(300, 300,A_ScriptDir "\YinYang2.png")
 
Yin_and_Yang(width, height, fileName
, color1=0xFFFFFFFF, color2=0xFF000000, outlineWidth=1){
 
pToken := gdip_Startup()
pBitmap := gdip_CreateBitmap(w := width, h := height)
w-=1, h-=1
pGraphics:= gdip_GraphicsFromImage(pBitmap)
pBrushW := gdip_BrushCreateSolid(color1)
pBrushB := gdip_BrushCreateSolid(color2)
 
gdip_SetSmoothingMode(pGraphics, 4) ; Antialiasing
 
If (outlineWidth){
pPen := gdip_CreatePen(0xFF000000, outlineWidth)
gdip_DrawEllipse(pGraphics, pPen, 0, 0, w, h)
gdip_DeletePen(pPen)
}
 
gdip_FillPie(pGraphics, pBrushB, 0, 0, w, h, -90, 180)
gdip_FillPie(pGraphics, pBrushW, 0, 0, w, h, 90, 180)
gdip_FillEllipse(pGraphics, pBrushB, w//4, h//2, w//2, h//2)
gdip_FillEllipse(pGraphics, pBrushW, w//4, 0 , w//2, h//2)
gdip_FillEllipse(pGraphics, pBrushB, 5*w//12, h//6, w//6, h//6)
gdip_FillEllipse(pGraphics, pBrushW, 5*w//12, 4*h//6,w//6,h//6)
 
r := gdip_SaveBitmapToFile(pBitmap, filename)
 
; cleanup:
gdip_DeleteBrush(pBrushW), gdip_deleteBrush(pBrushB)
gdip_DisposeImage(pBitmap)
gdip_DeleteGraphics(pGraphics)
gdip_Shutdown(pToken)
return r
}

[edit] BBC BASIC

Yinyangbbc.gif
      PROCyinyang(200, 200, 100)
PROCyinyang(700, 400, 300)
END
 
DEF PROCyinyang(xpos%, ypos%, size%)
CIRCLE xpos%, ypos%, size%
LINE xpos%, ypos%+size%, xpos%, ypos%-size%
FILL xpos%+size%/2, ypos%
CIRCLE FILL xpos%, ypos%-size%/2, size%/2+2
GCOL 15
CIRCLE FILL xpos%, ypos%+size%/2, size%/2+2
CIRCLE FILL xpos%, ypos%-size%/2, size%/6+2
GCOL 0
CIRCLE FILL xpos%, ypos%+size%/2, size%/6+2
CIRCLE xpos%, ypos%, size%
ENDPROC

[edit] C

Writes to stdout a SVG file with two yin-yangs (no, it's really just that big): Yinyang-C.svg

#include <stdio.h>
 
void draw_yinyang(int trans, double scale)
{
printf("<use xlink:href='#y' transform='translate(%d,%d) scale(%g)'/>",
trans, trans, scale);
}
 
int main()
{ printf(
"<?xml version='1.0' encoding='UTF-8' standalone='no'?>\n"
"<!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 1.1//EN'\n"
" 'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd'>\n"
"<svg xmlns='http://www.w3.org/2000/svg' version='1.1'\n"
" xmlns:xlink='http://www.w3.org/1999/xlink'\n"
" width='30' height='30'>\n"
" <defs><g id='y'>\n"
" <circle cx='0' cy='0' r='200' stroke='black'\n"
" fill='white' stroke-width='1'/>\n"
" <path d='M0 -200 A 200 200 0 0 0 0 200\n"
" 100 100 0 0 0 0 0 100 100 0 0 1 0 -200\n"
" z' fill='black'/>\n"
" <circle cx='0' cy='100' r='33' fill='white'/>\n"
" <circle cx='0' cy='-100' r='33' fill='black'/>\n"
" </g></defs>\n");
draw_yinyang(20, .05);
draw_yinyang(8, .02);
printf("</svg>");
return 0;
}

[edit] C#

This example does not show the output mentioned in the task description on this page (or a page linked to from here). Please ensure that it meets all task requirements and remove this message.
Note that phrases in task descriptions such as "print and display" and "print and show" for example, indicate that (reasonable length) output be a part of a language's solution.


Translation of: Visual Basic .NET (Cleaned up)

 
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Paint += Form1_Paint;
}
 
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
 
DrawTaijitu(g, new Point(50, 50), 200, true);
DrawTaijitu(g, new Point(10, 10), 60, true);
}
 
private void DrawTaijitu(Graphics g, Point pt, int width, bool hasOutline)
{
g.FillPie(Brushes.Black, pt.X, pt.Y, width, width, 90, 180);
g.FillPie(Brushes.White, pt.X, pt.Y, width, width, 270, 180);
float headSize = Convert.ToSingle(width * 0.5);
float headXPosition = Convert.ToSingle(pt.X + (width * 0.25));
g.FillEllipse(Brushes.Black, headXPosition, Convert.ToSingle(pt.Y), headSize, headSize);
g.FillEllipse(Brushes.White, headXPosition, Convert.ToSingle(pt.Y + (width * 0.5)), headSize, headSize);
float headBlobSize = Convert.ToSingle(width * 0.125);
float headBlobXPosition = Convert.ToSingle(pt.X + (width * 0.4375));
g.FillEllipse(Brushes.White, headBlobXPosition, Convert.ToSingle(pt.Y + (width * 0.1875)), headBlobSize, headBlobSize);
g.FillEllipse(Brushes.Black, headBlobXPosition, Convert.ToSingle(pt.Y + (width * 0.6875)), headBlobSize, headBlobSize);
if (hasOutline) g.DrawEllipse(Pens.Black, pt.X, pt.Y, width, width);
}
}

[edit] D

Translation of: Python
import std.stdio, std.algorithm, std.array, std.math, std.range,
std.conv, std.typecons;
 
string yinYang(in int n) {
enum : char { empty = ' ', white = '.', black = '#' }
const radii = [1, 3, 6].map!(i => i * n).array;
auto ranges = radii.map!(r => iota(-r, r + 1).array).array;
alias V = Tuple!(int,"x", int,"y");
V[][] squares, circles;
squares = ranges.map!(r=> cartesianProduct(r,r).map!V.array).array;
foreach (sqrPoints, radius; zip(squares, radii))
circles ~= sqrPoints.filter!(p => p[].hypot <= radius).array;
auto m = squares[$ - 1].zip(empty.repeat).assocArray;
foreach (p; circles[$ - 1])
m[p] = black;
foreach (p; circles[$ - 1])
if (p.x > 0)
m[p] = white;
foreach (p; circles[$ - 2]) {
m[V(p.x, p.y + 3 * n)] = black;
m[V(p.x, p.y - 3 * n)] = white;
}
foreach (p; circles[$ - 3]) {
m[V(p.x, p.y + 3 * n)] = white;
m[V(p.x, p.y - 3 * n)] = black;
}
return ranges[$ - 1]
.map!(y => ranges[$ - 1].retro.map!(x => m[V(x, y)]).text)
.join("\n");
}
 
void main() {
2.yinYang.writeln;
1.yinYang.writeln;
}
Output:
            .            
        ........#        
      ...........##      
     .............##     
    ........#.....###    
   ........###....####   
  ........#####....####  
  .........###....#####  
 ...........#.....###### 
 .................###### 
 ................####### 
 ...............######## 
.............############
 ........############### 
 .......################ 
 ......################# 
 ......#####.########### 
  .....####...#########  
  ....####.....########  
   ....####...########   
    ...#####.########    
     ..#############     
      ..###########      
        .########        
            #            
      .      
   ......#   
  ....#..##  
 ....###..## 
 .....#..### 
 ........### 
.......######
 ...######## 
 ...##.##### 
 ..##...#### 
  ..##.####  
   .######   
      #      

A simpler alternative version:

Translation of: PicoLisp
import std.stdio, std.math;
 
void yinYang(in int r) {
foreach (immutable y; -r .. r + 1) {
foreach (immutable x; -2 * r .. 2 * r + 1) {
enum circle = (in int c, in int r) pure nothrow =>
r ^^ 2 >= (x / 2) ^^ 2 + (y - c) ^^ 2;
write(circle(-r / 2, r / 6) ? '#' :
circle( r / 2, r / 6) ? '.' :
circle(-r / 2, r / 2) ? '.' :
circle( r / 2, r / 2) ? '#' :
circle( 0, r) ? "#."[x < 0] :
' ');
}
writeln;
}
}
 
void main() {
16.yinYang;
}
Output:
                               ...                               
                     ...................####                     
                 ...........................####                 
             .................................######             
           ...................................########           
         .......................................########         
       ........................###..............##########       
     ........................#######............############     
     ......................###########............##########     
   ..........................#######............##############   
   ............................###..............##############   
 ...............................................################ 
 .............................................################## 
 .............................................################## 
 ...........................................#################### 
 .......................................######################## 
..................................###############################
 ........................####################################### 
 ....................########################################### 
 ..................############################################# 
 ..................############################################# 
 ................############################################### 
   ..............##############...############################   
   ..............############.......##########################   
     ..........############...........######################     
     ............############.......########################     
       ..........##############...########################       
         ........#######################################         
           ........###################################           
             ......#################################             
                 ....###########################                 
                     ....###################                     
                               ###                               

[edit] Delphi

Instructions: Create an empty project. Paste code below and adjust the interface section for the form. Then assign 'FormCreate' to TForm1.OnCreate and 'FormPaint' to TForm1.OnPaint.

procedure DrawYinAndYang(Canv: TCanvas; R: TRect);
begin
Canv.Brush.Color := clWhite;
Canv.Pen.Color := clWhite;
Canv.Pie(R.Left, R.Top, R.Right, R.Bottom,
(R.Right + R.Left) div 2, R.Top, (R.Right + R.Left) div 2, R.Bottom);
Canv.Brush.Color := clBlack;
Canv.Pen.Color := clBlack;
Canv.Pie(R.Left, R.Top, R.Right, R.Bottom,
(R.Right + R.Left) div 2, R.Bottom, (R.Right + R.Left) div 2, R.Top);
Canv.Brush.Color := clWhite;
Canv.Pen.Color := clWhite;
Canv.Ellipse((R.Right + 3 * R.Left) div 4, R.Top,
(3 * R.Right + R.Left) div 4, (R.Top + R.Bottom) div 2);
Canv.Brush.Color := clBlack;
Canv.Pen.Color := clBlack;
Canv.Ellipse((R.Right + 3 * R.Left) div 4, (R.Top + R.Bottom) div 2,
(3 * R.Right + R.Left) div 4, R.Bottom);
 
Canv.Brush.Color := clWhite;
Canv.Pen.Color := clWhite;
Canv.Ellipse((7 * R.Right + 9 * R.Left) div 16, (11 * R.Bottom + 5 * R.Top) div 16,
(9 * R.Right + 7 * R.Left) div 16, (13 * R.Bottom + 3 * R.Top) div 16);
Canv.Brush.Color := clBlack;
Canv.Pen.Color := clBlack;
Canv.Ellipse((7 * R.Right + 9 * R.Left) div 16, (3 * R.Bottom + 13 * R.Top) div 16,
(9 * R.Right + 7 * R.Left) div 16, (5 * R.Bottom + 11 * R.Top) div 16);
end;
 
procedure TForm1.FormCreate(Sender: TObject);
begin
ClientWidth := 400;
ClientHeight := 400;
end;
 
procedure TForm1.FormPaint(Sender: TObject);
var
R: TRect;
begin
R := ClientRect;
Canvas.Brush.Color := clGray;
Canvas.FillRect(R);
 
InflateRect(R, -50, -50);
OffsetRect(R, -40, -40);
DrawYinAndYang(Canvas, R);
 
InflateRect(R, -90, -90);
OffsetRect(R, 170, 170);
DrawYinAndYang(Canvas, R);
end;
 


This example does not show the output mentioned in the task description on this page (or a page linked to from here). Please ensure that it meets all task requirements and remove this message.
Note that phrases in task descriptions such as "print and display" and "print and show" for example, indicate that (reasonable length) output be a part of a language's solution.


[edit] DWScript

Translation of: D
type
TColorFuncX = function (x : Integer) : Integer;
 
type
TSquareBoard = class
Scale : Integer;
Pix : array of array of Integer;
 
constructor Create(aScale : Integer);
begin
Scale := aScale;
Pix := new Integer[aScale*12+1, aScale*12+1];
end;
 
method Print;
begin
var i, j : Integer;
for i:=0 to Pix.High do begin
for j:=0 to Pix.High do begin
case Pix[j, i] of
1 : Print('.');
2 : Print('#');
else
Print(' ');
end;
end;
PrintLn('');
end;
end;
 
method DrawCircle(cx, cy, cr : Integer; color : TColorFuncX);
begin
var rr := Sqr(cr*Scale);
var x, y : Integer;
for x := 0 to Pix.High do begin
for y := 0 to Pix.High do begin
if Sqr(x-cx*Scale)+Sqr(y-cy*Scale)<=rr then
Pix[x, y] := color(x);
end;
end;
end;
 
method ColorHalf(x : Integer) : Integer;
begin
if (x<6*Scale) then
Result:=1
else Result:=2;
end;
 
method ColorYin(x : Integer) : Integer;
begin
Result:=2;
end;
 
method ColorYang(x : Integer) : Integer;
begin
Result:=1;
end;
 
method YinYang;
begin
DrawCircle(6, 6, 6, ColorHalf);
DrawCircle(6, 3, 3, ColorYang);
DrawCircle(6, 9, 3, ColorYin);
DrawCircle(6, 9, 1, ColorYang);
DrawCircle(6, 3, 1, ColorYin);
end;
 
end;
 
var sq := new TSquareBoard(2);
sq.YinYang;
sq.Print;
 
sq := new TSquareBoard(1);
sq.YinYang;
sq.Print;

Output:

            .            
        ........#        
      ...........##      
     .............##     
    ........#.....###    
   ........###....####   
  ........#####....####  
  .........###....#####  
 ...........#.....###### 
 .................###### 
 ................####### 
 ...............######## 
............#############
 ........############### 
 .......################ 
 ......################# 
 ......#####.########### 
  .....####...#########  
  ....####.....########  
   ....####...########   
    ...#####.########    
     ..#############     
      ..###########      
        .########        
            #            
      .      
   ......#   
  ....#..##  
 ....###..## 
 .....#..### 
 ........### 
......#######
 ...######## 
 ...##.##### 
 ..##...#### 
  ..##.####  
   .######   
      #      

[edit] Go

There are some emerging third-party 2D graphics libraries for Go; meanwhile, here is an SVG solution using only standard libraries.

GoYinYang.svg
package main
 
import (
"fmt"
"os"
"text/template"
)
 
var tmpl = `<?xml version="1.0"?>
<svg xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
width="210" height="150">
<symbol id="yy" viewBox="0 0 200 200">
<circle stroke="black" stroke-width="2" fill="white"
cx="100" cy="100" r="99" />
<path fill="black"
d="M100 100 a49 49 0 0 0 0 -98
v-1 a99 99 0 0 1 0 198
v-1 a49 49 0 0 1 0 -98" />
<circle fill="black" cx="100" cy="51" r="17" />
<circle fill="white" cx="100" cy="149" r="17" />
</symbol>
{{range .}}<use xlink:href="#yy"
x="{{.X}}" y="{{.Y}}" width="{{.Sz}}" height="{{.Sz}}"/>
{{end}}</svg>
`

 
// structure specifies position and size to draw symbol
type xysz struct {
X, Y, Sz int
}
 
// example data to specify drawing the symbol twice,
// with different position and size.
var yys = []xysz{
{20, 20, 100},
{140, 30, 60},
}
 
func main() {
xt := template.New("")
template.Must(xt.Parse(tmpl))
f, err := os.Create("yy.svg")
if err != nil {
fmt.Println(err)
return
}
if err := xt.Execute(f, yys); err != nil {
fmt.Println(err)
}
f.Close()
}

[edit] Haskell

Yin and Yang Haskell SVG output.

This program uses the diagrams package to produce the Yin and Yang image. The package implements an embedded DSL for producing vector graphics. Depending on the command-line arguments, the program can generate SVG, PNG, PDF or PostScript output. The sample output was created with the command yinyang -o YinYang-Haskell.svg.

{-# LANGUAGE NoMonomorphismRestriction #-}
 
import Diagrams.Prelude
import Diagrams.Backend.Cairo.CmdLine
 
yinyang = lw 0 $
perim # lw 0.003 <>
torus white black # xform id <>
torus black white # xform negate <>
clipBy perim base
where perim = arc 0 (360 :: Deg) # scale (1/2)
torus c c' = circle (1/3) # fc c' <> circle 1 # fc c
xform f = translateY (f (1/4)) . scale (1/4)
base = rect (1/2) 1 # fc white ||| rect (1/2) 1 # fc black
 
main = defaultMain $
pad 1.1 $
beside (2,-1) yinyang (yinyang # scale (1/4))

[edit] Icon and Unicon

Sample Output
link graphics
 
procedure main()
YinYang(100)
YinYang(40,"blue","yellow","white")
WDone() # quit on Q/q
end
 
procedure YinYang(R,lhs,rhs,bg) # draw YinYang with radius of R pixels and ...
/lhs := "white" # left hand side
/rhs := "black" # right hand side
/bg := "grey" # background
 
wsize := 2*(C := R + (margin := R/5))
 
W := WOpen("size="||wsize||","||wsize,"bg="||bg) | stop("Unable to open Window")
WAttrib(W,"fg="||lhs) & FillCircle(W,C,C,R,+dtor(90),dtor(180)) # main halves
WAttrib(W,"fg="||rhs) & FillCircle(W,C,C,R,-dtor(90),dtor(180))
WAttrib(W,"fg="||lhs) & FillCircle(W,C,C+R/2,R/2,-dtor(90),dtor(180)) # sub halves
WAttrib(W,"fg="||rhs) & FillCircle(W,C,C-R/2,R/2,dtor(90),dtor(180))
WAttrib(W,"fg="||lhs) & FillCircle(W,C,C-R/2,R/8) # dots
WAttrib(W,"fg="||rhs) & FillCircle(W,C,C+R/2,R/8)
end

graphics.icn provides graphical procedures

[edit] J

Based on the Python implementation:

yinyang=:3 :0
radii=. y*1 3 6
ranges=. i:each radii
squares=. ,"0/~each ranges
circles=. radii ([ >: +/"1&.:*:@])each squares
cInds=. ({:radii) +each circles #&(,/)each squares
 
M=. ' *.' {~ circles (* 1 + 0 >: {:"1)&(_1&{::) squares
offset=. 3*y,0
M=. '*' ((_2 {:: cInds) <@:+"1 offset)} M
M=. '.' ((_2 {:: cInds) <@:-"1 offset)} M
M=. '.' ((_3 {:: cInds) <@:+"1 offset)} M
M=. '*' ((_3 {:: cInds) <@:-"1 offset)} M
)

Note: although the structure of this program is based on the python implementation, some details are different. In particular, in the python implementation, the elements of squares and circles have no x,y structure -- they are flat list of coordinates.

Here, the three squares are each 3 dimensional arrays. The first two dimensions correspond to the x and y values and the last dimension is 2 (the first value being the y coordinate and the second being the x coordinate -- having the dimensions as y,x pairs like this works because in J the first dimension of a matrix is the number of rows and the second dimension is the number of columns).

Also, the three elements in the variable circles are represented by 2 dimensional arrays. The dimensions correspond to x and y values and the values are bits -- 1 if the corresponding coordinate pair in squares is a member of the circle and 0 if not.

Finally, the variable cInds corresponds very closely to the variable circles in the python code. Except, instead of having y and x values, cInds has indices into M. In other words, I added the last value from radii to the y and x values. In other words, instead of having values in the range -18..18, I would have values in the range 0..36 (but replace 18 and 36 with whatever values are appropriate).

Example use:

   yinyang 1
.
......*
....*..**
....***..**
.....*..***
........***
.......******
...********
...**.*****
..**...****
..**.****
.******
*
yinyang 2
.
........*
...........**
.............**
........*.....***
........***....****
........*****....****
.........***....*****
...........*.....******
.................******
................*******
...............********
.............************
........***************
.......****************
......*****************
......*****.***********
.....****...*********
....****.....********
....****...********
...*****.********
..*************
..***********
.********
*

[edit] Java

[edit] Graphical

This example shows how to draw using the built in graphics context of Java.

Java-yinyang-80.png
Java-yinyang-240.png
package org.rosettacode.yinandyang;
 
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
 
public class YinYangGenerator
{
private final int size;
 
public YinYangGenerator(final int size)
{
this.size = size;
}
 
/**
* Draw a yin yang symbol on the given graphics context.
*/

public void drawYinYang(final Graphics graphics)
{
// Preserve the color for the caller
final Color colorSave = graphics.getColor();
 
graphics.setColor(Color.WHITE);
// Use fillOval to draw a filled in circle
graphics.fillOval(0, 0, size-1, size-1);
 
graphics.setColor(Color.BLACK);
// Use fillArc to draw part of a filled in circle
graphics.fillArc(0, 0, size-1, size-1, 270, 180);
graphics.fillOval(size/4, size/2, size/2, size/2);
 
graphics.setColor(Color.WHITE);
graphics.fillOval(size/4, 0, size/2, size/2);
graphics.fillOval(7*size/16, 11*size/16, size/8, size/8);
 
graphics.setColor(Color.BLACK);
graphics.fillOval(7*size/16, 3*size/16, size/8, size/8);
// Use drawOval to draw an empty circle for the outside border
graphics.drawOval(0, 0, size-1, size-1);
 
// Restore the color for the caller
graphics.setColor(colorSave);
}
 
/**
* Create an image containing a yin yang symbol.
*/

public Image createImage(final Color bg)
{
// A BufferedImage creates the image in memory
final BufferedImage image = new BufferedImage(size, size, BufferedImage.TYPE_INT_RGB);
// Get the graphics object for the image; note in many
// applications you actually use Graphics2D for the
// additional API calls
final Graphics graphics = image.getGraphics();
// Color in the background of the image
graphics.setColor(bg);
graphics.fillRect(0,0,size,size);
drawYinYang(graphics);
return image;
}
 
public static void main(final String args[])
{
final int size = Integer.parseInt(args[0]);
final YinYangGenerator generator = new YinYangGenerator(size);
 
final JFrame frame = new JFrame("Yin Yang Generator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final Image yinYang = generator.createImage(frame.getBackground());
// Use JLabel to display an image
frame.add(new JLabel(new ImageIcon(yinYang)));
frame.pack();
frame.setVisible(true);
}
}

[edit] Text

Translation of: PicoLisp
Works with: Java version 1.8
import java.util.Collection;
import java.util.Map;
import java.util.Optional;
import java.util.function.BooleanSupplier;
import java.util.function.Supplier;
import java.util.stream.IntStream;
import java.util.stream.Stream;
 
import static java.util.Collections.singletonMap;
 
public interface YinYang {
public static boolean circle(
int x,
int y,
int c,
int r
) {
return
(r * r) >=
((x = x / 2) * x)
+ ((y = y - c) * y)
;
}
 
public static String pixel(int x, int y, int r) {
return Stream.<Map<BooleanSupplier, Supplier<String>>>of(
singletonMap(
() -> circle(x, y, -r / 2, r / 6),
() -> "#"
),
singletonMap(
() -> circle(x, y, r / 2, r / 6),
() -> "."
),
singletonMap(
() -> circle(x, y, -r / 2, r / 2),
() -> "."
),
singletonMap(
() -> circle(x, y, r / 2, r / 2),
() -> "#"
),
singletonMap(
() -> circle(x, y, 0, r),
() -> x < 0 ? "." : "#"
)
)
.sequential()
.map(Map::entrySet)
.flatMap(Collection::stream)
.filter(e -> e.getKey().getAsBoolean())
.map(Map.Entry::getValue)
.map(Supplier::get)
.findAny()
.orElse(" ")
;
}
 
public static void yinYang(int r) {
IntStream.rangeClosed(-r, r)
.mapToObj(
y ->
IntStream.rangeClosed(
0 - r - r,
r + r
)
.mapToObj(x -> pixel(x, y, r))
.reduce("", String::concat)
)
.forEach(System.out::println)
;
}
 
public static void main(String... arguments) {
Optional.of(arguments)
.filter(a -> a.length == 1)
.map(a -> a[0])
.map(Integer::parseInt)
.ifPresent(YinYang::yinYang)
;
}
}
 

Test:

> java YinYang 18
                                   ...
                         .....................##
                   .............................######
                 .................................######
             .......................................########
           ...........................................########
         ..........................###................##########
       ........................###########............############
       ........................###########............############
     ........................###############............############
   ............................###########............################
   ............................###########............################
   ................................###................################
 .....................................................##################
 ...................................................####################
 .................................................######################
 ...............................................########################
 .............................................##########################
......................................###################################
 ..........................#############################################
 ........................###############################################
 ......................#################################################
 ....................###################################################
 ..................#####################################################
   ................################...################################
   ................############...........############################
   ................############...........############################
     ............############...............########################
       ............############...........########################
       ............############...........########################
         ..........################...##########################
           ........###########################################
             ........#######################################
                 ......#################################
                   ......#############################
                         ..#####################
                                   ###

[edit] JavaScript

Another way, a more JavaScript-style way.

 
function Arc(posX,posY,radius,startAngle,endAngle,color){//Angle in radians.
this.posX=posX;
this.posY=posY;
this.radius=radius;
this.startAngle=startAngle;
this.endAngle=endAngle;
this.color=color;
}
//0,0 is the top left of the screen
var YingYang=[
new Arc(0.5,0.5,1,0.5*Math.PI,1.5*Math.PI,"white"),//Half white semi-circle
new Arc(0.5,0.5,1,1.5*Math.PI,0.5*Math.PI,"black"),//Half black semi-circle
new Arc(0.5,0.25,.5,0,2*Math.PI,"black"),//black circle
new Arc(0.5,0.75,.5,0,2*Math.PI,"white"),//white circle
new Arc(0.5,0.25,1/6,0,2*Math.PI,"white"),//small white circle
new Arc(0.5,0.75,1/6,0,2*Math.PI,"black")//small black circle
]
//Ying Yang is DONE!
//Now we'll have to draw it.
//We'll draw it in a matrix that way we can get results graphically or by text!
function Array2D(width,height){
this.height=height;
this.width=width;
this.array2d=[];
for(var i=0;i<this.height;i++){
this.array2d.push(new Array(this.width));
}
}
Array2D.prototype.resize=function(width,height){//This is expensive
//nheight and nwidth is the difference of the new and old height
var nheight=height-this.height,nwidth=width-this.width;
if(nwidth>0){
for(var i=0;i<this.height;i++){
if(i<height)
Array.prototype.push.apply(this.array2d[i],new Array(nwidth));
}
}
else if(nwidth<0){
for(var i=0;i<this.height;i++){
if(i<height)
this.array2d[i].splice(width,nwidth);
}
}
if(nheight>0){
Array.prototype.push.apply(this.array2d,new Array(width));
}
else if(nheight<0){
this.array2d.splice(height,nheight)
}
}
Array2D.prototype.loop=function(callback){
for(var i=0;i<this.height;i++)
for(var i2=0;i2<this.width;i++)
callback.call(this,this.array2d[i][i2],i,i2);
 
}
var mat=new Array2D(100,100);//this sounds fine;
YingYang[0];
//In construction.
 

[edit] Text

Translation of: ALGOL 68
YinYang = (function () {
var scale_x = 2,
scale_y = 1,
black = "#",
white = ".",
clear = " ",
out = "";
 
function draw(radius) {
function inCircle(centre_x, centre_y, radius, x, y) {
return Math.pow(x - centre_x, 2) + Math.pow(y - centre_y, 2) <= Math.pow(radius, 2)
}
var bigCircle = function (x, y) {
return inCircle(0, 0, radius, x, y)
}, whiteSemiCircle = function (x, y) {
return inCircle(0, radius / 2, radius / 2, x, y)
}, smallBlackCircle = function (x, y) {
return inCircle(0, radius / 2, radius / 6, x, y)
}, blackSemiCircle = function (x, y) {
return inCircle(0, -radius / 2, radius / 2, x, y)
}, smallWhiteCircle = function (x, y) {
return inCircle(0, -radius / 2, radius / 6, x, y)
};
i = 0
for (var sy = Math.round(radius * scale_y); sy >= -Math.round(radius * scale_y); sy--) {
//console.log(sy)
for (var sx = -Math.round(radius * scale_x); sx <= Math.round(radius * scale_x); sx++) {
 
var x = sx / scale_x,
y = sy / scale_y;
//out+=sx
//console.log(sx,bigCircle(x,y))
if (bigCircle(x, y)) {
//out+="";
if (whiteSemiCircle(x, y)) {
//console.log(x,y)
if (smallBlackCircle(x, y)) {
out += black
} else {
out += white
}
} else if (blackSemiCircle(x, y)) {
if (smallWhiteCircle(x, y)) {
out += white
} else {
out += black
}
} else if (x < 0) {
out += white
} else {
out += black
}
 
} else {
out += clear;
}
 
}
out += "\n";
}
return out;
}
return draw
})()
console.log(YinYang(17))
console.log(YinYang(8))

[edit] SVG

JavaScript is amazing in this case for the reason that in can be embedded in SVG itself! This is a SVG embedded in a HTML document; it can be isolated from the HTML document too, making it a standalone SVG

<!DOCTYPE html>
<html>
 
<head>
 
<body>
<svg
id="svg"
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
version="1.1"
width="100%"
height="100%">
</svg>
<script>
function makeElem(elemName, attribs) { //atribs must be an Object
var e = document.createElementNS("http://www.w3.org/2000/svg", elemName),
a, b, d = attribs.style;
for (a in attribs) {
if (attribs.hasOwnProperty(a)) {
 
if (a == 'style') {
for (b in d) {
if (d.hasOwnProperty(b)) {
e.style[b] = d[b];
}
}
continue;
}
e.setAttributeNS(null, a, attribs[a]);
}
}
return e;
}
var svg = document.getElementById("svg");
 
function drawYingYang(n, x, y) {
var d = n / 10;
h = d * 5, q = h / 2, t = q * 3;
//A white circle, for the bulk of the left-hand part
svg.appendChild(makeElem("circle", {
cx: h,
cy: h,
r: h,
fill: "white"
}));
//A black semicircle, for the bulk of the right-hand part
svg.appendChild(makeElem("path", {
d: "M " + (h + x) + "," + y + " A " + q + "," + q + " -" + d * 3 + " 0,1 " + (h + x) + "," + (n + y) + " z",
fill: "black"
}));
//Circles to extend each part
svg.appendChild(makeElem("circle", {
cx: h + x,
cy: q + y,
r: q,
fill: "white"
}));
svg.appendChild(makeElem("circle", {
cx: h + x,
cy: t + y,
r: q,
fill: "black"
}));
//The spots
svg.appendChild(makeElem("circle", {
cx: h + x,
cy: q + y,
r: d,
fill: "black"
}));
svg.appendChild(makeElem("circle", {
cx: h + x,
cy: t + y,
r: q,
fill: "black"
}));
svg.appendChild(makeElem("circle", {
cx: h + x,
cy: t + y,
r: d,
fill: "white"
}));
//An outline for the whole shape
svg.appendChild(makeElem("circle", {
cx: h + x,
cy: h + y,
r: h,
fill: "none",
stroke: "gray",
"stroke-width": d / 3
}));
if (svg.height.baseVal.valueInSpecifiedUnits < n) {
svg.setAttributeNS(null, "height", y * 1.25 + n + "px")
}
//svg.appendChild(makeElem("circle",{cx:"100", cy:h, r:"40", stroke:"black", "stroke-width":"2", fill:"red"}))
}
drawYingYang(100, 30, 30);
drawYingYang(1000, 200, 200);
</script>
</body>
</head>
 
</html>

[edit] Liberty BASIC

Liberty BASIC Graphic Output
    WindowWidth  =410
WindowHeight =440
 
open "Yin & Yang" for graphics_nf_nsb as #w
 
#w "trapclose [quit]"
 
call YinYang 200, 200, 200
call YinYang 120, 50, 50
 
wait
 
sub YinYang x, y, size
 
#w "up ; goto "; x; " "; y
#w "backcolor black ; color black"
#w "down ; circlefilled "; size /2
 
#w "color 255 255 255 ; backcolor 255 255 255"
#w "up  ; goto "; x -size /2; " "; y -size /2
#w "down ; boxfilled "; x; " "; y +size /2
 
#w "up ; goto "; x; " "; y -size /4
#w "down ; backcolor black ; color black  ; circlefilled "; size /4
#w "up ; goto "; x; " "; y -size /4
#w "down ; backcolor white ; color white ; circlefilled "; size /12
 
#w "up ; goto "; x; " "; y +size /4
#w "down ; backcolor white ; color white ; circlefilled "; size /4
#w "up ; goto "; x; " "; y +size /4
#w "down ; backcolor black ; color black ; circlefilled "; size /12
 
#w "up ; goto "; x; " "; y
#w "down ; color black ; circle "; size /2
 
#w "flush"
 
end sub
 
scan
 
wait
 
[quit]
close #w
end

[edit]

UCB Logo Graphic Output
Works with: UCB_Logo version 5.5
Works with: MSW_Logo version 6.5b
to taijitu :r 
 ; Draw a classic Taoist taijitu of the given radius centered on the current
 ; turtle position. The "eyes" are placed along the turtle's heading, the
 ; filled one in front, the open one behind.
 
 ; don't bother doing anything if the pen is not down
if not pendown? [stop]
 
 ; useful derivative values
localmake "r2 (ashift :r -1)
localmake "r4 (ashift :r2 -1)
localmake "r8 (ashift :r4 -1)
 
 ; remember where we started
localmake "start pos
 
 ; draw outer circle
pendown
arc 360 :r
 
 ; draw upper half of S
penup
forward :r2
pendown
arc 180 :r2
 
 ; and filled inner eye
arc 360 :r8
fill
 
 ; draw lower half of S
penup
back :r
pendown
arc -180 :r2
 
 ; other inner eye
arc 360 :r8
 
 ; fill this half of the symbol
penup
forward :r4
fill
 
 ; put the turtle back where it started
setpos :start
pendown
end
 
; demo code to produce image at right
clearscreen
pendown
hideturtle
taijitu 100
penup
forward 150
left 90
forward 150
pendown
taijitu 75

[edit] Mathematica

Mathca.png

Mathematica's ability to symbolically build up graphics is often underrated. The following function will create a yin-yang symbol with the parameter size indicating the diameter in multiples of 40 pixels.

YinYang[size_] := 
Graphics[{{Circle[{0, 0}, 2]}, {Disk[{0, 0},
2, {90 Degree, -90 Degree}]}, {White, Disk[{0, 1}, 1]}, {Black,
Disk[{0, -1}, 1]}, {Black, Disk[{0, 1}, 1/4]}, {White,
Disk[{0, -1}, 1/4]}}, ImageSize -> 40 size]

[edit] Metapost

Metapost output (once converted to jpg)

The "function" yinyang returns a picture (a primitive type) that can be drawn (and transformed of course in any way)

vardef yinyang(expr u) =
picture pic_;
path p_;
p_ := halfcircle scaled 2u rotated -90 --
halfcircle scaled u rotated 90 shifted (0, 1/2u) reflectedabout ((0,1), (0,-1)) --
halfcircle scaled u rotated -270 shifted (0, -1/2u) -- cycle;
 
pic_ := nullpicture;
addto pic_ contour fullcircle scaled 2u withcolor black;
addto pic_ contour p_ withcolor white;
addto pic_ doublepath p_ withcolor black withpen pencircle scaled 0.5mm;
addto pic_ contour fullcircle scaled 1/3u shifted (0, 1/2u) withcolor white;
addto pic_ contour fullcircle scaled 1/3u shifted (0, -1/2u) withcolor black;
pic_
enddef;
 
beginfig(1)
 % let's create a Yin Yang symbol with a radius of 5cm
draw yinyang(5cm) shifted (5cm, 5cm);
 % and another one, radius 2.5cm, rotated 180 degrees and translated
draw yinyang(2.5cm) rotated 180 shifted (11cm, 11cm);
endfig;
 
end.

[edit] NetRexx

Writes an SVG document to standard output: Yinyang-NRX.svg

Translation of: C
/* NetRexx */
 
options replace format comments java crossref savelog symbols binary
 
say "<?xml version='1.0' encoding='UTF-8' standalone='no'?>"
say "<!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 1.1//EN'"
say " 'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd'>"
say "<svg xmlns='http://www.w3.org/2000/svg' version='1.1'"
say " xmlns:xlink='http://www.w3.org/1999/xlink'"
say " width='30' height='30'>"
say " <defs><g id='y'>"
say " <circle cx='0' cy='0' r='200' stroke='black'"
say " fill='white' stroke-width='1'/>"
say " <path d='M0 -200 A 200 200 0 0 0 0 200"
say " 100 100 0 0 0 0 0 100 100 0 0 1 0 -200"
say " z' fill='black'/>"
say " <circle cx='0' cy='100' r='33' fill='white'/>"
say " <circle cx='0' cy='-100' r='33' fill='black'/>"
say " </g></defs>"
 
say draw_yinyang(20, 0.05)
say draw_yinyang(8, 0.02)
 
say "</svg>"
 
return
 
method draw_yinyang(trans = int, scale = double) inheritable static returns String
yy = String.format(" <use xlink:href='#y' transform='translate(%d,%d) scale(%g)'/>", -
[Object Integer(trans), Integer(trans), Double(scale)])
return yy

[edit] OCaml

open Graphics
 
let draw_yinyang x y radius black white =
let hr = radius / 2 in
let sr = radius / 6 in
set_color black;
set_line_width 6;
draw_circle x y radius;
set_line_width 0;
set_color black;
fill_arc x y radius radius 270 450;
set_color white;
fill_arc x y radius radius 90 270;
fill_arc x (y + hr) hr hr 270 450;
set_color black;
fill_arc x (y - hr) hr hr 90 270;
fill_circle x (y + hr) sr;
set_color white;
fill_circle x (y - hr) sr
 
let () =
open_graph "";
let width = size_x()
and height = size_y() in
set_color (rgb 200 200 200);
fill_rect 0 0 width height;
let w = width / 3
and h = height / 3 in
let r = (min w h) / 3 in
draw_yinyang w (h*2) (r*2) black white;
draw_yinyang (w*2) h r blue magenta;
ignore(read_key())

run with:

$ ocaml graphics.cma yinyang.ml

[edit] Perl

Yinyang-perl.svg
sub circle {
my ($radius, $cx, $cy, $fill, $stroke) = @_;
print "<circle cx='$cx' cy='$cy' r='$radius' ",
"fill='$fill' stroke='$stroke' stroke-width='1'/>\n";
}
 
sub yin_yang {
my ($rad, $cx, $cy, %opt) = @_;
my ($c, $w) = (1, 0);
$opt{fill} //= 'white';
$opt{stroke} //= 'black';
$opt{recurangle} //= 0;
 
print "<g transform='rotate($opt{angle}, $cx, $cy)'>"
if $opt{angle};
 
if ($opt{flip}) { ($c, $w) = ($w, $c) };
 
circle($rad, $cx, $cy, $opt{fill}, $opt{stroke});
 
print "<path d='M $cx ", $cy + $rad, "A ",
$rad/2, " ", $rad/2, " 0 0 $c $cx $cy ",
$rad/2, " ", $rad/2, " 0 0 $w $cx ", $cy - $rad, " ",
$rad, " ", $rad, " 0 0 $c $cx ", $cy + $rad, " ",
"z' fill='$opt{stroke}' stroke='none' />";
 
if ($opt{recur} and $rad > 1) {
# recursive "eyes" are slightly larger
yin_yang($rad/4, $cx, $cy + $rad/2, %opt,
angle => $opt{recurangle},
fill => $opt{stroke},
stroke => $opt{fill} );
yin_yang($rad/4, $cx, $cy - $rad/2, %opt,
angle => 180 + $opt{recurangle});
} else {
circle($rad/5, $cx, $cy + $rad/2, $opt{fill}, $opt{stroke});
circle($rad/5, $cx, $cy - $rad/2, $opt{stroke}, $opt{fill});
}
print "</g>" if $opt{angle};
}
 
print <<'HEAD';
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg xmlns="http://www.w3.org/2000/svg" version="1.1"
xmlns:xlink="http://www.w3.org/1999/xlink">
HEAD

 
yin_yang(200, 250, 250, recur=>1,
angle=>0, recurangle=>90, fill=>'white', stroke=>'black');
yin_yang(100, 500, 500);
 
print "</svg>"

Messy code. Note that the larger yin-yang is drawn recursively.

[edit] Perl 6

Yin-yang-perl6.svg

Translation / Modification of C and Perl examples.

sub circle ($rad, $cx, $cy, $fill = 'white', $stroke = 'black' ){
say "<circle cx='$cx' cy='$cy' r='$rad' fill='$fill' stroke='$stroke' stroke-width='1'/>";
}
 
sub yin_yang ($rad, $cx, $cy, :$fill = 'white', :$stroke = 'black', :$angle = 90) {
my ($c, $w) = (1, 0);
say "<g transform='rotate($angle, $cx, $cy)'>" if $angle;
circle($rad, $cx, $cy, $fill, $stroke);
say "<path d='M $cx {$cy + $rad}A {$rad/2} {$rad/2} 0 0 $c $cx $cy ",
"{$rad/2} {$rad/2} 0 0 $w $cx {$cy - $rad} $rad $rad 0 0 $c $cx ",
"{$cy + $rad} z' fill='$stroke' stroke='none' />";
circle($rad/5, $cx, $cy + $rad/2, $fill, $stroke);
circle($rad/5, $cx, $cy - $rad/2, $stroke, $fill);
say "</g>" if $angle;
}
 
say '<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink">'
;
 
yin_yang(40, 50, 50);
yin_yang(20, 120, 120);
 
say '</svg>';

Seems like something of a cheat since it relies on a web browser / svg image interpreter to actually view the output image. If that's the case, we may as well cheat harder. ;-)

sub cheat_harder ($scale) {
"<span style=\"font-size:$scale%;\">&#x262f;</span>";
}
 
say '<div><br /><br />', cheat_harder(700), cheat_harder(350), '</div>';


[edit] PHL

Translation of: ALGOL 68
module circles;
 
extern printf;
 
@Boolean in_circle(@Integer centre_x, @Integer centre_y, @Integer radius, @Integer x, @Integer y) [
return (x-centre_x)*(x-centre_x)+(y-centre_y)*(y-centre_y) <= radius*radius;
]
 
@Boolean in_big_circle (@Integer radius, @Integer x, @Integer y) [
return in_circle(0, 0, radius, x, y);
]
 
@Boolean in_while_semi_circle (@Integer radius, @Integer x, @Integer y) [
return in_circle(0, radius/2, radius/2, x, y);
]
 
@Boolean in_small_white_circle (@Integer radius, @Integer x, @Integer y) [
return in_circle(0, 0-radius/2, radius/6, x, y);
]
 
@Boolean in_black_semi_circle (@Integer radius, @Integer x, @Integer y) [
return in_circle(0, 0-radius/2, radius/2, x, y);
]
 
@Boolean in_small_black_circle (@Integer radius, @Integer x, @Integer y) [
return in_circle(0, radius/2, radius/6, x, y);
]
 
@Void print_yin_yang(@Integer radius) [
var white = '.';
var black = '#';
var clear = ' ';
 
var scale_y = 1;
var scale_x = 2;
for (var sy = radius*scale_y; sy >= -(radius*scale_y); sy=sy-1) {
for (var sx = -(radius*scale_x); sx <= radius*scale_x; sx=sx+1) {
var x = sx/(scale_x);
var y = sy/(scale_y);
 
if (in_big_circle(radius, x, y)) {
if (in_while_semi_circle(radius, x, y))
if (in_small_black_circle(radius, x, y))
printf("%c", black);
else
printf("%c", white);
else if (in_black_semi_circle(radius, x, y))
if (in_small_white_circle(radius, x, y))
printf("%c", white);
else
printf("%c", black);
else if (x < 0)
printf("%c", white);
else
printf("%c", black);
} else printf("%c", clear);
}
printf("\n");
}
]
 
@Integer main [
print_yin_yang(17);
print_yin_yang(8);
return 0;
]

Output:

                                 ###                                 
                       .............##########                       
                 .........................##########                 
               ...............................########               
             ...................................########             
         .......................................############         
         .........................................##########         
       ..........................###..............############       
     ..........................#######............##############     
   ..........................###########............##############   
   ............................#######............################   
   ..............................###..............################   
 .................................................################## 
 ...............................................#################### 
 ...............................................#################### 
 .............................................###################### 
 .........................................########################## 
....................................#################################
 ..........................######################################### 
 ......................############################################# 
 ....................############################################### 
 ....................############################################### 
 ..................################################################# 
   ................##############...##############################   
   ................############.......############################   
   ..............############...........##########################   
     ..............############.......##########################     
       ............##############...##########################       
         ..........#########################################         
         ............#######################################         
             ........###################################             
               ........###############################               
                 ..........#########################                 
                       ..........#############                       
                                 ###                                 
               ...               
         .............##         
     ...................####     
   ............###......######   
   ..........#######......####   
 ..............###......######## 
 .......................######## 
 .....................########## 
..................###############
 ..........##################### 
 ........####################### 
 ........######...############## 
   ....######.......##########   
   ......######...############   
     ....###################     
         ..#############         
               ###

[edit] PicoLisp

(de circle (X Y C R)
(>=
(* R R)
(+
(* (setq X (/ X 2)) X)
(* (dec 'Y C) Y) ) ) )
 
(de yinYang (R)
(for Y (range (- R) R)
(for X (range (- 0 R R) (+ R R))
(prin
(cond
((circle X Y (- (/ R 2)) (/ R 6))
"#" )
((circle X Y (/ R 2) (/ R 6))
"." )
((circle X Y (- (/ R 2)) (/ R 2))
"." )
((circle X Y (/ R 2) (/ R 2))
"#" )
((circle X Y 0 R)
(if (lt0 X) "." "#") )
(T " ") ) ) )
(prinl) ) )

Test:

: (yinYang 18)
                                   ...
                         .....................##
                   .............................######
                 .................................######
             .......................................########
           ...........................................########
         ..........................###................##########
       ........................###########............############
       ........................###########............############
     ........................###############............############
   ............................###########............################
   ............................###########............################
   ................................###................################
 .....................................................##################
 ...................................................####################
 .................................................######################
 ...............................................########################
 .............................................##########################
......................................###################################
 ..........................#############################################
 ........................###############################################
 ......................#################################################
 ....................###################################################
 ..................#####################################################
   ................################...################################
   ................############...........############################
   ................############...........############################
     ............############...............########################
       ............############...........########################
       ............############...........########################
         ..........################...##########################
           ........###########################################
             ........#######################################
                 ......#################################
                   ......#############################
                         ..#####################
                                   ###

[edit] PostScript

Output:
PSyinyang.png
%!PS-Adobe-3.0
%%BoundingBox: 0 0 400 400
 
/fs 10 def
/ed { exch def } def
/dist { 3 -1 roll sub dup mul 3 1 roll sub dup mul add sqrt } def
/circ {
/r exch def
[r neg 1 r {
/y exch def
[ r 2 mul neg 1 r 2 mul {
/x ed x 2 div y 0 0 dist r .05 add gt {( )}{
x 2 div y 0 r 2 div dist dup
r 5 div le { pop (.) } {
r 2 div le { (@) }{
x 2 div y 0 r 2 div neg dist dup
r 5 div le { pop (@)} {
r 2 div le {(.)}{
x 0 le {(.)}{(@)}ifelse
} ifelse
} ifelse
} ifelse
} ifelse
} ifelse
} for]
} for]
} def
 
/dis { moveto gsave
{ grestore 0 fs 1.15 mul neg rmoveto gsave
{show} forall
} forall grestore
} def
 
/Courier findfont fs scalefont setfont
 
11 circ 10 390 dis
6 circ 220 180 dis
showpage
%%EOF

[edit] POV-Ray

Yys.png
 
// ====== General Scene setup ======
#version 3.7;
global_settings { assumed_gamma 2.2 }
 
camera{ location <0,2.7,4> look_at <0,.1,0> right x*1.6
aperture .2 focal_point <1,0,0> blur_samples 200 variance 1/10000 }
light_source{<2,4,8>, 1 spotlight point_at 0 radius 10}
sky_sphere {pigment {granite scale <1,.1,1> color_map {[0 rgb 1][1 rgb <0,.4,.6>]}}}
#default {finish {diffuse .9 reflection {.1 metallic} ambient .3}
normal {granite scale .2}}
plane { y, -1 pigment {hexagon color rgb .7 color rgb .75 color rgb .65}
normal {hexagon scale 5}}
 
// ====== Declare one side of the symbol as a sum and difference of discs ======
 
#declare yang =
difference {
merge {
difference {
cylinder {0 <0,.1,0> 1} // flat disk
box {-1 <1,1,0>} // cut in half
cylinder {<.5,-.1,0> <.5,.2,0> .5} // remove half-cicle on one side
}
cylinder {<-.5,0,0> <-.5,.1,0> .5} // add on the other side
cylinder {<.5,0,0> <.5,.1,0> .15} // also add a little dot
}
cylinder {<-.5,-.1,0> <-.5,.2,0> .15} // and carve out a hole
pigment{color rgb 0.1}
}
 
// ====== The other side is white and 180-degree turned ======
 
#declare yin =
object {
yang
rotate <0,180,0>
pigment{color rgb 1}
}
 
// ====== Here we put the two together: ======
 
#macro yinyang( ysize )
union {
object {yin}
object {yang}
scale ysize
}
#end
 
// ====== Here we put one into a scene: ======
 
object { yinyang(1)
translate -y*1.08 }
 
// ====== And a bunch more just for fun: ======
 
#declare scl=1.1;
#while (scl > 0.01)
 
object { yinyang(scl)
rotate <0,180,0> translate <-scl*4,scl*2-1,0>
rotate <0,scl*360,0> translate <-.5,0,0>}
 
object { yinyang(scl)
translate <-scl*4,scl*2-1,0>
rotate <0,scl*360+180,0> translate <.5,0,0>}
 
#declare scl = scl*0.85;
#end

[edit] Prolog

Works with SWI-Prolog and XPCE.

ying_yang(N) :-
R is N * 100,
sformat(Title, 'Yin Yang ~w', [N]),
new(W, window(Title)),
new(Wh, colour(@default, 255*255, 255*255, 255*255)),
new(Bl, colour(@default, 0, 0, 0)),
CX is R + 50,
CY is R + 50,
R1 is R / 2,
R2 is R / 8,
CY1 is R1 + 50,
CY2 is 3 * R1 + 50,
 
new(E, semi_disk(point(CX, CY), R, w, Bl)),
new(F, semi_disk(point(CX, CY), R, e, Wh)),
new(D1, disk(point(CX, CY1), R, Bl)),
new(D2, disk(point(CX, CY2), R, Wh)),
new(D3, disk(point(CX, CY1), R2, Wh)),
new(D4, disk(point(CX, CY2), R2, Bl)),
 
send_list(W, display, [E, F, D1, D2, D3, D4]),
 
WD is 2 * R + 100,
send(W, size, size(WD, WD )),
send(W, open).
 
:- pce_begin_class(semi_disk, path, "Semi disk with color ").
 
initialise(P, C, R, O, Col) :->
send(P, send_super, initialise),
get(C, x, CX),
get(C, y, CY),
choose(O, Deb, End),
forall(between(Deb, End, I),
( X is R * cos(I * pi/180) + CX,
Y is R * sin(I * pi/180) + CY,
send(P, append, point(X,Y)))),
send(P, closed, @on),
send(P, fill_pattern, Col).
 
:- pce_end_class.
 
choose(s, 0, 180).
choose(n, 180, 360).
choose(w, 90, 270).
choose(e, -90, 90).
 
:- pce_begin_class(disk, ellipse, "disk with color ").
 
initialise(P, C, R, Col) :->
send(P, send_super, initialise, R, R),
send(P, center, C),
send(P, pen, 0),
send(P, fill_pattern, Col).
 
:- pce_end_class.

Example of output :

 ?- ying_yang(1).
true.

 ?- ying_yang(2).
true.
Prolog-yin-yang.png

[edit] PureBasic

Yin And yang.png

Procedure Yin_And_Yang(x, y, radius)
DrawingMode(#PB_2DDrawing_Outlined)
Circle(x, y, 2 * radius, #Black) ;outer circle
DrawingMode(#PB_2DDrawing_Default)
LineXY(x, y - 2 * radius, x, y + 2 * radius, #Black)
FillArea(x + 1, y, #Black, #Black)
Circle(x, y - radius, radius - 1, #White)
Circle(x, y + radius, radius - 1, #Black)
Circle(x, y - radius, radius / 3, #Black) ;small contrasting inner circles
Circle(x, y + radius, radius / 3, #White)
EndProcedure
 
If CreateImage(0, 700, 700) And StartDrawing(ImageOutput(0))
FillArea(1, 1, -1, #White)
Yin_And_Yang(105, 105, 50)
Yin_And_Yang(400, 400, 148)
StopDrawing()
;
UsePNGImageEncoder()
path$ = SaveFileRequester("Save image", "Yin And yang.png", "*.png", 0)
If path$ <> "": SaveImage(0, path$, #PB_ImagePlugin_PNG, 0, 2): EndIf
EndIf

[edit] Python

[edit] Text

For positive integer n > 0, the following generates an ASCII representation of the Yin yang symbol.

Works with: Python version 3.x
import math
def yinyang(n=3):
radii = [i * n for i in (1, 3, 6)]
ranges = [list(range(-r, r+1)) for r in radii]
squares = [[ (x,y) for x in rnge for y in rnge]
for rnge in ranges]
circles = [[ (x,y) for x,y in sqrpoints
if math.hypot(x,y) <= radius ]
for sqrpoints, radius in zip(squares, radii)]
m = {(x,y):' ' for x,y in squares[-1]}
for x,y in circles[-1]:
m[x,y] = '*'
for x,y in circles[-1]:
if x>0: m[(x,y)] = '·'
for x,y in circles[-2]:
m[(x,y+3*n)] = '*'
m[(x,y-3*n)] = '·'
for x,y in circles[-3]:
m[(x,y+3*n)] = '·'
m[(x,y-3*n)] = '*'
return '\n'.join(''.join(m[(x,y)] for x in reversed(ranges[-1])) for y in ranges[-1])
Sample generated symbols for n = 2 and n = 3
>>> print(yinyang(2))
            ·            
        ········*        
      ···········**      
     ·············**     
    ········*·····***    
   ········***····****   
  ········*****····****  
  ·········***····*****  
 ···········*·····****** 
 ·················****** 
 ················******* 
 ···············******** 
·············************
 ········*************** 
 ·······**************** 
 ······***************** 
 ······*****·*********** 
  ·····****···*********  
  ····****·····********  
   ····****···********   
    ···*****·********    
     ··*************     
      ··***********      
        ·********        
            *            
>>> print(yinyang(1))
      ·      
   ······*   
  ····*··**  
 ····***··** 
 ·····*··*** 
 ········*** 
·······******
 ···******** 
 ···**·***** 
 ··**···**** 
  ··**·****  
   ·******   
      *      
>>> 

[edit] Turtle Graphics

This was inspired by the Logo example but diverged as some of the Python turtle graphics primitives such as filling and the drawing of arcs work differently.

Python turtle graphics program output
from turtle import *
 
mode('logo')
 
def taijitu(r):
'''\
Draw a classic Taoist taijitu of the given radius centered on the current
turtle position. The "eyes" are placed along the turtle's heading, the
filled one in front, the open one behind.
'''

 
# useful derivative values
r2, r4, r8 = (r >> s for s in (1, 2, 3))
 
# remember where we started
x0, y0 = start = pos()
startcolour = color()
startheading = heading()
color('black', 'black')
 
# draw outer circle
pendown()
circle(r)
 
# draw two 'fishes'
begin_fill(); circle(r, 180); circle(r2, 180); circle(-r2, 180); end_fill()
 
# black 'eye'
setheading(0); penup(); goto(-(r4 + r8) + x0, y0); pendown()
begin_fill(); circle(r8); end_fill()
 
# white 'eye'
color('white', 'white'); setheading(0); penup(); goto(-(r+r4+r8) + x0, y0); pendown()
begin_fill(); circle(r8); end_fill()
 
# put the turtle back where it started
penup()
setpos(start)
setheading(startheading)
color(*startcolour)
 
 
if __name__ == '__main__':
# demo code to produce image at right
reset()
#hideturtle()
penup()
goto(300, 200)
taijitu(200)
penup()
goto(-150, -150)
taijitu(100)
hideturtle()

[edit] R

Output of this R program
plot.yin.yang <- function(x=5, y=5, r=3, s=10, add=F){
suppressMessages(require("plotrix"))
if(!add) plot(1:10, type="n", xlim=c(0,s), ylim=c(0,s), xlab="", ylab="", xaxt="n", yaxt="n", bty="n", asp=1)
draw.circle(x, y, r, border="white", col= "black")
draw.ellipse(x, y, r, r, col="white", angle=0, segment=c(90,270), arc.only=F)
draw.ellipse(x, y - r * 0.5, r * 0.5, r * 0.5, col="black", border="black", angle=0, segment=c(90,270), arc.only=F)
draw.circle(x, y - r * 0.5, r * 0.125, border="white", col= "white")
draw.circle(x, y + r * 0.5, r * 0.5, col="white", border="white")
draw.circle(x, y + r * 0.5, r * 0.125, border="black", lty=1, col= "black")
draw.circle(x, y, r, border="black")
}
png("yin_yang.png")
plot.yin.yang()
plot.yin.yang(1,7,1, add=T)
dev.off()

[edit] Racket

Rosetta-yin-yang.png
 
#lang racket
(require slideshow/pict)
 
(define (yin-yang d)
(define base
(hc-append (inset/clip (circle d) 0 0 (- (/ d 2)) 0)
(inset/clip (disk d) (- (/ d 2)) 0 0 0)))
(define with-top
(ct-superimpose
base
(cc-superimpose (colorize (disk (/ d 2)) "white")
(disk (/ d 8)))))
(define with-bottom
(cb-superimpose
with-top
(cc-superimpose (disk (/ d 2))
(colorize (disk (/ d 8)) "white"))))
(cc-superimpose with-bottom (circle d)))
 
(yin-yang 200)
 

[edit] Rascal

Yinyang.jpg
import util::Math;
import vis::Render;
import vis::Figure;
 
public void yinyang(){
template = ellipse(fillColor("white"));
 
smallWhite = ellipse(fillColor("white"), shrink(0.1), valign(0.75));
smallBlack = ellipse(fillColor("black"), shrink(0.1), valign(0.25));
 
dots= [ellipse(fillColor("white"), shrink(0.000001), align(0.5 + sin(0.0031415*n)/4, 0.25 + cos(0.0031415*n)/-4)) | n <- [1 .. 1000]];
dots2 = [ellipse(fillColor("black"), shrink(0.000001), align(0.5 + sin(0.0031415*n)/-4, 0.75 + cos(0.0031415*n)/-4)) | n <- [1 .. 1000]];
dots3= [ellipse(fillColor("black"), shrink(0.000001), align(0.5 + sin(0.0031415*n)/2, 0.5-cos(0.0031415*n)/-2)) | n <- [1 .. 1000]];
 
black= overlay([*dots, *dots2, *dots3], shapeConnected(true), shapeClosed(true), shapeCurved(true), fillColor("black"));
 
render(hcat([vcat([overlay([template, black, smallWhite, smallBlack], aspectRatio (1.0)), space(), space()]),
overlay([template, black, smallWhite, smallBlack], aspectRatio (1.0))]));
}

[edit] Ruby

Output of this Ruby Shoes program
Library: Shoes
Shoes.app(:width => 470, :height => 380) do
PI = Shoes::TWO_PI/2
 
strokewidth 1
 
def yin_yang(x, y, radius)
fill black; stroke black
arc x, y, radius, radius, -PI/2, PI/2
 
fill white; stroke white
arc x, y, radius, radius, PI/2, -PI/2
oval x-radius/4, y-radius/2, radius/2-1
 
fill black; stroke black
oval x-radius/4, y, radius/2-1
oval x-radius/12, y-radius/4-radius/12, radius/6-1
 
fill white; stroke white
oval x-radius/12, y+radius/4-radius/12, radius/6-1
 
nofill
stroke black
oval x-radius/2, y-radius/2, radius
end
 
yin_yang 190, 190, 360
yin_yang 410, 90, 90
end

[edit] Scala

Library: Scala
import scala.swing.Swing.pair2Dimension
import scala.swing.{ MainFrame, Panel }
import java.awt.{ Color, Graphics2D }
 
object YinYang extends scala.swing.SimpleSwingApplication {
var preferedSize = 500
 
/** Draw a Taijitu symbol on the given graphics context.
*/

def drawTaijitu(g: Graphics2D, size: Int) {
val sizeMinsOne = size - 1
// Preserve the color for the caller
val colorSave = g.getColor()
 
g.setColor(Color.WHITE)
// Use fillOval to draw a filled in circle
g.fillOval(0, 0, sizeMinsOne, sizeMinsOne)
 
g.setColor(Color.BLACK)
// Use fillArc to draw part of a filled in circle
g.fillArc(0, 0, sizeMinsOne, sizeMinsOne, 270, 180)
g.fillOval(size / 4, size / 2, size / 2, size / 2)
 
g.setColor(Color.WHITE)
g.fillOval(size / 4, 0, size / 2, size / 2)
g.fillOval(7 * size / 16, 11 * size / 16, size / 8, size / 8)
 
g.setColor(Color.BLACK)
g.fillOval(7 * size / 16, 3 * size / 16, size / 8, size / 8)
// Use drawOval to draw an empty circle for the outside border
g.drawOval(0, 0, sizeMinsOne, sizeMinsOne)
 
// Restore the color for the caller
g.setColor(colorSave)
}
 
def top = new MainFrame {
title = "Rosetta Code >>> Yin Yang Generator | Language: Scala"
contents = gui(preferedSize)
 
def gui(sizeInterior: Int) = new Panel() {
preferredSize = (sizeInterior, sizeInterior)
 
/** Draw a Taijitu symbol in this graphics context.
*/

override def paintComponent(graphics: Graphics2D) = {
super.paintComponent(graphics)
 
// Color in the background of the image
background = Color.RED
drawTaijitu(graphics, sizeInterior)
}
} // def gui(
}
 
override def main(args: Array[String]) = {
preferedSize = args.headOption.map(_.toInt).getOrElse(preferedSize)
super.main(args)
}
}

[edit] Seed7

Output of the Seed7 program
$ include "seed7_05.s7i";
include "float.s7i";
include "math.s7i";
include "draw.s7i";
include "keybd.s7i";
 
const proc: yinYang (in integer: xPos, in integer: yPos, in integer: size) is func
begin
pieslice(xPos, yPos, size, 3.0 * PI / 2.0, PI, black);
pieslice(xPos, yPos, size, PI / 2.0, PI, white);
fcircle(xPos, yPos - size div 2, size div 2, white);
fcircle(xPos, yPos + size div 2, size div 2, black);
fcircle(xPos, yPos - size div 2, size div 6, black);
fcircle(xPos, yPos + size div 2, size div 6, white);
circle(xPos, yPos, size, black);
end func;
 
const proc: main is func
begin
screen(640, 480);
clear(white);
KEYBOARD := GRAPH_KEYBOARD;
yinYang(100, 100, 80);
yinYang(400, 250, 200);
readln(KEYBOARD);
end func;

[edit] SVG

A rendering

SVG has no proper functions or variables, but we can translate and rescale a shape after defining it.

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg xmlns="http://www.w3.org/2000/svg" version="1.1"
xmlns:xlink="http://www.w3.org/1999/xlink"
width="600" height="600">
 
<!-- We create the symbol in the rectangle from (0, 0) to (1, 1)
and then translate it so it's centered on the origin. -->
<symbol id="yinyang">
<g transform="translate(-0.5, -0.5)">
<!-- A white circle, for the bulk of the left-hand part -->
<circle cx="0.5" cy="0.5" r="0.5" fill="white"/>
<!-- A black semicircle, for the bulk of the right-hand part -->
<path d="M 0.5,0 A 0.5,0.5 0 0,1 0.5,1 z" fill="black"/>
<!-- Circles to extend each part -->
<circle cx="0.5" cy="0.25" r="0.25" fill="white"/>
<circle cx="0.5" cy="0.75" r="0.25" fill="black"/>
<!-- The spots -->
<circle cx="0.5" cy="0.25" r="0.1" fill="black"/>
<circle cx="0.5" cy="0.75" r="0.1" fill="white"/>
<!-- An outline for the whole shape -->
<circle cx="0.5" cy="0.5" r="0.5" fill="none"
stroke="gray" stroke-width=".01"/>
</g>
</symbol>
 
<use xlink:href="#yinyang"
transform="translate(125, 125) scale(200, 200)"/>
 
<use xlink:href="#yinyang"
transform="translate(375, 375) scale(400, 400)"/>
 
</svg>

[edit] Tcl

Output of this Tcl program
Library: Tk
package require Tcl 8.5
package require Tk
 
namespace import tcl::mathop::\[-+\] ;# Shorter coordinate math
proc yinyang {c x y r {colors {white black}}} {
lassign $colors a b
set tt [expr {$r * 2 / 3.0}]
set h [expr {$r / 2.0}]
set t [expr {$r / 3.0}]
set s [expr {$r / 6.0}]
$c create arc [- $x $r] [- $y $r] [+ $x $r] [+ $y $r] \
-fill $a -outline {} -extent 180 -start 90
$c create arc [- $x $r] [- $y $r] [+ $x $r] [+ $y $r] \
-fill $b -outline {} -extent 180 -start 270
$c create oval [- $x $h] [- $y $r] [+ $x $h] $y \
-fill $a -outline {}
$c create oval [- $x $h] [+ $y $r] [+ $x $h] $y \
-fill $b -outline {}
$c create oval [- $x $s] [- $y $tt] [+ $x $s] [- $y $t] \
-fill $b -outline {}
$c create oval [- $x $s] [+ $y $tt] [+ $x $s] [+ $y $t] \
-fill $a -outline {}
}
 
pack [canvas .c -width 300 -height 300 -background gray50]
yinyang .c 110 110 90
yinyang .c 240 240 40

[edit] Visual Basic .NET

Some lines in this example are too long (more than 80 characters). Please fix the code if it's possible and remove this message.
Output of this VB.Net program

This version is based behind a Windows Form Application project in Visual Studio, single form with base values.

Public Class Form1
 
Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
Dim g As Graphics = e.Graphics
g.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
 
DrawTaijitu(g, New Point(50, 50), 200, True)
DrawTaijitu(g, New Point(10, 10), 60, True)
End Sub
 
Private Sub DrawTaijitu(ByVal g As Graphics, ByVal pt As Point, ByVal width As Integer, ByVal hasOutline As Boolean)
g.FillPie(Brushes.Black, pt.X, pt.Y, width, width, 90, 180)
g.FillPie(Brushes.White, pt.X, pt.Y, width, width, 270, 180)
g.FillEllipse(Brushes.Black, CSng(pt.X + (width * 0.25)), CSng(pt.Y), CSng(width * 0.5), CSng(width * 0.5))
g.FillEllipse(Brushes.White, CSng(pt.X + (width * 0.25)), CSng(pt.Y + (width * 0.5)), CSng(width * 0.5), CSng(width * 0.5))
g.FillEllipse(Brushes.White, CSng(pt.X + (width * 0.4375)), CSng(pt.Y + (width * 0.1875)), CSng(width * 0.125), CSng(width * 0.125))
g.FillEllipse(Brushes.Black, CSng(pt.X + (width * 0.4375)), CSng(pt.Y + (width * 0.6875)), CSng(width * 0.125), CSng(width * 0.125))
If hasOutline Then g.DrawEllipse(Pens.Black, pt.X, pt.Y, width, width)
End Sub
 
End Class

[edit] XPL0

Output
include c:\cxpl\codes;          \intrinsic 'code' declarations
 
def Black=0, Red=4, White=$F;
 
proc Circle(X0, Y0, R, CL, CR); \Show a filled circle
int X0, Y0, R, CL, CR; \left and right half colors
int X, Y;
[for Y:= -R to R do
for X:= -R to R do
if X*X + Y*Y <= R*R then
Point(X+X0, Y+Y0, if X<0 then CL else CR);
]; \Circle
 
proc YinYang(X0, Y0, R);
int X0, Y0, R;
[Circle(X0, Y0, R, White, Black);
Circle(X0, Y0-R/2, R/2, White, White);
Circle(X0, Y0-R/2, R/6, Black, Black);
Circle(X0, Y0+R/2, R/2, Black, Black);
Circle(X0, Y0+R/2, R/6, White, White);
]; \YinYang
 
[SetVid($101); \640x480 graphics
Circle(320, 240, 400, Red, Red);\fill screen with background color
YinYang(80, 80, 70);
YinYang(240, 240, 150);
if ChIn(1) then []; \wait for keystroke
SetVid(3); \restore normal text mode
]

[edit] zkl

Writes to stdout a SVG file with two yin-yangs.

fcn draw_yinyang(trans,scale){
0'|<use xlink:href="#y" transform="translate(%d,%d) scale(%g)"/>|
.fmt(trans,trans,scale).print();
}
 
print(
"<?xml version='1.0' encoding='UTF-8' standalone='no'?>\n"
"<!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 1.1//EN'\n"
" 'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd'>\n"
"<svg xmlns='http://www.w3.org/2000/svg' version='1.1'\n"
" xmlns:xlink='http://www.w3.org/1999/xlink'\n"
" width='30' height='30'>\n"
" <defs><g id='y'>\n"
" <circle cx='0' cy='0' r='200' stroke='black'\n"
" fill='white' stroke-width='1'/>\n"
" <path d='M0 -200 A 200 200 0 0 0 0 200\n"
" 100 100 0 0 0 0 0 100 100 0 0 1 0 -200\n"
" z' fill='black'/>\n"
" <circle cx='0' cy='100' r='33' fill='white'/>\n"
" <circle cx='0' cy='-100' r='33' fill='black'/>\n"
" </g></defs>\n");
 
draw_yinyang(20, 0.05);
draw_yinyang( 8, 0.02);
print("</svg>");
Output:
$ zkl zz  > foo.html 
copy to browswer

Yinyang-C.svg

Personal tools
Namespaces

Variants
Actions
Community
Explore
Misc
Toolbox