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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T08:33:58+00:00 2026-06-03T08:33:58+00:00

I would like to be able to iterate through the grid elements with a

  • 0

I would like to be able to iterate through the grid elements with a set step size. The fun part of this problem is that the grid will be rotated. I have developed an algorithm to do this and it is successful for some cases. The image below specifies the problem:

enter image description here

The conditions of the problem are that a grid spacing will be provided that is a factor of the grid length and width (As a side note the grid can be rectangular). The Algorithm must iterate through the grid and print out where it is. Here is some code and an example of it working:

int main() {
vector< vector<double> > bound;
vector<double> point;
point.push_back(0);
point.push_back(4);

bound.push_back(point);
point[0] = 6; point[1] = 10;
bound.push_back(point);
point[0] = 4; point[1] = 0;
bound.push_back(point);
point[0] = 10; point[1] = 6;
bound.push_back(point);

double d = 0.5;
double x, y;
int countx = 0, county = 0;
for (double i = bound[0][0]; i < bound[2][0]; i+=d) {
    //std::cout << "I: " << i << std::endl;
    for (double j = bound[0][1]; j < bound[1][1]; j+=d) {
        //std::cout << "J: " << j << std::endl;
        x = i+d+(double)county*d;
        y = j-(double)countx*d;
        ++county;
        std::cout << "i, j, x and y: " << i << "\t" << j << "\t" << x << "\t" << y << std::endl;
    }
    std::cout << "new Row--------------------\n";
    ++countx;
    county = 0;
}   
}

The code above works and prints correctly the grid elements, ie:

x and y: 4, 0.5
x and y: 4.5, 1
etc.

However when trying a rectangle with bounds:

[(0.5, 6), (3, 8.5), (5.5, 1), (8, 3.5)]

and a step size (d) of 1

It iterates to outside the rectangle bounds. I can see why this is happening, the iterator condition in the for loop will not contain it because of the extra +d.

My question is, is there a better way to approach this problem and how would i go about it?

Does anyone know if this has been implemented before and has some source code?

Cheers for the help.

Ben

  • 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-06-03T08:34:00+00:00Added an answer on June 3, 2026 at 8:34 am

    The Way I ended up going about it was to calculate the length and width of the rectangle by Pythagoras rule on two of the sides. I then made a grid on a virtual rectangle which is aligned with its bottom corner at the origin. Then by using a pre-developed library for matrix rotations and translations, I transformed the points individually by translating them to by the displacement to the bottom left corner and rotating them to the calculated angle of the rectangle.

    This is similar to the above solution but uses a full transformation matrix. The answer ended up being simpler then I thought it would be.

    Thanks for the help.

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

Sidebar

Related Questions

I have a dictionary that I would like to iterate through and copy the
I would like to be able to iterate through the values of a struct
I would like to iterate through all elements in an std::list in parallel fashion
Would like to be able to set colors of headings and such, different font
I would like to be able to change the size of text in a
I would like to be able to spawn a linux process that would only
I'm trying to write a script in jQuery that would iterate through each text
My input elements are followed by a div. I would like to set the
I would like to iterate through an xml document to get its values. See
I would like to be able to abort a specific sql statement if it

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.