<commit_before>var gridWidth = 22;
var gridHeight = 12;

var values=[], positions=[];//arrays for values and edges
var verticalEdges=[], horizontalEdges=[];
//values are currently 0 for empty and 1 for material
//TODO add more materials

function initializeArrays(){
   var i,j;
   for(i=0;i<gridWidth;i++){
      values[i] = [];
      positions[i] = [];
      for(j=0;j<gridHeight;j++){
         values[i][j]=0;//init to air
         positions[i][j]={x:0.5,y:0.5};
      }
   }
   for(i=0;i<gridWidth;i++){
      verticalEdges[i] = [];
      horizontalEdges[i] = [];
      for(j=0;j<gridHeight;j++){
         verticalEdges[i][j]={p:0,nx:1,ny:1};//no intersection
         horizontalEdges[i][j]={p:0,nx:1,ny:1};//no intersection
      }
   }
}
function getValue(x,y){
   if(x<0||x>=gridWidth||y<0||y>=gridHeight) return 0;//default to air
   return values[x][y];
}

function gridHasSignChange(x,y){
   var sgn = getValue(x,y);
   for(var i=0;i<2;i++){
      for(var j=0;j<2;j++){
         if(sgn!=getValue(x+i,y+j)) return true;//TODO more materials
      }
   }
   return false;
}
function edgeHasSignChange(x,y,isVertical){
   if(isVertical){
      return (getValue(x,y)!=getValue(x,y+1));
   }else{
      return (getValue(x,y)!=getValue(x+1,y));
   }
}

function getIntersectionPoint(x,y,isVertical,dx,dy){
   if(x<0||x>=gridWidth||y<0||y>=gridHeight) return {x:dx+0.5,y:dy+0.5};
   if(isVertical){
      return {x:dx, y:dy+verticalEdges[x][y].p};
   }else{
      return {x:dx+horizontalEdges[x][y].p, y:dy};
   }
}

function updateSquare(x,y){
   if(x<0||y<0) return;
   //recompute current gridposition
   var px=0.0,py=0.0,sx=0.0,sy=0.0;
   if(gridHasSignChange(x,y)){
      var p,n,nx,ny;
      if(edgeHasSignChange(x,y,true)){
         p = getIntersectionPoint(x,y,true,0,0);
         n = verticalEdges[x][y];
         nx=n.nx*n.nx;ny=n.ny*n.ny;
         //if(nx+ny<0.01){nx=0.5;ny=0.5;}
         px+=p.x*nx;
         sx+=nx;
         py+=p.y*ny;
         sy+=ny;
      }
      if(edgeHasSignChange(x,y,false)){
         p = getIntersectionPoint(x,y,false,0,0);
         n = horizontalEdges[x][y];
         nx=n.nx*n.nx;ny=n.ny*n.ny;
         //if(nx+ny<0.01){nx=0.5;ny=0.5;}
         px+=p.x*nx;
         sx+=nx;
         py+=p.y*ny;
         sy+=ny;
      }
      if(edgeHasSignChange(x+1,y,true)){
         p = getIntersectionPoint(x+1,y,true,1,0);
         n = verticalEdges[x+1][y];
         nx=n.nx*n.nx;ny=n.ny*n.ny;
         //if(nx+ny<0.01){nx=0.5;ny=0.5;}
         px+=p.x*nx;
         sx+=nx;
         py+=p.y*ny;
         sy+=ny;
      }
      if(edgeHasSignChange(x,y+1,false)){
         p = getIntersectionPoint(x,y+1,false,0,1);
         n = horizontalEdges[x][y+1];
         nx=n.nx*n.nx;ny=n.ny*n.ny;
         //if(nx+ny<0.01){nx=0.5;ny=0.5;}
         px+=p.x*nx;
         sx+=nx;
         py+=p.y*ny;
         sy+=ny;
      }
      px/=sx;
      py/=sy;
      //px = clamp(px,0,1);//it doesn't seem to be needed?
      //py = clamp(py,0,1);
      positions[x][y] = {x:px,y:py};
      renderer_updatePosition(x,y,px,py);
      //console.log(x,y,px,py,sx,sy);
   }else{
      positions[x][y].x=0.5;//reset
      positions[x][y].y=0.5;//reset
   }
   //rerender edges
   rerenderEdge(x,y,true);
   rerenderEdge(x,y,false);
   rerenderEdge(x+1,y,true);
   rerenderEdge(x,y+1,false);
}

