i dont know if anyone uses processing here, but im writing some code for simplifying point clouds

i have written code that makes a random point cloud, and the second part of the code finds the points that are close to each other in a certain tolerance, and deletes them, leaving one point in the center of the deleted group. i cant seem to get the second part of the code to work.

first part of code :

void setup() {

size (400, 400);

background(255);

int xnum = 1000;

int ynum = 1000;

int[] xarray = new int[xnum];

int[] yarray = new int[ynum];

// create random numbers

for (int i = 0; i < xnum; i++) {

for (int j = 0; j < xnum; j++) {

xarray[i] = int(random(0, 400));

yarray[j] = int(random(0, 400));

}

}

//create points

for (int i = 0; i < xnum; i++) {

for (int j = 0; j < xnum; j++) {

point(xarray[i], yarray[i]);

}

}

}

Converge(ArrayList<PVector> pts, double tol)

{

PVector[] deltaPos = new PVector[pts.length];

boolean check = false;

tol = tol * tol;

for (int i = 0; i < pts.length; i++)

{

deltaPos[i] = PVector.sub(LocalCentroid(pts[i], pts, tol), pts[i]);

if (deltaPos[i].x + deltaPos[i].y > ModelAbsoluteTolerance) // ModelAbsoluteTolerance: to be defined!

{

check = true;

}

}

for (int i = 0; i < pts.length; i++)

{

pts[i].add(deltaPos[i]);

}

return check;

}

LocalCentroid(PVector p, ArrayList<PVector> pts, double tolSqr)

{

PVector cntr = new PVector(0, 0);

int count = 0;

for (int i = 0; i < pts.length; i++)

{

PVector testVec = PVector.sub(pts[i], p);

double lsqr = testVec.x * testVec.x + testVec.y * testVec.y;

if (lsqr < tolSqr)

{

cntr.add(pts[i]);

count += 1;

}

}

return PVector.div(cntr, count);

}