-1

I have a lot of unsorted 2D points which represent the positions of randomized picked pixels of an image.

In the next step I try sort/rasterize them in a 2D array, by point with smallest x, y value at

array[0][0] 

and Point with highest x,y value at

array[n][k]

with condition 1:

all the other 2D should be between this boundaries and almost sorted.

condition 2:

all rows of the array should be filled nearly with same number of values, same for the columns.

Any ideas how to solve this problem?

I computed delaunay-triangulation and thought about a voronoi diagram for going step by step threw each cell, but I don´t know if I am on the right path.

My random positions are created in that way:

std::vector<Point_d> sample_rand_points(){
  std::cout<<"sampling random points\n";
  std::vector<Point_d> output_pattern;

  //PREPARE:
  std::vector<std::pair<int, int> > not_sampled_yet;
  for(int x=0; x<_X; x++)
  {
    for(int y=0; y<_Y; y++)
    {
      not_sampled_yet.push_back(std::pair<int,int>(x,y));
    }
  }
  //SAMPLING
  Point_d pix;
  for (int i=0; i<_Amount; i++)
  {
    //std::cout<<i<<"\n";
    int n= rand()% not_sampled_yet.size();
    pix.x= (double)not_sampled_yet[n].first;
    pix.y= (double)not_sampled_yet[n].second;
    not_sampled_yet[n]=not_sampled_yet.back();
    not_sampled_yet.pop_back();
    output_pattern.push_back(pix);
  }
  return output_pattern;

}

Output is one vector with Points {{x1,y1},{x2,y2},......}

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
cosmonaut
  • 414
  • 1
  • 3
  • 14
  • How is the input data (2D points) generated? – BlueTune Mar 15 '20 at 16:02
  • I inserted some code of my randomfunction – cosmonaut Mar 15 '20 at 16:10
  • 1
    At the first glance it makes sense to store the x and y coordinates in two separate `std::vector`. This makes it easier to find the minimum and the maximum. – BlueTune Mar 15 '20 at 16:21
  • I chose this data format as an input for a library for delaunay. At first I had written the method i the way I inserted now. – cosmonaut Mar 15 '20 at 16:27
  • The ammount of randomized chosen pixels is arround 60-70% of the original Pixelammount. minimum is always Points with x= 0 or y= 0 und max is with and heigth. – cosmonaut Mar 15 '20 at 16:32
  • IMHO "Delaunay triangulation" is not what you want. Why don't you start with a `std::vector>`. Then you implement two nested for-loops and generate the "2d-array" right from the start. Your function `sample_rand_points` seems to be an unnecessary detour. – BlueTune Mar 15 '20 at 16:41
  • It's impossible to both sort by `x` *and* `y` simultaneously. Points in two dimensions need to projecting on a line if you are going to sort. Easy ones are `x` and `y` because you just ignore the other coördinate, but one always loses some information on projection. – Neil Mar 16 '20 at 18:14

1 Answers1

-1

With the following code you can generate an "2d-array" with random pixel. In my opinion there is no need to use a generator function like sample_rand_points.

#include <iostream>
#include <vector>
#include <cstdlib>

int main()
{
    int NumX =10, NumY =20;
    std::vector<std::vector<bool>> data(NumX, std::vector<bool>(NumY));

    for(int i=0; i<NumX ; ++i)
    {
        for(int m=0; m<NumY ; ++m)
        {
            //Generation
            bool val = false;
            if(rand() % 2 == 0)
                val = true;

            //The Data
            data[i][m] = val;

            //Output
            if(val)
                std::cout << "*";
            else
                std::cout << "_";

        }
        std::cout << std::endl;
    }
}

You can run the above code online to see the following output:

_*____**__*_*__*****
_*__***____***___*_*
____*_**_*_*_**_***_
__*_*_*___*_*_*_**_*
_*****__*_****_****_
_***___*_***_**___*_
*__________*__*_*__*
*_*___***_*_*_*_*___
_***_*_*****_*_____*
*__*__*_*_____**__*_

Note that the line if(rand() % 2 == 0) controls the density of the "selected" pixel.

BlueTune
  • 1,023
  • 6
  • 18
  • I don´t understand what that has to do with my question. I have Datatype Pixel, with position and color information and want to sort them in a 2D array – cosmonaut Mar 15 '20 at 17:05
  • Ok, and I don't understand why you implemented the function `sample_rand_points`. – BlueTune Mar 15 '20 at 17:09
  • your function choses randomly true or false for each pixel. I have discrete ammount of random Pixels, I want to bring back to rasterformat. – cosmonaut Mar 15 '20 at 17:10
  • Yes, your methods are different. But the final result is the same. I would like to understand why do you need to generate the points {{x0,y0},{x1,y1},......}? – BlueTune Mar 15 '20 at 17:22