How to pick a line closest to the center on a grid?

I am drawing a simple grid, and I want the line closest to the center of the screen to be highlighted a different color.

What is the formula to determine what line was drawn that closely resembles the center of the screen?

It doesn't have to be the exact center, just one that appears to be in the middle of the screen. But it must be a line that was drawn. The user can change the size of the grid at anytime, so this line must move with it.

I am drawing a new line on the screen using a different stroke color, but I can't determine which line to overlap. I can get close but I am always off by a few pixels.

Take a look at this picture in Photoshop. The green line represents the true center of the image. While the pink line is the desired result (center line) because the grid isn't even to the screen size (look at the last grid on the right) The grid is 34x34 and the screen size is 320 x 480.

How to draw the grid:

int xStart = 0, yStart = 0;
int gsX = 19; //Distance between lines
int gsY = 25;

        // draw vertical lines
        for(int xId=0; xId<=(screenWidth/gsX); xId++) {
            int x = xStart + xId * gsX;
            [gPath moveToPoint:CGPointMake(x, yStart)];
            [gPath addLineToPoint:CGPointMake(x, yStart+screenHeight)];

        }
        // draw horizontal lines
        for(int yId=0; yId<=(screenHeight/gsY); yId++) {
            int y = yStart + yId * gsY;
            [gPath moveToPoint:CGPointMake(xStart, y)];
            [gPath addLineToPoint:CGPointMake(xStart+screenWidth, y)];

        }

My centerline code: This moves the line based upon the grid spacing value, but it isn't drawn over one of the lines near the center.

int x = (screenWidth/gsX) /2;
NSLog(@"New X: %i gsX: %i",x, gsX);

//Veritical
[centerLines moveToPoint:CGPointMake(x, 0)];
[centerLines addLineToPoint:CGPointMake(x, screenHeight)];

Answers


Actually every one is right. I ran into something similar not to long ago actually. I couldn't explain it but it felt like the order of operations wasn't being followed correctly. So I broke your equation down so that you can follow the order of the operations. Any way your solution is as followed.

    int centerX = (screenWidth/gsX);
    int tempA = ( centerX / 2 );
    int tempB = tempA * gsX;
    NSLog(@"screenwidth / gsX = %i", centerX);
    NSLog(@"Temp A: %i ", tempA);
    NSLog(@"Temp B: %i ", tempB);


    //Veritical
    [centerLines moveToPoint:CGPointMake(tempB, 0)];
    [centerLines addLineToPoint:CGPointMake(tempB, screenHeight)];

Here's whats happening. You're already drawing this line at one point in your grid code. You just have to figure out which one it is.You know that screenWidth/gsX is the last "line that will be drawn. So that number divided by 2 is the middle line. It's already a factor of the screenSize. Then just multiply that number by how big your grid is. Since it is the 'middle line' closest to the center (screenWidth/gsX) your line should show up on top of the grid

That should always draw a middle line. I don't see any code where you are changing the color. So you will have to take it on blind faith that it is being drawn. If you can change your line color you should be able to see it.

I'll leave it to you to figure out horizontal. (hint: it deals with y value ;-) )

I hope this helps!

Have fun and good luck Mr. Bourne!


Center is

int centerX = ((screenWidth/2) / gsX )* gsX;
int centerY = ((screenHeight/2) / gsY ) * gsY;

Just make sure you are doing integer math above! no floats. It should work out fine.


int x = xStart + (screenWidth/gsX)/2 * gsX;

Need Your Help

python 3.2 pickle.load results in EOFError randomly

python python-3.x pickle eoferror

I encounter a very strange error. I have a cronjob that run daily: it opens a pickled file, load that file and then process the data. At the end of the script, it will save the data to that file fo...

Reset action bar after using SearchView

android android-actionbar searchview

I'm using SearchView widget to enable searching in my app. After the initial click on the search icon, the SearchView widget expands into the search field and the "back" arrow is shown next to the

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.