xxxxxxxxxx
111
let points =[];
function setup() {
createCanvas(700, 400);
for(let i = 0; i < 3; i++){
points.push(createVector(random(0,width), random(0,height)));
}
// # GRID SETTINGS
let maxCellSize = 10;
let subdivX = ceil(width / maxCellSize) - 1; // number of x subdivisions
let subdivY = ceil(height / maxCellSize) - 1; // number of y subdivisions
let sX = subdivX + 1; // number of columns
let sY = subdivY + 1; // number of rows
// # CREATE GRID
let flatgrid = [];
// # INSERT POINTS IN GRID
for(let i = 0 ; i < points.length ; i++){
// check in which x axis
let x = ceil(map(points[i].x, 0 , width, 0, sX));
// check in which y axis
let y = ceil(map(points[i].y, 0 , height, 0, sY));
// calculate cell id
let id = ( (y - 1) * sX + x ) - 1;
// insert into flat grid
flatgrid.push(id, points[i].x, points[i].y, 0);
}
// # DRAW POINTS
for(let i = 0 ; i < points.length ; i++){
circle(points[i].x, points[i].y, 10);
}
// # define circle
stroke(0,255,0);
noFill();
let cx = width/2 + 30;
let cy = height/2 + 30;
let radius = 150;
circle(cx, cy, radius * 2);
// # print grid array
//print(flatgrid);
// # DRAW GRID
let RectWidth = width / sX;
let RectHeight = height / sY;
let RectXFit = floor( ( radius * 2 ) / RectWidth ) + 2; // cb de rectangle entier, + 2 pour les bords
let RectYFit = floor( ( radius * 2 ) / RectHeight ) + 2;
let xRange = ( (RectXFit % 2 == 0) ? RectXFit + 1 : RectXFit); // convert to odd number
let yRange = ( (RectXFit % 2 == 0) ? RectYFit + 1 : RectYFit); // convert to odd number
intersection = getRects(cx, cy, sX, sY, xRange, yRange) // get intersecting rects
console.log(intersection);
for(let i = 0 ; i < sX * sY ; i++){
let RectX = ( i % sX ) * RectWidth;
let RectY = floor( i / sX ) * RectHeight;
if(intersection.includes(i)){
stroke(0,255,0);
}
else{
stroke(0);
}
rect(RectX, RectY, RectWidth, RectHeight);
}
}
function getRects(cx, cy, sX, sY, xRange, yRange){
// calculate the id of the rectangle that contains the circle
let x = ceil(map(cx, 0 , width, 0, sX));
// check in which y axis
let y = ceil(map(cy, 0 , height, 0, sY));
// calculate cell id
let id = ( (y - 1) * sX + x ) - 1;
// calculate all other
let results = [];
//console.log(id);
//console.log(ceil(- xRange/2));
//console.log(floor(xRange/2));
for(let i = ceil(- xRange / 2) ; i < ceil( xRange / 2 ) ; i++){
for(let j = ceil(- yRange / 2) ; j < ceil( yRange / 2 ) ; j++){
let intersection = id + i + j * sX;
if(intersection > 0 && intersection < sX * sY){
results.push(intersection);
}
}
}
console.log(xRange* yRange)
return results;
}