Re: Creating a shape from a set of points
In article
<bf4643d7-36a9-4d1c-bd2d-11de84344805@25g2000hsx.googlegroups.com>,
Todd <todd.heidenthal@lmco.com> wrote:
[...]
Imagine a bubble-letter C or U, like the kind middle-school girls
draw. Now, imagine just taking a pen and filling the bubble-letter
with dots. Remove the bubble-letter border, leaving just the dots,
and you have the data set that I am starting with.
Visually, you can see the "edge" defined by where the points are
and where the points are not. This interface between have and
have not is what I am calling the boundary.
I am trying to create an algorithm that will locate those points
that are at the interface between have and have not. And then
use those points to create an approximation of the shape (imagine
a non-smooth bubble-letter). This closed polygon can then be
used to define those points which are inside and those points
which are not.
[...]
Ah, you need to find the points defining the outline of a raster. The
outline is simple and closed but not necessarily convex. You might look
at the wand tool in ImageJ <http://rsb.info.nih.gov/ij/>.
Once you get the hull, contains() works correctly even for a non-convex
Shape. In the example below, outline is the implicitly closed, simple,
non-convex polygonal boundary of a "U".
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import javax.swing.JFrame;
/**
* Test Shape.contains
* @author John B. Matthews
*/
public class Contains extends JFrame {
private static Shape outline = initPoly();
private static AffineTransform at = new AffineTransform();
public static void main(String args[]) {
JFrame frame = new Contains();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.setVisible(true);
}
public void paint(Graphics g) {
Graphics2D g2D = (Graphics2D) g;
g2D.setBackground(Color.WHITE);
g2D.clearRect(0, 0, 400, 400);
g2D.setPaint(Color.BLACK);
g2D.drawLine(0, 200, 400, 200);
g2D.drawLine(200, 0, 200, 400);
g2D.setPaint(Color.BLUE);
drawShape(g2D, 200, 200);
}
/** Draw a scaled and translated outline. */
private void drawShape(Graphics2D g2D, int x, int y) {
at.setToIdentity();
at.translate(x, y);
at.scale(30, 50);
Shape shape = at.createTransformedShape(outline);
g2D.fill(shape);
System.out.println("Inside(" + (x + 1) + ", " + (y + 1) + ") "
+ shape.contains(x + 1, y + 1));
System.out.println("Inside(" + (x - 1) + ", " + (y - 1) + ") "
+ shape.contains(x - 1, y - 1));
}
/** Create a U shaped outline. */
private static Polygon initPoly()
{
Polygon poly = new Polygon();
poly.addPoint( 1, 0);
poly.addPoint( 1, -2);
poly.addPoint( 2, -2);
poly.addPoint( 2, 1);
poly.addPoint(-2, 1);
poly.addPoint(-2, -2);
poly.addPoint(-1, -2);
poly.addPoint(-1, 0);
return poly;
}
}
John
--
John B. Matthews
trashgod at gmail dot com
home dot woh dot rr dot com slash jbmatthews