开发者

how can i draw a line between two points on a grid in WPF

开发者 https://www.devze.com 2022-12-19 21:48 出处:网络
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 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 bet开发者_JS百科ter approach i have missed


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);


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号