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 6757387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:38:43+00:00 2026-05-26T13:38:43+00:00

I’m currently working on a game whereby I will have a player traversing a

  • 0

I’m currently working on a game whereby I will have a player traversing a 2D matrix. I want to be able to modify the environment around the player based on his location, as part of a rendering process.

Basically, the player will be at point x,y and will have 2 points around him at all times. The player might move x,y points in any direction and I need to know what the old no longer necessary pointers are, and also the new points the player is close to.

I’ve drawn up a quick diagram of what I mean:

Diagram of Matrix's

I need a list of the old points (in red), and the new points (in green) that I can iterate over to perform actions on.

I’ll be writing this method in C++, so I’m really looking for the sudo-logic steps required to make this happen. I’m about 50% way through my own way of doing it, but I believe it to be wholly inefficient and I also believe that is a simple mathematical way to do this.

  • 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-26T13:38:44+00:00Added an answer on May 26, 2026 at 1:38 pm

    Probably the most efficient way would be computing a rectangle-rectangle subtraction that may seem difficult and with a lot of cases, but indeed it’s not that hard:

    struct Rect{
        int x0, y0, x1, y1;
        Rect(int x0, int y0, int x1, int y1)
            : x0(x0), y0(y0), x1(x1), y1(y1)
        {}
    };
    
    std::vector<Rect> subtract(const Rect& a, const Rect& b) {
        std::vector<Rect> result;
        if (a.y1 <= b.y0 || a.y0 >= b.y1 || a.x1 <= b.x0 || a.x0 >= b.x1) {
            // Trivial case: rectangles are not overlapping
            result.push_back(a);
        } else {
            int ystart = a.y0, yend = a.y1;
            if (ystart < b.y0) { // Something visible above
                result.push_back(Rect(a.x0, ystart, a.x1, b.y0));
                ystart = b.y0;
            }
            if (yend > b.y1) { // Something visible below
                result.push_back(Rect(a.x0, b.y1, a.x1, yend));
                yend = b.y1;
            }
            if (a.x0 < b.x0) { // Something visible on the left
                result.push_back(Rect(a.x0, ystart, b.x0, yend));
            }
            if (a.x1 > b.x1) { // Something visible on the right
                result.push_back(Rect(b.x1, ystart, a.x1, yend));
            }
        }
        return result;
    }
    

    The above function given two rectangles A and B returns a vector of rectangles with the result of A-B. This vector may be empty (B covers A) or may have from one to four rectangles (four is when B is stricly contained in A, thus the result will be a rectangle with a rectangular hole in it).

    Using this function you can easily compute new-old and old-new areas.

    Note that the coordinate schema used in the above code assumes the point-base coordinate system (not a pixel-based coordinate system):

    enter image description here

    In the above picture note that horizontal X coordinates of rectangles go from 0 to W (not W-1) and vertical Y coordinates go from 0 to H (and not to H-1).

    Pixels are just rectangles of area 1 with coordinates (x, y)-(x+1, y+1); the center of this pixel is (x+0.5, y+0.5). A rectangle with x0==x1 or y0==y1 is empty.

    Note also that the code assumes (and returns) non-empty oriented rectangles, i.e. x0<x1 && y0<y1.

    This approach of separating the concept of pixel coordinate from the concept of point coordinate simplifies a lot of the pixel math: for example rectangle area is width*height and not (width-1)*(height-1).

    A little program to test with your input case is the following

    void print_result(const char *name,
                      const std::vector<Rect>& rects)
    {
        printf("Result '%s' (%i rects):\n", name, int(rects.size()));
        for (int i=0,n=rects.size(); i<n; i++)
        {
            printf("  %i) (%i, %i) - (%i, %i)\n",
                   i+1,
                   rects[i].x0, rects[i].y0,
                   rects[i].x1, rects[i].y1);
        }
    }
    
    int main()
    {
        Rect A(1, 1, 6, 6);
        Rect B(3, 2, 8, 7);
        print_result("A-B", subtract(A, B));
        print_result("B-A", subtract(B, A));
        return 0;
    }
    

    and the output of this program is

    Result 'A-B' (2 rects):
      1) (1, 1) - (6, 2)
      2) (1, 2) - (3, 6)
    Result 'B-A' (2 rects):
      1) (3, 6) - (8, 7)
      2) (6, 2) - (8, 6)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I have a French site that I want to parse, but am running into
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.