Skip to content

Commit

Permalink
assorted
Browse files Browse the repository at this point in the history
  • Loading branch information
micycle1 committed Feb 14, 2025
1 parent 9d1338a commit 667b466
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* `maximumInscribedTriangle()` to `PGS_Optimisation`. Finds an approximate largest area triangle (of arbitrary orientation) contained within a polygon.
* `closestPoint()` to `PGS_Optimisation`. Finds the closest point in a collection of points to a specified point.
* `distanceTree()` to `PGS_Contour`. Generates a tree structure representing the shortest paths from a start point to all other vertices in a mesh.
* `vertexCount()` to `PGS_ShapePredicates`. Returns the total number of vertices that make up a shape.

### Changes
* Optimised `PGS_CirclePacking.tangencyPack()`. It's now around 1.5-2x faster and has higher precision.
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/micycle/pgs/PGS_Conversion.java
Original file line number Diff line number Diff line change
Expand Up @@ -1504,7 +1504,7 @@ public static PShape fromArray(double[][] shape, boolean close) {
/**
* Flattens a collection of PShapes into a single GROUP PShape which has the
* input shapes as its children. If the collection contains only one shape, it
* is directly returned.
* is returned directly as a non-GROUP shape.
*
* @since 1.2.0
* @see #flatten(PShape...)
Expand Down
20 changes: 19 additions & 1 deletion src/main/java/micycle/pgs/PGS_PointSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,27 @@
* Generation of random sets of 2D points having a variety of different
* distributions and constraints (and associated functions).
*
* <p>
* <strong>Note on Floating-Point Values and Collisions:</strong>
* </p>
* <p>
* When generating many random points, collisions in coordinate values are
* expected due to the limited precision of floating-point numbers. For example:
* <ul>
* <li>A {@code float} has ~24 bits of precision (23 explicit mantissa bits + 1
* implicit leading bit), which allows for ~16.8 million unique values in the
* range [0, 1).</li>
* <li>When generating a large number of points (e.g., 100,000), the probability
* of collisions follows the birthday paradox formula: <code>n² / (2m)</code>,
* where <code>n</code> is the number of samples and <code>m</code> is the
* number of possible unique values.</li>
* <li>For 100,000 points, this results in ~300 expected collisions in the
* x-coordinate, even when using a high-quality random number generator.</li>
* </ul>
* </p>
*
* @author Michael Carleton
* @since 1.2.0
*
*/
public final class PGS_PointSet {

Expand Down
14 changes: 13 additions & 1 deletion src/main/java/micycle/pgs/PGS_ShapePredicates.java
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,18 @@ public static double convexity(PShape shape) {
return g.getArea() / g.convexHull().getArea();
}

/**
* Returns the total number of vertices that make up a shape.
* <p>
* Unlike <code>PShape.getVertexCount()</code> this method properly returns the
* number of vertices in GROUP and primitive shapes.
*
* @since 2.1
*/
public static int vertexCount(PShape shape) {
return fromPShape(shape).getNumPoints();
}

/**
* Counts the number of holes in a shape.
* <p>
Expand Down Expand Up @@ -514,7 +526,7 @@ public static double maximumInteriorAngle(PShape shape) {
}

/**
* Calculates the interior angles of a polygon represented by a {@link PShape}.
* Calculates all interior angles of a polygon represented by a {@link PShape}.
* The method calculates the interior angle at each vertex.
* <p>
* The vertices of the input {@code shape} are assumed to represent a simple
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/micycle/pgs/commons/ChaikinCut.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,14 @@ private static ArrayList<PVector> chaikinCut(PVector a, PVector b, float ratio)

/**
* Determines whether to cut an edge. Returns false for edges with a euclidean
* distance less than 1.0.
* distance less than 0.5.
*/
private static boolean cut(PVector a, PVector b) {
// TODO expand to exclude almost coincident edge pairs
final float dx = b.x - a.x;
final float dy = b.y - a.y;
final float d2 = dx * dx + dy * dy;
return d2 > 1;
return d2 > 0.5;
}

}

0 comments on commit 667b466

Please sign in to comment.