6

I want to visualize two different algorithms that decide if there's overlap in a collection of circles in a plane in Java:

  • an O(n²) algorithm that checks every combination of circles
  • an O(nlogn) algorithm using a sweep line

Is there a way to let an object of a vizualization class 'listen' to an object of the algorithm class in a way that it can for example see when the algorithm is performing an overlap check between a pair of circles and knows when to update the visualization?

other example: I can keep the list of active circles(those which intersect the sweep line) as a variable of the sweep line algorithm and let another class(visualization class) get that variable. But how will that class know when the list is updated and it has to update the visualization?

That's just the strategy I was thinking of. Maybe there are better ways...

3
  • I don't know if you have considered it, but I just have one word in mind: Multithreading. One algorithm-thread and one visualization-thread? Commented Apr 5, 2012 at 22:20
  • I will definitely need multiple threads. The main problem is: how wil the visualization thread know it needs to refresh? Commented Apr 5, 2012 at 22:32
  • Draw a picture. (Does visualize it. Does not modify the code.) Commented Apr 5, 2012 at 23:16

3 Answers 3

1

Perhaps reading up on the Observer pattern can help you: https://en.wikipedia.org/wiki/Observer_pattern

You can either implement java.util.Observer or give the algorithm a callback function / object.

You can hand the observer arbitrary data, enough to allow it do decide when the algorithm is performing an overlap check.

Sign up to request clarification or add additional context in comments.

Comments

1

I'm not sure whether or not this will help you or not, but if you can't change the code of the algorithm to support observers then one (interesting) option would be to look into aspect-oriented programming.

For example, in AspectJ (e.g. see http://en.wikipedia.org/wiki/AspectJ) you can specify (using things called 'pointcuts') places (called 'join points') where bits of extra code (called 'advice') should be run. You could use this to detect overlap checks being performed by the algorithm and respond to them as you see fit.

Of course, doing things this way would involve using AspectJ, so it wouldn't be possible with just normal Java - but it's something interesting you might want to look into.

Comments

0
  1. Have class(es) which represent circle (and any other object present in this problem / algorithm) and contain the methods for each operation.
  2. Implement algorithm as the operation on the objects from (1) - as calls to the methods.
  3. Create a visualization class which examines the objects from 1 and visualizes their state on each - say - Update() method.
  4. Create subclasses of all classes in (1) which, apart from their original behavior, which call Visualization.Update() on each operation.

Build "your world" out of (4) classes instead of (1) to have a visualization.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.