How to use for loop multiple condition ?

Hi all.
how to use for loop multiple condition?

for (int inc = 0, int col = 0; inc < 81, col < 59;  inc++, col =+1) {
   
  }

or:

for (int inc = 0, int col = 0; inc < 81 && col < 59;  inc++, col =+1) {
   
  }

Thanks
Adam

Neither compiles. So don't use either.

Once fixed, they both compile

  for (int inc = 0, col = 0; inc < 81, col < 59;  inc++, col =+1) {
    
  }

  for (int inc = 0, col = 0; inc < 81 && col < 59;  inc++, col =+1) {
    
  }

But it looks like the second one is what you want. At a glance, however, col will hit 59 and terminate the loop long before inc never reaches 81.

Also why add 1 for one variable, and increment the other?

  for (int inc = 0, col = 0; inc < 81 && col < 59;  inc++, col++) {
    
  }

Review the syntax of the comma operator.

Also, what are you trying to do with this multiple conditions loop? I have to confess it looks to me
like you are trying for something other that what the loop does...

a7

2 Likes

Fixes aside, what you are doing is running diagonally through a 2D matrix, hitting only those locations where inc=col, unless you start manipulating one of the two counters within the loop.
So yeah, what are you trying to do?

2 Likes

Thank you alto777.
the multi condition for-loop I'm try to use in TFT_ArcFill from:

that was designed to fill arc inside loop function that is relatively easy?
I'd like to put the fillRac in the setup function, that's why need a for-loop.

/* https://github.com/Bodmer/TFT_eSPI/blob/master/examples/320%20x%20240/TFT_ArcFill/TFT_ArcFill.ino
*  
*  
*/

// Demo using arcFill to draw ellipses and a segmented elipse
#include <TFT_eSPI.h> // Hardware-specific library
#include <SPI.h>

TFT_eSPI tft = TFT_eSPI();       // Invoke custom library

#define DEG2RAD 0.0174532925

#define LOOP_DELAY 10 // Loop delay to slow things down

byte inc = 0;
unsigned int col = 0;

byte red = 31; // Red is the top 5 bits of a 16 bit colour value
byte green = 0;// Green is the middle 6 bits
byte blue = 0; // Blue is the bottom 5 bits
byte state = 0;

void setup(void) {
 tft.begin();

 tft.setRotation(1);

 tft.fillScreen(TFT_BLACK);

}


void loop() {

 // Continuous elliptical arc drawing
 fillArc(160, 120, inc * 6, 1, 140, 100, 10, rainbow(col));

 // Continuous segmented (inc*2) elliptical arc drawing
 fillArc(160, 120, ((inc * 2) % 60) * 6, 1, 120, 80, 30, rainbow(col));

 // Circle drawing using arc with arc width = radius
 fillArc(160, 120, inc * 6, 1, 42, 42, 42, rainbow(col));

 inc++;
 col += 1;
 if (col > 191) col = 0;
 if (inc > 59) inc = 0;

 delay(LOOP_DELAY);
}


// #########################################################################
// Draw a circular or elliptical arc with a defined thickness
// #########################################################################

// x,y == coords of centre of arc
// start_angle = 0 - 359
// seg_count = number of 6 degree segments to draw (60 => 360 degree arc)
// rx = x axis outer radius
// ry = y axis outer radius
// w  = width (thickness) of arc in pixels
// colour = 16 bit colour value
// Note if rx and ry are the same then an arc of a circle is drawn

void fillArc(int x, int y, int start_angle, int seg_count, int rx, int ry, int w, unsigned int colour)
{

 byte seg = 6; // Segments are 3 degrees wide = 120 segments for 360 degrees
 byte inc = 6; // Draw segments every 3 degrees, increase to 6 for segmented ring

 // Calculate first pair of coordinates for segment start
 float sx = cos((start_angle - 90) * DEG2RAD);
 float sy = sin((start_angle - 90) * DEG2RAD);
 uint16_t x0 = sx * (rx - w) + x;
 uint16_t y0 = sy * (ry - w) + y;
 uint16_t x1 = sx * rx + x;
 uint16_t y1 = sy * ry + y;

 // Draw colour blocks every inc degrees
 for (int i = start_angle; i < start_angle + seg * seg_count; i += inc) {

   // Calculate pair of coordinates for segment end
   float sx2 = cos((i + seg - 90) * DEG2RAD);
   float sy2 = sin((i + seg - 90) * DEG2RAD);
   int x2 = sx2 * (rx - w) + x;
   int y2 = sy2 * (ry - w) + y;
   int x3 = sx2 * rx + x;
   int y3 = sy2 * ry + y;

   tft.fillTriangle(x0, y0, x1, y1, x2, y2, colour);
   tft.fillTriangle(x1, y1, x2, y2, x3, y3, colour);

   // Copy segment end to sgement start for next segment
   x0 = x2;
   y0 = y2;
   x1 = x3;
   y1 = y3;
 }
}

