Yin and yang: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (syntax highlighting fixup automation)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(34 intermediate revisions by 9 users not shown)
Line 1,994:
....###################
### </pre>
 
 
=={{header|EasyLang}}==
[https://easylang.dev/show/#cod=VY/dCsIwDEbv8xTftbIapx30cWYso1jXEVHs20t13SbkIj8nJ2TSJJCgAoXAwBAASTEp5JsGleihZGgqaA5j7scBb2Ro5e/p5UunLqDFDgpmXm3OuTp++GEmHKNxvFWggS6aVfGrDjhVz8Lv//jNlZkvBkOXXm6Dpud4hbWWJPpeqf7ScoluqS2jYxzP9AE= Run it]
 
<syntaxhighlight>
proc circ r c . .
color c
circle r
.
proc yinyang x y r . .
move x y
circ 2 * r 000
color 999
circseg 2 * r 90 -90
move x y - r
circ r 000
circ r / 3 999
move x y + r
circ r 999
circ r / 3 000
.
background 555
clear
yinyang 20 20 6
yinyang 50 60 14
</syntaxhighlight>
 
{{out}}
<pre>
</pre>
 
=={{header|Evaldraw}}==
 
===(x,y,r,g,b) 2D graphing mode===
 
{{trans|xpl0}}
 
Inspired by the xpl0 solution. First out is the implicit 2D function mode.
<syntaxhighlight lang="c">
(x,y,&r,&g,&b) {
r=255; g=0; b=0;
// Notice rad is radius square
YinYang(x-8,y+8,7,r,g,b);
YinYang(x-25,y+24,15,r,g,b);
}//main
 
YinYang(x,y,rad,&r,&g,&b) {
circ0 = Circle(x, y, rad);
circ1 = Circle(x, y-rad/2, rad/2);
circ2 = Circle(x, y-rad/2, rad/6);
circ3 = Circle(x, y+rad/2, rad/2);
circ4 = Circle(x, y+rad/2, rad/6);
if (circ0 <= rad) { if (x<0) { r=g=b=255; } else {r=g=b=0; } }
if (circ1 <= rad/6) { r=g=b=255; }
if (circ2 <= rad/6) { r=g=b=0; }
if (circ3 <= rad/2) { r=g=b=0; }
if (circ4 <= rad/6) { r=g=b=255; }
}
 
Circle(x,y,r) { return (x^2+y^2)-r^2 }</syntaxhighlight>
 
===General program mode===
{{trans|xpl0}}
Another solution is to use the general () program mode.
[[File:Evaldraw yin and yang.gif|thumb|alt=Compilation message visible. Shows assembly code size at time spent.|2 half circles and 4 filled circles can be used to draw the yin and the yang ]]
<syntaxhighlight lang="c">()
{
cls(0x646464);
YinYang(80, 80, 70);
YinYang(240, 240, 150);
}
 
circle(x0, y0, r, col_left, col_right) {
for(y=-r; y<r; y++)
for(x=-r; x<r; x++) {
if (x^2 + y^2 <= r^2) {
if (x<0) setcol(col_left); else setcol(col_right);
setpix(x+x0, y+y0);
}
}
}
 
YinYang(x0, y0, r) {
white = rgb(255,255,255);
black = 0;
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);
}
</syntaxhighlight>
 
=={{header|Dart}}==
===Text===
<syntaxhighlight lang="dart">
/* Imports and Exports */
import 'dart:io';
 
/* Main Block */
int main() {
yinYang(18);
return 0;
}
 
/* Function Definitions */
bool circle(int x, int y, int c, int r) {
return (r * r) >= ((x = x ~/ 2) * x) + ((y = y - c) * y);
}
 
String pixel(int x, int y, int r) {
if (circle(x, y, -r ~/ 2, r ~/ 6)) {
return '#';
}
if (circle(x, y, r ~/ 2, r ~/ 6)) {
return '.';
}
if (circle(x, y, -r ~/ 2, r ~/ 2)) {
return '.';
}
if (circle(x, y, r ~/ 2, r ~/ 2)) {
return '#';
}
if (circle(x, y, 0, r)) {
if (x < 0) {
return '.';
} else {
return '#';
}
}
return ' ';
}
 
