C# Getting Nearby Points on a Grid within radius









up vote
0
down vote

favorite












I have a grid that has X and Y coordinates. On this grid I have a starting point and I want to get all nearby cells within a certain radius.



I have made made the following function that goes +1 one in each direction and returns the spots.



 public List<Point> GetLocationsStraightLine(Point fromLocation, int Distance)

var nearbySpots = new List<Point>();
int StartDistance = 1;

while (StartDistance < Distance)

nearbySpots.Add(new Point(fromLocation.X, (short)(fromLocation.Y - StartDistance)));
nearbySpots.Add(new Point((short)(fromLocation.X + StartDistance), fromLocation.Y));
nearbySpots.Add(new Point(fromLocation.X, (short)(fromLocation.Y + StartDistance)));
nearbySpots.Add(new Point((short)(fromLocation.X - StartDistance), fromLocation.Y));

StartDistance++;


return nearbySpots;



This returns all points in a straight line from my starting point. However I want to to also grab the inbetween spots.



This is what I currently get (Sorry for the bad image)



image of points i return



However I want to be able to enter a distance of 2 and get the full square around the start location.



what i want full square with diagnonal



So I am asking is there any easy way I can get the diagonals and not just a straight line from my starting point?










share|improve this question

























    up vote
    0
    down vote

    favorite












    I have a grid that has X and Y coordinates. On this grid I have a starting point and I want to get all nearby cells within a certain radius.



    I have made made the following function that goes +1 one in each direction and returns the spots.



     public List<Point> GetLocationsStraightLine(Point fromLocation, int Distance)

    var nearbySpots = new List<Point>();
    int StartDistance = 1;

    while (StartDistance < Distance)

    nearbySpots.Add(new Point(fromLocation.X, (short)(fromLocation.Y - StartDistance)));
    nearbySpots.Add(new Point((short)(fromLocation.X + StartDistance), fromLocation.Y));
    nearbySpots.Add(new Point(fromLocation.X, (short)(fromLocation.Y + StartDistance)));
    nearbySpots.Add(new Point((short)(fromLocation.X - StartDistance), fromLocation.Y));

    StartDistance++;


    return nearbySpots;



    This returns all points in a straight line from my starting point. However I want to to also grab the inbetween spots.



    This is what I currently get (Sorry for the bad image)



    image of points i return



    However I want to be able to enter a distance of 2 and get the full square around the start location.



    what i want full square with diagnonal



    So I am asking is there any easy way I can get the diagonals and not just a straight line from my starting point?










    share|improve this question























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have a grid that has X and Y coordinates. On this grid I have a starting point and I want to get all nearby cells within a certain radius.



      I have made made the following function that goes +1 one in each direction and returns the spots.



       public List<Point> GetLocationsStraightLine(Point fromLocation, int Distance)

      var nearbySpots = new List<Point>();
      int StartDistance = 1;

      while (StartDistance < Distance)

      nearbySpots.Add(new Point(fromLocation.X, (short)(fromLocation.Y - StartDistance)));
      nearbySpots.Add(new Point((short)(fromLocation.X + StartDistance), fromLocation.Y));
      nearbySpots.Add(new Point(fromLocation.X, (short)(fromLocation.Y + StartDistance)));
      nearbySpots.Add(new Point((short)(fromLocation.X - StartDistance), fromLocation.Y));

      StartDistance++;


      return nearbySpots;



      This returns all points in a straight line from my starting point. However I want to to also grab the inbetween spots.



      This is what I currently get (Sorry for the bad image)



      image of points i return



      However I want to be able to enter a distance of 2 and get the full square around the start location.



      what i want full square with diagnonal



      So I am asking is there any easy way I can get the diagonals and not just a straight line from my starting point?










      share|improve this question













      I have a grid that has X and Y coordinates. On this grid I have a starting point and I want to get all nearby cells within a certain radius.



      I have made made the following function that goes +1 one in each direction and returns the spots.



       public List<Point> GetLocationsStraightLine(Point fromLocation, int Distance)

      var nearbySpots = new List<Point>();
      int StartDistance = 1;

      while (StartDistance < Distance)

      nearbySpots.Add(new Point(fromLocation.X, (short)(fromLocation.Y - StartDistance)));
      nearbySpots.Add(new Point((short)(fromLocation.X + StartDistance), fromLocation.Y));
      nearbySpots.Add(new Point(fromLocation.X, (short)(fromLocation.Y + StartDistance)));
      nearbySpots.Add(new Point((short)(fromLocation.X - StartDistance), fromLocation.Y));

      StartDistance++;


      return nearbySpots;



      This returns all points in a straight line from my starting point. However I want to to also grab the inbetween spots.



      This is what I currently get (Sorry for the bad image)



      image of points i return



      However I want to be able to enter a distance of 2 and get the full square around the start location.



      what i want full square with diagnonal



      So I am asking is there any easy way I can get the diagonals and not just a straight line from my starting point?







      c# grid points






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 10 at 3:53









      Incognito

      32




      32






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          Since it's a rectangle, you can use a very simple approach. Assuming that Distance is always positive:



          public List<Point> GetLocationsStraightLine(Point fromLocation, int Distance) 

          var nearbySpots = new List<Point>();

          for (int i = -Distance; i <= Distance; ++i)
          for (int j = -Distance; j <= Distance; ++j)
          nearbySpots.Add(new Point(fromLocation.X + j, fromLocation.Y + i));

          return nearbySpots;




          The idea is to start at the upper left point, and add each point in the rectangle row by row, ending at the bottom right point.






          share|improve this answer




















            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53235863%2fc-sharp-getting-nearby-points-on-a-grid-within-radius%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote



            accepted










            Since it's a rectangle, you can use a very simple approach. Assuming that Distance is always positive:



            public List<Point> GetLocationsStraightLine(Point fromLocation, int Distance) 

            var nearbySpots = new List<Point>();

            for (int i = -Distance; i <= Distance; ++i)
            for (int j = -Distance; j <= Distance; ++j)
            nearbySpots.Add(new Point(fromLocation.X + j, fromLocation.Y + i));

            return nearbySpots;




            The idea is to start at the upper left point, and add each point in the rectangle row by row, ending at the bottom right point.






            share|improve this answer
























              up vote
              0
              down vote



              accepted










              Since it's a rectangle, you can use a very simple approach. Assuming that Distance is always positive:



              public List<Point> GetLocationsStraightLine(Point fromLocation, int Distance) 

              var nearbySpots = new List<Point>();

              for (int i = -Distance; i <= Distance; ++i)
              for (int j = -Distance; j <= Distance; ++j)
              nearbySpots.Add(new Point(fromLocation.X + j, fromLocation.Y + i));

              return nearbySpots;




              The idea is to start at the upper left point, and add each point in the rectangle row by row, ending at the bottom right point.






              share|improve this answer






















                up vote
                0
                down vote



                accepted







                up vote
                0
                down vote



                accepted






                Since it's a rectangle, you can use a very simple approach. Assuming that Distance is always positive:



                public List<Point> GetLocationsStraightLine(Point fromLocation, int Distance) 

                var nearbySpots = new List<Point>();

                for (int i = -Distance; i <= Distance; ++i)
                for (int j = -Distance; j <= Distance; ++j)
                nearbySpots.Add(new Point(fromLocation.X + j, fromLocation.Y + i));

                return nearbySpots;




                The idea is to start at the upper left point, and add each point in the rectangle row by row, ending at the bottom right point.






                share|improve this answer












                Since it's a rectangle, you can use a very simple approach. Assuming that Distance is always positive:



                public List<Point> GetLocationsStraightLine(Point fromLocation, int Distance) 

                var nearbySpots = new List<Point>();

                for (int i = -Distance; i <= Distance; ++i)
                for (int j = -Distance; j <= Distance; ++j)
                nearbySpots.Add(new Point(fromLocation.X + j, fromLocation.Y + i));

                return nearbySpots;




                The idea is to start at the upper left point, and add each point in the rectangle row by row, ending at the bottom right point.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 10 at 4:23









                Squidy

                533514




                533514



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53235863%2fc-sharp-getting-nearby-points-on-a-grid-within-radius%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Use pre created SQLite database for Android project in kotlin

                    Darth Vader #20

                    Ondo