Create a row of images in a bound loop?











up vote
0
down vote

favorite












So I have a row of 3 tea cups that I want to generate in a single for loop. Depending on the value of 'count', it will show either an PImage "emptyCup" or "fullCup". The code mostly works, but the graphical order from the rows is not what I want.



Here is the loop in a nutshell.



int count = 2;
int width = 210;
int length = 180;

for (int i = 1; i <= 3; i++) {
if (i <= 3) {
length = length1;
} else if ((3 <= i) && (i <= 6)) {
length = length2;
} else {
length = length3;
}
if ((i <= count)) {
image(fullCup, (width * (i % 3)), length);
println("FULL CUP: Count = " + count + ", i = " + i);
} else {
println("EMPTY CUP: Count = " + count + ", i = " + i);
image(emptyCup, (width * (i % 3)), length);
}
}


The following should give me the following output, where [o] is a full cup and [ ] is an empty one:



[o] [o] [ ] 


instead it gives me this:



[ ] [o] [o]


Likewise, if I change "count" to 4, and continue the for-loop to 6, then I would get this:



[o] [o] [o]
[ ] [o] [ ]


I'm aware my use of the modulus operator isn't getting me what I want, but I can't put my finger on what I should be saying instead. Any advice?










share|improve this question
























  • Can you please post a Minimal, Complete, and Verifiable example? Maybe use colored rectangles instead of images, so we can copy and paste your code to run it?
    – Kevin Workman
    Nov 22 at 16:24















up vote
0
down vote

favorite












So I have a row of 3 tea cups that I want to generate in a single for loop. Depending on the value of 'count', it will show either an PImage "emptyCup" or "fullCup". The code mostly works, but the graphical order from the rows is not what I want.



Here is the loop in a nutshell.



int count = 2;
int width = 210;
int length = 180;

for (int i = 1; i <= 3; i++) {
if (i <= 3) {
length = length1;
} else if ((3 <= i) && (i <= 6)) {
length = length2;
} else {
length = length3;
}
if ((i <= count)) {
image(fullCup, (width * (i % 3)), length);
println("FULL CUP: Count = " + count + ", i = " + i);
} else {
println("EMPTY CUP: Count = " + count + ", i = " + i);
image(emptyCup, (width * (i % 3)), length);
}
}


The following should give me the following output, where [o] is a full cup and [ ] is an empty one:



[o] [o] [ ] 


instead it gives me this:



[ ] [o] [o]


Likewise, if I change "count" to 4, and continue the for-loop to 6, then I would get this:



[o] [o] [o]
[ ] [o] [ ]


I'm aware my use of the modulus operator isn't getting me what I want, but I can't put my finger on what I should be saying instead. Any advice?










share|improve this question
























  • Can you please post a Minimal, Complete, and Verifiable example? Maybe use colored rectangles instead of images, so we can copy and paste your code to run it?
    – Kevin Workman
    Nov 22 at 16:24













up vote
0
down vote

favorite









up vote
0
down vote

favorite











So I have a row of 3 tea cups that I want to generate in a single for loop. Depending on the value of 'count', it will show either an PImage "emptyCup" or "fullCup". The code mostly works, but the graphical order from the rows is not what I want.



Here is the loop in a nutshell.



int count = 2;
int width = 210;
int length = 180;

for (int i = 1; i <= 3; i++) {
if (i <= 3) {
length = length1;
} else if ((3 <= i) && (i <= 6)) {
length = length2;
} else {
length = length3;
}
if ((i <= count)) {
image(fullCup, (width * (i % 3)), length);
println("FULL CUP: Count = " + count + ", i = " + i);
} else {
println("EMPTY CUP: Count = " + count + ", i = " + i);
image(emptyCup, (width * (i % 3)), length);
}
}


The following should give me the following output, where [o] is a full cup and [ ] is an empty one:



[o] [o] [ ] 


instead it gives me this:



[ ] [o] [o]


