A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: [RESOLVED] finding xy from the given number of a tile?

  1. #1
    Member
    Join Date
    Jun 2009
    Posts
    35

    resolved [RESOLVED] finding xy from the given number of a tile?

    I've set up a simple grid of lets say of 4 x 4, and number each tile accordingly:

    0 1 2 3
    0 '00''01''02''03'
    1 '04''05''06''07'
    2 '08''09''10''11'
    3 '12''13''14''15'

    I need to then find the x and the y values based on the tile number? So say I'm given tile 11, I need two equations, one to find its x value(3), and another the y value(2). I'm sure this must be pretty simple stuff, but I'm completely stuck at the moment! thanks.

  2. #2
    Member
    Join Date
    Jun 2009
    Posts
    35
    I worked out a solution that seems to work:

    x = value - ( RoundDown(value/sale) *scale)

    y = RoundDown(value/scale)

    where scale in this example is 4. Is there a better formula I could use?

  3. #3
    Senior Member joshstrike's Avatar
    Join Date
    Jan 2001
    Location
    Alhama de Granada, España
    Posts
    1,136
    Your way of finding y is the only way. As for x, I think you're looking for the modulo % operator.

    Code:
    scale = 4
    x = value%scale;
    y = Math.Floor(value/scale);

  4. #4
    Member
    Join Date
    Jun 2009
    Posts
    35
    nice one, I didn't know about modulo % operator - sounds pretty useful

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center