-
Notifications
You must be signed in to change notification settings - Fork 0
/
CreateDrawing.c
174 lines (147 loc) · 5.54 KB
/
CreateDrawing.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Struct.h"
#include "Initializer.h"
#include "InternalLogic.h"
extern drawing *head;
int getNumber(int maxShapeSize) {
char *stringSize = getUserInput();
if (!stringSize) {
printf("<<< Error, illegal input was detected or memory issue.\r\n");
return 0;
}
int shapeSize = atoi(stringSize);
free(stringSize);
if (shapeSize <= 0) {
printf("<<< Error, wrong input or value supplied equal or lower from zero.\r\n");
return 0;
}
else if (shapeSize > maxShapeSize) {
printf("<<< Error, value entered is bigger than allowed, please read guide before continuing.\r\n");
return 0;
}
return shapeSize;
}
int getFlag() {
int charFlag = 0;
char *fillFlag = getUserInput();
if (!fillFlag) {
printf("<<< Error, memory issue.\r\n");
charFlag = 2;
}
else if (!strcmp(fillFlag, "regular"))
charFlag = 1;
else if (!strcmp(fillFlag, "invert"))
charFlag = 0;
else {
printf("<<< Can\'t create square with the flag input given, use \"regular\" or \"invert\" only.\r\n");
charFlag = 2;
}
free(fillFlag);
return charFlag;
}
int validateInput(int size, int drawChar) {
if (size == 0) {
printf("<<< Can\'t create shape with this size input.\r\n");
return 1;
}
else if (drawChar > 1 || drawChar < 0) {
printf("<<< Can\'t create shape with drawing option input.\r\n");
return 1;
}
return 0;
}
drawing *header(drawing *temp, int shapeSize, int drawFillFlag) {
int rowSpaces = rowSize - shapeSize;
for (unsigned int i = 0; i < rowSpaces / 2; i++) {
for (unsigned int j = 0; j < columnSize; j++)
temp->rowData[j] = drawFillFlag ? blankChar : filledChar;
temp = temp->next;
}
return temp;
}
drawing *squareBody(drawing *temp, int shapeSize, int drawFillFlag) {
unsigned int sidesIndex = 0;
int columnSpaces = columnSize - shapeSize;
for (unsigned int i = 0; i < shapeSize; i++) {
for (sidesIndex = 0; sidesIndex < columnSpaces / 2; sidesIndex++)
temp->rowData[sidesIndex] = drawFillFlag ? blankChar : filledChar;
for (unsigned short bodyIndex = 0; bodyIndex < shapeSize; bodyIndex++)
temp->rowData[bodyIndex + sidesIndex] = drawFillFlag ? filledChar : blankChar;
sidesIndex += shapeSize;
for (; sidesIndex < columnSize; sidesIndex++)
temp->rowData[sidesIndex] = drawFillFlag ? blankChar : filledChar;
temp = temp->next;
}
return temp;
}
void footer(drawing *temp, int shapeSize, int drawFillFlag) {
while (temp) {
for (unsigned int i = 0; i < columnSize; i++)
temp->rowData[i] = drawFillFlag ? blankChar : filledChar;
temp = temp->next;
}
}
void startCreatingSquare() {
drawing *temp = head;
int shapeSize = getNumber(rowSize < columnSize ? rowSize - 2 : columnSize - 2);
int drawingFillFlag = getFlag();
if (validateInput(shapeSize, drawingFillFlag))
return;
temp = header(temp, shapeSize, drawingFillFlag);
temp = squareBody(temp, shapeSize, drawingFillFlag);
footer(temp, shapeSize, drawingFillFlag);
printDrawing();
}
drawing *triangleBody(drawing *temp, int shapeSize, int drawFillFlag) {
unsigned int sidesIndex = 0;
for (int i = 1; i <= shapeSize; i++) {
for (sidesIndex = 0; sidesIndex < columnSize / 2 - i; sidesIndex++)
temp->rowData[sidesIndex] = drawFillFlag ? blankChar : filledChar;
for (unsigned int bodyIndex = 0; bodyIndex < 2 * i - 1; bodyIndex++)
temp->rowData[bodyIndex + sidesIndex] = drawFillFlag ? filledChar : blankChar;
sidesIndex += 2 * i - 1;
for (; sidesIndex < columnSize; sidesIndex++)
temp->rowData[sidesIndex] = drawFillFlag ? blankChar : filledChar;
temp = temp->next;
}
return temp;
}
void startCreatingTriangle() {
drawing *temp = head;
int shapeSize = getNumber(rowSize < columnSize ? rowSize / 2 - 1 : columnSize / 2 - 1);
int drawingFillFlag = getFlag();
if (validateInput(shapeSize, drawingFillFlag))
return;
temp = header(temp, shapeSize, drawingFillFlag);
temp = triangleBody(temp, shapeSize, drawingFillFlag);
footer(temp, shapeSize, drawingFillFlag);
printDrawing();
}
drawing *reverseTriangleBody(drawing *temp, int shapeSize, int drawFillFlag) {
unsigned int sidesIndex = 0;
for (int i = shapeSize; i >= 1; i--) {
for (sidesIndex = 0; sidesIndex < columnSize / 2 - i; sidesIndex++)
temp->rowData[sidesIndex] = drawFillFlag ? blankChar : filledChar;
for (unsigned int bodyIndex = 0; bodyIndex < 2 * i - 1; bodyIndex++)
temp->rowData[bodyIndex + sidesIndex] = drawFillFlag ? filledChar : blankChar;
sidesIndex += 2 * i - 1;
for (; sidesIndex < columnSize; sidesIndex++)
temp->rowData[sidesIndex] = drawFillFlag ? blankChar : filledChar;
temp = temp->next;
}
return temp;
}
void startCreatingRhombus() {
drawing *temp = head;
int shapeSize = getNumber(rowSize < columnSize ? rowSize / 2 - 1 : columnSize / 2 - 1);
int drawingFillFlag = getFlag();
if (validateInput(shapeSize, drawingFillFlag))
return;
temp = header(temp, shapeSize * 2, drawingFillFlag);
temp = triangleBody(temp, shapeSize, drawingFillFlag);
temp = reverseTriangleBody(temp, shapeSize, drawingFillFlag);
footer(temp, shapeSize, drawingFillFlag);
printDrawing();
}