June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -0,0 +1,103 @@
// patscc -O2 -flto -D_GNU_SOURCE -DATS_MEMALLOC_LIBC sierpinski.dats -o sierpinski -latslib -lSDL2
#include "share/atspre_staload.hats"
typedef point = (int, int)
extern fun midpoint(A: point, B: point): point = "mac#"
extern fun sierpinski_draw(n: int, A: point, B: point, C: point): void = "mac#"
extern fun triangle_remove(A: point, B: point, C: point): void = "mac#"
extern fun sdl_drawline(x1: int, y1: int, x2: int, y2: int): void = "ext#sdl_drawline"
extern fun line(A: point, B: point): void
extern fun ats_tredraw(): void = "mac#ats_tredraw"
implement midpoint(A, B) = (xmid, ymid) where {
val xmid = (A.0 + B.0) / 2
val ymid = (A.1 + B.1) / 2
}
implement triangle_remove(A, B, C) = (
line(A, B);
line(B, C);
line(C, A);
)
implement sierpinski_draw(n, A, B, C) =
if n > 0 then
let
val AB = midpoint(A, B)
val BC = midpoint(B, C)
val CA = midpoint(C, A)
in
triangle_remove(AB, BC, CA);
sierpinski_draw(n-1, A, AB, CA);
sierpinski_draw(n-1, B, BC, AB);
sierpinski_draw(n-1, C, CA, BC);
end
implement line(A, B) = sdl_drawline(A.0, A.1, B.0, B.1)
extern fun SDL_Init(): void = "ext#sdl_init"
extern fun SDL_Quit(): void = "ext#sdl_quit"
extern fun SDL_Loop(): void = "ext#sdl_loop"
implement ats_tredraw() = sierpinski_draw(7, (320, 0), (0, 480), (640, 480))
implement main0() = (
SDL_Init();
SDL_Loop();
SDL_Quit();
)
%{
#include <SDL2/SDL.h>
#include <unistd.h>
extern void ats_tredraw();
SDL_Window *sdlwin;
SDL_Renderer *sdlren;
void sdl_init() {
if (SDL_Init(SDL_INIT_VIDEO)) {
exit(1);
}
if ((sdlwin = SDL_CreateWindow("sierpinski triangles", 100, 100, 640, 480, SDL_WINDOW_SHOWN)) == NULL) {
SDL_Quit();
exit(2);
}
if ((sdlren = SDL_CreateRenderer(sdlwin, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC)) == NULL) {
SDL_DestroyWindow(sdlwin);
SDL_Quit();
exit(3);
}
}
void sdl_clear() {
SDL_SetRenderDrawColor(sdlren, 0, 0, 0, SDL_ALPHA_OPAQUE);
SDL_RenderClear(sdlren);
SDL_SetRenderDrawColor(sdlren, 255, 255, 255, SDL_ALPHA_OPAQUE);
}
void sdl_loop() {
SDL_Event event;
while (1) {
sdl_clear();
ats_tredraw();
SDL_RenderPresent(sdlren);
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
return;
}
}
}
}
void sdl_quit() {
SDL_DestroyRenderer(sdlren);
SDL_DestroyWindow(sdlwin);
SDL_Quit();
}
void sdl_drawline(int x1, int y1, int x2, int y2) {
SDL_RenderDrawLine(sdlren, x1, y1, x2, y2);
}
%}

View file

@ -0,0 +1,29 @@
-module(sierpinski).
-author("zduchac").
-export([start/0]).
sierpinski(DC, Order) ->
Size = 1 bsl Order,
sierpinski(DC, Order, Size, 0, 0).
sierpinski(_, _, Size, _, Y) when Y =:= Size ->
ok;
sierpinski(DC, Order, Size, X, Y) when X =:= Size ->
sierpinski(DC, Order, Size, 0, Y + 1);
sierpinski(DC, Order, Size, X, Y) when X band Y =:= 0 ->
wxDC:drawPoint(DC, {X, Y}),
sierpinski(DC, Order, Size, X + 1, Y);
sierpinski(DC, Order, Size, X, Y) ->
sierpinski(DC, Order, Size, X + 1, Y).
start() ->
Wx = wx:new(),
Frame = wxFrame:new(Wx, -1, "Raytracer", []),
wxFrame:connect(Frame, paint, [{callback,
fun(_Evt, _Obj) ->
DC = wxPaintDC:new(Frame),
sierpinski(DC, 8),
wxPaintDC:destroy(DC)
end
}]),
wxFrame:show(Frame).

View file

@ -0,0 +1,21 @@
using Luxor
function sierpinski(txy, levelsyet)
nxy = zeros(6)
if levelsyet > 0
for i in 1:6
pos = i < 5 ? i + 2 : i - 4
nxy[i] = (txy[i] + txy[pos]) / 2.0
end
sierpinski([txy[1],txy[2],nxy[1],nxy[2],nxy[5],nxy[6]], levelsyet-1)
sierpinski([nxy[1],nxy[2],txy[3],txy[4],nxy[3],nxy[4]], levelsyet-1)
sierpinski([nxy[5],nxy[6],nxy[3],nxy[4],txy[5],txy[6]], levelsyet-1)
else
poly([Point(txy[1],txy[2]),Point(txy[3],txy[4]),Point(txy[5],txy[6])], :fill ,close=true)
end
end
Drawing(800, 800)
sierpinski([400., 100., 700., 500., 100., 500.], 7)
finish()
preview()

