Skip to content

Commit 2f93344

Browse files
committed
Since random is no longer guaranteed to be actually "random", catch infinite loops for built-in point generation
1 parent acd538b commit 2f93344

6 files changed

Lines changed: 17 additions & 10 deletions

File tree

SharpVoronoiLib/GlobalUsings.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Global using directives
22

33
global using System.Runtime.CompilerServices;
4-
global using JetBrains.Annotations;
4+
global using JetBrains.Annotations;
5+
global using SharpVoronoiLib.Exceptions;

SharpVoronoiLib/Point Generation/RandomGaussianPointGeneration.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ protected override double GetNextRandomValue(IRandomNumberGenerator random, doub
1616

1717
double mid = (max + min) / 2;
1818

19+
int safetyCounter = 0;
20+
1921
do
2022
{
2123
double u1 = 1.0 - random.NextDouble(); //uniform(0,1] random doubles
@@ -31,6 +33,10 @@ protected override double GetNextRandomValue(IRandomNumberGenerator random, doub
3133
if (coord > min && coord < max)
3234
return coord;
3335

36+
safetyCounter++;
37+
38+
if (safetyCounter > 100) throw new VoronoiRandomPointGenerationEncounteredTooManyInvalidSites();
39+
3440
} while (true);
3541
}
3642

SharpVoronoiLib/Point Generation/RandomPointGeneration.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using SharpVoronoiLib.Exceptions;
2-
3-
namespace SharpVoronoiLib;
1+
namespace SharpVoronoiLib;
42

53
internal abstract class RandomPointGeneration : IPointGenerationAlgorithm
64
{

SharpVoronoiLib/Point Generation/RandomUniformPointGeneration.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,18 @@ protected override void Prepare(IRandomNumberGenerator random, double minX, doub
99

1010
protected override double GetNextRandomValue(IRandomNumberGenerator random, double min, double max, int index, ValuePurpose valuePurpose)
1111
{
12+
int safetyCounter = 0;
13+
1214
do
1315
{
1416
double value = min + random.NextDouble() * (max - min);
1517

1618
if (value > min && value < max)
1719
return value;
20+
21+
safetyCounter++;
22+
23+
if (safetyCounter > 100) throw new VoronoiRandomPointGenerationEncounteredTooManyInvalidSites();
1824

1925
} while (true);
2026
}

SharpVoronoiLib/Voronoi/VoronoiSite.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using SharpVoronoiLib.Exceptions;
2-
3-
namespace SharpVoronoiLib;
1+
namespace SharpVoronoiLib;
42

53
/// <summary>
64
/// The point/site/seed on the Voronoi plane.

SharpVoronoiLib/VoronoiPlane.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using SharpVoronoiLib.Exceptions;
2-
3-
namespace SharpVoronoiLib;
1+
namespace SharpVoronoiLib;
42

53
/// <summary>
64
/// An Euclidean plane where a Voronoi diagram can be constructed from <see cref="VoronoiSite"/>s

0 commit comments

Comments
 (0)