How to iterate over pixels on edge of a square in 1 iteration [closed]











up vote
-1
down vote

favorite












Hi is there a way of looping around the edges of a square using one for loop? The square will be aligned with the x-axis and y-axis. The square will also have a known length and center position.










share|improve this question















closed as too broad by idursun, user6910411, Max Vollmer, Skynet, Rob Nov 22 at 18:06


Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.











  • 3




    It's hard to answer without having some more details e.g. code.
    – Rene Knop
    Nov 22 at 14:10






  • 1




    The answer is most likely "Yes" but we need to see your code.
    – Stefan
    Nov 22 at 14:22










  • so far i'm just planning the function that this is for and haven't got any code yet but the image will be a binary image in a 2d x and y array of 1's and 0's. the location of the center of the square is known and so is the distance from its center to the edge.
    – Richard8365
    Nov 22 at 14:27















up vote
-1
down vote

favorite












Hi is there a way of looping around the edges of a square using one for loop? The square will be aligned with the x-axis and y-axis. The square will also have a known length and center position.










share|improve this question















closed as too broad by idursun, user6910411, Max Vollmer, Skynet, Rob Nov 22 at 18:06


Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.











  • 3




    It's hard to answer without having some more details e.g. code.
    – Rene Knop
    Nov 22 at 14:10






  • 1




    The answer is most likely "Yes" but we need to see your code.
    – Stefan
    Nov 22 at 14:22










  • so far i'm just planning the function that this is for and haven't got any code yet but the image will be a binary image in a 2d x and y array of 1's and 0's. the location of the center of the square is known and so is the distance from its center to the edge.
    – Richard8365
    Nov 22 at 14:27













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











Hi is there a way of looping around the edges of a square using one for loop? The square will be aligned with the x-axis and y-axis. The square will also have a known length and center position.










share|improve this question















Hi is there a way of looping around the edges of a square using one for loop? The square will be aligned with the x-axis and y-axis. The square will also have a known length and center position.







java image geometry coordinates






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 at 14:17









Ishaan

7711417




7711417










asked Nov 22 at 14:07









Richard8365

33




33




closed as too broad by idursun, user6910411, Max Vollmer, Skynet, Rob Nov 22 at 18:06


Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.






closed as too broad by idursun, user6910411, Max Vollmer, Skynet, Rob Nov 22 at 18:06


Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.










  • 3




    It's hard to answer without having some more details e.g. code.
    – Rene Knop
    Nov 22 at 14:10






  • 1




    The answer is most likely "Yes" but we need to see your code.
    – Stefan
    Nov 22 at 14:22










  • so far i'm just planning the function that this is for and haven't got any code yet but the image will be a binary image in a 2d x and y array of 1's and 0's. the location of the center of the square is known and so is the distance from its center to the edge.
    – Richard8365
    Nov 22 at 14:27














  • 3




    It's hard to answer without having some more details e.g. code.
    – Rene Knop
    Nov 22 at 14:10






  • 1




    The answer is most likely "Yes" but we need to see your code.
    – Stefan
    Nov 22 at 14:22










  • so far i'm just planning the function that this is for and haven't got any code yet but the image will be a binary image in a 2d x and y array of 1's and 0's. the location of the center of the square is known and so is the distance from its center to the edge.
    – Richard8365
    Nov 22 at 14:27








3




3




It's hard to answer without having some more details e.g. code.
– Rene Knop
Nov 22 at 14:10




It's hard to answer without having some more details e.g. code.
– Rene Knop
Nov 22 at 14:10




1




1




The answer is most likely "Yes" but we need to see your code.
– Stefan
Nov 22 at 14:22




The answer is most likely "Yes" but we need to see your code.
– Stefan
Nov 22 at 14:22












so far i'm just planning the function that this is for and haven't got any code yet but the image will be a binary image in a 2d x and y array of 1's and 0's. the location of the center of the square is known and so is the distance from its center to the edge.
– Richard8365
Nov 22 at 14:27




so far i'm just planning the function that this is for and haven't got any code yet but the image will be a binary image in a 2d x and y array of 1's and 0's. the location of the center of the square is known and so is the distance from its center to the edge.
– Richard8365
Nov 22 at 14:27












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










