Add tasks for all the new languages

This commit is contained in:
Tina Müller 2016-12-05 23:44:36 +01:00
parent 9dc3c2bb62
commit bba7bfd280
13208 changed files with 134745 additions and 0 deletions

View file

@ -0,0 +1,47 @@
PROGRAM DRAGON
!
! for rosettacode.org
!
!$DYNAMIC
DIM RQS[0]
!$INCLUDE="PC.LIB"
PROCEDURE DRAGON
IF LEVEL<=0 THEN
YN=SIN(ROTATION)*INSIZE+Y
XN=COS(ROTATION)*INSIZE+X
LINE(X,Y,XN,YN,12,FALSE)
ITER=ITER+1
X=XN Y=YN
EXIT PROCEDURE
END IF
INSIZE=INSIZE/SQ
ROTATION=ROTATION+RQ*QPI
LEVEL=LEVEL-1
RQS[LEVEL]=RQ
RQ=1 DRAGON
ROTATION=ROTATION-RQS[LEVEL]*QPI*2
RQ=-1 DRAGON
RQ=RQS[LEVEL]
ROTATION=ROTATION+RQ*QPI
LEVEL=LEVEL+1
INSIZE=INSIZE*SQ
END PROCEDURE
BEGIN
SCREEN(9)
LEVEL=12 INSIZE=287 ! initial values
X=200 Y=120 !
SQ=SQR(2) QPI=ATN(1) ! constants
ROTATION=0 ITER=0 RQ=1 ! state variables
!$DIM RQS[LEVEL]
! stack for RQ (ROTATION coefficient)
LINE(0,0,639,349,14,TRUE)
DRAGON
GET(A$)
END PROGRAM

View file

