How to add texture (with an image) to a line?

So i have this code (its a function that creates a line in a line object):
void dibujarLinea() //draw color line
{
    pushMatrix();
    pushStyle();
    strokeCap(PROJECT);
    strokeWeight( grosor );
    translate( x , y );
    rotate( angulo ); //angle
    
    for ( int i=0; i<cantidad; i++ ) {
      float x1 = i*tam;  //tam=size
      float x2 = (i+1)*tam; //tam=size
      stroke( colores[i] ); //color
      line( x1, 0, x2, 0 );
      //add a texture here//
    }
    popStyle();
    popMatrix(); }

and I want to add a texture everytime a line is created, how could I do it?
Here is an example of what my sketch generates (I want to add a texture in every color of my line).

Thanks!
PS: I do not want to use vertex

Assuming you have the center of each segment and its width and height, you can do this:

-Set imageMode to CENTER: https://processing.org/reference/imageMode_.html

  • Load your rectangular texture in a PImage object
  • Use translate/rotate. Translate is used to set the center of the sketch at the center of the line segment. You do a rotate and then you draw the image
  • Use push/pop. Push/pop allows to set and reset your sketch coordinate system. This comes handy when you are ready to set your next segment

In your code above, the translate/rotate and push/pop would be inside the for loop. You will need to have a PImage array that you can get the textures from while interacting with each line segment.

By the way, iamgeMode CENTER is just a recommendation. You can use any image mode you want but I find that CENTER works better in these situations.

Kf