newbie question on conversion between world to screen coords

Hi all

 i know this is very simple to implement, but the converter i wrote doesnt work for all cases. How to have a conversion from world to screen and screen to world coordinates in 2D. Screen coordinates can have 'Y' at the top left or at bottom left.

Any help is appreciated

Thanks
schiz

xStartOfTileArea (pixels) = the leftmost pixel where tiles can be drawn on the screen
xTile (tile) = the x-coordinate of the tile in the map
xLeftmostTileOnScreen (tile) = the x-coordinate of the leftmost tile currently being displayed on the screen
widthOfTileInPixels (pixels) = self-explanatory

int xDrawPosition = xStartOfTileArea + (xTile - xLeftmostTileOnScreen) * widthOfTileInPixels;

Similarly for y-coordinates.

It may not work for you if you’re coordinate system isn’t quite normal. Someone might be able to help you if you post the relevant parts of your source code so that they know what’s going on.

Hi
Thanks for the reply . the following worked for me.

         double pixelsX = ( umax - umin ) / ( xmax - xmin );
    double pixelsY = ( vmax - vmin ) / ( ymax - ymin );
    
    trans = new AffineTransform(pixelsX , 0, 0, pixelsY, -xmin * pixelsX + umin, -ymin * pixelsY + vmin);

where screen bounds are vmin,umin, vmax , umax and world bounds are xmin,xmacx,ymin,ymax . I used the affine transform to do conversions.

Regards
Schiz

It looks fine to me, but you might want to change the variable names to make them more descriptive. “umax” could mean anything. “screenWidth” and “panelWidth” are both fairly self-explanatory except that you have to be aware that they represent width in pixels as opposed to tiles or something.

Having readable code makes it alot easier to modify later. It’s much more important for large multi-coder projects than small single-coder demos though.