HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Implementing Mitchell's best candidate algorithm

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
candidatealgorithmmitchellimplementingbest

Problem

I have written an implementation of Mitchell's best candidate algorithm. Mitchell’s best-candidate algorithm generates a new random sample by creating k candidate samples and picking the best of k. Here the “best” sample is defined as the sample that is farthest away from previous samples. The algorithm approximates Poisson-disc sampling, producing a much more natural appearance (better blue noise spectral characteristics) than uniform random sampling.

Here is my implementation of it.

First I have a class that I used to generate random values or pick - up values randomly from an array

public  class Randomizer {

    private static Random randomHelper = new Random();

    public static Random getHelper() {
        return randomHelper;
    }

    private Randomizer() {

    }

    public static Object oneOf(Object...objects) {

        if (null == objects) {          
            return null;            
        } else {
            return (objects[randomHelper.nextInt(objects.length)]);
        }

    }
}


Second I have a PaintPanel that I use to draw a bunch of points.

class PaintPanel extends JPanel {   
/**
 * 
 */
    private static final long serialVersionUID = 5614021935627523089L;
    private List pointsToDraw;

    public PaintPanel(List pointsToDraw) {
        this.pointsToDraw = pointsToDraw;
        this.setBackground(Color.BLACK);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (null== pointsToDraw) {
            return;
        }

        for (Point p: pointsToDraw) {

            g.setColor(Color.WHITE);
            g.drawArc(
                    (int)p.getX(), 
                    (int)p.getY(), 
                    5, 5, 0,(int) (2*Math.PI * 180));   
        }

    }
}


Third, I have a class that implements the algorithms, computes all the points and send them to the panel to be drawn.

```
public class MitchellBestCandidateIV extends JFrame {

/**
*
*/

Solution

Bug

I only scanned your code briefly, but it looks to me like this code that is in your main loop:

currentPoint = getRandomPoint();
        mitchellPoints.add(currentPoint);
        currentPointIndex++;


should be outside the loop. Otherwise you are adding one completely random point along with one Mitchell point on every iteration. I think that code was only meant to generate the first point.
Unnecessary Hashing

One other thing I noticed is that you used a HashMap to store your minimal distances. You could instead just make an array of doubles of the same length as your array of points. It would be faster because it would eliminate the need for hashing and comparing of keys (all your keys are unique).

Code Snippets

currentPoint = getRandomPoint();
        mitchellPoints.add(currentPoint);
        currentPointIndex++;

Context

StackExchange Code Review Q#87843, answer score: 2

Revisions (0)

No revisions yet.