Likewise, if I change "count" to 4, and continue the for-loop to 6, then I would get this:



[o] [o] [o]
[ ] [o] [ ]


I'm aware my use of the modulus operator isn't getting me what I want, but I can't put my finger on what I should be saying instead. Any advice?










share|improve this question















So I have a row of 3 tea cups that I want to generate in a single for loop. Depending on the value of 'count', it will show either an PImage "emptyCup" or "fullCup". The code mostly works, but the graphical order from the rows is not what I want.



Here is the loop in a nutshell.



int count = 2;
int width = 210;
int length = 180;

for (int i = 1; i <= 3; i++) {
if (i <= 3) {
length = length1;
} else if ((3 <= i) && (i <= 6)) {
length = length2;
} else {
length = length3;
}
if ((i <= count)) {
image(fullCup, (width * (i % 3)), length);
println("FULL CUP: Count = " + count + ", i = " + i);
} else {
println("EMPTY CUP: Count = " + count + ", i = " + i);
image(emptyCup, (width * (i % 3)), length);
}
}


The following should give me the following output, where [o] is a full cup and [ ] is an empty one:



[o] [o] [ ] 


instead it gives me this:



[ ] [o] [o]


Likewise, if I change "count" to 4, and continue the for-loop to 6, then I would get this:



[o] [o] [o]
[ ] [o] [ ]


I'm aware my use of the modulus operator isn't getting me what I want, but I can't put my finger on what I should be saying instead. Any advice?







image for-loop processing modulus






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 at 13:03

























asked Nov 22 at 12:54









NoStupidQuestions

84




84












  • Can you please post a Minimal, Complete, and Verifiable example? Maybe use colored rectangles instead of images, so we can copy and paste your code to run it?
    – Kevin Workman
    Nov 22 at 16:24


















  • Can you please post a Minimal, Complete, and Verifiable example? Maybe use colored rectangles instead of images, so we can copy and paste your code to run it?
    – Kevin Workman
    Nov 22 at 16:24
















Can you please post a Minimal, Complete, and Verifiable example? Maybe use colored rectangles instead of images, so we can copy and paste your code to run it?
– Kevin Workman
Nov 22 at 16:24




Can you please post a Minimal, Complete, and Verifiable example? Maybe use colored rectangles instead of images, so we can copy and paste your code to run it?
– Kevin Workman
Nov 22 at 16:24












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










I hope you found the answer by now, but I'll answer it anyway.



This is a classic case of an error caused by bad programming habits. You may be new at this and I don't mean this in a bad way, but so you can learn from it ;)



First off, 'width' and 'length' are keywords in Processing, that should not be assigned, only read out.



Second, it is custom to start a for-loop at 0, unless there is a specific reason. This is because of how a computer works and because it makes the logic in the rest of the code easier. For example the modulus.



This is also the error your code.




  • The first cup is drawn at x = width*(1%3) = width

  • The second cup is drawn at x = width*(2%3) = 2*width

  • The third cup is drawn at x = width*(3%3) = 0*width=0


The logic used is not wrong, but based on a for-loop that starts at 0.



This code does what you want. It draws the first image at x=0 and y=0. If necessary you can add an offset.



void setup() {
PImage fullCup = loadImage("fullCup.jpg");
PImage emptyCup = loadImage("emptyCup.jpg");
size(800, 800);

int count = 4;
int numberOfCups = 6;
int img_width = 210;
int img_length = 180;

for (int i = 0; i < numberOfCups; i++) {
if (i < count) {
image(fullCup, img_width * (i % 3), img_length*(i/3));
} else {
image(emptyCup, img_width * (i % 3), img_length*(i/3));
}
}
}





share|improve this answer





















  • Thank you kindly for your answer! I'm rather new at Processing, hence the width/length mistake. I'm aware of the zero-bound principle, but regrettably, there was a reason I had to bind it at 1 in relation to other code in the project - not my idea. Still, perhaps I can adjust it around that limitation (e.g "i < count-1", etc.)
    – NoStupidQuestions
    Dec 5 at 13:11











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%2f53431521%2fcreate-a-row-of-images-in-a-bound-loop%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
1
down vote



