September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -0,0 +1,17 @@
|
|||
# triangle_x(n) and triangle_y(n) return X,Y coordinates for the
|
||||
# Sierpinski triangle point number n, for integer n.
|
||||
triangle_x(n) = (n > 0 ? 2*triangle_x(int(n/3)) + digit_to_x(int(n)%3) : 0)
|
||||
triangle_y(n) = (n > 0 ? 2*triangle_y(int(n/3)) + digit_to_y(int(n)%3) : 0)
|
||||
digit_to_x(d) = (d==0 ? 0 : d==1 ? -1 : 1)
|
||||
digit_to_y(d) = (d==0 ? 0 : 1)
|
||||
|
||||
# Plot the Sierpinski triangle to "level" many replications.
|
||||
# "trange" and "samples" are chosen so the parameter t runs through
|
||||
# integers t=0 to 3**level-1, inclusive.
|
||||
#
|
||||
level=6
|
||||
set trange [0:3**level-1]
|
||||
set samples 3**level
|
||||
set parametric
|
||||
set key off
|
||||
plot triangle_x(t), triangle_y(t) with points
|
||||
|
|
@ -0,0 +1,57 @@
|
|||
<!-- SierpinskiTriangle.html -->
|
||||
<html>
|
||||
<head><title>Sierpinski Triangle Fractal</title>
|
||||
<script>
|
||||
// HF#1 Like in PARI/GP: return random number 0..max-1
|
||||
function randgp(max) {return Math.floor(Math.random()*max)}
|
||||
// HF#2 Random hex color
|
||||
function randhclr() {
|
||||
return "#"+
|
||||
("00"+randgp(256).toString(16)).slice(-2)+
|
||||
("00"+randgp(256).toString(16)).slice(-2)+
|
||||
("00"+randgp(256).toString(16)).slice(-2)
|
||||
}
|
||||
// HFJS#3: Plot any matrix mat (filled with 0,1)
|
||||
function pmat01(mat, color) {
|
||||
// DCLs
|
||||
var cvs = document.getElementById('cvsId');
|
||||
var ctx = cvs.getContext("2d");
|
||||
var w = cvs.width; var h = cvs.height;
|
||||
var m = mat[0].length; var n = mat.length;
|
||||
// Cleaning canvas and setting plotting color
|
||||
ctx.fillStyle="white"; ctx.fillRect(0,0,w,h);
|
||||
ctx.fillStyle=color;
|
||||
// MAIN LOOP
|
||||
for(var i=0; i<m; i++) {
|
||||
for(var j=0; j<n; j++) {
|
||||
if(mat[i][j]==1) { ctx.fillRect(i,j,1,1)};
|
||||
}//fend j
|
||||
}//fend i
|
||||
}//func end
|
||||
|
||||
// Prime function
|
||||
// Plotting Sierpinski triangle. aev 4/9/17
|
||||
// ord - order, fn - file name, ttl - plot title, clr - color
|
||||
function pSierpinskiT() {
|
||||
var cvs=document.getElementById("cvsId");
|
||||
var ctx=cvs.getContext("2d");
|
||||
var w=cvs.width, h=cvs.height;
|
||||
var R=new Array(w);
|
||||
for (var i=0; i<w; i++) {R[i]=new Array(w)
|
||||
for (var j=0; j<w; j++) {R[i][j]=0}
|
||||
}
|
||||
ctx.fillStyle="white"; ctx.fillRect(0,0,w,h);
|
||||
for (var y=0; y<w; y++) {
|
||||
for (var x=0; x<w; x++) {
|
||||
if((x & y) == 0 ) {R[x][y]=1}
|
||||
}}
|
||||
pmat01(R, randhclr());
|
||||
}
|
||||
</script></head>
|
||||
<body style="font-family: arial, helvatica, sans-serif;">
|
||||
<b>Please click to start and/or change color: </b>
|
||||
<input type="button" value=" Plot it! " onclick="pSierpinskiT();">
|
||||
<h3>Sierpinski triangle fractal</h3>
|
||||
<canvas id="cvsId" width="640" height="640" style="border: 2px inset;"></canvas>
|
||||
<!--canvas id="cvsId" width="1280" height="1280" style="border: 2px inset;"></canvas-->
|
||||
</body></html>
|
||||
|
|
@ -1,15 +1,17 @@
|
|||
import javax.swing.*
|
||||
import java.awt.*
|
||||
import javax.swing.JFrame
|
||||
import javax.swing.JPanel
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
var i = 8 // Default
|
||||
if (args.any())
|
||||
if (args.any()) {
|
||||
try {
|
||||
i = args.first().toInt()
|
||||
} catch (e: NumberFormatException) {
|
||||
i = 8
|
||||
println("Usage: 'java SierpinskyTriangle [level]'\nNow setting level to $i")
|
||||
}
|
||||
}
|
||||
|
||||
object : JFrame("Sierpinsky Triangle - Kotlin") {
|
||||
val panel = object : JPanel() {
|
||||
|
|
@ -21,8 +23,9 @@ fun main(args: Array<String>) {
|
|||
|
||||
public override fun paintComponent(g: Graphics) {
|
||||
g.color = Color.BLACK
|
||||
if (g is Graphics2D)
|
||||
if (g is Graphics2D) {
|
||||
g.drawSierpinskyTriangle(i, 20, 20, size - 40)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,5 @@
|
|||
[x, x0] = deal(cat(3, [1 0]', [-1 0]', [0 sqrt(3)]'));
|
||||
for k = 1 : 6
|
||||
x = x(:,:) + x0 * 2 ^ k / 2;
|
||||
end
|
||||
patch('Faces', reshape(1 : 3 * 3 ^ k, 3, '')', 'Vertices', x(:,:)')
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
t = 0 : 2^16 - 1;
|
||||
plot(t, bitand(t, bitshift(t, -8)), 'k.')
|
||||
|
|
@ -0,0 +1,70 @@
|
|||
--
|
||||
-- demo\rosetta\SierpinskyTriangle.exw
|
||||
--
|
||||
include pGUI.e
|
||||
|
||||
Ihandle dlg, canvas
|
||||
cdCanvas cddbuffer, cdcanvas
|
||||
|
||||
procedure SierpinskyTriangle(integer level, atom x, atom y, atom w, atom h)
|
||||
atom w2 = w/2, w4 = w/4, h2 = h/2
|
||||
if level=1 then
|
||||
cdCanvasBegin(cddbuffer,CD_CLOSED_LINES)
|
||||
cdCanvasVertex(cddbuffer, x, y)
|
||||
cdCanvasVertex(cddbuffer, x+w2, y+h)
|
||||
cdCanvasVertex(cddbuffer, x+w, y)
|
||||
cdCanvasEnd(cddbuffer)
|
||||
else
|
||||
SierpinskyTriangle(level-1, x, y, w2, h2)
|
||||
SierpinskyTriangle(level-1, x+w4, y+h2, w2, h2)
|
||||
SierpinskyTriangle(level-1, x+w2, y, w2, h2)
|
||||
end if
|
||||
end procedure
|
||||
|
||||
integer level = 7
|
||||
|
||||
function redraw_cb(Ihandle /*ih*/, integer /*posx*/, integer /*posy*/)
|
||||
integer {w, h} = IupGetIntInt(canvas, "DRAWSIZE")
|
||||
cdCanvasActivate(cddbuffer)
|
||||
cdCanvasClear(cddbuffer)
|
||||
SierpinskyTriangle(level, w*0.05, h*0.05, w*0.9, h*0.9)
|
||||
cdCanvasFlush(cddbuffer)
|
||||
IupSetStrAttribute(dlg, "TITLE", "Sierpinsky Triangle (level %d)",{level})
|
||||
return IUP_DEFAULT
|
||||
end function
|
||||
|
||||
function map_cb(Ihandle ih)
|
||||
cdcanvas = cdCreateCanvas(CD_IUP, ih)
|
||||
cddbuffer = cdCreateCanvas(CD_DBUFFER, cdcanvas)
|
||||
cdCanvasSetBackground(cddbuffer, CD_WHITE)
|
||||
cdCanvasSetForeground(cddbuffer, CD_GRAY)
|
||||
return IUP_DEFAULT
|
||||
end function
|
||||
|
||||
function esc_close(Ihandle /*ih*/, atom c)
|
||||
if c=K_ESC then return IUP_CLOSE end if
|
||||
if find(c,"+-") then
|
||||
level = max(1,min(12,level+','-c))
|
||||
IupRedraw(canvas)
|
||||
end if
|
||||
return IUP_CONTINUE
|
||||
end function
|
||||
|
||||
procedure main()
|
||||
IupOpen()
|
||||
|
||||
canvas = IupCanvas(NULL)
|
||||
IupSetAttribute(canvas, "RASTERSIZE", "640x640")
|
||||
IupSetCallback(canvas, "MAP_CB", Icallback("map_cb"))
|
||||
IupSetCallback(canvas, "ACTION", Icallback("redraw_cb"))
|
||||
|
||||
dlg = IupDialog(canvas)
|
||||
IupSetAttribute(dlg, "TITLE", "Sierpinsky Triangle")
|
||||
IupSetCallback(dlg, "K_ANY", Icallback("esc_close"))
|
||||
|
||||
IupShow(dlg)
|
||||
IupSetAttribute(canvas, "RASTERSIZE", NULL)
|
||||
IupMainLoop()
|
||||
IupClose()
|
||||
end procedure
|
||||
main()
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
## Plotting Sierpinski triangle. aev 4/1/17
|
||||
## ord - order, fn - file name, ttl - plot title, clr - color
|
||||
pSierpinskiT <- function(ord, fn="", ttl="", clr="navy") {
|
||||
m=640; abbr="STR"; dftt="Sierpinski triangle";
|
||||
n=2^ord; M <- matrix(c(0), ncol=n, nrow=n, byrow=TRUE);
|
||||
cat(" *** START", abbr, date(), "\n");
|
||||
if(fn=="") {pf=paste0(abbr,"o", ord)} else {pf=paste0(fn, ".png")};
|
||||
if(ttl!="") {dftt=ttl}; ttl=paste0(dftt,", order ", ord);
|
||||
cat(" *** Plot file:", pf,".png", "title:", ttl, "\n");
|
||||
for(y in 1:n) {
|
||||
for(x in 1:n) {
|
||||
if(bitwAnd(x, y)==0) {M[x,y]=1}
|
||||
##if(bitwAnd(x, y)>0) {M[x,y]=1} ## Try this for "reversed" ST
|
||||
}}
|
||||
plotmat(M, pf, clr, ttl);
|
||||
cat(" *** END", abbr, date(), "\n");
|
||||
}
|
||||
## Executing:
|
||||
pSierpinskiT(6,,,"red");
|
||||
pSierpinskiT(8);
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
func sierpinski_triangle(n) -> Array {
|
||||
var triangle = ['*']
|
||||
{ |i|
|
||||
var sp = (' ' * Math.pow(2, i-1));
|
||||
var sp = (' ' * 2**i)
|
||||
triangle = (triangle.map {|x| sp + x + sp} +
|
||||
triangle.map {|x| x + ' ' + x})
|
||||
} * n
|
||||
|
|
|
|||
|
|
@ -0,0 +1,4 @@
|
|||
const Order=8, Size=(1).shiftLeft(Order);
|
||||
img:=PPM(300,300);
|
||||
foreach y,x in (Size,Size){ if(x.bitAnd(y)==0) img[x,y]=0xff0000 }
|
||||
img.write(File("sierpinskiTriangle.ppm","wb"));
|
||||
Loading…
Add table
Add a link
Reference in a new issue