Sandro,

Here's a little demo app for your code.  It displays a random convex
polygon and the associated MER.  Click on the display panel to
generate new polygons.

Michael


package jtsdemos.minimumenclosure;

import com.vividsolutions.jts.algorithm.ConvexHull;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.Polygon;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;


/**
 * A simple app to demonstrate Sandro Zacchino's minimum enclosing
 * rectangle (MER) code.  It generates a random convex polygon, calculates the
 * MER, and displays both on screen.  Mouse click on the display panel to
 * generate new polygons.
 *
 * @author Michael Bedward
 */
public class Demo {

    private static final double CLOUD_MIN = 150;
    private static final double CLOUD_W = 200;
    private static final int IMGW = (int)(CLOUD_W + 2 * CLOUD_MIN);

    private Polygon hullPoly;
    private Polygon merPoly;

    public static void main(String[] args) {
        (new Demo()).doDemo();
    }

    private void doDemo() {
        generatePolys();

        final JPanel panel = new JPanel() {

            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);

                BufferedImage bufImg = new BufferedImage(IMGW, IMGW,
BufferedImage.TYPE_INT_ARGB);
                Graphics2D gr = (Graphics2D) bufImg.getGraphics();

                gr.setColor(Color.BLUE);

                Coordinate[] hullCoords = hullPoly.getCoordinates();
                Coordinate c0 = hullCoords[0];
                for (int i = 1; i < hullCoords.length; i++) {
                    gr.fillOval((int)c0.x-4, (int)c0.y-4, 9, 9);
                    Coordinate c1 = hullCoords[i];
                    gr.drawLine((int)c0.x, (int)c0.y, (int)c1.x, (int)c1.y);
                    c0 = c1;
                }

                gr.setColor(Color.MAGENTA);
                Coordinate[] merCoords = merPoly.getCoordinates();
                c0 = merCoords[0];
                for (int i = 1; i < merCoords.length; i++) {
                    Coordinate c1 = merCoords[i];
                    gr.drawLine((int)c0.x, (int)c0.y, (int)c1.x, (int)c1.y);
                    c0 = c1;
                }

                ((Graphics2D)g).drawImage(bufImg, null, 0, 0);
            }
        };

        panel.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                generatePolys();
                panel.repaint();
            }
        });

        panel.setToolTipText("click for new polygon");

        final JFrame frame = new JFrame("MER demo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(panel);
        frame.setSize(IMGW, IMGW);

        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                frame.setVisible(true);
            }
        });
    }

    private void generatePolys() {
        hullPoly = randomConvexPoly();
        merPoly = MinimumEnclosure.computeMER(hullPoly);
    }

    private Polygon randomConvexPoly() {
        Random rand = new Random();
        final Coordinate[] cloud = new Coordinate[10];

        for (int i = 0; i < cloud.length; i++) {
            cloud[i] = new Coordinate(
                    CLOUD_MIN + CLOUD_W * rand.nextDouble(),
                    CLOUD_MIN + CLOUD_W * rand.nextDouble());
        }

        ConvexHull hull = new ConvexHull(cloud, new GeometryFactory());
        return (Polygon) hull.getConvexHull();
    }

}
_______________________________________________
jts-devel mailing list
jts-devel@lists.jump-project.org
http://lists.refractions.net/mailman/listinfo/jts-devel

Reply via email to