Skip to content
Snippets Groups Projects
Commit a81b5294 authored by Peter Gerwinski's avatar Peter Gerwinski
Browse files

Vorlesung 17.12.2015

parent 105d1deb
Branches
No related tags found
No related merge requests found
Showing
with 649 additions and 0 deletions
#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;
}
File added
#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;
}
File added
#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;
}
File added
#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;
}
File added
#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;
}
File added
#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;
}
File added
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment