codious
codious

Reputation: 3511

assign cell to coordinates in a 3 by 3 grid

I am reading some coordinates from a file and trying to assign them cell values (0-2) by diving them into a 3x3 grid. For some values the code is assigning negative cell values like for x=268 or x=269. Where am I going wrong?

I have file output.txt as follows with :

76 62
77 62
78 62
79 62
81 62
83 62
86 62
etc

The code to assign cells:

int x_points[99];
int y_points[99];
int i=0;
int x,y;
int max_x, max_y;
int min_x, min_y;

while(out.good())
{
    out>>x;
    out>>y;

        if(i==0)
{
  max_x=x;
  min_x=x;
  min_y=y;
  max_y=y;
 }
 else if (x>max_x)
 {
    max_x=x;
 }
 else
 if(x < min_x)
 {
   min_x=x;
 }
else if (y>max_y)
 {
    max_y=y;
 }
 else
 if(y < min_y)
 {
   min_y=y;
 }
x_points[i]=x;
y_points[i]=y;
i++;
}


for(i=0; i<99; i++)
cout<<x_points[i]<<","<<y_points[i]<<"\n";

int cells_x[99];
int cells_y[99];

float x_width;
float y_width;
int divide = 3;

//To find out the cells. Divide it by 20

x_width=(max_x-min_x)/divide;
y_width=(max_y-min_y)/divide;

cout<<"\nx_width:"<<x_width;
cout<<"y_width:"<<y_width<<"\n";

int x1;
int y1;
for(i=0; i<99; i++)
{
x1=x_points[i]-min_x;
y1=y_points[i]-min_y;

    for(int j=0; j<divide; j++)
    {
        if(j*x_width<=x1 && x1<((j+1)*x_width))
            cells_x[i]=j;

        if(j*y_width<=y1 && y1<((j+1)*y_width))
            cells_y[i]=j;
    }
    cout<<cells_x[i]<<" "<<i<<" "<<cells_y[i]<<"\n";

}

Upvotes: 0

Views: 313

Answers (1)

Kevin Coffey
Kevin Coffey

Reputation: 386

The first problem I see is where you try to find the maximum and minimum values of x and y:

if(i==0)
{
    max_x=x;
    min_x=x;
    min_y=y;
    max_y=y;
}
else if (x>max_x)
{
    max_x=x;
}
else if(x < min_x)
{
    min_x=x;
}
else if (y>max_y) /* <-- This should not have an else! */
{
    max_y=y;
}
else if(y < min_y)
{
    min_y=y;
}

As written, it prevents the analysis of y values when an x is a new max or min. This will cause issues with the rest of your program, since max_y and min_y don't actually contain the max and min.

Next up is calculating the x_width and y_width:

x_width=(max_x-min_x)/divide; // should be x_width=(max_x-min_x + 1)/divide;
y_width=(max_y-min_y)/divide; //           y_width=(max_y-min_y + 1)/divide;

If max_x = 8, and min_x = 0, your code would result in x_width = 8/3, or about 2.6666666666666666666666666666667. But 0-8 can be split up into 3 groups of 3. Adding 1 corrects this by calculating the number of points, not the length of the line.

It also prevents you from having a width of 0, which would happen if max_x == min_x. A width of 0 causes trouble in the next section:

for(int j=0; j<divide; j++)
{
    /* an x_width of 0 will cause this to never evaluate to true */
    /* x1 cannot ever be >= to 0 AND < 0 */
    if(j*x_width<=x1 && x1<((j+1)*x_width))
        cells_x[i]=j;

    if(j*y_width<=y1 && y1<((j+1)*y_width))
        cells_y[i]=j;
}

if x_width is zero, then you will never assign a value to cells_x[i], meaning it will still be set to its initialization value, which could be a negative number.

Upvotes: 1

Related Questions