2D Dot Accuracy

I have a list of points in 2 dimensional.

For example:

x = c (4,3,3,5,6,6,4)

and

y = c (5,3,1,0,1,3,5)

Plot these 2D points

enter image description here

I would like to draw a wrapper around this point, for example:

enter image description here

Note that the perpendicular distance between the border (wrapper) and the nearest point is 2 units.

Please note that : I have many pointsets like the above pointset. I would like to do the same for all sets.

I want to have this bounding polygon. Can anyone suggest me how to do this.

Any ideas are greatly appreciated, Janak.

+3


source to share


2 answers


Using Java this becomes very easy. The program demonstrates the result by building it. The contour can also be obtained by repetition area.getPathIterator(at)

, which will return all points one by one.

import java.awt.*;
import java.awt.geom.*;
import java.util.*;

public class PointSet {
public static final int W = 2;
Area area = new Area();

public void add( double x, double y ){
    area.add( new Area( new Rectangle2D.Double( x-W, y-W,2*W, 2*W ) ) );
}
public void plot(){
    Board board = new Board();
    board.go( area );
}
public static void main( String[] args ){
    PointSet ps = new PointSet();
    ps.add( 4, 5);
    ps.add( 3, 3);
    ps.add( 3, 1);
    ps.add( 5, 0);
    ps.add( 6, 1);
    ps.add( 6, 3);
    ps.plot();
}
}

      



and

import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import java.util.*;
public class Board extends JPanel {
Area area;
void go( Area area ) {
    this.area = area;
    JFrame frame = new JFrame("Circle Test");
    frame.getContentPane().add(this);
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    repaint();
    frame.setPreferredSize(new Dimension(800,800));
    frame.pack();
    frame.setVisible(true);
}

public void paintComponent(Graphics g) {
  AffineTransform at = new AffineTransform();
  at.translate( 100, 100 );
  at.scale( 50, 50 );
  PathIterator pit = area.getPathIterator( at );
  Path2D path = new Path2D.Double();
  path.append( pit, true );
  Graphics2D g2d = (Graphics2D)g;
  g2d.draw( path );
}
}

      

+1


source


You can use this simple algorithm for this.
First we need the center of your coordinates (red dot) .
This can be done by adding all of your x values ​​and dividing the result by their number, same with the y-values.

The next step is to compute a rectange that wraps the current coordinate and center point . (Don't forget to add your 2 units offset)

First wrapping rectangle

We will do this for all coordinates

enter image description here

We could already stop at this. Just render all those rectangles and then your coordinates on top of the image, but let's improve that a little more.
We don't really need all of these rectangles, we want a polygon to wrap around these points. This polygon is defined by the intersection of our slabs and their edges (blue dots).

enter image description here

Note that we only need the edges and intersections that are farthest from our center.
Now we can connect these blue points by connecting points that share one common coordinate and are "neighbors".



enter image description here


Update:

import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;

public class PolyWrapper {

    public static void main(String[] args){

        //your example coords:
        int[] x_coords = {4,3,3,5,6,6,4};
        int[] y_coords = {5,3,1,0,1,3,5};       

        //make sure the coordinates have the same length, else they won't match
        if(x_coords.length != y_coords.length){
            System.err.println("Bad parameters given. X and Y don't match!");
            System.exit(1);
        }

        //this will hold our points:
        ArrayList<Point2D> points = new ArrayList<>();
        for(int i = 0; i < x_coords.length; i++){
            Point2D p = new Point2D.Double(x_coords[i], y_coords[i]);
            points.add(p);
        }
        //lets get the center of all those points:
        final Point2D center = get_center(points);
        ArrayList<Rectangle2D> rectangles = new ArrayList<>();

        //now lets create those wrapping rectangles:
        for(Point2D p : points){
            Rectangle2D r = new Rectangle2D.Double();
            r.setFrameFromDiagonal(center, p);
            rectangles.add(r);
        }

        //now show the wrapping rectangles:
        for(Rectangle2D r : rectangles){
            System.out.println(r.toString());
        }


    }

    //this method returns the center of a list of points
    public static Point2D get_center(ArrayList<Point2D> points){
        double x = 0,y =0;
        for(Point2D p : points){
            x += p.getX();
            y += p.getY();
        }
        x = x / points.size();
        y = y / points.size();
        Point2D c = new Point2D.Double();
        c.setLocation(x, y);
        return c;
    }
}

      

So here's some sample code. I haven't taken the time to finish it yet, but since your question is really interesting, I'll keep working on it.
So far this code calculates the center point and creates rectangles around the center and the given coordinates.

This exit provides the top-left corner of each rectangle, its width and height.
Output example:

java.awt.geom.Rectangle2D$Double[x=4.0,y=2.5714285714285716,w=0.4285714285714288,h=2.4285714285714284]
java.awt.geom.Rectangle2D$Double[x=3.0,y=2.5714285714285716,w=1.4285714285714288,h=0.4285714285714284]
java.awt.geom.Rectangle2D$Double[x=3.0,y=1.0,w=1.4285714285714288,h=1.5714285714285716]
java.awt.geom.Rectangle2D$Double[x=4.428571428571429,y=0.0,w=0.5714285714285712,h=2.5714285714285716]
java.awt.geom.Rectangle2D$Double[x=4.428571428571429,y=1.0,w=1.5714285714285712,h=1.5714285714285716]
java.awt.geom.Rectangle2D$Double[x=4.428571428571429,y=2.5714285714285716,w=1.5714285714285712,h=0.4285714285714284]
java.awt.geom.Rectangle2D$Double[x=4.0,y=2.5714285714285716,w=0.4285714285714288,h=2.4285714285714284]

      


Ps: I tried to improve the algorithm from now on but ran into a problem that seems to be difficult to solve. Maybe I'll start a new question about this issue.
(This is a painting with blue dots. When you have all the points from the rectangles and their intersections, it is difficult to determine which of the resulting points is really needed for our polygon). I think I am close to a solution, so stay tuned for me.

+3


source







All Articles