void yinYang(int r) {
for (int y = -r; y <= r; y++) {
for (int x = -2 * r; x <= 2 * r; x++) {
stdout.write(pixel(x, y, r));
}
stdout.write('\n');
}
}
</syntaxhighlight>
 
{{out}}
<pre> ...
.....................##
.............................######
.................................######
.......................................########
...........................................########
..........................###................##########
........................###########............############
........................###########............############
........................###############............############
............................###########............################
............................###########............################
................................###................################
.....................................................##################
...................................................####################
.................................................######################
...............................................########################
.............................................##########################
......................................###################################
..........................#############################################
........................###############################################
......................#################################################
....................###################################################
..................#####################################################
................################...################################
................############...........############################
................############...........############################
............############...............########################
............############...........########################
............############...........########################
..........################...##########################
........###########################################
........#######################################
......#################################
......#############################
..#####################
###
</pre>
===Flutter===
[[File:YinYang-flutter.png]]<br>
[https://dartpad.dev/?id=c54bafac1d8f46c07db626dca64e13e4 Watch/play online DartPad]
<syntaxhighlight lang="dart">import 'dart:math' show pi;
import 'package:flutter/material.dart';
 
Path yinYang(double r, double x, double y, [double th = 1.0]) {
cR(double dY, double radius) => Rect.fromCircle(center: Offset(x, y + dY), radius: radius);
return Path()
..fillType = PathFillType.evenOdd
..addOval(cR(0, r + th))
..addOval(cR(r / 2, r / 6))
..addOval(cR(-r / 2, r / 6))
..addArc(cR(0, r), -pi / 2, -pi)
..addArc(cR(r / 2, r / 2), pi / 2, pi)
..addArc(cR(-r / 2, r / 2), pi / 2, -pi);
}
 
void main() => runApp(CustomPaint(painter: YinYangPainter()));
 
class YinYangPainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
final fill = Paint()..style = PaintingStyle.fill;
canvas
..drawColor(Colors.white, BlendMode.src)
..drawPath(yinYang(50.0, 60, 60), fill)
..drawPath(yinYang(20.0, 140, 30), fill);
}
 
@override
bool shouldRepaint(CustomPainter oldDelegate) => true;
}
</syntaxhighlight>
===Flutter (without CustomPaint)===
[https://dartpad.dev/?id=e0502d089330620c9866ef5fc2310042 Run online in DartPad]
<syntaxhighlight lang="dart">import 'package:flutter/material.dart';
 
const color = [Colors.black, Colors.white];
 
Widget cR(int iColor, double r, {Widget? child}) => DecoratedBox(
decoration: BoxDecoration(color: color[iColor], shape: BoxShape.circle),
child: SizedBox.square(dimension: r * 2, child: Center(child: child)));
 
Widget yinYang(double r, [double th = 1.0]) => Padding(
padding: const EdgeInsets.all(5),
child: ClipOval(
child: cR(0, r + th,
child: cR(1, r,
child: Stack(alignment: Alignment.center, children: [
Container(color: color[0], margin: EdgeInsets.only(left: r)),
Column(children: List.generate(2, (i) => cR(1 - i, r / 2, child: cR(i, r / 6))))
])))));
 
void main() => runApp(MaterialApp(
home: ColoredBox(color: color[1], child: Wrap(children: [yinYang(50), yinYang(20)]))));
</syntaxhighlight>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
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.
{{libheader|SysUtils,StdCtrls}}
<syntaxhighlight lang="delphi">procedure DrawYinAndYang(Canv: TCanvas; R: TRect);
[[File:DelphiYinYang.png|frame|none]]
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);
 
<syntaxhighlight lang="Delphi">
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);
procedure DrawCircle(Canvas: TCanvas; Center: TPoint; Radius: integer);
{Draw circle at specified center and size (Radius)}
var R: TRect;
begin
R.TopLeft:=Center;
ClientWidth := 400;
R.BottomRight:=Center;
ClientHeight := 400;
InflateRect(R,Radius,Radius);
Canvas.Ellipse(R);
end;
 
procedure DrawYinYang(Canvas: TCanvas; Center: TPoint; Radius: integer);
procedure TForm1.FormPaint(Sender: TObject);
{Draw Yin-Yang symbol at specified center and size (Radius)}
var
var X1,Y1,X2,Y2,X3,Y3,X4,Y4: integer;
R: TRect;
var R2,R6: integer;
begin
R2:=Radius div 2;
R := ClientRect;
R6:=Radius div 6;
Canvas.Brush.Color := clGray;
Canvas.FillRect(R)Pen.Width:=3;
 
