tags:

views:

278

answers:

2

I have a grid with 100 columns and 100 rows. I want to draw a line between the centre of one of those grid squares and another, say 45,25 to 75,38.

I am happy with being able to draw a line, but how can i find the x and y pos of the centre of the two grid squares? Or is there a better approach i have missed

A: 

The coordinates of the center of the line is (center_x, center_y) where:

center_x = x1 + (x2 - x1/2)
center_y = y1 + (y2 - y1/2)

when x2 > x1 and y2 > y1.

So for your example:

center_x = 45 + ((75 - 45) / 2)
center_y = 25 + ((38 - 25) / 2)

HTH.

0xfe
I think you've defined how to determine the centre of the LINE, but the question is finding the centre of the grid squares.
Bernhard Hofmann
A: 

Your presentation should work with the grid you define. It seems the issue is that the representation of the thing you are trying to join is not centred on the grid location you specified. Your line should be drawn from 45,25 to 75,38. If that appears to not draw from the centres, then the things drawn at 45,25 and 75,38 are not drawn on the centre of the grid location.

If your display is grid based, then finding the centre of the screen grid from your theoretical 45,25 location would be something along these lines:

screenXcentre = ((xPos-1)*CellWidth)-(CellWidth/2);
screenYcentre = ((yPos-1)*CellHeight)-(CellHeight/2);
Bernhard Hofmann