Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6860847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:27:36+00:00 2026-05-27T02:27:36+00:00

I have a grid of images, like a 2D tile set. This method is

  • 0

I have a grid of images, like a 2D tile set. This method is supposed to give me the image from the tile set when I just give the the tile index.

Can anyone see where my math is screwed up? Currently this function just gives me n and n.(random decimal) for the x and y.

- (CGImageRef)tileFromTileset:(int)gid {
    CGImageRef tile;
    CGPoint point;
    CGFloat fGid = (CGFloat)gid;
    point.x = fmodf(fGid, tileSource.size.width);
    point.y = (fGid - (fmod(fGid, tileSource.size.width)) / (tileSource.size.width) + 1);
    NSLog(@"%@", NSStringFromCGPoint(point));
    tile = CGImageCreateWithImageInRect([tileSource CGImage], CGRectMake(point.x, point.y, kTileWidth, kTileHeight));
    return tile;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T02:27:37+00:00Added an answer on May 27, 2026 at 2:27 am

    I think you have a simple error of units. At the moment, you’re dividing a number representing an index by a number representing a coordinate; the unit that you end up with is therefore “index per coordinate”.* What you want, though, is just coordinate units.

    The assignment to point.x should therefore be:

    point.x = fmodf(fGid * kTileWidth, tileSource.size.width);
    

    That is, the tile index (units: “index”), multiplied by the width of one tile (units: “coordinates per index”), gives you its location if this were a single row of tiles (units: “coordinates”). Taking the remainder after dividing by the actual width of a row of tiles gives you the correct x coordinate.

    For point.y, you need the quotient of the same calculation:

    point.y = trunc(fGid * kTileWidth / tileSouce.size.width) * kTileHeight;
    

    The index, multiplied by the width of a tile, divided by the width of a row, tells you how many rows are completed at that index; multiply by the height of a tile to get the coordinate.

    This might be even clearer with one more constant, kNumTilesInRow:

    point.x = (gid % kNumTilesInRow) * kTileWidth;
    point.y = (gid / kNumTilesInRow) * kTileHeight;
    

    *You keep getting n back because, for abs(n) < abs(m), n % m always equals n.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have something like this in my xaml: <Grid> <Image Name=image Source=../../Images/DefaultImage.png HorizontalAlignment=Stretch VerticalAlignment=Stretch></Image>
I have a gridview set up with a list of images like this: public
This is my quandary. Let me just set the scene: I have a grid
I have grid like this. How to change index for each element, i.e. to
I have a grid laid out like so; +---------+---------+ | Image | Details |
I have grid of images, when the mouse is over any given image a
I have a grid of UIImageViews. The images names come from a sqlite3 database.
I have grid something like this: Ext.define('Exp.view.dashboard.Tv', { extend: 'Ext.grid.Panel', initComponent: function() { this.columns
I have a grid of images, all set to have a max-width of 200px,
I have grid of thumbnail images on a form, and I would like for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.