// #########################################################################
// Return the 16 bit colour with brightness 0-100%
// #########################################################################
unsigned int brightness(unsigned int colour, int brightness)
{
 byte red   = colour >> 11;
 byte green = (colour & 0x7E0) >> 5;
 byte blue  = colour & 0x1F;

 blue =  (blue * brightness) / 100;
 green = (green * brightness) / 100;
 red =   (red * brightness) / 100;

 return (red << 11) + (green << 5) + blue;
}

// #########################################################################
// Return a 16 bit rainbow colour
// #########################################################################
unsigned int rainbow(byte value)
{
 // Value is expected to be in range 0-127
 // The value is converted to a spectrum colour from 0 = blue through to 127 = red

 switch (state) {
   case 0:
     green ++;
     if (green == 64) {
       green = 63;
       state = 1;
     }
     break;
   case 1:
     red--;
     if (red == 255) {
       red = 0;
       state = 2;
     }
     break;
   case 2:
     blue ++;
     if (blue == 32) {
       blue = 31;
       state = 3;
     }
     break;
   case 3:
     green --;
     if (green == 255) {
       green = 0;
       state = 4;
     }
     break;
   case 4:
     red ++;
     if (red == 32) {
       red = 31;
       state = 5;
     }
     break;
   case 5:
     blue --;
     if (blue == 255) {
       blue = 0;
       state = 0;
     }
     break;
 }
 return red << 11 | green << 5 | blue;
}

the modified code put fillArc as a class FillArc_loop() and put in setup:

#include "HCONFIG.h"
#include "Ajpeg.h"


void setup(void) {
  tft.begin();

  tft.setRotation(1);
  tft.fillScreen(TFT_BLACK);
  tft.setSwapBytes(true);

  tft.fillScreen(TFT_BLACK);

  ///  tft.pushImage(0, 0,  320, 240, pict1);
  tft.pushImage(Cx0, Cy0,  wt0, ht0, image_test);

  FillArc_loop();
}


void loop()
{

}
void FillArc_loop() {

  for (inc = 0, col = 0; inc < 81, col < 59; inc++, col =+1) {
    // Continuous elliptical arc drawing
    fillArc(Cxarc, Cyarc, inc * 6, 1, Rax, Ray, 10, TFT_RED);
    fillArc(Cxarc, Cyarc, inc * 6, 1, Rax - 20, Ray - 20, 10, TFT_WHITE);

    delay(LOOP_DELAY);
  }
}

Maybe col += 1 ?

But then, as @alto777 said, why use ++ on one and += on the other? :man_shrugging:

1 Like

I, not alone perhaps, lsdyexised and saw col += 1;

Now about this clever bit of code:

  for (int row=0, col=0; row < NUM_ROWS; (++col < NUM_COLS) ? :(col=0 , row++)){

I haven't run across anything like that. And… I hope I never do. In the absence of your complete sketch that does indeed what you say, I would not have been able to tell you what that does. I would not get the job.

Is there something you don't like about nested loops? I dare say @shanren might do better with them for awhile…

a7

1 Like

Thank you Delta_G.
what I like to have is tft.fillArc and tft.fillTriangle works at same code.

  1. I tested to put the original loop() as a function into setup, didn't get what excepted, to draw two filled arcs. if it should work in setup, no second question;
  2. yes, the for-loop is not right. actually it works in my testing, I guess because of the 'inc' is not strict, inc < 59/inc <29 ... all same by my test.

yes, it is.

Thanks.
sure I like any thing new and better.
this topic can be simple as how to put the function below inside setup().

void loop() {

  // Continuous elliptical arc drawing
  fillArc(160, 120, inc * 6, 1, 140, 100, 10, rainbow(col));

  // Continuous segmented (inc*2) elliptical arc drawing
  fillArc(160, 120, ((inc * 2) % 60) * 6, 1, 120, 80, 30, rainbow(col));

  // Circle drawing using arc with arc width = radius
  fillArc(160, 120, inc * 6, 1, 42, 42, 42, rainbow(col));

  inc++;
  col += 1;
  if (col > 191) col = 0;
  if (inc > 59) inc = 0;

  delay(LOOP_DELAY);
}

There you go again - why do you use ++ for one variable, but +=1 for the other?
Why not be consistent?

1 Like

yes. should be consistent better.

you are right. testing shown that. I'll set both inc and col < 60.

Thank you for many helps.

Sorry of if any missed answer.
--- one question from #4 was answered at #10 indirectly;
--- 2 questions in #6 was answered in #9, answers may not so relevant;
--- #11: to be honestly, I am not quite understand the meaning of;
--- #14 was answered in #16;
--- #17: here now: I'll attach pictures to show what the code for and how the result is, may better the my description.
fillRac:

fillTriangle:

what I like to have is both of them appear on screen, with both of the functions sit in setup.

THANKS FOR ALL, QUESTION IS SOLVED by set inc = col = 60.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.