function updateEdge(x,y,isVertical){
   //update neighbouring grids<commit_msg>Refactored the intersection algorithm for greater readability and easier changing.<commit_after>var gridWidth = 22;
var gridHeight = 12;

var values=[], positions=[];//arrays for values and edges
var verticalEdges=[], horizontalEdges=[];
//values are currently 0 for empty and 1 for material
//TODO add more materials

function initializeArrays(){
   var i,j;
   for(i=0;i<gridWidth;i++){
      values[i] = [];
      positions[i] = [];
      for(j=0;j<gridHeight;j++){
         values[i][j]=0;//init to air
         positions[i][j]={x:0.5,y:0.5};
      }
   }
   for(i=0;i<gridWidth;i++){
      verticalEdges[i] = [];
      horizontalEdges[i] = [];
      for(j=0;j<gridHeight;j++){
         verticalEdges[i][j]={p:0,nx:1,ny:1};//no intersection
         horizontalEdges[i][j]={p:0,nx:1,ny:1};//no intersection
      }
   }
}
function getValue(x,y){
   if(x<0||x>=gridWidth||y<0||y>=gridHeight) return 0;//default to air
   return values[x][y];
}

function gridHasSignChange(x,y){
   var sgn = getValue(x,y);
   for(var i=0;i<2;i++){
      for(var j=0;j<2;j++){
         if(sgn!=getValue(x+i,y+j)) return true;//TODO more materials
      }
   }
   return false;
}
function edgeHasSignChange(x,y,isVertical){
   if(isVertical){
      return (getValue(x,y)!=getValue(x,y+1));
   }else{
      return (getValue(x,y)!=getValue(x+1,y));
   }
}

function getIntersectionPoint(x,y,isVertical,dx,dy){
   if(x<0||x>=gridWidth||y<0||y>=gridHeight) return {x:dx+0.5,y:dy+0.5};
   if(isVertical){
      return {x:dx, y:dy+verticalEdges[x][y].p};
   }else{
      return {x:dx+horizontalEdges[x][y].p, y:dy};
   }
}
///QEF

function mul(v,x){
   return {x:v.x*x,y:v.y*x};
}

function dot(a,b){
   return a.x*b.x+a.y*b.y;
}

function normalToRightVector(normal){
   return {x:-normal.y,y:normal.x};
}

function projection(planeNormal, point){
   var plane = normalToRightVector(planeNormal);
   return mul(plane, dot(point,plane));
}

function updateSquare(x,y){
   if(x<0||y<0) return;
   //recompute current gridposition
   var px=0.0,py=0.0;
   if(gridHasSignChange(x,y)){
      var intersections = [];
      if(edgeHasSignChange(x,y,true)){
         intersections.push({p:getIntersectionPoint(x,y,true,0,0),n:verticalEdges[x][y]});
      }
      if(edgeHasSignChange(x,y,false)){
         intersections.push({p:getIntersectionPoint(x,y,false,0,0),n:horizontalEdges[x][y]});
      }
      if(edgeHasSignChange(x+1,y,true)){
         intersections.push({p:getIntersectionPoint(x+1,y,true,1,0),n:verticalEdges[x+1][y]});
      }
      if(edgeHasSignChange(x,y+1,false)){
         intersections.push({p:getIntersectionPoint(x,y+1,false,0,1),n:horizontalEdges[x][y+1]});
      }

      var sx=0.0,sy=0.0;
      for(var i=0;i<intersections.length;i++){
         var p = intersections[i].p;
         var n = intersections[i].n;
         px+=p.x*n.nx*n.nx;
         sx+=n.nx*n.nx;
         py+=p.y*n.ny*n.ny;
         sy+=n.ny*n.ny;
      }
      px/=sx;
      py/=sy;

      //px = clamp(px,0,1);//it doesn't seem to be needed?
      //py = clamp(py,0,1);
      positions[x][y] = {x:px,y:py};
      renderer_updatePosition(x,y,px,py);
      //console.log(x,y,px,py,sx,sy);
   }else{
      positions[x][y].x=0.5;//reset
      positions[x][y].y=0.5;//reset
   }
   //rerender edges
   rerenderEdge(x,y,true);
   rerenderEdge(x,y,false);
   rerenderEdge(x+1,y,true);
   rerenderEdge(x,y+1,false);
}

function updateEdge(x,y,isVertical){
   //update neighbouring grids
{'fraction_non_alphanumeric': 0.20582637973942322, 'fraction_numerical': 0.024740155174937783, 'mean_word_length': 2.988324576765908, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1815746', 'n_tokens_mistral': 2873, 'n_tokens_neox': 2819, 'n_words': 370}