In this example I don’t draw points, only convex hull. The thing is that it is not convex as you see.
I wander if it is bug in algorithm. Maybe the reason is that some points are so close that this happens, which I doubt. Probably, the real reason is wrong algorithm
I tried with different values ranging from 10_000 to 100_000 and it seems to appear randomly. Maybe it’s a rounding error caused by the proximity of the points as you said so when you compare the angles, it breaks…
Like @josephh I tried your code and as you increase the number of points the more frequent the artifacts. I even tried with 100,000 points and although the artifact appeared more often it didn’t happen all the time.
This tells me that the problem almost certainly not the algorithm, it is something to do with the data set.
I suspect that some of the intermediate calculations are producing NaN(s) I had this problem using floats and had to uses doubles instead.
In your method newConvex you have this conditional statement
You should test 2 floating point number for equality (==) or inequality (!=) because it can lead to logical errors. What would you expect from this sketch?
float a, b, c;
a = 3.1415927;
b = 3.1415928;
c = 3.1415926;
println(a == b);
println(a == c);
You get
true
false
which is obviously wrong.
You can improve the first part of the newConvex method to avoid this issue and also improve efficiency by avoiding reading from the list like this -
void newConvex(){
init = new ArrayList<PVector>();
// strokeWeight(2);
int index = -1;
float maxY = -1;
for (int i = 0; i < NUM; i++) {
PVector p = new PVector(border+random(width-2*border),border+random(height-2*border));
init.add(p);
if (p.y > maxY) {
index = i;
maxY = p.y;
}
}
BTW this does not get rid of the visual artifact unfortunately