How to change start position next letter in "key pressed"?

Hello!

I am trying to create drawing letter, witch consist of ellipse. Every next letter type after key pressed. I can’t change fist position every next letter to create new letter after previous letter and gap space.

ell [][] newell;
int cols = 40;
int rows = 40;
float dist;
int x2;
int y2;

void setup()
{
  size(1000,1000);
  background(255);
  newell = new ell [cols][rows];
  ellipseMode(CORNER);
  
  dist = min(width/cols, height/rows);
  
  for (int i = 0; i < cols; i++)
  {
    for (int j = 0; j < rows; j++)
    {
      if (i%2==0)
      {
        newell [i][j] = new ell (i*dist, j*dist, dist-3);
      } else 
      {
        newell [i][j] = new ell (i*dist, j*dist + 10, dist-3);
      }
      }
    }
}

void draw ()
{ 
  drawdot();
  posXY();
}

void drawdot()
{
  for (int i = 0; i < cols; i++)
  {
    for (int j = 0; j < rows; j++)
    {
      newell [i][j].update();
    }
  }
}

void posXY()
{
  for (int x1 = 2; x1 < 30; x1++) //x1 = 2; y1 = 2; fist dot
  {
    for (int y1 = 2; y1 < rows; y1++)
    {
      x2 = x1 + 4;
      y2 = y1; 
    }
  }
  
  //drawA(x2, y2); not work
  
  drawA(2, 2);
}


void drawA(int x, int y)
{
  if (keyPressed)
  {
    if (key == 'a')
    {
      newell [x][y+1].c1 = 255;
      newell [x+1][y].c1 = 255;
      newell [x+2][y+1].c1 = 255;
      newell [x+2][y+2].c1 = 255;
      newell [x+2][y+3].c1 = 255;
      newell [x+0][y+3].c1 = 255;
      newell [x+1][y+2].c1 = 255;
      newell [x+1][y+3].c1 = 255;
    }
  }
}

class ell
{
  color c1;
  float x;
  float y;
  float s;
  
  ell (float tempx, float tempy, float temps)
  {
    x=tempx;
    y=tempy;
    s=temps;
    c1 = #C0CBD1;
  }
  
  void update()
  {
    fill (c1);
    stroke(#C0CBD1);
    ellipse (x, y, s, s);
  }
}

Thank you for every advice!
And sorry for my bad English. I hope I could explain what I plan to do and what my problem

Which line of code draws the letter? Where are you telling that letter to be drawn?

What does this for loop do?

for (int x1 = 2; x1 < 30; x1++) //x1 = 2; y1 = 2; fist dot
  {
    for (int y1 = 2; y1 < rows; y1++)
    {
      x2 = x1 + 4;
      y2 = y1; 
    }
  }

My advice would be to start with something simple. Can you display a rectangle that moves the way you’re describing whenever you press any key?

1 Like

Hi @Nina_Moskaleva,
I was looking at your code and I have to say it is hard to read. One thing you need to keep in mind when writing code is that you need to make it readable. Why is that? You will find in the life cycle of any software (any code) that people read the code more often that it is written. There are two ways to make your code readable. My favorite method is to write code that you can read by itself. For instance, s=temps; in your ell constructor is hard to read. Instead, what about dotSize=temp_size;? You see, with this small change I can start reading your code and there is no need to figure out what s means.
A second way to document the code is by adding comments to it. I prefer the first one over the second one and I use the second one as a compliment of the first one. Adding comments just means you need to update the comments when you update your code. It is not difficult, but it is more work that self-documenting your code, exactly what is done when writing readable code. At the end, adding comments will always help to add clarity to your code and I always encourage it for the sake of making your code more readable to your users and even for yourself in the future.

I encourage you to use the same code, change it a bit to make it readable, add some comments (for instance, explain why you are using i%2 and I would be glad to assist you further and even explore other strategies (later on).

Cheers,
Kf

1 Like

Kevin, thank you for you for your answer and advice. I try to start with simple sketch but I don’t understand how to change position of coursor.

I have not yet been able to figure out how to add the size of the object to the X coordinate with each press, as happens when printing letters, for example, in Microsoft Word or a typewriter. Maybe you know how to do this? Or what processing reference to look at?

float cursorPosition;
float xFistPos;
float objektSize;

void setup()
{
  size(500,500);
  background(255);
  ellipseMode(CORNER);
}
void draw()
{
  xFistPos=100; // fist position of X position of rectangle
  objektSize = 40; // size of ellipse or rectangle
  cursorPosition = xFistPos + objektSize; // I don't undestand how I can change cursor position
  
  if (keyPressed)
  {
    if (key == 'a')
    {
      rect(cursorPosition,100, objektSize, objektSize);
    }
    if(key == 'b')
    {
      ellipse(cursorPosition,100, objektSize, objektSize);
    }
  }
}

Thank you @kfrajer for your advice, I try to comment my code. But I understand that I even don’t know how to do what I wish for more simple sketch (in my previous comment). Maybe you can suggest a strategy by which this can be done? Or what processing reference to look at?

ell [][] newell;
int cols = 40;
int rows = 40;
float dist;
int x2;
int y2;

void setup()
{
  size(1000,1000);
  background(255);
  newell = new ell [cols][rows];
  ellipseMode(CORNER);
  
  dist = min(width/cols, height/rows);
  
  for (int i = 0; i < cols; i++)
  {
    for (int j = 0; j < rows; j++)
    {
      if (i%2==0)
      // this is done to shift every second line of ellipse vertically 
      {
        newell [i][j] = new ell (i*dist, j*dist, dist-3);
      } else 
      {
        newell [i][j] = new ell (i*dist, j*dist + 10, dist-3);
      }
      }
    }
}

void draw ()
{ 
  drawdot();
  posXY();
}

void drawdot()
{
  for (int i = 0; i < cols; i++)
  {
    for (int j = 0; j < rows; j++)
    {
      newell [i][j].update();
    }
  }
}

void posXY()
{
  // here I try to change X possition for next letter
  for (int x1 = 2; x1 < 30; x1++) //x1 = 2; y1 = 2; fist dot
  {
    for (int y1 = 2; y1 < rows-1; y1++)
    {
      x2 = x1 + 4;
      y2 = y1; 
    }
  }
  
 // drawA(x2, y2); //not work
  
  drawA(2, 2);
}


void drawA(int x, int y)
{
  if (keyPressed)
  {
    // draw 'a' letter
    if (key == 'a')
    {
      newell [x][y+1].c1 = 255;
      newell [x+1][y].c1 = 255;
      newell [x+2][y+1].c1 = 255;
      newell [x+2][y+2].c1 = 255;
      newell [x+2][y+3].c1 = 255;
      newell [x+0][y+3].c1 = 255;
      newell [x+1][y+2].c1 = 255;
      newell [x+1][y+3].c1 = 255;
    }
  }
}

class ell
{
  color c1;
  float x;
  float y;
  float s;
  
  ell (float tempx, float tempy, float dotSise)
  {
    x=tempx;
    y=tempy;
    s=dotSise;
    c1 = #C0CBD1;
  }
  
  void update()
  {
    fill (c1);
    stroke(#C0CBD1);
    ellipse (x, y, s, s);
  }
}