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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:52:29+00:00 2026-05-31T00:52:29+00:00

How would you solve the problem of finding the points of a (integer) grid

  • 0

How would you solve the problem of finding the points of a (integer) grid within a circle centered on the origin of the axis, with the results ordered by norm, as in distance from the centre, in C++?

I wrote an implementation that works (yeah, I know, it is extremely inefficient, but for my problem anything more would be overkill). I’m extremely new to C++, so my biggest problem was finding a data structure capable of

  1. being sort-able;
  2. being able to save an array in one of its elements,

rather than the implementation of the algorithm. My code is as follows. Thanks in advance, everyone!

typedef std::pair<int, int[2]> norm_vec2d;

bool norm_vec2d_cmp (norm_vec2d a, norm_vec2d b)
{
    bool bo;
    bo = (a.first < b.first ? true: false);
    return bo;
}

int energy_to_momenta_2D (int energy, std::list<norm_vec2d> *momenta)
{
    int i, j, norm, n=0;
    int energy_root = (int) std::sqrt(energy);

    norm_vec2d temp;

    for (i=-energy_root; i<=energy_root; i++)
    {
        for (j =-energy_root; j<=energy_root; j++)
        {
            norm = i*i + j*j;
            if (norm <= energy)
            {
                temp.first = norm;
                temp.second[0] = i;
                temp.second[1] = j;
                (*momenta).push_back (temp);
                n++;
            }
        }
    }
    (*momenta).sort(norm_vec2d_cmp);
    return n;
}
  • 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-31T00:52:30+00:00Added an answer on May 31, 2026 at 12:52 am

    How would you solve the problem of finding the points of a (integer) grid within a circle centered on the origin of the axis, with the results ordered by norm, as in distance from the centre, in C++?

    I wouldn’t use a std::pair to hold the points. I’d create my own more descriptive type.

    struct Point {
      int x;
      int y;
      int square() const { return x*x + y*y; }
      Point(int x = 0, int y = 0)
        : x(x), y(y) {}
      bool operator<(const Point& pt) const {
        if( square() < pt.square() )
          return true;
        if( pt.square() < square() )
          return false;
        if( x < pt.x )
          return true;
        if( pt.x < x)
          return false;
        return y < pt.y;
      }
      friend std::ostream& operator<<(std::ostream& os, const Point& pt) {
        return os << "(" << pt.x << "," << pt.y << ")";
      }
    };
    

    This data structure is (probably) exactly the same size as two ints, it is less-than comparable, it is assignable, and it is easily printable.

    The algorithm walks through all of the valid points that satisfy x=[0,radius] && y=[0,x] && (x,y) inside circle:

    std::set<Point>
    GetListOfPointsInsideCircle(double radius = 1) {
      std::set<Point> result;
    
      // Only examine bottom half of quadrant 1, then
      // apply symmetry 8 ways
      for(Point pt(0,0); pt.x <= radius; pt.x++, pt.y = 0) {
        for(; pt.y <= pt.x && pt.square()<=radius*radius; pt.y++) {
          result.insert(pt);
          result.insert(Point(-pt.x, pt.y));
          result.insert(Point(pt.x, -pt.y));
          result.insert(Point(-pt.x, -pt.y));
          result.insert(Point(pt.y, pt.x));
          result.insert(Point(-pt.y, pt.x));
          result.insert(Point(pt.y, -pt.x));
          result.insert(Point(-pt.y, -pt.x));
        }
      }
      return result;
    }
    

    I chose a std::set to hold the data for two reasons:

    • It is stored is sorted order, so I don’t have to std::sort it, and
    • It rejects duplicates, so I don’t have to worry about points whose reflection are identical

    Finally, using this algorithm is dead simple:

    int main () {
      std::set<Point> vp = GetListOfPointsInsideCircle(2);
      std::copy(vp.begin(), vp.end(),
        std::ostream_iterator<Point>(std::cout, "\n"));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Could you give an example where static_assert(...) ('C++11') would solve the problem in hand
I would like solve the problem (now hypothetical but propably real in future) of
What would be the best algorithm to solve this problem? I spent a couple
Trying to solve this problem . I would like to learn how the bootstrapper
I would like your advice about how best to solve my problem. In a
I would like to find a better algorithm to solve the following problem: There
I have an interesting genetics problem that I would like to solve in native
I need to solve a job affectation problem and I would like to find
I tried to solve problems from Project Euler. I know my method would work
I have a problem that I would like have solved via a SQL query.

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.