Yin and yang: Difference between revisions

From Rosetta Code
Content added Content deleted
m (→‎{{header|D}}: reimplemented with hexagonal board)
m (→‎{{header|D}}: reverse to privous version)
Line 5: Line 5:


=={{header|D}}==
=={{header|D}}==
{{incorrect|The output does not sufficiently resemble a Yin-yang symbol. Please fix the code and remove this message.}}
<lang d>import std.stdio, std.string, std.algorithm, std.math ;
<lang d>import std.stdio, std.string, std.algorithm ;

enum { Void = 0, Yan = 1, Yin = 2, I = 3} ;
enum { Void = 0, Yan = 1, Yin = 2, I = 3} ;
enum Tiles = [" ","","",""] ; // full-width unicode char
enum Tiles = [" ","·","#","?"] ;
enum Shift = " " ; // half-width space
alias int delegate(int) Draw ;

struct Hex { int x, y ; } ;
struct Coord { real x, y ; } ;
alias int delegate(Hex) Draw ;

struct Board { // a square board
struct Board { // a square board
immutable int scale, size ;
immutable int scale ;
immutable int size ;
Coord org ; // origin point coordinate
int[][] hex ;
int[][] pix ;

this(int s) {
this(int s) {
scale = s ;
scale = s ;
size = s*12 ;
size = s*12 ;
hex = new int[][](size + 3,size + 3) ; // make the board bigger
pix = new int[][](size + 1,size + 1) ;
org = hex2Coord( Hex(1 + size/2 , 1 + size/2)) ;
}
}
alias pix this ;

static Coord hex2Coord(Hex h) {
real shiftx = (h.y % 2)* 0.5L + h.x ;
return Coord(shiftx, -h.y) ;
}

static string str(int v) { return Tiles[(v % 4)] ; }
static string str(int v) { return Tiles[(v % 4)] ; }

string toString() {
string toString() {
string[] s ;
string[] s ;
foreach( i, r ; hex)
foreach( r ; pix)
s ~= (i % 2 == 1 ? Shift : "") ~ reduce!"a~b"(map!str(r)) ;
s ~= reduce!"a~b"(map!str(r)) ;
return s.join("\n") ;
return s.join("\n") ;
}
}

void circle(Coord center, real cr, Draw action) {
void drawCircle(int cx, int cy, int cr, Draw action) {
auto rr = (cr*scale)^^2 ;
auto rr = (cr*scale)^^2 ;
foreach(y, ref r ; hex)
foreach(y, ref r ; pix)
foreach( x , ref v ; r) {
foreach( x , ref v ; r) {
auto here = hex2Coord(Hex(x, y)) ;
auto dx = x - cx*scale ;
auto dx = here.x - org.x - center.x*scale ;
auto dy = y - cy*scale ;
auto dy = here.y - org.y - center.y*scale ;
if(dx^^2 + dy^^2 <= rr)
if(dx^^2 + dy^^2 <= rr)
v = action(Hex(x,y)) ;
v = action(x) ;
}
}
}
}

Board yinYan() {
Board yanYin() {
foreach(r ; hex) // clear
foreach(r ; pix) // clear
r[] = Void ;
r[] = 0 ;
drawCircle(6,6,6, (int x) { return (x < 6*scale) ? Yan : Yin ; }) ;
circle(Coord( 0, 0),6,
(Hex h) { return (hex2Coord(h).x <= org.x) ? Yan : Yin ; }
drawCircle(6,3,3, (int x) { return Yan ; } ) ;
) ;
drawCircle(6,9,3, (int x) { return Yin ; } ) ;
circle(Coord( 0, 3),3, (Hex h) { return Yan ; } ) ;
drawCircle(6,9,1, (int x) { return Yan ; } ) ;
circle(Coord( 0,-3),3, (Hex h) { return Yin ; } ) ;
drawCircle(6,3,1, (int x) { return Yin ; } ) ;
circle(Coord( 0,-3),1, (Hex h) { return Yan ; } ) ;
circle(Coord( 0, 3),1, (Hex h) { return Yin ; } ) ;
return this ;
return this ;
}
}
}
}

void main(string[] args) {
void main(string[] args) {
writeln(Board(2).yinYan) ;
writeln(Board(2).yanYin) ;
writeln(Board(1).yinYan) ;
writeln(Board(1).yanYin) ;
}</lang>
}</lang>
Output:
Output:
<pre style="line-height:0.99em; font-family:Courier New;">              .             
<pre style="line-height:0.99em; font-family:Courier New;"> ·
········#
         ........##        
···········##
       ...........##       
·············##
      .............###     
········#·····###
     ........#.....###     