accepted










I hope you found the answer by now, but I'll answer it anyway.



This is a classic case of an error caused by bad programming habits. You may be new at this and I don't mean this in a bad way, but so you can learn from it ;)



First off, 'width' and 'length' are keywords in Processing, that should not be assigned, only read out.



Second, it is custom to start a for-loop at 0, unless there is a specific reason. This is because of how a computer works and because it makes the logic in the rest of the code easier. For example the modulus.



This is also the error your code.




  • The first cup is drawn at x = width*(1%3) = width

  • The second cup is drawn at x = width*(2%3) = 2*width

  • The third cup is drawn at x = width*(3%3) = 0*width=0


The logic used is not wrong, but based on a for-loop that starts at 0.



This code does what you want. It draws the first image at x=0 and y=0. If necessary you can add an offset.



void setup() {
PImage fullCup = loadImage("fullCup.jpg");
PImage emptyCup = loadImage("emptyCup.jpg");
size(800, 800);

int count = 4;
int numberOfCups = 6;
int img_width = 210;
int img_length = 180;

for (int i = 0; i < numberOfCups; i++) {
if (i < count) {
image(fullCup, img_width * (i % 3), img_length*(i/3));
} else {
image(emptyCup, img_width * (i % 3), img_length*(i/3));
}
}
}





share|improve this answer





















  • Thank you kindly for your answer! I'm rather new at Processing, hence the width/length mistake. I'm aware of the zero-bound principle, but regrettably, there was a reason I had to bind it at 1 in relation to other code in the project - not my idea. Still, perhaps I can adjust it around that limitation (e.g "i < count-1", etc.)
    – NoStupidQuestions
    Dec 5 at 13:11















up vote
1
down vote



accepted










I hope you found the answer by now, but I'll answer it anyway.



This is a classic case of an error caused by bad programming habits. You may be new at this and I don't mean this in a bad way, but so you can learn from it ;)



First off, 'width' and 'length' are keywords in Processing, that should not be assigned, only read out.



Second, it is custom to start a for-loop at 0, unless there is a specific reason. This is because of how a computer works and because it makes the logic in the rest of the code easier. For example the modulus.



This is also the error your code.




  • The first cup is drawn at x = width*(1%3) = width

  • The second cup is drawn at x = width*(2%3) = 2*width

  • The third cup is drawn at x = width*(3%3) = 0*width=0


The logic used is not wrong, but based on a for-loop that starts at 0.



This code does what you want. It draws the first image at x=0 and y=0. If necessary you can add an offset.



void setup() {
PImage fullCup = loadImage("fullCup.jpg");
PImage emptyCup = loadImage("emptyCup.jpg");
size(800, 800);

int count = 4;
int numberOfCups = 6;
int img_width = 210;
int img_length = 180;

for (int i = 0; i < numberOfCups; i++) {
if (i < count) {
image(fullCup, img_width * (i % 3), img_length*(i/3));
} else {
image(emptyCup, img_width * (i % 3), img_length*(i/3));
}
}
}





share|improve this answer





















  • Thank you kindly for your answer! I'm rather new at Processing, hence the width/length mistake. I'm aware of the zero-bound principle, but regrettably, there was a reason I had to bind it at 1 in relation to other code in the project - not my idea. Still, perhaps I can adjust it around that limitation (e.g "i < count-1", etc.)
    – NoStupidQuestions
    Dec 5 at 13:11













up vote
1
down vote



accepted







up vote
1
down vote



accepted






I hope you found the answer by now, but I'll answer it anyway.



This is a classic case of an error caused by bad programming habits. You may be new at this and I don't mean this in a bad way, but so you can learn from it ;)



First off, 'width' and 'length' are keywords in Processing, that should not be assigned, only read out.