{Draw outer circle}
InflateRect(R, -50, -50);
DrawCircle(Canvas,Center,Radius);
OffsetRect(R, -40, -40);
DrawYinAndYang(Canvas, R);
 
{Draw bottom half circle}
InflateRect(R, -90, -90);
X1:=Center.X - R2; Y1:=Center.Y;
OffsetRect(R, 170, 170);
X2:=Center.X + R2; Y2:=Center.Y + Radius;
DrawYinAndYang(Canvas, R);
X3:=Center.X; Y3:=Center.Y;
X4:=Center.X; Y4:=Center.Y + Radius;
Canvas.Arc(X1,Y1, X2,Y2, X3,Y3, X4, Y4);
 
{Draw top half circle}
X1:=Center.X - R2; Y1:=Center.Y;
X2:=Center.X + R2; Y2:=Center.Y - Radius;
X3:=Center.X; Y3:=Center.Y;
X4:=Center.X; Y4:=Center.Y- Radius;
Canvas.Arc(X1,Y1, X2,Y2, X3,Y3, X4, Y4);
 
{Fill right half with black}
Canvas.Brush.Color:=clBlack;
Canvas.FloodFill(Center.X,Center.Y+5,clWhite, fsSurface);
 
{Draw top small circle}
DrawCircle(Canvas, Point(Center.X, Center.Y-R2), R6);
 
{Draw bottom small circle}
Canvas.Brush.Color:=clWhite;
DrawCircle(Canvas, Point(Center.X, Center.Y+R2), R6);
end;
 
 
procedure ShowYinYang(Image: TImage);
begin
DrawYinYang(Image.Canvas,Point(75,75),50);
DrawYinYang(Image.Canvas,Point(200,200),100);
Image.Invalidate;
end;
 
 
 
</syntaxhighlight>
{{out}}
<pre>
 
Elapsed Time: 0.595 ms.
{{output?}}
 
</pre>
 
=={{header|Draco}}==
<syntaxhighlight lang="draco">proc circle(int x, c, y, r) bool:
r*r >= (x/2)*(x/2) + (y-c)*(y-c)
corp
 
proc pixel(int x, y, r) char:
if circle(x, y, -r/2, r/6) then '\#'
elif circle(x, y, r/2, r/6) then '.'
elif circle(x, y, -r/2, r/2) then '.'
elif circle(x, y, r/2, r/2) then '\#'
elif circle(x, y, 0, r) then
if x<0 then '.' else '\#' fi
else ' '
fi
corp
 
proc yinyang(int r) void:
int x, y;
for y from -r upto r do
for x from -2*r upto 2*r do
write(pixel(x, y, r))
od;
writeln()
od
corp
 
proc main() void:
yinyang(4);
yinyang(8)
corp</syntaxhighlight>
{{out}}
<pre> ...
.........##
......###....##
...........####
..........#######
....###########
..####...######
..#########
###
...
.............##
...................####
............###......######
..........#######......####
..............###......########
.......................########
.....................##########
..................###############
..........#####################
........#######################
........######...##############
....######.......##########
......######...############
....###################
..#############
###</pre>
 
=={{header|DWScript}}==
Line 2,173 ⟶ 2,488:
#
</pre>
 
 
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Yin_and_yang}}
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for storage and transfer purposes more than visualization and edition.
 
'''Solution'''
 
[[File:Fōrmulæ - Yin and yang 01.png]]
 
'''Test cases'''
 
