aerod
aerod

Reputation: 61

How to visualize an algorithm without changing the code of that algorithm?

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

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...

Upvotes: 6

Views: 284

Answers (3)

Stuart Golodetz
Stuart Golodetz

Reputation: 20616

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.

Upvotes: 1

j13r
j13r

Reputation: 2671

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.

Upvotes: 1

Krizz
Krizz

Reputation: 11552

  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.

Upvotes: 0

Related Questions