[ 
https://issues.apache.org/jira/browse/GEOMETRY-146?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17514757#comment-17514757
 ] 

Matt Juntunen commented on GEOMETRY-146:
----------------------------------------

How about this?

{code:java}
// interface allowing easy conversion to streams in situations where
// the number of elements is not known ahead of time
interface StreamingIterable<T> extends Iterable<T> {
        default Stream<T> stream() {
                return StreamSupport.stream(spliterator(), false);
        }
}

// represents a view of a collection ordered by distance
// relative to a reference point 
interface DistanceOrdering<T> {
        T nearest();
        T farthest();
        StreamingIterable<T> nearToFar();
        StreamingIterable<T> farToNear();
}

interface PointSet<P> {
        DistanceOrdering<P> from(P pt);
        DistanceOrdering<P> withinRadius(P pt, double radius);
}

interface PointMap<P, V> {
        DistanceOrdering<Entry<P, V>> entriesFrom(P pt);
        DistanceOrdering<Entry<P, V>> entriesWithinRadius(P pt, double radius);
}
{code}

Usage would then look like this:
{code:java}
// get the entry in the map farthest from the origin
PointMap<Vector3D, Integer> map = ...
Entry<Vector3D, Integer> result = map.from(Vector3D.ZERO).farthest();

// find the element nearest to refPt in the set that has a positive x coordinate
// and is not farther away than 3
PointSet<Vector3D> set = ...
Vector3D refPt = Vector3D.of(1, 2, 3);
Vector3D result = null;
for (Vector3D pt : set.withinRadius(refPt, 3).nearToFar()) {
    if (pt.getX() > 0) {
            result = pt;
            break;
    }
}

// find the k nearest neighbors of pt
PointSet<Vector3D> set = ...
Vector3D pt = Vector3D.of(1, 2, 3);
List<Vector3D> neighbors = set.from(pt).nearToFar().stream()
        .limit(k)
        .collect(Collectors.toList());
{code}

> PointSet/Map closest points
> ---------------------------
>
>                 Key: GEOMETRY-146
>                 URL: https://issues.apache.org/jira/browse/GEOMETRY-146
>             Project: Commons Geometry
>          Issue Type: New Feature
>            Reporter: Matt Juntunen
>            Priority: Major
>             Fix For: 1.1
>
>
> Add methods to the new {{PointSet}} and {{PointMap}} interfaces to allow 
> querying of points in order of distance from a query point.
> {code:java}
> PointSet<P> {
>     // find the closest point to pt or null if empty 
>     P closest(P pt);
>     // iterate through points in order, with points closest to pt coming first
>     Iterable<P> closestFirst(P pt);
>     // find the farthest point from pt or null if emtpy
>     P farthest(P pt);
>     // iterate through point in order, with points farthest from pt coming 
> first
>     Iterable<P> farthestFirst(P pt);
> }
> {code}
> {{PointMap}} should have similar methods providing access to the map keys and 
> entries.



--
This message was sent by Atlassian Jira
(v8.20.1#820001)

Reply via email to