Skip to content

Commit

Permalink
random_deposition
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrieleLabanca committed Dec 26, 2017
1 parent 55524f8 commit a92f43c
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 66 deletions.
Binary file added .index.html.swp
Binary file not shown.
75 changes: 9 additions & 66 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,83 +10,26 @@
</style>
<meta charset="utf-8">
<script type="text/javascript" src="http://mbostock.github.com/d3/d3.js"></script>
<script type="text/javascript" src="js/grid.js"></script>

</head>
<body>
<svg class="svgc" id="svg1"></svg>

<script type="text/javascript">
var sq = 10;
var N = 100;
var M = 200;


window.onload = function(){
var mydata = generateGrid();
var svg1 = d3.select('#svg1');
svg1.attr('width',N*sq).attr('height',M*sq);
render(svg1,mydata);

setInterval(step,1000/15,svg1,mydata);
};

function step(svg1,mydata){
update(mydata);
animation(svg1,mydata);
}


function render(svg1,mydata){
svg1.selectAll('rect')
.data(mydata)
.enter()
.append('rect')
.attr("width",function(d){ return sq;})
.attr("height",function(d){ return sq; })
.attr("fill",function(d){ return associateColor(d.status);})
.attr("x",function(d){ return d.x;})
.attr("y",function(d){ return d.y;});
}

function update(arr){
if( typeof update.counter == 'undefined' ) {
update.counter = 0;
for(var n=0; n<10; n++){
for(var m=0; m<10; m++){
}
arr[update.counter].status="full";
update.counter++;


}

function animation(svg1,mydata){
svg1.selectAll('rect')
.data(mydata)
.transition()
.attr("width",function(d){ return sq;})
.attr("height",function(d){ return sq; })
.attr("fill",function(d){ return associateColor(d.status);})
.attr("x",function(d){ return d.x;})
.attr("y",function(d){ return d.y;});


function step(svg1,mydata){
//sequential_update(mydata);
random_deposition_update(mydata);

animation(svg1,mydata);
}

function generateGrid(){
var grid = [];
var y = -1;
for(var i=0; i<(N*M); i++){
if(i%N == 0) y++;
grid.push(new Object({"index":i,"x":i%N,"y":y,"status":"void"}));
}
return grid;
}

function associateColor(S){
if(S === "void") return 'grey';
if(S === "full") return 'red';
}



</script>
</body>
</html>
Binary file added js/.grid.js.swp
Binary file not shown.
101 changes: 101 additions & 0 deletions js/grid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
var sq = 10;
var N = 100;
var M = 200;


window.onload = function(){
var mydata = generateGrid(); //is an array[N*M]
var svg1 = d3.select('#svg1');
svg1.attr('width',N*sq).attr('height',M*sq);
render(svg1,mydata);

setInterval(step,1000/15,svg1,mydata); // step defined in index.html
};



function render(svg1,mydata){
svg1.selectAll('rect')
.data(mydata)
.enter()
.append('rect')
.attr("width",function(d){ return sq;})
.attr("height",function(d){ return sq; })
.attr("fill",function(d){ return associateColor(d.status);})
.attr("x",function(d){ return sq*d.x;})
.attr("y",function(d){ return sq*d.y;});
}

function sequential_update(arr){
if( typeof sequential_update.counter == 'undefined' ) {
sequential_update.counter = N*M -1 ;
}
arr[sequential_update.counter].status="full";
sequential_update.counter--;
}

function block_update(arr){
if(typeof this.already == 'undefined'){
this.already = 0;
}
if(this.already == 0){
for(var n=0; n<10; n++){
for(var m=0; m<10; m++){
arr[getn(n,m)].status = 'full';
}
}
this.already = 1;
}
}

function random_deposition_update(arr){
var rnd = Math.floor(Math.random()*N);
arr[getn(rnd,col_h(arr,rnd)+1)].status = 'full';
}


function col_h(arr,n){ // get height of n-th column
var h = M;
var this_full = false;
do{
h--;
if(arr[getn(n,h)].status == 'full'){ break; }
if(h==0) return -1;
}while(h>0);
return h;
}

function animation(svg1,mydata){
svg1.selectAll('rect')
.data(mydata)
.transition()
.attr("width",function(d){ return sq;})
.attr("height",function(d){ return sq; })
.attr("fill",function(d){ return associateColor(d.status);})
.attr("x",function(d){ return sq*d.x;})
.attr("y",function(d){ return sq*d.y;});
}

function generateGrid(){
var grid = [];
var y = -1;
for(var i=0; i<(N*M); i++){
if(i%N == 0) y++;
grid.push(new Object({"index":i,"x":i%N,"y":y,"status":"void"}));
}
return grid;
}

function associateColor(S){
if(S === "void") return 'grey';
if(S === "full") return 'red';
}

function getn(n,m){ // converts (x,y) position into index
return N*M-(N-n+m*N); // (0,0) is bottom-left

////N*M-1-(n+m*N); //OLD
}



0 comments on commit a92f43c

Please sign in to comment.