diff --git a/20151217/neat-tree-1.c b/20151217/neat-tree-1.c new file mode 100644 index 0000000000000000000000000000000000000000..7fe6a906ea4088542b792d576be0e98f9aa379ea --- /dev/null +++ b/20151217/neat-tree-1.c @@ -0,0 +1,68 @@ +#include <stdio.h> +#include <stdlib.h> + +int width, height, size; +char *buffer = NULL; +#define MAX_COLOR 255 + +void init (int w, int h) +{ + width = w; + height = h; + size = width * height * 3 * sizeof (char); + buffer = malloc (size); +} + +void neat_background (void) +{ + for (int y = 0; y < height; y++) + for (int x = 0; x < width; x++) + { + char r = x; + char g = 2 * y; + char b = x + y; + char *pixel = buffer + (y * width + x) * 3; + pixel[0] = r; + pixel[1] = g; + pixel[2] = b; + } +} + +void draw_line (int x0, int y0, int x1, int y1, char r, char g, char b) +{ + for (float p = 0.0; p <= 1.0; p += 0.001) + { + int x = x0 + p * (x1 - x0); + int y = y0 + p * (y1 - y0); + char *pixel = buffer + (y * width + x) * 3; + pixel[0] = r; + pixel[1] = g; + pixel[2] = b; + } +} + +void save_as (char *filename) +{ + FILE *f = fopen (filename, "wb"); + fprintf (f, "P6\n"); + fprintf (f, "%d %d\n", width, height); + fprintf (f, "%d\n", MAX_COLOR); + for (int i = 0; i < size; i++) + fprintf (f, "%c", buffer[i]); + fclose (f); +} + +void done (void) +{ + free (buffer); +} + +int main (void) +{ + init (1024, 768); + neat_background (); + draw_line (10, 10, width - 10, height - 10, MAX_COLOR, MAX_COLOR, 0); + save_as ("neat-tree-1.ppm"); + done (); + return 0; +} diff --git a/20151217/neat-tree-1.ppm b/20151217/neat-tree-1.ppm new file mode 100644 index 0000000000000000000000000000000000000000..c09d85738ff6126eecff831055b0d73d4f06c75e Binary files /dev/null and b/20151217/neat-tree-1.ppm differ diff --git a/20151217/neat-tree-2.c b/20151217/neat-tree-2.c new file mode 100644 index 0000000000000000000000000000000000000000..d95bab9bc4d0cbc98472e9099da7295c9cae70c0 --- /dev/null +++ b/20151217/neat-tree-2.c @@ -0,0 +1,77 @@ +#include <stdio.h> +#include <stdlib.h> + +int width, height, size; +char *buffer = NULL; +#define MAX_COLOR 255 + +void init (int w, int h) +{ + width = w; + height = h; + size = width * height * 3 * sizeof (char); + buffer = malloc (size); +} + +void neat_background (void) +{ + for (int y = 0; y < height; y++) + for (int x = 0; x < width; x++) + { + char r = x; + char g = 2 * y; + char b = x + y; + char *pixel = buffer + (y * width + x) * 3; + pixel[0] = r; + pixel[1] = g; + pixel[2] = b; + } +} + +void draw_line (int x0, int y0, int x1, int y1, char r, char g, char b) +{ + for (float p = 0.0; p <= 1.0; p += 0.001) + { + int x = x0 + p * (x1 - x0); + int y = y0 + p * (y1 - y0); + char *pixel = buffer + (y * width + x) * 3; + pixel[0] = r; + pixel[1] = g; + pixel[2] = b; + } +} + +void save_as (char *filename) +{ + FILE *f = fopen (filename, "wb"); + fprintf (f, "P6\n"); + fprintf (f, "%d %d\n", width, height); + fprintf (f, "%d\n", MAX_COLOR); + for (int i = 0; i < size; i++) + fprintf (f, "%c", buffer[i]); + fclose (f); +} + +void done (void) +{ + free (buffer); +} + +void draw_tree (double x0, double y0, double x1, double y1) +{ + int ix0 = width / 2 + width / 4 * x0; + int iy0 = height - height / 3 - height / 3 * y0; + int ix1 = width / 2 + width / 4 * x1; + int iy1 = height - height / 3 - height / 3 * y1; + draw_line (ix0, iy0, ix1, iy1, MAX_COLOR, MAX_COLOR, 0); +} + +int main (void) +{ + init (1024, 768); + neat_background (); + draw_tree (0.0, 0.0, 0.0, 1.0); + save_as ("neat-tree-2.ppm"); + done (); + return 0; +} diff --git a/20151217/neat-tree-2.ppm b/20151217/neat-tree-2.ppm new file mode 100644 index 0000000000000000000000000000000000000000..645194fb803befd7afa4a648e72fdaea82b5dc28 Binary files /dev/null and b/20151217/neat-tree-2.ppm differ diff --git a/20151217/neat-tree-3.c b/20151217/neat-tree-3.c new file mode 100644 index 0000000000000000000000000000000000000000..ec4ed1ca84daa11e81c75b4dbd533939d3876604 --- /dev/null +++ b/20151217/neat-tree-3.c @@ -0,0 +1,77 @@ +#include <stdio.h> +#include <stdlib.h> + +int width, height, size; +char *buffer = NULL; +#define MAX_COLOR 255 + +void init (int w, int h) +{ + width = w; + height = h; + size = width * height * 3 * sizeof (char); + buffer = malloc (size); +} + +void neat_background (void) +{ + for (int y = 0; y < height; y++) + for (int x = 0; x < width; x++) + { + char r = x; + char g = 2 * y; + char b = x + y; + char *pixel = buffer + (y * width + x) * 3; + pixel[0] = r; + pixel[1] = g; + pixel[2] = b; + } +} + +void draw_line (int x0, int y0, int x1, int y1, char r, char g, char b) +{ + for (float p = 0.0; p <= 1.0; p += 0.001) + { + int x = x0 + p * (x1 - x0); + int y = y0 + p * (y1 - y0); + char *pixel = buffer + (y * width + x) * 3; + pixel[0] = r; + pixel[1] = g; + pixel[2] = b; + } +} + +void save_as (char *filename) +{ + FILE *f = fopen (filename, "wb"); + fprintf (f, "P6\n"); + fprintf (f, "%d %d\n", width, height); + fprintf (f, "%d\n", MAX_COLOR); + for (int i = 0; i < size; i++) + fprintf (f, "%c", buffer[i]); + fclose (f); +} + +void done (void) +{ + free (buffer); +} + +void draw_tree (double x0, double y0, double x1, double x2, double y2) +{ + int ix0 = width / 2 + width / 4 * x0; + int iy0 = height - height / 3 - height / 3 * y0; + int ix2 = width / 2 + width / 4 * x2; + int iy2 = height - height / 3 - height / 3 * y2; + draw_line (ix0, iy0, ix2, iy2, MAX_COLOR, MAX_COLOR, 0); +} + +int main (void) +{ + init (1024, 768); + neat_background (); + draw_tree (-0.3, 0.0, 0.3, 0.0, 1.0); + save_as ("neat-tree-3.ppm"); + done (); + return 0; +} diff --git a/20151217/neat-tree-3.ppm b/20151217/neat-tree-3.ppm new file mode 100644 index 0000000000000000000000000000000000000000..856a1bfdb693698d52aeba2f2e2e00bbb1ad8f92 Binary files /dev/null and b/20151217/neat-tree-3.ppm differ diff --git a/20151217/neat-tree-4.c b/20151217/neat-tree-4.c new file mode 100644 index 0000000000000000000000000000000000000000..8413ea90b8ed2dd60fe6b762d9626effa2b38fb0 --- /dev/null +++ b/20151217/neat-tree-4.c @@ -0,0 +1,83 @@ +#include <stdio.h> +#include <stdlib.h> + +int width, height, size; +char *buffer = NULL; +#define MAX_COLOR 255 + +void init (int w, int h) +{ + width = w; + height = h; + size = width * height * 3 * sizeof (char); + buffer = malloc (size); +} + +void neat_background (void) +{ + for (int y = 0; y < height; y++) + for (int x = 0; x < width; x++) + { + char r = x; + char g = 2 * y; + char b = x + y; + char *pixel = buffer + (y * width + x) * 3; + pixel[0] = r; + pixel[1] = g; + pixel[2] = b; + } +} + +void draw_line (int x0, int y0, int x1, int y1, char r, char g, char b) +{ + for (float p = 0.0; p <= 1.0; p += 0.001) + { + int x = x0 + p * (x1 - x0); + int y = y0 + p * (y1 - y0); + char *pixel = buffer + (y * width + x) * 3; + pixel[0] = r; + pixel[1] = g; + pixel[2] = b; + } +} + +void save_as (char *filename) +{ + FILE *f = fopen (filename, "wb"); + fprintf (f, "P6\n"); + fprintf (f, "%d %d\n", width, height); + fprintf (f, "%d\n", MAX_COLOR); + for (int i = 0; i < size; i++) + fprintf (f, "%c", buffer[i]); + fclose (f); +} + +void done (void) +{ + free (buffer); +} + +void draw_tree (double x0, double y0, double x1, double x2, double y2) +{ + int ix0 = width / 2 + width / 4 * x0; + int iy0 = height - height / 3 - height / 3 * y0; + int ix1 = width / 2 + width / 4 * x1; + int ix2 = width / 2 + width / 4 * x2; + int iy2 = height - height / 3 - height / 3 * y2; + int ix = ix0; + while (ix < ix1) + { + draw_line (ix, iy0, ix2, iy2, MAX_COLOR, MAX_COLOR, 0); + ix += 2; + } +} + +int main (void) +{ + init (1024, 768); + neat_background (); + draw_tree (-0.3, 0.0, 0.3, 0.0, 1.0); + save_as ("neat-tree-4.ppm"); + done (); + return 0; +} diff --git a/20151217/neat-tree-4.ppm b/20151217/neat-tree-4.ppm new file mode 100644 index 0000000000000000000000000000000000000000..6b8bf6b132aeb75201058058db4ffa4dca7db748 Binary files /dev/null and b/20151217/neat-tree-4.ppm differ diff --git a/20151217/neat-tree-5.c b/20151217/neat-tree-5.c new file mode 100644 index 0000000000000000000000000000000000000000..7ab55ad8f266493de4f4919a1fc120bb0b6a5e11 --- /dev/null +++ b/20151217/neat-tree-5.c @@ -0,0 +1,84 @@ +#include <stdio.h> +#include <stdlib.h> + +int width, height, size; +char *buffer = NULL; +#define MAX_COLOR 255 + +void init (int w, int h) +{ + width = w; + height = h; + size = width * height * 3 * sizeof (char); + buffer = malloc (size); +} + +void neat_background (void) +{ + for (int y = 0; y < height; y++) + for (int x = 0; x < width; x++) + { + char r = x; + char g = 2 * y; + char b = x + y; + char *pixel = buffer + (y * width + x) * 3; + pixel[0] = r; + pixel[1] = g; + pixel[2] = b; + } +} + +void draw_line (int x0, int y0, int x1, int y1, char r, char g, char b) +{ + for (float p = 0.0; p <= 1.0; p += 0.001) + { + int x = x0 + p * (x1 - x0); + int y = y0 + p * (y1 - y0); + char *pixel = buffer + (y * width + x) * 3; + pixel[0] = r; + pixel[1] = g; + pixel[2] = b; + } +} + +void save_as (char *filename) +{ + FILE *f = fopen (filename, "wb"); + fprintf (f, "P6\n"); + fprintf (f, "%d %d\n", width, height); + fprintf (f, "%d\n", MAX_COLOR); + for (int i = 0; i < size; i++) + fprintf (f, "%c", buffer[i]); + fclose (f); +} + +void done (void) +{ + free (buffer); +} + +void draw_tree (int x0, int y0, int x1, int x2, int y2) +{ + int x = x0; + while (x < x1) + { + draw_line (x, y0, x2, y2, 0, MAX_COLOR, 0); + x += 2; + } +} + +int main (void) +{ + init (1024, 768); + neat_background (); + for (int i = 0; i < 10; i++) + { + int x = rand () % (width / 2) + width / 4; + int y = rand () % (height / 2) + height / 4; + int size = rand () % (height / 2); + draw_tree (x - size / 5, y + size / 2, x + size / 5, x, y - size / 2); + } + save_as ("neat-tree-5.ppm"); + done (); + return 0; +} diff --git a/20151217/neat-tree-5.ppm b/20151217/neat-tree-5.ppm new file mode 100644 index 0000000000000000000000000000000000000000..0c974913518a5a2c150463389c9fab57334c2c46 Binary files /dev/null and b/20151217/neat-tree-5.ppm differ diff --git a/20151217/neat-tree-6.c b/20151217/neat-tree-6.c new file mode 100644 index 0000000000000000000000000000000000000000..21b59407e7894773ed35012f439158222827a246 --- /dev/null +++ b/20151217/neat-tree-6.c @@ -0,0 +1,85 @@ +#include <stdio.h> +#include <stdlib.h> +#include <stdint.h> + +int width, height, size; +uint8_t *buffer = NULL; +#define MAX_COLOR 255 + +void init (int w, int h) +{ + width = w; + height = h; + size = width * height * 3 * sizeof (uint8_t); + buffer = malloc (size); +} + +void neat_background (void) +{ + for (int y = 0; y < height; y++) + for (int x = 0; x < width; x++) + { + uint8_t r = x; + uint8_t g = 2 * y; + uint8_t b = x + y; + uint8_t *pixel = buffer + (y * width + x) * 3; + pixel[0] = r / 3; + pixel[1] = g / 3; + pixel[2] = b / 3; + } +} + +void draw_line (int x0, int y0, int x1, int y1, uint8_t r, uint8_t g, uint8_t b) +{ + for (float p = 0.0; p <= 1.0; p += 0.001) + { + int x = x0 + p * (x1 - x0); + int y = y0 + p * (y1 - y0); + uint8_t *pixel = buffer + (y * width + x) * 3; + pixel[0] = r; + pixel[1] = g; + pixel[2] = b; + } +} + +void save_as (char *filename) +{ + FILE *f = fopen (filename, "wb"); + fprintf (f, "P6\n"); + fprintf (f, "%d %d\n", width, height); + fprintf (f, "%d\n", MAX_COLOR); + for (int i = 0; i < size; i++) + fprintf (f, "%c", buffer[i]); + fclose (f); +} + +void done (void) +{ + free (buffer); +} + +void draw_tree (int x0, int y0, int x1, int x2, int y2) +{ + int x = x0; + while (x < x1) + { + draw_line (x, y0, x2, y2, 0, MAX_COLOR, 0); + x += 2; + } +} + +int main (void) +{ + init (1024, 768); + neat_background (); + for (int i = 0; i < 30; i++) + { + int x = rand () % (width / 2) + width / 4; + int y = rand () % (height / 2) + height / 4; + int size = rand () % (height / 2); + draw_tree (x - size / 5, y + size / 2, x + size / 5, x, y - size / 2); + } + save_as ("neat-tree-6.ppm"); + done (); + return 0; +} diff --git a/20151217/neat-tree-6.ppm b/20151217/neat-tree-6.ppm new file mode 100644 index 0000000000000000000000000000000000000000..074750ad8e14fcb86faacd828f92c7d98663b389 Binary files /dev/null and b/20151217/neat-tree-6.ppm differ diff --git a/20151217/sort-0.c b/20151217/sort-0.c new file mode 100644 index 0000000000000000000000000000000000000000..43814eb3ff763bb05e82cde70014d7a02ad4727a --- /dev/null +++ b/20151217/sort-0.c @@ -0,0 +1,16 @@ +#include <stdio.h> + +char *find_first (char **name) +{ + return "Anna"; +} + +int main (void) +{ + char *name[] = { "Otto", "Lisa", "Anna", "Heinrich", "Siegfried", "Peter", + "Dieter", "Hugo", "Berta", "Maria", "Fritz", "Box", "Hans", + "Thomas", "Ulrich", "Zacharias", NULL }; + char *first = find_first (name); + printf ("%s\n", first); + return 0; +} diff --git a/20151217/sort-1.c b/20151217/sort-1.c new file mode 100644 index 0000000000000000000000000000000000000000..7a0137aaedb47c1255bb1940822ac6c2ba463dca --- /dev/null +++ b/20151217/sort-1.c @@ -0,0 +1,28 @@ +#include <stdio.h> + +int strcmp (char *a, char *b) +{ + int i = 0; + while (a[i] && b[i]) + { + if (a[i] < b[i]) + return +1; + else if (a[i] > b[i]) + return -1; + i++; + } + if (a[i]) + return +1; + else if (b[i]) + return -1; + else + return 0; +} + +int main (void) +{ + printf ("%d\n", strcmp ("Anton", "Zacharias")); + printf ("%d\n", strcmp ("Müller", "Meier")); + printf ("%d\n", strcmp ("Jan", "Jana")); + return 0; +} diff --git a/20151217/sort-2.c b/20151217/sort-2.c new file mode 100644 index 0000000000000000000000000000000000000000..6b987f39a845a0280bb06db6bf9f778e03c5b07a --- /dev/null +++ b/20151217/sort-2.c @@ -0,0 +1,10 @@ +#include <stdio.h> +#include <string.h> + +int main (void) +{ + printf ("%d\n", strcmp ("Anton", "Zacharias")); + printf ("%d\n", strcmp ("Müller", "Meier")); + printf ("%d\n", strcmp ("Jan", "Jana")); + return 0; +} diff --git a/20151217/sort-3.c b/20151217/sort-3.c new file mode 100644 index 0000000000000000000000000000000000000000..a69f326bee5338373bb34326017f75e4a2820e03 --- /dev/null +++ b/20151217/sort-3.c @@ -0,0 +1,17 @@ +#include <stdio.h> +#include <string.h> + +int find_first (char **name) +{ + return 2; +} + +int main (void) +{ + char *name[] = { "Otto", "Lisa", "Anna", "Heinrich", "Siegfried", "Peter", + "Dieter", "Hugo", "Berta", "Maria", "Fritz", "Box", "Hans", + "Thomas", "Ulrich", "Zacharias", NULL }; + int first = find_first (name); + printf ("%s\n", name[first]); + return 0; +} diff --git a/20151217/sort-4.c b/20151217/sort-4.c new file mode 100644 index 0000000000000000000000000000000000000000..d98f9032ec358084477ca6601133f37f4679c4a4 --- /dev/null +++ b/20151217/sort-4.c @@ -0,0 +1,26 @@ +#include <stdio.h> +#include <string.h> + +int find_first (char **name) +{ + int min = 0; + for (int i = 1; name[i]; i++) + { + int comparison = strcmp (name[i - 1], name[i]); + if (comparison < 0) + min = i - 1; + else + min = i; + } + return min; +} + +int main (void) +{ + char *name[] = { "Otto", "Lisa", "Anna", "Heinrich", "Siegfried", "Peter", + "Dieter", "Hugo", "Berta", "Maria", "Fritz", "Box", "Hans", + "Thomas", "Ulrich", "Zacharias", NULL }; + int first = find_first (name); + printf ("%s\n", name[first]); + return 0; +} diff --git a/20151217/sort-5.c b/20151217/sort-5.c new file mode 100644 index 0000000000000000000000000000000000000000..75c91c71810bc49e7f685f4f4da4315f6d7751f5 --- /dev/null +++ b/20151217/sort-5.c @@ -0,0 +1,24 @@ +#include <stdio.h> +#include <string.h> + +int find_first (char **name) +{ + int min = 0; + for (int i = 1; name[i]; i++) + { + int comparison = strcmp (name[min], name[i]); + if (comparison > 0) + min = i; + } + return min; +} + +int main (void) +{ + char *name[] = { "Otto", "Lisa", "Anna", "Heinrich", "Siegfried", "Peter", + "Dieter", "Hugo", "Berta", "Maria", "Fritz", "Box", "Hans", + "Thomas", "Ulrich", "Zacharias", NULL }; + int first = find_first (name); + printf ("%s\n", name[first]); + return 0; +} diff --git a/20151217/sort-6.c b/20151217/sort-6.c new file mode 100644 index 0000000000000000000000000000000000000000..5ea803c6839e99f0d692ae5c258e329e9e49beef --- /dev/null +++ b/20151217/sort-6.c @@ -0,0 +1,24 @@ +#include <stdio.h> +#include <string.h> + +void sort (char **name) +{ + int min = 0; + for (int i = 1; name[i]; i++) + { + int comparison = strcmp (name[min], name[i]); + if (comparison > 0) + min = i; + } +} + +int main (void) +{ + char *name[] = { "Otto", "Lisa", "Anna", "Heinrich", "Siegfried", "Peter", + "Dieter", "Hugo", "Berta", "Maria", "Fritz", "Box", "Hans", + "Thomas", "Ulrich", "Zacharias", NULL }; + sort (name); + for (int i = 0; name[i]; i++) + printf ("%s\n", name[i]); + return 0; +} diff --git a/20151217/sort-7.c b/20151217/sort-7.c new file mode 100644 index 0000000000000000000000000000000000000000..f7066c555c7849104521c035831fefdbc7768b6c --- /dev/null +++ b/20151217/sort-7.c @@ -0,0 +1,30 @@ +#include <stdio.h> +#include <string.h> + +void sort (char **name) +{ + int min = 0; + for (int i = 1; name[i]; i++) + { + int comparison = strcmp (name[min], name[i]); + if (comparison > 0) + min = i; + } + if (min != 0) + { + char *temp = name[0]; + name[0] = name[min]; + name[min] = temp; + } +} + +int main (void) +{ + char *name[] = { "Otto", "Lisa", "Anna", "Heinrich", "Siegfried", "Peter", + "Dieter", "Hugo", "Berta", "Maria", "Fritz", "Box", "Hans", + "Thomas", "Ulrich", "Zacharias", NULL }; + sort (name); + for (int i = 0; name[i]; i++) + printf ("%s\n", name[i]); + return 0; +} diff --git a/20151217/sort-8.c b/20151217/sort-8.c new file mode 100644 index 0000000000000000000000000000000000000000..9cf4a6e2a11b025e762563ec570ae9240de70626 --- /dev/null +++ b/20151217/sort-8.c @@ -0,0 +1,35 @@ +#include <stdio.h> +#include <string.h> + +void sort (char **name) +{ + int start = 0; + while (name[start]) + { + int min = start; + for (int i = start + 1; name[i]; i++) + { + int comparison = strcmp (name[min], name[i]); + if (comparison > 0) + min = i; + } + if (min != start) + { + char *temp = name[start]; + name[start] = name[min]; + name[min] = temp; + } + start++; + } +} + +int main (void) +{ + char *name[] = { "Otto", "Lisa", "Anna", "Heinrich", "Siegfried", "Peter", + "Dieter", "Hugo", "Berta", "Maria", "Fritz", "Box", "Hans", + "Thomas", "Ulrich", "Zacharias", NULL }; + sort (name); + for (int i = 0; name[i]; i++) + printf ("%s\n", name[i]); + return 0; +}