View file

@ -1,20 +1,9 @@
#!/usr/bin/env python
##########################################################################################
# import necessary modules
# ------------------------
from numpy import *
import turtle
##########################################################################################
# Functions defining the drawing actions
# (used by the function DrawSierpinskiTriangle).
# ----------------------------------------------
def Left(turn, point, fwd, angle, turt):
turt.left(angle)
return [turn, point, fwd, angle, turt]
def Right(turn, point, fwd, angle, turt):
turt.right(angle)
return [turn, point, fwd, angle, turt]
def Forward(turn, point, fwd, angle, turt):
turt.forward(fwd)
return [turn, point, fwd, angle, turt]
# likely the simplest possible version?
import turtle as t
def sier(n,length):
if (n==0):
return
for i in range(3):
sier(n-1, length/2)
t.fd(length)
t.rt(120)

View file

@ -1,60 +1,21 @@
#!/usr/bin/env python
##########################################################################################
# The drawing function
# --------------------
#
# level level of Sierpinski triangle (minimum value = 1)
# ss screensize (Draws on a screen of size ss x ss. Default value = 400.)
#-----------------------------------------------------------------------------------------
def DrawSierpinskiTriangle(level, ss=400):
# typical values
turn = 0 # initial turn (0 to start horizontally)
angle=60.0 # in degrees
# a very complicated version
# import necessary modules
# ------------------------
from numpy import *
import turtle
# Initialize the turtle
turtle.hideturtle()
turtle.screensize(ss,ss)
turtle.penup()
turtle.degrees()
# The starting point on the canvas
fwd0 = float(ss)
point=array([-fwd0/2.0, -fwd0/2.0])
# Setting up the Lindenmayer system
# Assuming that the triangle will be drawn in the following way:
# 1.) Start at a point
# 2.) Draw a straight line - the horizontal line (H)
# 3.) Bend twice by 60 degrees to the left (--)
# 4.) Draw a straight line - the slanted line (X)
# 5.) Bend twice by 60 degrees to the left (--)
# 6.) Draw a straight line - another slanted line (X)
# This produces the triangle in the first level. (so the axiom to begin with is H--X--X)
# 7.) For the next level replace each horizontal line using
# X->XX
# H -> H--X++H++X--H
# The lengths will be halved.
decode = {'-':Left, '+':Right, 'X':Forward, 'H':Forward}
axiom = 'H--X--X'
# Start the drawing
turtle.goto(point[0], point[1])
turtle.pendown()
turtle.hideturtle()
turt=turtle.getpen()
startposition=turt.clone()
# Get the triangle in the Lindenmayer system
fwd = fwd0/(2.0**level)
path = axiom
for i in range(0,level):
path=path.replace('X','XX')
path=path.replace('H','H--X++H++X--H')
# Draw it.
for i in path:
[turn, point, fwd, angle, turt]=decode[i](turn, point, fwd, angle, turt)
##########################################################################################
DrawSierpinskiTriangle(5)
# Functions defining the drawing actions
# (used by the function DrawSierpinskiTriangle).
# ----------------------------------------------
def Left(turn, point, fwd, angle, turt):
turt.left(angle)
return [turn, point, fwd, angle, turt]
def Right(turn, point, fwd, angle, turt):
turt.right(angle)
return [turn, point, fwd, angle, turt]
def Forward(turn, point, fwd, angle, turt):
turt.forward(fwd)
return [turn, point, fwd, angle, turt]

View file

@ -0,0 +1,60 @@
##########################################################################################
# The drawing function
# --------------------
#
# level level of Sierpinski triangle (minimum value = 1)
# ss screensize (Draws on a screen of size ss x ss. Default value = 400.)
#-----------------------------------------------------------------------------------------
def DrawSierpinskiTriangle(level, ss=400):
# typical values
turn = 0 # initial turn (0 to start horizontally)
angle=60.0 # in degrees
# Initialize the turtle
turtle.hideturtle()
turtle.screensize(ss,ss)
turtle.penup()
turtle.degrees()
# The starting point on the canvas
fwd0 = float(ss)
point=array([-fwd0/2.0, -fwd0/2.0])
# Setting up the Lindenmayer system
# Assuming that the triangle will be drawn in the following way:
# 1.) Start at a point
# 2.) Draw a straight line - the horizontal line (H)
# 3.) Bend twice by 60 degrees to the left (--)
# 4.) Draw a straight line - the slanted line (X)
# 5.) Bend twice by 60 degrees to the left (--)
# 6.) Draw a straight line - another slanted line (X)
# This produces the triangle in the first level. (so the axiom to begin with is H--X--X)
# 7.) For the next level replace each horizontal line using
# X->XX
# H -> H--X++H++X--H
# The lengths will be halved.
decode = {'-':Left, '+':Right, 'X':Forward, 'H':Forward}
axiom = 'H--X--X'
# Start the drawing
turtle.goto(point[0], point[1])
turtle.pendown()
turtle.hideturtle()
turt=turtle.getpen()
startposition=turt.clone()
# Get the triangle in the Lindenmayer system
fwd = fwd0/(2.0**level)
path = axiom
for i in range(0,level):
path=path.replace('X','XX')
path=path.replace('H','H--X++H++X--H')
# Draw it.
for i in path:
[turn, point, fwd, angle, turt]=decode[i](turn, point, fwd, angle, turt)
##########################################################################################
DrawSierpinskiTriangle(5)