Complete.Org: Mailing Lists: Archives: freeciv-dev: March 2003:
[Freeciv-Dev] (PR#2412) (PR#3800) canvas_to_map_pos() off by one
Home

[Freeciv-Dev] (PR#2412) (PR#3800) canvas_to_map_pos() off by one

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: a-l@xxxxxxx
Subject: [Freeciv-Dev] (PR#2412) (PR#3800) canvas_to_map_pos() off by one
From: "Jason Short" <jdorje@xxxxxxxxxxxxxxxxxxxxx>
Date: Wed, 26 Mar 2003 10:38:43 -0800
Reply-to: rt@xxxxxxxxxxxxxx

On Wed, 2003-03-26 at 09:48, a-l@xxxxxxx wrote:
> 
> 1 year+ old issue.
> 
> In isometric mode, this operation still decreases tile_x by 1:
> 
> {
>   map_to_canvas_pos(&canvas_x, &canvas_y, tile_x, tile_y);
>   canvas_to_map_pos(&tile_x, &tile_y, canvas_x, canvas_y);
> }
> 
> I work around the problem like this:
> 
> {
>   map_to_canvas_pos(&canvas_x, &canvas_y, tile_x, tile_y);
>   canvas_x += NORMAL_TILE_WIDTH / 2;
>   canvas_to_map_pos(&tile_x, &tile_y, canvas_x, canvas_y);
> }

This is a problem.

It is clear that map_to_canvas_pos and canvas_to_map_pos do work
properly.  When you click on the mapview, Freeciv correctly knows which
tile you have clicked on.  Since the drawing code uses map_to_canvas_pos
and the mouse-clicking code uses canvas_to_map_pos, these two are
definitely working properly, within their definitions.

The problem is that when you call map_to_canvas_pos on (map_x, map_y),
you are given the canvas position of the top-left corner of the bounding
box of the tile at (map_x, map_y).  This canvas position is not itself
in the (map_x, map_y) tile; it's actually in the (map_x-1, map_y) tile. 
If you're drawing something, that's what you want - but if you're doing
a more explicit canvas coordinate calculation you're probably more
interested in the *center* of the tile rather than the top-left.

One example of this *type* of calculation is in center_tile_mapcanvas. 
Because this function only deals in map coordinates, it can suffer from
off-by-one errors.  But it's not clear (to me) how this should be done
differently.

jason




[Prev in Thread] Current Thread [Next in Thread]