Post-Cycle Programming

I am trying to make hexagons. I have a form, but I am doing something wrong while printing. A.

Here is my code and what I tried:

#include <stdio.h> int main() { int i, x; for (i = 1; i <= 2; i++) { for (x = 1; x <= 2; x++) { printf(" %d,%d\t \n", i, x); printf(" / \t \\\n"); printf("| \t |\n"); printf("| \t |\n"); printf(" \\ \t /\n"); } printf("\n\n"); } } 

I need them horizontal, not vertical.

I know what I did wrong, that prints the hexadecimal form "m, n + 1" below the hexagon "m, n", and not on its right side.
But I do not know how to fix it. Thanks!

Fixed!

Update: Now Results

 #include <stdio.h> #define COLS 4 #define ROWS 3 void subrow(char* pcWhat, char* pcEndWhat) { int col; for (col = 0; col < COLS; col++) { printf("%s", pcWhat); } printf("%s\n", pcEndWhat); } void indexedSubrow(char* pcWhat, char* pcEndWhat, int row, int offset) { int col; for (col = 0; col < COLS; col++) { printf(pcWhat, row, col+offset); } printf("%s\n", pcEndWhat); } int main(void) { int i, x; subrow(" ___ ",""); for (i = 0; i < ROWS; i++) { if(i==0){ subrow(" / \\ "," "); indexedSubrow("/ %d,%d \\___"," ", i, 0); subrow("\\ / ","\\"); indexedSubrow(" \\___/ %d,%d"," \\", i, 1); } //else if(i==ROWS-1){} else{ subrow(" / \\ "," /"); indexedSubrow("/ %d,%d \\___","/", i, 0); subrow("\\ / ","\\"); indexedSubrow(" \\___/ %d,%d"," \\", i, 1); } } return 0; } 

More explanations and art of what I need:

 The last subrow is: \ / \ / \ / \ / \ \___/ 2,1 \___/ 2,3 \___/ 2,5 \___/ 2,7 \ Which should be \ / \ / \ / \ / \ \___/ 2,1 \___/ 2,3 \___/ 2,5 \___/ 2,7 \ \ / \ / \ / \ / \___/ \___/ \___/ \___/ So the missing part is: \ / \ / \ / \ / \___/ \___/ \___/ \___/ 

I'm also interested in how to add numbers ("Let them say 1 as the second digit in all hex grids") Example:

Current art:

  ___ ___ ___ ___ / \ / \ / \ / \ / 0,0 \___/ 0,1 \___/ 0,2 \___/ 0,3 \___ \ / \ / \ / \ / \ \___/ 0,1 \___/ 0,2 \___/ 0,3 \___/ 0,4 \ / \ / \ / \ / \ / / 1,0 \___/ 1,1 \___/ 1,2 \___/ 1,3 \___/ \ / \ / \ / \ / \ \___/ 1,1 \___/ 1,2 \___/ 1,3 \___/ 1,4 \ / \ / \ / \ / \ / / 2,0 \___/ 2,1 \___/ 2,2 \___/ 2,3 \___/ \ / \ / \ / \ / \ \___/ 2,1 \___/ 2,2 \___/ 2,3 \___/ 2,4 \ 

Expectation: (overall as an end result)

  ___ ___ ___ ___ / \ / \ / \ / \ / 0,0 \___/ 0,2 \___/ 0,4 \___/ 0,6 \___ \ 1 / \ 1 / \ 1 / \ 1 / \ \___/ 0,1 \___/ 0,3 \___/ 0,5 \___/ 0,7 \ / \ 1 / \ 1 / \ 1 / \ 1 / / 1,0 \___/ 1,2 \___/ 1,4 \___/ 1,6 \___/ \ 1 / \ 1 / \ 1 / \ 1 / \ \___/ 1,1 \___/ 1,3 \___/ 1,5 \___/ 1,7 \ / \ 1 / \ 1 / \ 1 / \ 1 / / 2,0 \___/ 2,2 \___/ 2,4 \___/ 2,6 \___/ \ 1 / \ 1 / \ 1 / \ 1 / \ \___/ 2,1 \___/ 2,3 \___/ 2,5 \___/ 2,7 \ \ 1 / \ 1 / \ 1 / \ 1 / \___/ \___/ \___/ \___/ 

Thanks!

+5
source share
2 answers

You want to make strings of two (possibly larger) hexagons. To do this, you should not print new lines after a substring of one hexadecimal form.
Instead, print the substrings of all (two) hexadecimal shapes, with the corresponding spaces, to align them. For this, you can probably get away from the tabs.
So basically you should go from

  for (x = 1; x <= 2; x++) { printf(" %d,%d\t \n", i, x); printf(" / \t \\\n"); printf("| \t |\n"); printf("| \t |\n"); printf(" \\ \t /\n"); } 

to

  for (i = 1; i <= 2; i++) { for (x = 1; x <= 2; x++) { printf(" %d,%d ", i, x); } printf("\n"); for (x = 1; x <= 2; x++) { printf(" / \\ "); } printf("\n"); for (x = 1; x <= 2; x++) { printf("| |"); } printf("\n"); for (x = 1; x <= 2; x++) { printf("| |"); } printf("\n"); for (x = 1; x <= 2; x++) { printf(" \\ / "); } printf("\n"); } 