········###····####
    ........####....####   
········#####····####
   ........#####....####   
·········###····#####
   .........####....#####  
···········#·····######
  ...........#.....######  
·················######
  .................####### 
················#######
  ................#######  
···············########
  ...............######### 
············#############
 ............############# 
········###############
  .........############### 
·······################
  .......################  
······#################
  .......################# 
······#####·###########
  ......#####.###########  
·····####···#########
   .....####....#########  
····####·····########
   ....####.....########   
····####···########
    ....####....########   
···#####·########
     ...#####.########     
··#############
      ...#############     
··###########
       ..###########       
·########
         ..########        
#
             #             
·
                           
······#
               
····#··##
       .       
····###··##
     .....#    
·····#··###
   ....#..##   
········###
   ....##..##  
······#######
  .....#..###  
···########
  ........#### 
···##·#####
 ......####### 
··##···####
  ....######## 
··##·####
  ...##.#####  
·######
   ..##..####  
# </pre>
   ..##.####   
     .#####    
       #</pre>


=={{header|Python}}==
=={{header|Python}}==

Revision as of 14:50, 5 April 2011

Yin and yang is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

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.

D

This example is incorrect. It does not accomplish the given task. Please fix the code and remove this message.

<lang d>import std.stdio, std.string, std.algorithm ;

enum { Void = 0, Yan = 1, Yin = 2, I = 3} ; enum Tiles = [" ","·","#","?"] ;

alias int delegate(int) Draw ;

struct Board { // a square board

   immutable int scale ;
   immutable int size ;
   int[][] pix ;
   this(int s) {
       scale = s ;
       size = s*12 ;
       pix = new int[][](size + 1,size + 1) ;
   }
   alias pix this ;

   static string str(int v) { return Tiles[(v % 4)] ; }

   string toString() {
       string[] s ;
       foreach( r ; pix)
           s ~= reduce!"a~b"(map!str(r)) ;
       return s.join("\n") ;
   }

   void drawCircle(int cx, int cy, int cr, Draw action) {
       auto rr = (cr*scale)^^2 ;
       foreach(y, ref r ; pix)
           foreach( x , ref v ; r) {
               auto dx = x - cx*scale ;
               auto dy = y - cy*scale ;
               if(dx^^2 + dy^^2 <= rr)
                   v = action(x) ;
           }
   }

   Board yanYin() {
       foreach(r ; pix) // clear
           r[] = 0 ;
       drawCircle(6,6,6, (int x) { return (x < 6*scale) ? Yan : Yin ; }) ;
       drawCircle(6,3,3, (int x) { return Yan ; } ) ;
       drawCircle(6,9,3, (int x) { return Yin ; } ) ;
       drawCircle(6,9,1, (int x) { return Yan ; } ) ;
       drawCircle(6,3,1, (int x) { return Yin ; } ) ;
       return this ;
   }

}

void main(string[] args) {

   writeln(Board(2).yanYin) ;
   writeln(Board(1).yanYin) ;

}</lang> Output:

            ·            
        ········#        
      ···········##      
     ·············##     
    ········#·····###    
   ········###····####   
  ········#####····####  
  ·········###····#####  
 ···········#·····###### 
 ·················###### 
 ················####### 
 ···············######## 
············#############
 ········############### 
 ·······################ 
 ······################# 
 ······#####·########### 
  ·····####···#########  
  ····####·····########  
   ····####···########   
    ···#####·########    
     ··#############     
      ··###########      
        ·########        
            #            
      ·      
   ······#   
  ····#··##  
 ····###··## 
 ·····#··### 
 ········### 
······#######
 ···######## 
 ···##·##### 
 ··##···#### 
  ··##·####  
   ·######   
      #      

Python

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

Works with: Python version 3.x

<lang python>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])</lang>

Sample generated symbols for n = 2 and n = 3
>>> print(yinyang(2))
            ·            
        ········*        
      ···········**      
     ·············**     
    ········*·····***    
   ········***····****   
  ········*****····****  
  ·········***····*****  
 ···········*·····****** 
 ·················****** 
 ················******* 
 ···············******** 
·············************
 ········*************** 
 ·······**************** 
 ······***************** 
 ······*****·*********** 
  ·····****···*********  
  ····****·····********  
   ····****···********   
    ···*****·********    
     ··*************     
      ··***********      
        ·********        
            *            
>>> print(yinyang(1))
      ·      
   ······*   
  ····*··**  
 ····***··** 
 ·····*··*** 
 ········*** 
·······******
 ···******** 
 ···**·***** 
 ··**···**** 
  ··**·****  
   ·******   
      *      
>>>