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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:06:39+00:00 2026-05-26T11:06:39+00:00

I have a rectangular map, stored as multidimensional array (ie $map[row][col] ) and I

  • 0

I have a rectangular map, stored as multidimensional array (ie $map[row][col]) and I have to track down which squares are seen by a player, placed anywhere on this map.

Player visibility is circular with unknown radius (but given at run-time) and I only need integer solutions.

I know that circumference formula is

x^2 + y^2 <= r^2

but how can I store everything?
I need these values since then I can “reveal” map squares.


The best would be a multidimesional array (ie __$sol[x][y]__).
This is a piece of code that I’m using. It’s not corrected since it assumes that vision is a square and not a circle.

Calculating the square

$this->vision_offsets_2 = array();
//visibility given as r^2
$mx = (int)(sqrt($this->viewradius2));

$mxArr = range($mx * -1, $mx + 1);
foreach ($mxArr as $d_row)
{
    foreach ($mxArr as $d_col)
    {
         $this->vision_offsets_2[] = array($d_row, $d_col);
    }
}

This is how I apply that

    foreach($player as $bot)
    {
        foreach($visibility as $offset)
        {
            $vision_row = $offset[0] + $bot[0];
            $vision_col = $offset[1] + $bot[1];

            if(isset($map[$vision_row][$vision_col]))
            {
                if( $map[$vision_row][$vision_col] == UNSEEN) {
                    $map[$vision_row][$vision_col] =  LAND; }
            }
        }
    }

Here you can find the bot view: as you can see is a non perfect circle.
How can I track it? By the way, in this example radius^2 is 55, the orange circle is the player, brown squares are visible ones.

bot view

  • 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-26T11:06:39+00:00Added an answer on May 26, 2026 at 11:06 am

    Structure

    You’re already referencing terrain in a grid. Store terrain objects in those grid values. Apply attributes to those objects. Check with something like

    $map[$x][$y]->isVisible($player);
    

    You’ll need some methods in there for setting vision and tests for checking the user that is passed against a list of users who can see it. While you’re at it, setup other related methods in those objects (I see references to land… isLand() and isWater() perhaps?).

    You can even have vision cascade within objects such that you only need to move the position of a user and the object takes care of triggering off all the code to set nearby plots of land to visible.

    Math

    We are given circumference.

    double diameter = circumference / 3.14159
    double radius = diameter / 2 //Normally done in one step / variable
    

    Now we must know the distance between two points to compare it. Let’s use map[4][7] and map[3][9].

    int x0 = 4;
    int y0 = 7;
    int x1 = 3;
    int y1 = 9;
    
    double distance = Math.sqrt(
            Math.pow(x0 - x1, 2) + 
            Math.pow(y0 - y1, 2)
            );
    
    System.out.println(distance); //2.23606797749979
    

    Test distance > radius.

    Testing each square

    • Put the above in a method: visibleFrom(Square target)
      • radius should be a globally accessible static variable when comparing.
      • Your Square object should be able to hand over its coordinates.
        • target.getX()
        • target.getY()
    • Some optimizations can be had
      • Only checking things for circular distance when they’re in the square.
      • Not checking anything for circular distance when purely along the x or y axis.
      • Figuring out the largest square that fits inside the circle and not checking boxes in that range for circular distance.
    • Remember that premature optimization and over optimization are pitfalls.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a 2-dimensional jagged array (though it's always rectangular), which I initialize using
Greetings. I have an 2-D array of size [N][N] which will represent a rectangular
I have a page which has a rectangular area with text and icons in
Many applications have dialogs which have totally custom-looking dialogs, big fat tool-bars and non-rectangular
I have a set of numbers which I need to put on a rectangular
I have N squares. I have a Rectangular box. I want all the squares
I have a 2D array. Lets suppose that it has some connected region which
I have N rectangular items with an aspect ratio Aitem (X:Y). I have a
I have a grid of rectangular cells covering a plane sitting at some distance
I have an n-by-m rectangular matrix (n != m). What's the best way to

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.