Skip to content

Instantly share code, notes, and snippets.

@lacan
Last active October 30, 2023 12:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lacan/4f83637732a83a9e7568f90a6caba90e to your computer and use it in GitHub Desktop.
Save lacan/4f83637732a83a9e7568f90a6caba90e to your computer and use it in GitHub Desktop.
[Relative Neighborhood Graph Algorithm] Simple RNG graph thanks to ChatGPT #groovy #chatgpt #fiji
// DISCLAIMER: The algorithm was taken from a ChatGPT prompt "What is the Groovy implementation of the 2D Relative Neighborhood Graph Algorithm"
// It is merey an alpha version of the code and has not been thorougly tested at all, nor has the code been optimized.
// The original prompt has been slightly modifided to make better use of ImageJ methods to display the graph
// Create a list of points for testing
def rand = new Random()
// Or to make it reproducible
//def rand = new Random( 1000 )
def points = (1..100).collect{ new Point2D.Double( rand.nextInt( 200 ) + 21, rand.nextInt( 200 ) + 21 ) }
// Compute the RNG
def rngEdges = getRNG( points )
// Make image to display the results
def imp = IJ.createImage("RNG", "8-bit black", 256, 256, 1);
// Print the edges of the Relative Neighborhood Graph
def ov = new Overlay()
// Make ROIs for each edge and add them to the overlay
rngEdges.each { edge ->
//Make the point
def p1 = new PointRoi( edge[0].x, edge[0].y )
def p2 = new PointRoi( edge[1].x, edge[1].y )
def link = new Line( edge[0].x, edge[0].y, edge[1].x, edge[1].y )
ov.add( p1 )
ov.add( p2 )
ov.add( link )
// Make the line
println("Edge: (${edge[0].x}, ${edge[0].y}) - (${edge[1].x}, ${edge[1].y})")
}
imp.setOverlay( ov )
imp.show()
// Actual method to get the RNG
def getRNG( def points) {
// Create the Relative Neighborhood Graph
def rngEdges = []
for (int i = 0; i < points.size(); i++) {
for (int j = i + 1; j < points.size(); j++) {
def p = points[i]
def q = points[j]
if (!hasRNGNeighbor(p, q, points)) {
rngEdges << [p, q]
}
}
}
return rngEdges
}
// Function to determine if there's a point z that satisfies RNG condition
def hasRNGNeighbor(Point2D.Double p, Point2D.Double q, List<Point2D.Double> points) {
for (def z : points) {
if (z != p && z != q) {
def dpz = p.distance(z)
def dqz = q.distance(z)
if (dpz < p.distance(q) && dqz < p.distance(q)) {
return true
}
}
}
return false
}
// Required Imports
import ij.IJ
import java.awt.geom.Point2D
import ij.gui.*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment