0

I need a function that returns an array of points, it has to maximize the number of points in a rectangle. The function has to favour 100mm spacing (in any direction) but it can deviate by x (integer) to create points that would almost fit. We also want to minimize deviation.

There are shapes in the given area that points cannot fall within. I have a working function boolean isCollide(Point point).

The is actually a CAD problem, and the logic will be applied to Catia v5. I am hoping for sudo code. The rectangle size is constant and is dimensioned with PLATEXMAX and PLATEYMAX.

My solution has to be exhaustive, all my attempts have only given heuristic solutions. Algorithm efficiency is not an issue. Anyone faced a similar issue, or have any suggestions?

I have created an illustration to show a visual representation of my problem. http://tinypic.com/r/vebtj5/8

EDIT:

Point[] tempArray = new Point[]

for i=0 to PLATEXMX{

 for j=0 to PLATEYMAX{

     Point test = new Point(i,j)
     Boolean found = false

     foreach tpoint in tempArray{
     if !found{
       for n=0 to deviation{
         if tpoint.distanceTo(test) < (100 + n){
               if !(isCollide(test){
                   tempArray.add(test)
                   break
                } else{
                     //check points offset from this point
                     //check every offset point 360deg around test
                     //increase checking radius to max deviation

              }
           }
         }
   }

1 Answers1

0

If there are only a few points, it may be feasible to 'try every solution'. So if you have 8 points, try all 2^8 permutations of the points being included/not included, checking whether that set of points would fit in a given rectangle. This would be easiest to implement: use a for loop with some integer i from 0 .. 2^n, and use bit masking to include/not include some element in the test.

As a slightly less brute-force approach, try backtracking. Essentially, you try all permutations of points but stop as soon as a solution is invalid (i.e. if the set of points doesn't fit into a given rectangle. Wiki offers an implementation of this in pseudocode.

Luke
  • 1,724
  • 1
  • 12
  • 17