D
D
dimaweyder2019-11-19 23:58:20
C++ / C#
dimaweyder, 2019-11-19 23:58:20

How to traverse a matrix along the diagonals?

5dd4569aa62ce048511411.png
It is necessary to fill the text box in this direction.
Dimensions: 80x24
80 columns, 24 lines

void gotoxy(int x, int y)
{
  COORD coord;
  coord.X = x;
  coord.Y = y;
  SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

int main()
{
  int i = 0, j, k;
  printf("Input start: ");
  scanf_s("Input start: ");
  for (i = 0; i < 80; i++)
  {
    int x = 1;
    int y = 24;
    if (i % 2 == 0) {
      x += i;
      for (j = 0;j <= i;j++) {
        gotoxy(x - j, y - j);
        printf("1");
        Sleep(20);
      }
    }
    else {
      y -= i;
      for (j = 0;j <= i;j++) {
        gotoxy(x + j, y + j);
        printf("0");
        Sleep(20);
        
      }
    }
  }

upd: I made the direction of bypassing the field 80x24 as in the first photo, but with a large value of i , an error appears as in the second photo.
How to fix?
5dda9d2f63163117780506.jpeg

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
vanyamba-electronics, 2019-11-20
@vanyamba-electronics

Write a state machine.

enum { MoveDownRight, MoveUpLeft } state = MoveUpLeft;
while (TRUE) {
   switch (state) {
   case MoveUpLeft:
       move_up_left();
       break;
   case MoveDownRight:
       move_down_right();
       break;
    }
    draw_pixel();
}

A
Alexey Kazakov, 2019-11-21
@gorpsys

Use loops instead of conditions.
More or less like this:

int order = -1;
int x=0;
int y=maxY-1;
while (x<maxX || y>0)
{
     while (x<0 && y<0) {x+=2; y++; order*=-1};
     while (x>=maxX && y>=maxY) {x--; y-=2; order*=-1};
     while (x<0 ) {x++; order*=-1};
     while (y<0 ) {y++; order*=-1};
     while (y>=maxY ) {y--; order*=-1};
     while (x>=maxX ) {x--; order*=-1};

      gotoxy((x+1),(y+1));
                         printf("1");
      x+=order;
      y+=order;
      Sleep(150);
}

A
Anton Fedoryan, 2019-11-25
@AnnTHony

5ddbc5b338f31121226147.gif

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>

const int _DELAY = 300;

void set_cursor_position(int row, int col)
{
    COORD coord;
    coord.X = col;
    coord.Y = row;

    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

void set_left_top(int *start_row, int *start_col, const int finish_row, const int finish_col)
{
    while (true)
    {
        Sleep(_DELAY);

        set_cursor_position(*start_row, *start_col);
        printf("1");

        *start_row = *start_row - 1;
        *start_col = *start_col - 1;

        if (*start_row < finish_row)
        {
            *start_row = *start_row + 1;
            *start_col = *start_col + 2;

            break;
        }

        if (*start_col < finish_col)
        {
            *start_col = *start_col + 1;

            break;
        }
    }
}

void set_right_bottom(int *start_row, int *start_col, const int finish_row, const int finish_col)
{
    while (true)
    {
        Sleep(_DELAY);

        set_cursor_position(*start_row, *start_col);
        printf("0");

        *start_row = *start_row + 1;
        *start_col = *start_col + 1;

        if (*start_col > finish_col)
        {
            *start_row = *start_row - 2;
            *start_col = *start_col - 1;

            break;
        }

        if (*start_row > finish_row)
        {
            *start_row = *start_row - 1;

            break;
        }
    }
}

int main()
{
    printf("*** MATRIX RESOLUTION ***\n\n");

    int rows;
    printf("ROWS: ");
    scanf("%d", &rows);

    int padding_top = rows + 4;
    int padding_left = 2;

    int cols;
    printf("COLS: ");
    scanf("%d", &cols);
    cols = cols + padding_left;

    bool direction = true;

    int start_row = padding_top;
    int start_col = padding_left;

    while (start_row != (padding_top - rows) + 1 || start_col != cols)
    {
        if (direction)
        {
            set_left_top(&start_row, &start_col, (padding_top - rows) + 1, padding_left);
        }
        else
        {
            set_right_bottom(&start_row, &start_col, padding_top, cols - 1);
        }

        direction = !direction;
    }

    padding_top = padding_top + 1;
    set_cursor_position(padding_top, 0);

    return 0;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question