use structs
This commit is contained in:
1 file changed
+48
-68
+48
-68
@@ -7,41 +7,46 @@
|
|||||||
|
|
||||||
// Absolutely no array notation anywhere
|
// Absolutely no array notation anywhere
|
||||||
|
|
||||||
|
// A01385066
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
int x;
|
||||||
|
int y;
|
||||||
|
} vector;
|
||||||
|
|
||||||
void drawLine(
|
void drawLine(
|
||||||
int x1,
|
vector start,
|
||||||
int y1,
|
vector end,
|
||||||
int x2,
|
|
||||||
int y2,
|
|
||||||
char **matrix,
|
char **matrix,
|
||||||
int h
|
int h
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
int x, y;
|
vector new;
|
||||||
|
|
||||||
x = x1;
|
new.x = start.x;
|
||||||
y = y1;
|
new.y = start.y;
|
||||||
|
|
||||||
while (x != x2 || y != y2)
|
while (new.x != end.x || new.y != end.y)
|
||||||
{
|
{
|
||||||
if (x2 > x) {
|
*(*(matrix + new.x) + (h - 1 - new.y)) = '*';
|
||||||
x++;
|
|
||||||
} else if (x2 < x) {
|
if (end.x > new.x) {
|
||||||
x--;
|
new.x++;
|
||||||
|
} else if (end.x < new.x) {
|
||||||
|
new.x--;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (y2 > y) {
|
if (end.y > new.y) {
|
||||||
y++;
|
new.y++;
|
||||||
} else if (y2 < y) {
|
} else if (end.y < new.y) {
|
||||||
y--;
|
new.y--;
|
||||||
}
|
}
|
||||||
|
|
||||||
*(*(matrix + x) + (h - 1 - y)) = '*';
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void drawShape(
|
void drawShape(
|
||||||
char *outputPath,
|
char *outputPath,
|
||||||
int **coords,
|
vector *coords,
|
||||||
int numCoords,
|
int numCoords,
|
||||||
int width,
|
int width,
|
||||||
int height
|
int height
|
||||||
@@ -94,45 +99,41 @@ void drawShape(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Plot coordinates in matrix
|
// draw lines between each coordinate
|
||||||
for (int i = 0; i < numCoords; i++)
|
for (int i = 0; i < numCoords; i++)
|
||||||
{
|
{
|
||||||
int x = *(*(coords + i));
|
vector start = *(coords + i);
|
||||||
int y = *(*(coords + i) + 1);
|
|
||||||
*(*(matrix + x) + (height - 1 - y)) = '*';
|
|
||||||
|
|
||||||
// draw line between coordinates
|
|
||||||
if (i < numCoords - 1)
|
if (i < numCoords - 1)
|
||||||
{
|
{
|
||||||
int nextX = *(*(coords + i + 1));
|
vector end = *(coords + i + 1);
|
||||||
int nextY = *(*(coords + i + 1) + 1);
|
|
||||||
|
|
||||||
drawLine(x, y, nextX, nextY, matrix, height);
|
drawLine(start, end, matrix, height);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check for spaces in matrix surrounded by coords
|
// Check for spaces in matrix surrounded by coords
|
||||||
for (int y = 0; y < height; y++)
|
for (int y = 0; y < height; y++)
|
||||||
{
|
{
|
||||||
int endX;
|
|
||||||
for (int x = 0; x < width; x++)
|
for (int x = 0; x < width; x++)
|
||||||
{
|
{
|
||||||
if (*(*(matrix + x) + y) == '*')
|
if (*(*(matrix + x) + y) == '*')
|
||||||
{
|
{
|
||||||
|
int endX;
|
||||||
for (int i = x; i < width; i++)
|
for (int i = x; i < width; i++)
|
||||||
{
|
{
|
||||||
if (*(*(matrix + i) + y) == '*') {
|
if (*(*(matrix + i) + y) == '*') endX = i;
|
||||||
endX = i;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
drawLine(x, y, endX, y, matrix, y * 2 + 1);
|
if (endX == x) continue;
|
||||||
}
|
|
||||||
|
vector start = {x, y};
|
||||||
|
vector end = {endX, y};
|
||||||
|
drawLine(start, end, matrix, y * 2 + 1);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
// Clear spaces
|
|
||||||
|
|
||||||
// matrix to output file
|
// matrix to output file
|
||||||
for (int y = 0; y < height; y++)
|
for (int y = 0; y < height; y++)
|
||||||
@@ -188,12 +189,12 @@ int main(int argc, char **argv)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int **coords;
|
vector *coords;
|
||||||
int numCoords;
|
int numCoords;
|
||||||
int width, height;
|
int width, height;
|
||||||
|
|
||||||
numCoords = 0;
|
numCoords = 0;
|
||||||
coords = malloc(MAX_COORDS * sizeof(int*));
|
coords = malloc(MAX_COORDS * sizeof(vector));
|
||||||
|
|
||||||
if (coords == NULL)
|
if (coords == NULL)
|
||||||
{
|
{
|
||||||
@@ -201,46 +202,24 @@ int main(int argc, char **argv)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < MAX_COORDS; i++)
|
|
||||||
{
|
|
||||||
*(coords + i) = malloc(COORD_2D * sizeof(int));
|
|
||||||
|
|
||||||
if (*(coords + i) == NULL)
|
|
||||||
{
|
|
||||||
perror("Failed to malloc coords entry\n");
|
|
||||||
|
|
||||||
for (int j = 0; j < i; j++)
|
|
||||||
{
|
|
||||||
free(*(coords + j));
|
|
||||||
}
|
|
||||||
free(coords);
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
while (numCoords < MAX_COORDS)
|
while (numCoords < MAX_COORDS)
|
||||||
{
|
{
|
||||||
int x, y;
|
vector coord;
|
||||||
|
|
||||||
if (fscanf(inputFile, "%d,%d", &x, &y) == 2)
|
if (fscanf(inputFile, "%d,%d", &coord.x, &coord.y) == 2)
|
||||||
{
|
{
|
||||||
if (x > width) width = x;
|
if (coord.x > width) width = coord.x;
|
||||||
if (y > height) height = y;
|
if (coord.y > height) height = coord.y;
|
||||||
|
|
||||||
*(*(coords + numCoords)) = x;
|
*(coords + numCoords) = coord;
|
||||||
*(*(coords + numCoords) + 1) = y;
|
|
||||||
numCoords++;
|
numCoords++;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (fscanf(inputFile, "%d", &x) == 0 ||
|
if (fscanf(inputFile, "%d", &coord.x) == 0 ||
|
||||||
fscanf(inputFile, "%d", &x) == 0)
|
fscanf(inputFile, "%d", &coord.x) == 0)
|
||||||
{
|
{
|
||||||
// Free and close file before exiting
|
// Free and close file before exiting
|
||||||
for (int i = 0; i < MAX_COORDS; i++) {
|
|
||||||
free(coords[i]);
|
|
||||||
}
|
|
||||||
free(coords);
|
free(coords);
|
||||||
fclose(inputFile);
|
fclose(inputFile);
|
||||||
|
|
||||||
@@ -251,9 +230,8 @@ int main(int argc, char **argv)
|
|||||||
char c;
|
char c;
|
||||||
if (fscanf(inputFile, "%c", &c) == 1 && c == 'E') {
|
if (fscanf(inputFile, "%c", &c) == 1 && c == 'E') {
|
||||||
break;
|
break;
|
||||||
} else {
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -267,5 +245,7 @@ int main(int argc, char **argv)
|
|||||||
++height
|
++height
|
||||||
);
|
);
|
||||||
|
|
||||||
|
free(coords);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user