[[File:Fōrmulæ - Yin and yang 02.png]]
Programs in Fōrmulæ are created/edited online in its [https://formulae.org website], However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.
 
[[File:Fōrmulæ - Yin and yang 03.png]]
In '''[https://formulae.org/?example=Yin_and_yang this]''' page you can see the program(s) related to this task and their results.
 
=={{header|Forth}}==
Line 2,258 ⟶ 2,578:
 
s" gyinyang.ppm" save_image \ save the image</syntaxhighlight>
 
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
 
_window = 1
begin enum output 1
_imageView1
_imageView2
end enum
 
local fn YinYangImage( size as float ) as ImageRef
CFDictionaryRef attributes = @{NSFontAttributeName:fn FontWithName( @"Menlo", size ), NSBackgroundColorAttributeName:fn ColorClear}
CFMutableAttributedStringRef aString = fn MutableAttributedStringWithAttributes( @"\u262F", attributes )
ImageRef image = fn ImageWithSize( fn AttributedStringSize( aString ) )
ImageLockFocus( image )
GraphicsContextSaveGraphicsState
AttributedStringDrawAtPoint( aString, fn CGPointMake( 0, 0 ) )
GraphicsContextRestoreGraphicsState
ImageUnlockFocus( image )
end fn = image
 
void local fn BuildWindow
CGRect r = fn CGRectMake( 0, 0, 300, 200 )
window _window, @"Rosetta Code Yin and Yang", r, NSWindowStyleMaskTitled + NSWindowStyleMaskClosable + NSWindowStyleMaskMiniaturizable
WindowSetBackgroundColor( _window, fn ColorWhite )
ImageRef yinyang = fn YinYangImage( 250.0 )
r = fn CGRectMake( 20, 10, 170, 180 )
imageview _imageView1, YES, yinyang, r, NSImageScaleNone, NSImageAlignCenter, NSImageFrameNone, _window
r = fn CGRectMake( 190, 90, 100, 100 )
imageview _imageView2, YES, yinyang, r, NSImageScaleProportionallyDown, NSImageAlignCenter, NSImageFrameNone, _window
end fn
 
void local fn DoDialog( ev as long, tag as long, wnd as long )
select ( ev )
case _windowWillClose : end
end select
end fn
 
on dialog fn DoDialog
 
fn BuildWindow
 
HandleEvents
</syntaxhighlight>
{{output}}
[[File:FutureBasic Yin and Yang.png]]
 
 
=={{header|Go}}==
Line 2,899 ⟶ 3,269:
 
</html></syntaxhighlight>
===SVG (path evenodd)===
Run this script from the browser console (F12) or from the <script> tag in the body of the html document.
<syntaxhighlight lang="javascript">function svgEl(tagName, attrs) {
const el = document.createElementNS('http://www.w3.org/2000/svg', tagName);
for (const key in attrs) el.setAttribute(key, attrs[key]);
return el;
}
 
function yinYang(r, x, y, th = 1) {
const cR = (dY, rad) => `M${x},${y + dY + rad} a${rad},${rad},0,1,1,.1,0z `;
const arc = (dY, rad, cw = 1) => `A${rad},${rad},0,0,${cw},${x},${y + dY} `;
const d = cR(0, r + th) + cR(r / 2, r / 6) + cR(-r / 2, r / 6)
+ `M${x},${y} ` + arc(r, r / 2, 0) + arc(-r, r) + arc(0, r / 2);
return svgEl('path', {d, 'fill-rule': 'evenodd'});
}
 
const dialog = document.body.appendChild(document.createElement('dialog'));
const svg = dialog.appendChild(svgEl('svg', {width: 170, height: 120}));
 
svg.appendChild(yinYang(50.0, 60, 60));
svg.appendChild(yinYang(20.0, 140, 30));
dialog.showModal();
</syntaxhighlight>
===Canvas===
{{trans|Flutter}}
Run this script from the browser console (F12) or from the <script> tag of an html document.
<syntaxhighlight lang="javascript">function yinYang(r, x, y, th = 1) {
const PI = Math.PI;
const path = new Path2D();
const cR = (dY, radius) => { path.arc(x, y + dY, radius, 0, PI * 2); path.closePath() };
cR(0, r + th);
cR(r / 2, r / 6);
cR(-r / 2, r / 6);
path.arc(x, y, r, PI / 2, -PI / 2);
path.arc(x, y - r / 2, r / 2, -PI / 2, PI / 2);
path.arc(x, y + r / 2, r / 2, -PI / 2, PI / 2, true);
return path;
}
 
document.documentElement.innerHTML = '<canvas width="170" height="120"/>';
const canvasCtx = document.querySelector('canvas').getContext('2d');
 
canvasCtx.fill(yinYang(50.0, 60, 60), 'evenodd');
canvasCtx.fill(yinYang(20.0, 140, 30), 'evenodd');
</syntaxhighlight>
 
=={{header|jq}}==
Line 3,227 ⟶ 3,642:
...####################
#</pre>
=={{header|M2000 Interpreter}}==
Using Drawing {} to make a emf file, which can play with various sizes and rotation.
[[File:Yin and yan.png|thumb|ScreenShotYinYang]]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
<syntaxhighlight lang="m2000 interpreter">
Module YinYang {
cls 5,0
Gradient 0, 5
Double
Print Over
Cursor 0,0
Report 2, "阴阳 Yin and yang 음양"
Normal
Drawing {
circle fill 0, 3000,1, 0
circle fill 15, 3000,1,0, pi/2, -pi/2
step 0, -1500
circle fill 15, 1500,1,15
width 4 {
circle fill 0, 500,1,0
}
step 0, 3000
circle fill 0, 1500,1,0
width 4 {
circle fill 15, 500,1,15
step 0, -1500
circle 3000,1,0
}
} as A
Move 6000, 5000-1500
Image A, 6000
Move 2000, 5000
Image A, 3000
Move 2000+1500, 5000+1500
hold // hold surface to release by statement release
Mouse.Icon Hide
i=0
every 10 {
if inkey$=" " or mouse=2 then exit
release
move mouse.x, mouse.y
Image A, 3000, ,i : i+=5:if i>355 then i=0
Refresh 20
if mouse=1 then hold
}
Mouse.Icon Show
filename$="yin and yang.emf"
// save to file
Open filename$ for output as #f
Put #f, A, 1
Close #f
// open mspaing
win "mspaint", quote$(dir$+filename$)
}
YinYang
</syntaxhighlight>
 
=={{header|Maple}}==
Line 4,659 ⟶ 5,147:
 
=={{header|Rust}}==
Creates an [[Media:YinYang rust.svg|svg file]] [[File:YinYang rust.svg|60px]]
Creates a [http://gist.githubusercontent.com/vvshard/b11ee56adbb21967e43fb606cba19e56/raw/yin_yang.svg '''yin_yang.svg file''']. Rust version 1.58.0 or higher required
 
'''[dependencies]'''<br>
svg = "0.1012.0"
<syntaxhighlight lang="fsharprust">use svg::node::element::Path;
 
fn main() {
let doc = svg::Document::new()
.add(yin_yang(15.0, 1.0).set("transform", "translate(20,20)"))
.add(yin_yang(6.0, 10.07).set("transform", "translate(50,1130)"));
svg::save("yin_yangYinYang_rust.svg", &doc.set("viewBox", (-20, -20, 60, 40))).unwrap();
}
/// th - the thickness of the outline around yang
Line 4,864 ⟶ 5,352:
readln(KEYBOARD);
end func;</syntaxhighlight>
 
=={{header|SETL}}==
<syntaxhighlight lang="setl>program yin_yang;
print(taijitu 4);
print(taijitu 8);
op taijitu(r);
return +/[+/[pixel(x,y,r) : x in [-2*r..2*r]] + "\n" : y in [-r..r]];
end op;
proc pixel(x,y,r);
return if circle(x,y,-r/2,r/6) then '#'
elseif circle(x,y,r/2,r/6) then '.'
elseif circle(x,y,-r/2,r/2) then '.'
elseif circle(x,y,r/2,r/2) then '#'
elseif circle(x,y,0,r) then
if x<0 then '.' else '#' end
else ' '
end;
end proc;
proc circle(x,c,y,r);
return r*r >= (x/2)**2 + (y-c)**2;
end proc;
end program;</syntaxhighlight>
{{out}}
<pre> .
.........##
.....###...##
...........####
.........########
....###########
..###...#####
..#########
#
 
.
.............##
.................####
...........###......#####
...........#####......#####
.............###......#######
......................#########
.....................##########
.................################
..........#####################
.........######################
.......######...#############
.....######.....###########
.....######...###########
....#################
..#############
#</pre>
 
=={{header|Sidef}}==
Line 5,070 ⟶ 5,611:
===Text===
{{trans|AWK}}
<syntaxhighlight lang="ecmascriptwren">var inCircle = Fn.new { |centerX, centerY, radius, x, y|
return (x-centerX)*(x-centerX)+(y-centerY)*(y-centerY) <= radius*radius
}
Line 5,169 ⟶ 5,710:
{{libheader|DOME}}
With a few minor changes, we can use the same code to draw these symbols in DOME.
<syntaxhighlight lang="ecmascriptwren">import "dome" for Window
import "graphics" for Canvas, Color
 
9,482

edits