@ -0,0 +1,94 @@
import Color exposing (..)
import Collage exposing (..)
import Element exposing (..)
import Time exposing (..)
import Html exposing (..)
import Html.App exposing (program)
type alias Point = (Float, Float)
type alias Model =
{ points : List Point
, level : Int
, frame : Int
}
maxLevel = 12
frameCount = 100
type Msg = Tick Time
init : (Model,Cmd Msg)
init = ( { points = [(-200.0, -70.0), (200.0, -70.0)]
, level = 0
, frame = 0
}
, Cmd.none )
-- New point between two existing points. Offset to left or right
newPoint : Point -> Point -> Float -> Point
newPoint (x0,y0) (x1,y1) offset =
let (vx, vy) = ((x1 - x0) / 2.0, (y1 - y0) / 2.0)
(dx, dy) = (-vy * offset , vx * offset )
in (x0 + vx + dx, y0 + vy + dy) --offset from midpoint
-- Insert between existing points. Offset to left or right side.
newPoints : Float -> List Point -> List Point
newPoints offset points =
case points of
[] -> []
[p0] -> [p0]
p0::p1::rest -> p0 :: newPoint p0 p1 offset :: newPoints -offset (p1::rest)
update : Msg -> Model -> (Model, Cmd Msg)
update _ model =
let mo = if (model.level == maxLevel)
then model
else let nextFrame = model.frame + 1
in if (nextFrame == frameCount)
then { points = newPoints 1.0 model.points
, level = model.level+1
, frame = 0
}
else { model | frame = nextFrame
}
in (mo, Cmd.none)
-- break a list up into n equal sized lists.
breakupInto : Int -> List a -> List (List a)
breakupInto n ls =
let segmentCount = (List.length ls) - 1
breakup n ls = case ls of
[] -> []
_ -> List.take (n+1) ls :: breakup n (List.drop n ls)
in if n > segmentCount
then [ls]
else breakup (segmentCount // n) ls
view : Model -> Html Msg
view model =
let offset = toFloat (model.frame) / toFloat frameCount
colors = [red, orange, green, blue]
in toHtml
<| layers
[ collage 700 500
(model.points
|> newPoints offset
|> breakupInto (List.length colors) -- for coloring
|> List.map path
|> List.map2 (\color path -> traced (solid color) path ) colors )
, show model.level
]
subscriptions : Model -> Sub Msg
subscriptions _ =
Time.every (5*millisecond) Tick
main =
program
{ init = init
, view = view
, update = update
, subscriptions = subscriptions
}

View file

@ -0,0 +1,63 @@
`Draw Dragon [ from .x1. .y1. to .x2. .y2. [level .level.] ]'
Draw a dragon curve going from .x1. .y1. to .x2. .y2. with recursion
depth .level.
The total number of line segments for the recursion is 2^level.
level=0 is a straight line from x1,y1 to x2,y2.
The default for x1,y1 and x2,y2 is to draw horizontally from 0,0
to 1,0.
{
new .x1. .y1. .x2. .y2. .level.
.x1. = \.word3.
.y1. = \.word4.
.x2. = \.word6.
.y2. = \.word7.
.level. = \.word9.
if {rpn \.words. 5 >=}
.x2. = 1
.y2. = 0
end if
if {rpn \.words. 7 >=}
.level. = 6
end if
if {rpn 0 .level. <=}
draw line from .x1. .y1. to .x2. .y2.
else
.level. = {rpn .level. 1 -}
# xmid,ymid is half way between x1,y1 and x2,y2 and up at
# right angles away.
#
# xmid,ymid xmid = (x1+x2 + y2-y1)/2
# ^ ^ ymid = (x1-x2 + y1+y2)/2
# / . \
# / . \
# x1,y1 ........... x2,y2
#
new .xmid. .ymid.
.xmid. = {rpn .x1. .x2. + .y2. .y1. - + 2 /}
.ymid. = {rpn .x1. .x2. - .y1. .y2. + + 2 /}
# The recursion is a level-1 dragon from x1,y1 to the midpoint
# and the same from x2,y2 to the midpoint (the latter
# effectively being a revered dragon.)
#
Draw Dragon from .x1. .y1. to .xmid. .ymid. level .level.
Draw Dragon from .x2. .y2. to .xmid. .ymid. level .level.
delete .xmid. .ymid.
end if
delete .x1. .y1. .x2. .y2. .level.
}
# Dragon curve from 0,0 to 1,0 extends out by 1/3 at the ends, so
# extents -0.5 to +1.5 for a bit of margin. The Y extent is the same
# size 2 to make the graph square.
set x axis -0.5 1.5 .25
set y axis -1 1 .25
Draw Dragon

View file

@ -0,0 +1,38 @@
import <Utilities/Math.sl>;
import <Utilities/Conversion.sl>;
initPoints := [[0,0],[1,0]];
f1(point(1)) :=
let
matrix := [[cos(45 * (pi/180)), -sin(45 * (pi/180))],
[sin(45 * (pi/180)), cos(45 * (pi/180))]];
in
head(transpose((1/sqrt(2)) * matmul(matrix, transpose([point]))));
f2(point(1)) :=
let
matrix := [[cos(135 * (pi/180)), -sin(135 * (pi/180))],
[sin(135 * (pi/180)), cos(135 * (pi/180))]];
in
head(transpose((1/sqrt(2)) * matmul(matrix, transpose([point])))) + initPoints[2];
matmul(X(2),Y(2))[i,j] := sum(X[i,all]*Y[all,j]);
entry(steps(0), maxX(0), maxY(0)) :=
let
scaleX := maxX / 1.5;
scaleY := maxY;
shiftX := maxX / 3.0 / 1.5;
shiftY := maxY / 3.0;
in
round(run(steps, initPoints) * [scaleX, scaleY] + [shiftX, shiftY]);
run(steps(0), result(2)) :=
let
next := f1(result) ++ f2(result);
in
result when steps <= 0
else
run(steps - 1, next);

View file

@ -0,0 +1,74 @@
#include <iostream>
#include <vector>
#include "SL_Generated.h"
#include "Cimg.h"
using namespace cimg_library;
using namespace std;
int main(int argc, char** argv)
{
int threads = 0;
if(argc > 1) threads = atoi(argv[1]);
Sequence< Sequence<int> > result;
sl_init(threads);
int width = 500;
if(argc > 2) width = atoi(argv[2]);
int height = width;
if(argc > 3) height = atoi(argv[3]);
CImg<unsigned char> visu(width, height, 1, 3, 0);
CImgDisplay draw_disp(visu);
SLTimer compTimer;
SLTimer drawTimer;
int steps = 0;
int maxSteps = 18;
if(argc > 4) maxSteps = atoi(argv[4]);
int waitTime = 200;
if(argc > 5) waitTime = atoi(argv[5]);
bool adding = true;
while(!draw_disp.is_closed())
{
compTimer.start();
sl_entry(steps, width, height, threads, result);
compTimer.stop();
drawTimer.start();
visu.fill(0);
double thirdSize = ((result.size() / 2.0) / 3.0);
thirdSize = (int)thirdSize == 0 ? 1 : thirdSize;
for(int i = 1; i <= result.size(); i+=2)
{
unsigned char shade = (unsigned char)(255 * ((((i / 2) % (int)thirdSize) / thirdSize)) + 0.5);
unsigned char r = i / 2 <= thirdSize ? shade : 255/2;
unsigned char g = thirdSize < i / 2 && i / 2 <= thirdSize * 2 ? shade : 255/2;
unsigned char b = thirdSize * 2 < i / 2 && i / 2 <= thirdSize * 3 ? shade : 255/2;
const unsigned char color[] = {r,g,b};
visu.draw_line(result[i][1], result[i][2], 0, result[i + 1][1], result[i + 1][2], 0, color);
}
visu.display(draw_disp);
drawTimer.stop();
draw_disp.set_title("Dragon Curve in SequenceL: %d Threads | Steps: %d | CompTime: %f Seconds | Draw Time: %f Seconds", threads, steps, drawTimer.getTime(), compTimer.getTime());
if(adding) steps++;
else steps--;
if(steps <= 0) adding = true;
else if(steps >= maxSteps) adding = false;
draw_disp.wait(waitTime);
}
sl_done();
return 0;
}

View file

@ -0,0 +1,37 @@
define halfpi = Math.pi/2;
# Computing the dragon with a L-System
var dragon = 'FX';
{
dragon.gsub!('X', 'x+yF+');
dragon.gsub!('Y', '-Fx-y');
dragon.tr!('xy', 'XY');
} * 10;
# Drawing the dragon in SVG
var (x, y) = (100, 100);
var theta = 0;
var r = 2;
print <<'EOT';
<?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 width='100%' height='100%' version='1.1'
xmlns='http://www.w3.org/2000/svg'>
EOT
dragon.each { |c|
given(c) {
when ('F') {
printf("<line x1='%.0f' y1='%.0f' ", x, y);
printf("x2='%.0f' ", x += r*Math.cos(theta));
printf("y2='%.0f' ", y += r*Math.sin(theta));
printf("style='stroke:rgb(0,0,0);stroke-width:1'/>\n");
}
when ('+') { theta += halfpi }
when ('-') { theta -= halfpi }
}
}
print '</svg>';

View file

@ -0,0 +1,56 @@
# MATRIX MATH
def mult(m; v):
[ m[0][0] * v[0] + m[0][1] * v[1],
m[1][0] * v[0] + m[1][1] * v[1] ];
def minus(a; b): [ a[0]-b[0], a[1]-b[1] ];
def plus(a; b): [ a[0]+b[0], a[1]+b[1] ];
# SVG STUFF
# default values of stroke and stroke-width are provided
def style(obj):
{ "stroke": "rgb(255, 15, 131)", "stroke-width": "2px" } as $default
| ($default + obj) as $s
| "<style type='text/css' media='all'>
.dragon { stroke:\($s.stroke); stroke-width:\($s["stroke-width"]); }
</style>";
def svg(id; width; height):
"<svg width='\(width // "100%")' height='\(height // "100%") '
id='\(id)'
xmlns='http://www.w3.org/2000/svg'>";
# Turn a pair of points into an SVG path like "M1 1L2 2" (M=move to; L=line to).
def toSVGpath(a; b):
"M\(a[0]) \(a[1])L\(b[0]) \(b[1])";
# DRAGON MAKING
def fractalMakeDragon(svgid; ptA; ptC; steps; left; css):
# Make a new point, either to the left or right
def growNewPoint(ptA; ptC; left):
[[ 1/2,-1/2 ], [ 1/2, 1/2 ]] as $left
| [[ 1/2, 1/2 ], [-1/2, 1/2 ]] as $right
| plus(ptA;
mult(if left then $left else $right end;
minus(ptC; ptA)));
def grow(ptA; ptC; steps; left):
# if we have more iterations to go...
if steps > 1 then
growNewPoint(ptA; ptC; left) as $ptB
# ... then recurse using each new line, one left, one right
| grow($ptB; ptA; steps-1; left),
grow($ptB; ptC; steps-1; left)
else
toSVGpath(ptA; ptC)
end;
svg(svgid; "100%"; "100%"),
style(css),
"<path class='dragon' d='",
grow(ptA; ptC; steps; left),
"'/>",
"</svg>";

View file

@ -0,0 +1,2 @@
# Default values are provided for the last argument
fractalMakeDragon("roar"; [100,300]; [500,300]; 15; false; {})

View file

@ -0,0 +1,11 @@
$ jq -n -r -f dragon.jq
<svg width='100%' height='100% '
id='roar'
xmlns='http://www.w3.org/2000/svg'>
<style type='text/css' media='all'>
.dragon { stroke:rgb(255, 15, 131); stroke-width:2px; }
</style>
<path class='dragon' d='
M259.375 218.75L259.375 221.875
M259.375 218.75L262.5 218.75
...