Note 1, I did not configure spaces between hexadecimal shapes on the same line. You will have to do it yourself. Thus, the result below may not satisfy you directly, but it should be clear that the main problem has been resolved.

Note 2, even in this case, you get a square grid of hexagons.
If you really need a hex grid of hexagons, you need to reposition | in every second six-line line.

Output:

  1,1 1,2 / \ / \ | || | | || | \ / \ / 2,1 2,2 / \ / \ | || | | || | \ / \ / 

I played trying to make something out of a hexagonal grid, according to Note2. I also suggest trying the ascii art of your pixel image,
in the second version with six grids,
based on Jonathan Leffler’s proposal to rotate hexes 90 degrees.

 #include <stdio.h> #define COLS 4 #define ROWS 3 void subrow(char* pcWhat, char* pcEndWhat) { int col; for (col = 0; col < COLS; col++) { printf("%s", pcWhat); } printf("%s\n", pcEndWhat); } void indexedSubrow(char* pcWhat, char* pcEndWhat, int row, int offset) { int col; for (col = 0; col < COLS; col++) { printf(pcWhat, row, col*2+offset); } printf("%s\n", pcEndWhat); } int main(void) { int i, x; for (i = 1; i <= 2; i++) { for (x = 1; x <= 2; x++) { printf(" %d,%d ", i, x); } printf("\n"); for (x = 1; x <= 2; x++) { printf(" / \\ "); } printf("\n"); for (x = 1; x <= 2; x++) { printf("| |"); } printf("\n"); for (x = 1; x <= 2; x++) { printf("| |"); } printf("\n"); for (x = 1; x <= 2; x++) { printf(" \\ / "); } printf("\n"); } printf("\n"); printf("\n"); printf("\n"); for (i = 0; i < ROWS; i++) { subrow(" / \\ ",""); subrow(" / \\ ",""); subrow(" / \\",""); subrow("| ","|"); subrow("| ","|"); subrow(" \\ /",""); subrow(" \\ / ",""); subrow(" \\ / ",""); subrow(" | ",""); subrow(" | ",""); } subrow(" / \\ ",""); subrow(" / \\ ",""); subrow(" / \\",""); subrow("| ","|"); subrow("| ","|"); subrow(" \\ /",""); subrow(" \\ / ",""); subrow(" \\ / ",""); printf("\n"); printf("\n"); subrow(" ___ ",""); for (i = 0; i < ROWS; i++) { subrow(" / \\ "," /"); indexedSubrow("/ %d,%d \\___","/", i, 0); subrow("\\ / ","\\"); indexedSubrow(" \\___/ %d,%d"," \\", i, 1); } return 0; } 

Display vertical hexes:

  / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ | | | | | | | | | | \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / | | | | | | | | / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ | | | | | | | | | | \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / 

Output horizontal hexes:

  ___ ___ ___ ___ / \ / \ / \ / \ / / 0,0 \___/ 0,2 \___/ 0,4 \___/ 0,6 \___/ \ / \ / \ / \ / \ \___/ 0,1 \___/ 0,3 \___/ 0,5 \___/ 0,7 \ / \ / \ / \ / \ / / 1,0 \___/ 1,2 \___/ 1,4 \___/ 1,6 \___/ \ / \ / \ / \ / \ \___/ 1,1 \___/ 1,3 \___/ 1,5 \___/ 1,7 \ / \ / \ / \ / \ / / 2,0 \___/ 2,2 \___/ 2,4 \___/ 2,6 \___/ \ / \ / \ / \ / \ \___/ 2,1 \___/ 2,3 \___/ 2,5 \___/ 2,7 \ 

I’m sure you won’t like the details around the edges and the position of the numbers,
but it is for you to tune in to your taste.

+6
source

Perhaps try the following:

 #include <stdio.h> int main() { int i, x; for (i = 1; i <= 2; i++) { for (x = 1; x <= 2; x++) { printf(" %d,%d ", i, x); } printf("\n"); for (x = 1; x <= 2; x++) { printf(" / \\ "); } printf("\n"); for (x = 1; x <= 2; x++) { printf("| | "); } printf("\n"); for (x = 1; x <= 2; x++) { printf("| | "); } printf("\n"); for (x = 1; x <= 2; x++) { printf(" \\ / "); } printf("\n"); } } 

Results:

  1,1 1,2 / \ / \ | | | | | | | | \ / \ / 2,1 2,2 / \ / \ | | | | | | | | \ / \ / 

A simple program that I just created using this thing using C:

  0,0 0,1 0,2 / \ / \ / \ | 3 || || 1 | \ / \ / \ / 1,0 1,1 1,2 / \ / \ / \ | 1 || a || 2 | \ / \ / \ / 2,0 2,1 2,2 / \ / \ / \ | 2 || 1 || | \ / \ / \ / 
0
source

Source: https://habr.com/ru/post/1274133/


All Articles