Second, it is custom to start a for-loop at 0, unless there is a specific reason. This is because of how a computer works and because it makes the logic in the rest of the code easier. For example the modulus.



This is also the error your code.




  • The first cup is drawn at x = width*(1%3) = width

  • The second cup is drawn at x = width*(2%3) = 2*width

  • The third cup is drawn at x = width*(3%3) = 0*width=0


The logic used is not wrong, but based on a for-loop that starts at 0.



This code does what you want. It draws the first image at x=0 and y=0. If necessary you can add an offset.



void setup() {
PImage fullCup = loadImage("fullCup.jpg");
PImage emptyCup = loadImage("emptyCup.jpg");
size(800, 800);

int count = 4;
int numberOfCups = 6;
int img_width = 210;
int img_length = 180;

for (int i = 0; i < numberOfCups; i++) {
if (i < count) {
image(fullCup, img_width * (i % 3), img_length*(i/3));
} else {
image(emptyCup, img_width * (i % 3), img_length*(i/3));
}
}
}





share|improve this answer












I hope you found the answer by now, but I'll answer it anyway.



This is a classic case of an error caused by bad programming habits. You may be new at this and I don't mean this in a bad way, but so you can learn from it ;)



First off, 'width' and 'length' are keywords in Processing, that should not be assigned, only read out.



Second, it is custom to start a for-loop at 0, unless there is a specific reason. This is because of how a computer works and because it makes the logic in the rest of the code easier. For example the modulus.



This is also the error your code.




  • The first cup is drawn at x = width*(1%3) = width

  • The second cup is drawn at x = width*(2%3) = 2*width

  • The third cup is drawn at x = width*(3%3) = 0*width=0


The logic used is not wrong, but based on a for-loop that starts at 0.



This code does what you want. It draws the first image at x=0 and y=0. If necessary you can add an offset.



void setup() {
PImage fullCup = loadImage("fullCup.jpg");
PImage emptyCup = loadImage("emptyCup.jpg");
size(800, 800);

int count = 4;
int numberOfCups = 6;
int img_width = 210;
int img_length = 180;

for (int i = 0; i < numberOfCups; i++) {
if (i < count) {
image(fullCup, img_width * (i % 3), img_length*(i/3));
} else {
image(emptyCup, img_width * (i % 3), img_length*(i/3));
}
}
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Dec 4 at 19:04









J.D.

2785




2785












  • Thank you kindly for your answer! I'm rather new at Processing, hence the width/length mistake. I'm aware of the zero-bound principle, but regrettably, there was a reason I had to bind it at 1 in relation to other code in the project - not my idea. Still, perhaps I can adjust it around that limitation (e.g "i < count-1", etc.)
    – NoStupidQuestions
    Dec 5 at 13:11


















  • Thank you kindly for your answer! I'm rather new at Processing, hence the width/length mistake. I'm aware of the zero-bound principle, but regrettably, there was a reason I had to bind it at 1 in relation to other code in the project - not my idea. Still, perhaps I can adjust it around that limitation (e.g "i < count-1", etc.)
    – NoStupidQuestions
    Dec 5 at 13:11
















Thank you kindly for your answer! I'm rather new at Processing, hence the width/length mistake. I'm aware of the zero-bound principle, but regrettably, there was a reason I had to bind it at 1 in relation to other code in the project - not my idea. Still, perhaps I can adjust it around that limitation (e.g "i < count-1", etc.)
– NoStupidQuestions
Dec 5 at 13:11




Thank you kindly for your answer! I'm rather new at Processing, hence the width/length mistake. I'm aware of the zero-bound principle, but regrettably, there was a reason I had to bind it at 1 in relation to other code in the project - not my idea. Still, perhaps I can adjust it around that limitation (e.g "i < count-1", etc.)
– NoStupidQuestions
Dec 5 at 13:11


















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%2f53431521%2fcreate-a-row-of-images-in-a-bound-loop%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

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)