You certainly can do it in one loop, but I think you are probably looking for something like this:



//start at top-left
int x = center_x - (len/2);
int y = center_y - (len/2);

//point to the right
int dx=1;
int dy=0;

for (int side=0; side<4; ++side) {
for (int i=1; i<len; ++i) {
do_something(x,y);
x+=dx;
y+=dy;
}
//turn right
int t=dx;
dx=-dy;
dy=t;
}





share|improve this answer





















  • Thanks, this should work
    – Richard8365
    Nov 22 at 14:30










  • this can be transferred to one loop but no reason to do that because the number of iterations will be the same - the number of points around the square are the same
    – Veselin Davidov
    Nov 22 at 14:35


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










You certainly can do it in one loop, but I think you are probably looking for something like this:



//start at top-left
int x = center_x - (len/2);
int y = center_y - (len/2);

//point to the right
int dx=1;
int dy=0;

for (int side=0; side<4; ++side) {
for (int i=1; i<len; ++i) {
do_something(x,y);
x+=dx;
y+=dy;
}
//turn right
int t=dx;
dx=-dy;
dy=t;
}





share|improve this answer





















  • Thanks, this should work
    – Richard8365
    Nov 22 at 14:30










  • this can be transferred to one loop but no reason to do that because the number of iterations will be the same - the number of points around the square are the same
    – Veselin Davidov
    Nov 22 at 14:35















up vote
1
down vote



accepted










You certainly can do it in one loop, but I think you are probably looking for something like this:



//start at top-left
int x = center_x - (len/2);
int y = center_y - (len/2);

//point to the right
int dx=1;
int dy=0;

for (int side=0; side<4; ++side) {
for (int i=1; i<len; ++i) {
do_something(x,y);
x+=dx;
y+=dy;
}
//turn right
int t=dx;
dx=-dy;
dy=t;
}





share|improve this answer





















  • Thanks, this should work
    – Richard8365
    Nov 22 at 14:30










  • this can be transferred to one loop but no reason to do that because the number of iterations will be the same - the number of points around the square are the same
    – Veselin Davidov
    Nov 22 at 14:35













up vote
1
down vote



accepted







up vote
1
down vote



accepted






You certainly can do it in one loop, but I think you are probably looking for something like this:



//start at top-left
int x = center_x - (len/2);
int y = center_y - (len/2);

//point to the right
int dx=1;
int dy=0;

for (int side=0; side<4; ++side) {
for (int i=1; i<len; ++i) {
do_something(x,y);
x+=dx;
y+=dy;
}
//turn right
int t=dx;
dx=-dy;
dy=t;
}





share|improve this answer












You certainly can do it in one loop, but I think you are probably looking for something like this:



//start at top-left
int x = center_x - (len/2);
int y = center_y - (len/2);

//point to the right
int dx=1;
int dy=0;

for (int side=0; side<4; ++side) {
for (int i=1; i<len; ++i) {
do_something(x,y);
x+=dx;
y+=dy;
}
//turn right
int t=dx;
dx=-dy;
dy=t;
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 at 14:26









Matt Timmermans

18.2k11532




18.2k11532












  • Thanks, this should work
    – Richard8365
    Nov 22 at 14:30










  • this can be transferred to one loop but no reason to do that because the number of iterations will be the same - the number of points around the square are the same
    – Veselin Davidov
    Nov 22 at 14:35


















  • Thanks, this should work
    – Richard8365
    Nov 22 at 14:30










  • this can be transferred to one loop but no reason to do that because the number of iterations will be the same - the number of points around the square are the same
    – Veselin Davidov
    Nov 22 at 14:35
















Thanks, this should work
– Richard8365
Nov 22 at 14:30




Thanks, this should work
– Richard8365
Nov 22 at 14:30












this can be transferred to one loop but no reason to do that because the number of iterations will be the same - the number of points around the square are the same
– Veselin Davidov
Nov 22 at 14:35




this can be transferred to one loop but no reason to do that because the number of iterations will be the same - the number of points around the square are the same
– Veselin Davidov
Nov 22 at 14:35



Popular posts from this blog

What visual should I use to simply compare current year value vs last year in Power BI desktop

Alexandru Averescu

Trompette piccolo