diff --git a/20191205/aufgabe-1.c b/20191205/aufgabe-1.c new file mode 100644 index 0000000000000000000000000000000000000000..f5e5c52a701ead8b9ab933b7c56616c84a192fc3 --- /dev/null +++ b/20191205/aufgabe-1.c @@ -0,0 +1,16 @@ +#include <stdio.h> +#include <string.h> + +void delete_char_from_string (char *s, int pos) +{ + for (int i = strlen (s) - 1; i > pos; i--) + s[i - 1] = s[i]; +} + +int main (void) +{ + char test[] = "Hochschule Boochum"; + delete_char_from_string (test, 12); + printf ("%s\n", test); + return 0; +} diff --git a/20191205/aufgabe-2.c b/20191205/aufgabe-2.c new file mode 100644 index 0000000000000000000000000000000000000000..2bcd5e63a71f212b7243d1f7d9c92406d1e41e77 --- /dev/null +++ b/20191205/aufgabe-2.c @@ -0,0 +1,10 @@ +#include <stdio.h> + +int main (void) +{ + char buffer[100]; + fgets (buffer, 100, stdin); + for (char *p = buffer; *p; p++) + printf ("%02x", *p); + printf ("\n"); +} diff --git a/20191205/gtk-1.c b/20191205/gtk-1.c new file mode 100644 index 0000000000000000000000000000000000000000..884a14ea528431e54f8391c801958a6e494dfe43 --- /dev/null +++ b/20191205/gtk-1.c @@ -0,0 +1,11 @@ +#include <gtk/gtk.h> + +int main (int argc, char **argv) +{ + gtk_init (&argc, &argv); + GtkWidget *window = gtk_window_new (GTK_WINDOW_TOPLEVEL); + gtk_window_set_title (GTK_WINDOW (window), "Hello"); + gtk_widget_show (window); + gtk_main (); + return 0; +} diff --git a/20191205/gtk-10.c b/20191205/gtk-10.c new file mode 100644 index 0000000000000000000000000000000000000000..928f80f9331b86af1e431385e32691dbd1591b60 --- /dev/null +++ b/20191205/gtk-10.c @@ -0,0 +1,53 @@ +#include <gtk/gtk.h> + +#define WIDTH 320 +#define HEIGHT 240 + +double t = 0.0; + +double x = 10; +double y = 200; +double vx = 20; +double vy = -60; +double g = 9.81; + +gboolean draw (GtkWidget *widget, cairo_t *c, gpointer data) +{ + GdkRGBA blue = { 0.0, 0.5, 1.0, 1.0 }; + + gdk_cairo_set_source_rgba (c, &blue); + cairo_rectangle (c, x + vx * t, y + vy * t + 0.5 * g * t * t, 10, 10); + cairo_fill (c); + + return FALSE; /* TRUE to stop other handlers from being invoked for the event. + FALSE to propagate the event further. */ +} + +gboolean timer (GtkWidget *widget) +{ + t += 0.05; + gtk_widget_queue_draw_area (widget, 0, 0, WIDTH, HEIGHT); + g_timeout_add (50, (GSourceFunc) timer, widget); + return FALSE; +} + +int main (int argc, char **argv) +{ + gtk_init (&argc, &argv); + + GtkWidget *window = gtk_window_new (GTK_WINDOW_TOPLEVEL); + gtk_widget_show (window); + gtk_window_set_title (GTK_WINDOW (window), "Hello"); + g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL); + + GtkWidget *drawing_area = gtk_drawing_area_new (); + gtk_widget_show (drawing_area); + gtk_container_add (GTK_CONTAINER (window), drawing_area); + gtk_widget_set_size_request (drawing_area, WIDTH, HEIGHT); + g_signal_connect (drawing_area, "draw", G_CALLBACK (draw), NULL); + + g_timeout_add (50, (GSourceFunc) timer, drawing_area); + + gtk_main (); + return 0; +} diff --git a/20191205/gtk-11.c b/20191205/gtk-11.c new file mode 100644 index 0000000000000000000000000000000000000000..303627b49953e1d37761213505b48fb7a0ff2c02 --- /dev/null +++ b/20191205/gtk-11.c @@ -0,0 +1,57 @@ +#include <gtk/gtk.h> + +#define WIDTH 320 +#define HEIGHT 240 + +double t = 0.0; +double dt = 0.05; + +double x = 10; +double y = 200; +double vx = 20; +double vy = -60; +double g = 9.81; + +gboolean draw (GtkWidget *widget, cairo_t *c, gpointer data) +{ + GdkRGBA blue = { 0.0, 0.5, 1.0, 1.0 }; + + gdk_cairo_set_source_rgba (c, &blue); + cairo_rectangle (c, x, y, 10, 10); + cairo_fill (c); + + return FALSE; /* TRUE to stop other handlers from being invoked for the event. + FALSE to propagate the event further. */ +} + +gboolean timer (GtkWidget *widget) +{ + t += dt; + x += vx * dt; + y += vy * dt; + vy += g * dt; + gtk_widget_queue_draw_area (widget, 0, 0, WIDTH, HEIGHT); + g_timeout_add (50, (GSourceFunc) timer, widget); + return FALSE; +} + +int main (int argc, char **argv) +{ + gtk_init (&argc, &argv); + + GtkWidget *window = gtk_window_new (GTK_WINDOW_TOPLEVEL); + gtk_widget_show (window); + gtk_window_set_title (GTK_WINDOW (window), "Hello"); + g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL); + + GtkWidget *drawing_area = gtk_drawing_area_new (); + gtk_widget_show (drawing_area); + gtk_container_add (GTK_CONTAINER (window), drawing_area); + gtk_widget_set_size_request (drawing_area, WIDTH, HEIGHT); + g_signal_connect (drawing_area, "draw", G_CALLBACK (draw), NULL); + + g_timeout_add (50, (GSourceFunc) timer, drawing_area); + + gtk_main (); + return 0; +} diff --git a/20191205/gtk-12.c b/20191205/gtk-12.c new file mode 100644 index 0000000000000000000000000000000000000000..d551f7a760c7aad6b7146094de6dfe7152eece6f --- /dev/null +++ b/20191205/gtk-12.c @@ -0,0 +1,63 @@ +#include <gtk/gtk.h> + +#define WIDTH 320 +#define HEIGHT 240 + +double t = 0.0; +double dt = 0.05; + +double x = 10; +double y = 200; +double vx = 20; +double vy = -60; +double g = 9.81; + +gboolean draw (GtkWidget *widget, cairo_t *c, gpointer data) +{ + GdkRGBA blue = { 0.0, 0.5, 1.0, 1.0 }; + + gdk_cairo_set_source_rgba (c, &blue); + cairo_rectangle (c, x, y, 10, 10); + cairo_fill (c); + + return FALSE; /* TRUE to stop other handlers from being invoked for the event. + FALSE to propagate the event further. */ +} + +gboolean timer (GtkWidget *widget) +{ + t += dt; + x += vx * dt; + y += vy * dt; + vy += g * dt; + if (y >= HEIGHT) + vy = -vy; + if (x >= WIDTH) + vx = -vx; + if (x <= 0) + vx = -vx; + gtk_widget_queue_draw_area (widget, 0, 0, WIDTH, HEIGHT); + g_timeout_add (50, (GSourceFunc) timer, widget); + return FALSE; +} + +int main (int argc, char **argv) +{ + gtk_init (&argc, &argv); + + GtkWidget *window = gtk_window_new (GTK_WINDOW_TOPLEVEL); + gtk_widget_show (window); + gtk_window_set_title (GTK_WINDOW (window), "Hello"); + g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL); + + GtkWidget *drawing_area = gtk_drawing_area_new (); + gtk_widget_show (drawing_area); + gtk_container_add (GTK_CONTAINER (window), drawing_area); + gtk_widget_set_size_request (drawing_area, WIDTH, HEIGHT); + g_signal_connect (drawing_area, "draw", G_CALLBACK (draw), NULL); + + g_timeout_add (50, (GSourceFunc) timer, drawing_area); + + gtk_main (); + return 0; +} diff --git a/20191205/gtk-13.c b/20191205/gtk-13.c new file mode 100644 index 0000000000000000000000000000000000000000..1b7c4dbf2434f88fa3e6ae838b8911e58447a2d9 --- /dev/null +++ b/20191205/gtk-13.c @@ -0,0 +1,65 @@ +#include <gtk/gtk.h> + +#define WIDTH 320 +#define HEIGHT 240 + +double t = 0.0; +double dt = 0.2; + +int r = 5; + +double x = 10; +double y = 200; +double vx = 20; +double vy = -60; +double g = 9.81; + +gboolean draw (GtkWidget *widget, cairo_t *c, gpointer data) +{ + GdkRGBA blue = { 0.0, 0.5, 1.0, 1.0 }; + + gdk_cairo_set_source_rgba (c, &blue); + cairo_arc (c, x, y, r, 0, 2 * G_PI); + cairo_fill (c); + + return FALSE; /* TRUE to stop other handlers from being invoked for the event. + FALSE to propagate the event further. */ +} + +gboolean timer (GtkWidget *widget) +{ + t += dt; + x += vx * dt; + y += vy * dt; + vy += g * dt; + if (y + r >= HEIGHT) + vy = -vy * 0.9; + if (x + r >= WIDTH) + vx = -vx * 0.9; + if (x - r <= 0) + vx = -vx * 0.9; + gtk_widget_queue_draw_area (widget, 0, 0, WIDTH, HEIGHT); + g_timeout_add (50, (GSourceFunc) timer, widget); + return FALSE; +} + +int main (int argc, char **argv) +{ + gtk_init (&argc, &argv); + + GtkWidget *window = gtk_window_new (GTK_WINDOW_TOPLEVEL); + gtk_widget_show (window); + gtk_window_set_title (GTK_WINDOW (window), "Hello"); + g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL); + + GtkWidget *drawing_area = gtk_drawing_area_new (); + gtk_widget_show (drawing_area); + gtk_container_add (GTK_CONTAINER (window), drawing_area); + gtk_widget_set_size_request (drawing_area, WIDTH, HEIGHT); + g_signal_connect (drawing_area, "draw", G_CALLBACK (draw), NULL); + + g_timeout_add (50, (GSourceFunc) timer, drawing_area); + + gtk_main (); + return 0; +} diff --git a/20191205/gtk-2.c b/20191205/gtk-2.c new file mode 100644 index 0000000000000000000000000000000000000000..c5db19f8d7e859ff12b08ffd6425b84deab35d07 --- /dev/null +++ b/20191205/gtk-2.c @@ -0,0 +1,12 @@ +#include <gtk/gtk.h> + +int main (int argc, char **argv) +{ + gtk_init (&argc, &argv); + GtkWidget *window = gtk_window_new (GTK_WINDOW_TOPLEVEL); + gtk_window_set_title (GTK_WINDOW (window), "Hello"); + g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL); + gtk_widget_show (window); + gtk_main (); + return 0; +} diff --git a/20191205/gtk-3.c b/20191205/gtk-3.c new file mode 100644 index 0000000000000000000000000000000000000000..26fcdd9bf83023b4f3e704a93218134149829f83 --- /dev/null +++ b/20191205/gtk-3.c @@ -0,0 +1,16 @@ +#include <gtk/gtk.h> + +int main (int argc, char **argv) +{ + gtk_init (&argc, &argv); + GtkWidget *window = gtk_window_new (GTK_WINDOW_TOPLEVEL); + gtk_window_set_title (GTK_WINDOW (window), "Hello"); + g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL); + GtkWidget *button = gtk_button_new_with_label ("Quit"); + g_signal_connect (button, "clicked", G_CALLBACK (gtk_main_quit), NULL); + gtk_container_add (GTK_CONTAINER (window), button); + gtk_widget_show (button); + gtk_widget_show (window); + gtk_main (); + return 0; +} diff --git a/20191205/gtk-4.c b/20191205/gtk-4.c new file mode 100644 index 0000000000000000000000000000000000000000..132dbcb61fb979372da88225e8cb881d2b81a2d4 --- /dev/null +++ b/20191205/gtk-4.c @@ -0,0 +1,26 @@ +#include <gtk/gtk.h> + +int main (int argc, char **argv) +{ + gtk_init (&argc, &argv); + GtkWidget *window = gtk_window_new (GTK_WINDOW_TOPLEVEL); + gtk_window_set_title (GTK_WINDOW (window), "Hello"); + g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL); + + GtkWidget *vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 5); + gtk_container_add (GTK_CONTAINER (window), vbox); + + GtkWidget *button = gtk_button_new_with_label ("Quit"); + g_signal_connect (button, "clicked", G_CALLBACK (gtk_main_quit), NULL); + gtk_container_add (GTK_CONTAINER (vbox), button); + + GtkWidget *drawing_area = gtk_drawing_area_new (); + gtk_container_add (GTK_CONTAINER (vbox), drawing_area); + + gtk_widget_show (drawing_area); + gtk_widget_show (button); + gtk_widget_show (vbox); + gtk_widget_show (window); + gtk_main (); + return 0; +} diff --git a/20191205/gtk-5.c b/20191205/gtk-5.c new file mode 100644 index 0000000000000000000000000000000000000000..c77feaac44766ae3e7b157d3a72e03f62c9c687b --- /dev/null +++ b/20191205/gtk-5.c @@ -0,0 +1,27 @@ +#include <gtk/gtk.h> + +int main (int argc, char **argv) +{ + gtk_init (&argc, &argv); + GtkWidget *window = gtk_window_new (GTK_WINDOW_TOPLEVEL); + gtk_window_set_title (GTK_WINDOW (window), "Hello"); + g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL); + + GtkWidget *vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 5); + gtk_container_add (GTK_CONTAINER (window), vbox); + + GtkWidget *button = gtk_button_new_with_label ("Quit"); + g_signal_connect (button, "clicked", G_CALLBACK (gtk_main_quit), NULL); + gtk_container_add (GTK_CONTAINER (vbox), button); + + GtkWidget *drawing_area = gtk_drawing_area_new (); + gtk_container_add (GTK_CONTAINER (vbox), drawing_area); + gtk_widget_set_size_request (drawing_area, 100, 100); + + gtk_widget_show (drawing_area); + gtk_widget_show (button); + gtk_widget_show (vbox); + gtk_widget_show (window); + gtk_main (); + return 0; +} diff --git a/20191205/gtk-6.c b/20191205/gtk-6.c new file mode 100644 index 0000000000000000000000000000000000000000..81e12e6958d3c0adbed8d315927104482bc4c681 --- /dev/null +++ b/20191205/gtk-6.c @@ -0,0 +1,53 @@ +#include <gtk/gtk.h> + +gboolean draw (GtkWidget *widget, cairo_t *c, gpointer data) +{ + GdkRGBA red = { 1.0, 0.0, 0.0, 0.8 }; + GdkRGBA yellow = { 1.0, 1.0, 0.0, 0.6 }; + GdkRGBA blue = { 0.0, 0.5, 1.0, 0.4 }; + + gdk_cairo_set_source_rgba (c, &red); + cairo_rectangle (c, 10, 10, 60, 40); + cairo_fill (c); + + gdk_cairo_set_source_rgba (c, &yellow); + cairo_arc (c, 65, 50, 30, 0, 2 * G_PI); + cairo_fill (c); + + gdk_cairo_set_source_rgba (c, &blue); + cairo_move_to (c, 10, 70); + cairo_line_to (c, 70, 70); + cairo_line_to (c, 40, 18); + cairo_close_path (c); + cairo_fill (c); + + return FALSE; /* TRUE to stop other handlers from being invoked for the event. + FALSE to propagate the event further. */ +} + +int main (int argc, char **argv) +{ + gtk_init (&argc, &argv); + GtkWidget *window = gtk_window_new (GTK_WINDOW_TOPLEVEL); + gtk_window_set_title (GTK_WINDOW (window), "Hello"); + g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL); + + GtkWidget *vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 5); + gtk_container_add (GTK_CONTAINER (window), vbox); + + GtkWidget *button = gtk_button_new_with_label ("Quit"); + g_signal_connect (button, "clicked", G_CALLBACK (gtk_main_quit), NULL); + gtk_container_add (GTK_CONTAINER (vbox), button); + + GtkWidget *drawing_area = gtk_drawing_area_new (); + gtk_container_add (GTK_CONTAINER (vbox), drawing_area); + g_signal_connect (drawing_area, "draw", G_CALLBACK (draw), NULL); + gtk_widget_set_size_request (drawing_area, 100, 100); + + gtk_widget_show (drawing_area); + gtk_widget_show (button); + gtk_widget_show (vbox); + gtk_widget_show (window); + gtk_main (); + return 0; +} diff --git a/20191205/gtk-7.c b/20191205/gtk-7.c new file mode 100644 index 0000000000000000000000000000000000000000..11041fbe1d27585ca4edb951f195f114ddad126e --- /dev/null +++ b/20191205/gtk-7.c @@ -0,0 +1,71 @@ +#include <stdio.h> +#include <gtk/gtk.h> + +gboolean draw (GtkWidget *widget, cairo_t *c, gpointer data) +{ + GdkRGBA red = { 1.0, 0.0, 0.0, 0.8 }; + GdkRGBA yellow = { 1.0, 1.0, 0.0, 0.6 }; + GdkRGBA blue = { 0.0, 0.5, 1.0, 0.4 }; + + gdk_cairo_set_source_rgba (c, &red); + cairo_rectangle (c, 10, 10, 60, 40); + cairo_fill (c); + + gdk_cairo_set_source_rgba (c, &yellow); + cairo_arc (c, 65, 50, 30, 0, 2 * G_PI); + cairo_fill (c); + + gdk_cairo_set_source_rgba (c, &blue); + cairo_move_to (c, 10, 70); + cairo_line_to (c, 70, 70); + cairo_line_to (c, 40, 18); + cairo_close_path (c); + cairo_fill (c); + + return FALSE; /* TRUE to stop other handlers from being invoked for the event. + FALSE to propagate the event further. */ +} + +gboolean button_press (GtkWidget *widget, GdkEventButton *e, gpointer data) +{ + printf ("button %d pressed at (%lf, %lf)\n", e->button, e->x, e->y); + return FALSE; +} + +gboolean key_press (GtkWidget *widget, GdkEventKey *e, gpointer data) +{ + printf ("key '%c' (%d) pressed\n", e->keyval, e->keyval); + return FALSE; +} + +int main (int argc, char **argv) +{ + gtk_init (&argc, &argv); + GtkWidget *window = gtk_window_new (GTK_WINDOW_TOPLEVEL); + gtk_window_set_title (GTK_WINDOW (window), "Hello"); + g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL); + + GtkWidget *vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 5); + gtk_container_add (GTK_CONTAINER (window), vbox); + + GtkWidget *button = gtk_button_new_with_label ("Quit"); + g_signal_connect (button, "clicked", G_CALLBACK (gtk_main_quit), NULL); + gtk_container_add (GTK_CONTAINER (vbox), button); + + GtkWidget *drawing_area = gtk_drawing_area_new (); + gtk_container_add (GTK_CONTAINER (vbox), drawing_area); + g_signal_connect (drawing_area, "draw", G_CALLBACK (draw), NULL); + gtk_widget_set_size_request (drawing_area, 100, 100); + gtk_widget_add_events (drawing_area, GDK_BUTTON_PRESS_MASK); + g_signal_connect (drawing_area, "button_press_event", G_CALLBACK (button_press), NULL); + + gtk_widget_add_events (window, GDK_KEY_PRESS_MASK); + g_signal_connect (window, "key_press_event", G_CALLBACK (key_press), NULL); + + gtk_widget_show (drawing_area); + gtk_widget_show (button); + gtk_widget_show (vbox); + gtk_widget_show (window); + gtk_main (); + return 0; +} diff --git a/20191205/gtk-8.c b/20191205/gtk-8.c new file mode 100644 index 0000000000000000000000000000000000000000..35edb31368161671917a1523845e73c6d944c8a4 --- /dev/null +++ b/20191205/gtk-8.c @@ -0,0 +1,50 @@ +#include <gtk/gtk.h> + +#define WIDTH 320 +#define HEIGHT 240 + +double t = 0.0; + +double vx = 40; +double vy = 30; + +gboolean draw (GtkWidget *widget, cairo_t *c, gpointer data) +{ + GdkRGBA blue = { 0.0, 0.5, 1.0, 1.0 }; + + gdk_cairo_set_source_rgba (c, &blue); + cairo_rectangle (c, 10 + vx * t, 10 + vy * t, 10, 10); + cairo_fill (c); + + return FALSE; /* TRUE to stop other handlers from being invoked for the event. + FALSE to propagate the event further. */ +} + +gboolean timer (GtkWidget *widget) +{ + t += 0.05; + gtk_widget_queue_draw_area (widget, 0, 0, WIDTH, HEIGHT); + g_timeout_add (50, (GSourceFunc) timer, widget); + return FALSE; +} + +int main (int argc, char **argv) +{ + gtk_init (&argc, &argv); + + GtkWidget *window = gtk_window_new (GTK_WINDOW_TOPLEVEL); + gtk_widget_show (window); + gtk_window_set_title (GTK_WINDOW (window), "Hello"); + g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL); + + GtkWidget *drawing_area = gtk_drawing_area_new (); + gtk_widget_show (drawing_area); + gtk_container_add (GTK_CONTAINER (window), drawing_area); + gtk_widget_set_size_request (drawing_area, WIDTH, HEIGHT); + g_signal_connect (drawing_area, "draw", G_CALLBACK (draw), NULL); + + g_timeout_add (50, (GSourceFunc) timer, drawing_area); + + gtk_main (); + return 0; +} diff --git a/20191205/gtk-9.c b/20191205/gtk-9.c new file mode 100644 index 0000000000000000000000000000000000000000..af68170dc104db075df0b609a2878d7256050074 --- /dev/null +++ b/20191205/gtk-9.c @@ -0,0 +1,53 @@ +#include <gtk/gtk.h> + +#define WIDTH 320 +#define HEIGHT 240 + +double t = 0.0; + +double x = 10; +double y = 10; +double vx = 40; +double vy = 30; +double g = 9.81; + +gboolean draw (GtkWidget *widget, cairo_t *c, gpointer data) +{ + GdkRGBA blue = { 0.0, 0.5, 1.0, 1.0 }; + + gdk_cairo_set_source_rgba (c, &blue); + cairo_rectangle (c, x + vx * t, y + vy * t - 0.5 * g * t * t, 10, 10); + cairo_fill (c); + + return FALSE; /* TRUE to stop other handlers from being invoked for the event. + FALSE to propagate the event further. */ +} + +gboolean timer (GtkWidget *widget) +{ + t += 0.05; + gtk_widget_queue_draw_area (widget, 0, 0, WIDTH, HEIGHT); + g_timeout_add (50, (GSourceFunc) timer, widget); + return FALSE; +} + +int main (int argc, char **argv) +{ + gtk_init (&argc, &argv); + + GtkWidget *window = gtk_window_new (GTK_WINDOW_TOPLEVEL); + gtk_widget_show (window); + gtk_window_set_title (GTK_WINDOW (window), "Hello"); + g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL); + + GtkWidget *drawing_area = gtk_drawing_area_new (); + gtk_widget_show (drawing_area); + gtk_container_add (GTK_CONTAINER (window), drawing_area); + gtk_widget_set_size_request (drawing_area, WIDTH, HEIGHT); + g_signal_connect (drawing_area, "draw", G_CALLBACK (draw), NULL); + + g_timeout_add (50, (GSourceFunc) timer, drawing_area); + + gtk_main (); + return 0; +} diff --git a/20191205/hp-20191205.pdf b/20191205/hp-20191205.pdf new file mode 100644 index 0000000000000000000000000000000000000000..4086393f57e295c46c0cb1618cec58fd032907f7 Binary files /dev/null and b/20191205/hp-20191205.pdf differ diff --git a/20191205/hp-20191205.tex b/20191205/hp-20191205.tex new file mode 100644 index 0000000000000000000000000000000000000000..9c13dd0f0f01af82b58cf040dffa145fb4f9b834 --- /dev/null +++ b/20191205/hp-20191205.tex @@ -0,0 +1,443 @@ +% hp-20191205.pdf - Lecture Slides on Low-Level Programming +% Copyright (C) 2012, 2013, 2015, 2016, 2017, 2018, 2019 Peter Gerwinski +% +% This document is free software: you can redistribute it and/or +% modify it either under the terms of the Creative Commons +% Attribution-ShareAlike 3.0 License, or under the terms of the +% GNU General Public License as published by the Free Software +% Foundation, either version 3 of the License, or (at your option) +% any later version. +% +% This document is distributed in the hope that it will be useful, +% but WITHOUT ANY WARRANTY; without even the implied warranty of +% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +% GNU General Public License for more details. +% +% You should have received a copy of the GNU General Public License +% along with this document. If not, see <http://www.gnu.org/licenses/>. +% +% You should have received a copy of the Creative Commons +% Attribution-ShareAlike 3.0 Unported License along with this +% document. If not, see <http://creativecommons.org/licenses/>. + +% README: Präprozessor, Bibliotheken + +\documentclass[10pt,t]{beamer} + +\usepackage{pgslides} +\usepackage{pdftricks} +\usepackage{tikz} + +\begin{psinputs} + \usepackage[utf8]{inputenc} + \usepackage[german]{babel} + \usepackage[T1]{fontenc} + \usepackage{helvet} + \renewcommand*\familydefault{\sfdefault} + \usepackage{pstricks,pst-grad} +\end{psinputs} + +\title{Hardwarenahe Programmierung} +\author{Prof.\ Dr.\ rer.\ nat.\ Peter Gerwinski} +\date{5.\ Dezember 2019} + +\begin{document} + +\maketitleframe + +\nosectionnonumber{\inserttitle} + +\begin{frame} + + \shownosectionnonumber + + \begin{itemize} + \item[\textbf{1}] \textbf{Einführung} + \hfill\makebox(0,0)[br]{\raisebox{2.25ex}{\url{https://gitlab.cvh-server.de/pgerwinski/hp}}} + \item[\textbf{2}] \textbf{Einführung in C} + \item[\textbf{3}] \textbf{Bibliotheken} + \begin{itemize} + \color{medgreen} + \item[3.1] Der Präprozessor + \item[3.2] Bibliotheken einbinden + \item[3.3] Bibliotheken verwenden + \color{black} + \item[3.4] Projekt organisieren: make +% \vspace*{-\smallskipamount} +% \item[\dots] + \end{itemize} + \vspace*{-\smallskipamount} + \item[\textbf{4}] \textbf{Hardwarenahe Programmierung} + \item[\textbf{5}] \textbf{Algorithmen} + \begin{itemize} + \color{red} + \item[5.1] Differentialgleichungen + \color{black} + \vspace*{-0.1cm} + \item[\dots] + \end{itemize} + \vspace*{-\smallskipamount} + \item[\textbf{\dots}] + \end{itemize} + +\end{frame} + +\setcounter{section}{2} +\section{Bibliotheken} +\subsection{Der Präprozessor} + +\begin{frame}[fragile] + + \showsection + \showsubsection + + \lstinline{#include}: %\pause: + Text einbinden + \begin{itemize} +% \pause + \item + \lstinline{#include <stdio.h>}: Standard-Verzeichnisse -- Standard-Header +% \pause + \item + \lstinline{#include "pruzzel.h"}: auch aktuelles Verzeichnis -- eigene Header + \end{itemize} + +% \pause + \bigskip + + \lstinline{#define VIER 4}: Text ersetzen lassen -- Konstante definieren + \begin{itemize} +% \pause + \item + Kein Semikolon! +% \pause + \item + Berechnungen in Klammern setzen:\\ + \lstinline{#define VIER (2 + 2)} +% \pause + \item + Konvention: Großbuchstaben + \end{itemize} + +\end{frame} + +\subsection{Bibliotheken einbinden} + +\begin{frame}[fragile] + + \showsection + \showsubsection + + Inhalt der Header-Datei: externe Deklarationen + +% \pause + \smallskip + \lstinline{extern int pruzzel (const char *s);} + +% \pause + \smallskip + \lstinline{extern int printf (__const char *__restrict __format, ...);} + +% \pause + \bigskip + Funktion wird "`anderswo"' definiert + \begin{itemize} +% \pause + \item + separater C-Quelltext: mit an \lstinline[style=terminal]{gcc} übergeben +% \pause + \item + Zusammenfügen zu ausführbarem Programm durch den \newterm{Linker} +% \pause + \item + vorcompilierte Bibliothek: \lstinline[style=terminal]{-lfoo}\\ +% \pause + = Datei \file{libfoo.a} in Standard-Verzeichnis + \end{itemize} + +\end{frame} + +\subsection{Bibliothek verwenden (Beispiel: GTK+)} + +\begin{frame}[fragile] + + \showsubsection + + \begin{itemize} + \item + \lstinline{#include <gtk/gtk.h>} +% \pause + \smallskip + \item + Mit \lstinline[style=cmd]{pkg-config --cflags --libs} erfährt man,\\ + welche Optionen und Bibliotheken man an \lstinline[style=cmd]{gcc} + übergeben muß\alt<2->{.}{:} +% \pause + \begin{onlyenv}<1> + \begin{lstlisting}[style=terminal,gobble=10] + $ ¡pkg-config --cflags --libs gtk+-3.0¿ + -pthread -I/usr/include/gtk-3.0 -I/usr/include/at-spi2- + atk/2.0 -I/usr/include/at-spi-2.0 -I/usr/include/dbus-1 + .0 -I/usr/lib/x86_64-linux-gnu/dbus-1.0/include -I/usr/ + include/gtk-3.0 -I/usr/include/gio-unix-2.0/ -I/usr/inc + lude/cairo -I/usr/include/pango-1.0 -I/usr/include/harf + buzz -I/usr/include/pango-1.0 -I/usr/include/atk-1.0 -I + /usr/include/cairo -I/usr/include/pixman-1 -I/usr/inclu + de/freetype2 -I/usr/include/libpng16 -I/usr/include/gdk + -pixbuf-2.0 -I/usr/include/libpng16 -I/usr/include/glib + -2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -lgtk + -3 -lgdk-3 -lpangocairo-1.0 -lpango-1.0 -latk-1.0 -lcai + ro-gobject -lcairo -lgdk_pixbuf-2.0 -lgio-2.0 -lgobject + -2.0 -lglib-2.0 + \end{lstlisting} + \vspace*{-1cm} + \end{onlyenv} + \pause + \arrowitem + Compiler-Aufruf: + \begin{onlyenv}<2> + \begin{lstlisting}[style=terminal,gobble=10] + $ ¡gcc -Wall -O hello-gtk.c -pthread -I/usr/include/gtk- + 3.0 -I/usr/include/at-spi2-atk/2.0 -I/usr/include/at-sp + i-2.0 -I/usr/include/dbus-1.0 -I/usr/lib/x86_64-linux-g + nu/dbus-1.0/include -I/usr/include/gtk-3.0 -I/usr/inclu + de/gio-unix-2.0/ -I/usr/include/cairo -I/usr/include/pa + ngo-1.0 -I/usr/include/harfbuzz -I/usr/include/pango-1. + 0 -I/usr/include/atk-1.0 -I/usr/include/cairo -I/usr/in + clude/pixman-1 -I/usr/include/freetype2 -I/usr/include/ + libpng16 -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/l + ibpng16 -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux + -gnu/glib-2.0/include -lgtk-3 -lgdk-3 -lpangocairo-1.0 + -lpango-1.0 -latk-1.0 -lcairo-gobject -lcairo -lgdk_pix + buf-2.0 -lgio-2.0 -lgobject-2.0 -lglib-2.0-o hello-gtk¿ + \end{lstlisting} + \vspace*{-1cm} + \end{onlyenv} + \begin{onlyenv}<3-> + \begin{lstlisting}[style=terminal,gobble=10] + $ ¡gcc -Wall -O hello-gtk.c $(pkg-config --cflags --libs) + -o hello-gtk¿ + \end{lstlisting} + \end{onlyenv} + \end{itemize} + \pause[3] + \begin{picture}(0,0) + \color{red} + \put(6.6,-0.6){\makebox(0,0)[bl]{\tikz{\draw[-latex](0,0)--(3,1.5);}}} + \put(6.3,-0.7){\makebox(0,0)[t]{\shortstack{\strut Optionen:\\ + \strut u.\,a.\ viele Include-Verzeichnisse:\\ + \lstinline[style=cmd]{-I/usr/include/gtk-3.0}}}} + \put(10.0,-2.1){\makebox(0,0)[bl]{\tikz{\draw[-latex](0,0)--(1.5,3);}}} + \put(10.3,-2.2){\makebox(0,0)[t]{\shortstack{\strut Bibliotheken:\\ + \strut u.\,a.\ \lstinline[style=cmd]{-lgtk-3 -lcairo}}}} + \end{picture} + +\end{frame} + +\begin{frame}[fragile] + + \showsubsection + + Selbst geschriebene Funktion übergeben: \newterm{Callback} + + \bigskip + + \begin{lstlisting}[xleftmargin=1em] + gboolean draw (GtkWidget *widget, cairo_t *c, gpointer data) + { + /* Zeichenbefehle */ + ... + + return FALSE; + } + + ... + + g_signal_connect (drawing_area, "draw", G_CALLBACK (draw), NULL); + \end{lstlisting} + + \medskip + + \begin{itemize} + \arrowitem + GTK+ ruft immer dann, wenn es etwas zu zeichnen gibt,\\ + die Funktion \lstinline{draw} auf. + \end{itemize} + + \pause + \begin{picture}(0,0) + \color{red} + \put(5.3,4.8){\makebox(0,0)[bl]{\tikz{\draw[-latex](0,0)--(1.90,1.15);}}} + \put(5.0,4.7){\makebox(0,0)[t]{\shortstack{\strut repräsentiert den\\ + \strut Bildschirm, auf den\\ + \strut gezeichnet werden soll}}} + \pause + \put(9.7,5.1){\makebox(0,0)[br]{\tikz{\draw[-latex](0,0)--(-0.2,0.85);}}} + \put(9.7,5.0){\makebox(0,0)[t]{\shortstack{\strut optionale Zusatzinformationen\\ + \strut für draw(), typischerweise\\ + \strut ein Zeiger auf ein struct}}} + \put(10.0,3.5){\makebox(0,0)[tl]{\tikz{\draw[-latex](0,0)--(0.6,-1.25);}}} + \end{picture} + +\end{frame} + +\begin{frame}[fragile] + + \showsubsection + + Selbst geschriebene Funktion übergeben: \newterm{Callback} + + \bigskip + + \begin{lstlisting}[xleftmargin=1em] + gboolean timer (GtkWidget *widget) + { + /* Rechenbefehle */ + ... + + gtk_widget_queue_draw_area (widget, 0, 0, WIDTH, HEIGHT); + g_timeout_add (50, (GSourceFunc) timer, widget); + return FALSE; + } + + ... + + g_timeout_add (50, (GSourceFunc) timer, drawing_area); + \end{lstlisting} + + \medskip + + \begin{itemize} + \arrowitem + GTK+ ruft nach 50 Millisekunden + die Funktion \lstinline{timer} auf. + \end{itemize} + + \pause + \begin{picture}(0,0) + \color{red} + \put(9.7,6.7){\makebox(0,0)[t]{\shortstack{\strut Dieser Bereich soll\\ + \strut neu gezeichnet werden.}}} + \put(9.7,5.7){\makebox(0,0)[tr]{\tikz{\draw[-latex](0,0)--(-0.6,-0.8);}}} + \pause + \put(4.3,3.2){\makebox(0,0)[br]{\tikz{\draw[-latex](0,0)--(-0.7,0.6);}}} + \put(4.3,3.1){\makebox(0,0)[t]{\shortstack{\strut In weiteren 50 Millisekunden soll\\ + \strut die Funktion erneut aufgerufen werden.}}} + \pause + \put(9.3,2.9){\makebox(0,0)[br]{\tikz{\draw[-latex](0,0)--(-3.3,0.9);}}} + \put(9.8,2.8){\makebox(0,0)[t]{\shortstack{\strut Explizite Typumwandlung\\ + \strut eines Zeigers (später)}}} + \end{picture} + +\end{frame} + +\section{Algorithmen} +\subsection{Differentialgleichungen} + +\begin{frame}[fragile] + + \showsection + \showsubsection + + \textbf{Beispiel 1: Gleichmäßig beschleunigte Bewegung} (Schräger Wurf) + + \strut\hfill + \begin{minipage}{2.5cm} + \vspace*{0.6cm} + \begin{align*} + x'(t) &= v_x(t) \\[0.65cm] + y'(t) &= v_y(t) \\[0.75cm] + v_x'(t) &= 0 \\[0.65cm] + v_y'(t) &= -g + \end{align*} + \vspace*{0.0cm} + \end{minipage}% + \only<1>{\hspace*{9.49cm}}\strut + \only<2->{\hfill$\Rightarrow$\hfill}% + \begin{onlyenv}<2-8> + \begin{minipage}{8.3cm} + \begin{align*} + x(t) &= \int v_x(t)\,dt + \visible<4->{= \int v_{0x}\,dt} + \visible<5->{= x_0 + v_{0x}\cdot t}\\[\medskipamount] + y(t) &= \int v_y(t)\,dt + \visible<7->{= \int v_{0y} - g\cdot t\,dt} + \visible<8->{= y_0 + v_{0y}\cdot t + - {\textstyle\frac12}gt^2}\\[\bigskipamount] + v_x(t) &= \int 0\,dt + \visible<3->{= v_{0x}} \\[\medskipamount] + v_y(t) &= \int -g\,dt + \visible<6->{= v_{0y} - g\cdot t} + \end{align*} + \end{minipage}% + \end{onlyenv}% + \begin{onlyenv}<9-> + \begin{minipage}{3.5cm} + \vspace*{0.5cm} + \begin{lstlisting}[gobble=8,xleftmargin=0.5em] + ¡x += vx * dt;¿ + \end{lstlisting} + \vspace{0.75cm} + \begin{lstlisting}[gobble=8,xleftmargin=0.5em] + ¡y += vy * dt;¿ + \end{lstlisting} + \vspace{0.90cm} + \begin{lstlisting}[gobble=8,xleftmargin=0.5em] + ¡vx += 0 * dt;¿ + \end{lstlisting} + \vspace{0.75cm} + \begin{lstlisting}[gobble=8,xleftmargin=0.5em] + ¡vy += -g * dt;¿ + \end{lstlisting} + \end{minipage}% + \begin{minipage}{5.13cm} + Siehe: \file{gtk-13.c} + \end{minipage} + \end{onlyenv}% + \hfill\strut + +\end{frame} + +\begin{frame}[fragile] + \showsection + \showsubsection + + \textbf{Beispiel 1: Gleichmäßig beschleunigte Bewegung} + + \medskip + + \textbf{Beispiel 2: Mathematisches Pendel} + + \vspace*{-2\bigskipamount} + + \begin{picture}(0,0) + \put(8,-6.5){\includegraphics{pendulum.pdf}} + \end{picture} + + \begin{eqnarray*} + \varphi'(t) &=& \omega(t) \\[\smallskipamount] + \omega'(t) &=& -\frac{g}{l}\cdot\sin\varphi(t)\hspace*{7.1cm} + \end{eqnarray*} + \vspace*{-1.5\medskipamount} + \begin{itemize} + \item + Von Hand (analytisch):\\ + Lösung raten (Ansatz), Parameter berechnen + \item + Mit Computer (numerisch):\\ + Eulersches Polygonzugverfahren + \end{itemize} + \smallskip + \begin{lstlisting}[gobble=0] + phi += dt * omega; + omega += - dt * g / l * sin (phi); + \end{lstlisting} + + \pause + \bigskip + + \textbf{Beispiel 3:} Praktikumsaufgabe + +\end{frame} + +\end{document} diff --git a/20191205/hp-uebung-20191205.pdf b/20191205/hp-uebung-20191205.pdf new file mode 100644 index 0000000000000000000000000000000000000000..f08bca1e0441b3cf78bf8c9d7d665861b1efbcc4 Binary files /dev/null and b/20191205/hp-uebung-20191205.pdf differ diff --git a/20191205/hp-uebung-20191205.tex b/20191205/hp-uebung-20191205.tex new file mode 100644 index 0000000000000000000000000000000000000000..96c788e30aa8cf9c5bbf71e5eb1f8d825888bf07 --- /dev/null +++ b/20191205/hp-uebung-20191205.tex @@ -0,0 +1,153 @@ +% hp-uebung-20191205.pdf - Exercises on Low-Level Programming +% Copyright (C) 2013, 2015, 2016, 2017, 2018, 2019 Peter Gerwinski +% +% This document is free software: you can redistribute it and/or +% modify it either under the terms of the Creative Commons +% Attribution-ShareAlike 3.0 License, or under the terms of the +% GNU General Public License as published by the Free Software +% Foundation, either version 3 of the License, or (at your option) +% any later version. +% +% This document is distributed in the hope that it will be useful, +% but WITHOUT ANY WARRANTY; without even the implied warranty of +% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +% GNU General Public License for more details. +% +% You should have received a copy of the GNU General Public License +% along with this document. If not, see <http://www.gnu.org/licenses/>. +% +% You should have received a copy of the Creative Commons +% Attribution-ShareAlike 3.0 Unported License along with this +% document. If not, see <http://creativecommons.org/licenses/>. + +% README: Löschen aus Strings, Hexdumps + +\documentclass[a4paper]{article} + +\usepackage{pgscript} + +\begin{document} + +% \thispagestyle{empty} + + \section*{Hardwarenahe Programmierung\\ + Übungsaufgaben -- 5.\ Dezember 2019} + + Diese Übung enthält Punkteangaben wie in einer Klausur. + Um zu "`bestehen"', müssen Sie innerhalb von 60 Minuten + unter Verwendung ausschließlich zugelassener Hilfsmittel + 10 Punkte (von insgesamt \totalpoints) erreichen. + + \exercise{Löschen aus Strings} + + Wir betrachten das folgende Programm (\gitfile{hp}{20191205}{aufgabe-1.c}): + \begin{lstlisting}[style=numbered] + #include <stdio.h> + #include <string.h> + + void delete_char_from_string (char *s, int pos) + { + for (int i = strlen (s) - 1; i > pos; i--) + s[i - 1] = s[i]; + } + + int main (void) + { + char test[] = "Hochschule Boochum"; + delete_char_from_string (test, 12); + printf ("%s\n", test); + return 0; + } + \end{lstlisting} + Die Ausgabe des Programms lautet: + \lstinline[style=terminal]{Hochschule Bmmmmmm} + + \begin{enumerate}[\quad(a)] + \item + Erklären Sie, wie die Ausgabe zustandekommt. + \points{3} +% \workspace{12} + \item + Schreiben Sie die Funktion \lstinline{delete_char_from_string()} so um, + daß sie aus dem String \lstinline{s} das Zeichen an der Stelle \lstinline{pos} + löscht.\par + Die Ausgabe des Programms müßte dann + \lstinline[style=terminal]{Hochschule Bochum} lauten. + \points{4} +% \workspace{15} + \item + Was kann passieren, wenn Sie + \lstinline{char test[] = "Hochschule Boochum";} \\ + durch + \lstinline{char *test = "Hochschule Boochum";} + ersetzen? Begründen Sie Ihre Antwort. + \points{2} +% \workspace{9} + \item + Schreiben Sie eine Funktion + \lstinline{void delete_from_string (char *s, int pos, int length)}, + die \lstinline{length} Zeichen ab der Stelle \lstinline{pos} + aus dem String \lstinline{s} löscht, + \emph{indem sie die in Aufgabenteil (b) korrigierte Funktion\/} + \lstinline{void delete_char_from_string (char *s, int pos)} + \emph{wiederholt aufruft}. \par +% Wie schnell (Landau-Symbol in Abhängigkeit von der Länge $n$ des Strings) +% arbeitet diese Funktion +% \lstinline{delete_from_string()}? +% Begründen Sie Ihre Antwort. +% \points{3} + \points{2} +% \workspace{15} + \item + Schreiben Sie eine \emph{optimierte\/} Funktion + \lstinline{void quick_delete_from_string (char *s, int pos, int length)}, + die ebenfalls \lstinline{length} Zeichen ab der Stelle \lstinline{pos} + aus dem String \lstinline{s} löscht, +% dafür aber höchstens $\mathcal{O}(n)$ an Rechenzeit benötigt. + aber mit einer einzigen Schleife auskommt. + \points{4} +% \workspace{15} + \end{enumerate} + + \clearpage + \exercise{Hexdumps} + + Das folgende Programm (\gitfile{hp}{20191205}{aufgabe-2.c}) liest + einen String ein und gibt die ASCII-Werte der Buchstaben hexadezimal aus. + (Anders als z.\,B.\ \lstinline{scanf()} + akzeptiert die Funktion \lstinline{fgets()} zum Lesen von Strings auch Leerzeichen, + und sie vermeidet Pufferüberläufe.) + \begin{lstlisting}[style=numbered] + #include <stdio.h> + + int main (void) + { + char buffer[100]; + fgets (buffer, 100, stdin); + for (char *p = buffer; *p; p++) + printf ("%02x", *p); + printf ("\n"); + } + \end{lstlisting} + Beispiel: Bei der Eingabe von \lstinline[style=cmd]{Dies ist ein Test.} + erscheint die Ausgabe\\ + \lstinline[style=terminal]{44696573206973742065696e20546573742e0a}. + + Schreiben Sie ein Programm, das diese Umwandlung in umgekehrter Richtung vornimmt, + also z.\,B.\ bei Eingabe von \lstinline[style=cmd]{44696573206973742065696e20546573742e0a} + wieder \lstinline[style=terminal]{Dies ist ein Test.} ausgibt. + + \points{6} + + Hinweis für die Klausur: + Abgabe in digitaler Form ist erwünscht, aber nicht zwingend. + + \begin{flushright} + \textit{Viel Erfolg!} + \end{flushright} + + \makeatletter + \immediate\write\@mainaux{\string\gdef\string\totalpoints{\arabic{points}}} + \makeatother + +\end{document} diff --git a/20191205/logo-hochschule-bochum-cvh-text-v2.pdf b/20191205/logo-hochschule-bochum-cvh-text-v2.pdf new file mode 120000 index 0000000000000000000000000000000000000000..4aa99b8f81061aca6dcaf43eed2d9efef40555f8 --- /dev/null +++ b/20191205/logo-hochschule-bochum-cvh-text-v2.pdf @@ -0,0 +1 @@ +../common/logo-hochschule-bochum-cvh-text-v2.pdf \ No newline at end of file diff --git a/20191205/logo-hochschule-bochum.pdf b/20191205/logo-hochschule-bochum.pdf new file mode 120000 index 0000000000000000000000000000000000000000..b6b9491e370e499c9276918182cdb82cb311bcd1 --- /dev/null +++ b/20191205/logo-hochschule-bochum.pdf @@ -0,0 +1 @@ +../common/logo-hochschule-bochum.pdf \ No newline at end of file diff --git a/20191205/pendulum-0.c b/20191205/pendulum-0.c new file mode 100644 index 0000000000000000000000000000000000000000..6faebdc7ad779d89ab8e2eacd189240ace1fb406 --- /dev/null +++ b/20191205/pendulum-0.c @@ -0,0 +1,69 @@ +#include <gtk/gtk.h> +#include <math.h> + +int width = 320; +int height = 240; +int gap = height / 20; +int r = gap; +double visual_length = height - 2 * gap - r; + +double phi0 = -0.5; +double omega0 = 0.0; +double t0 = 0.0; +double g = 9.81; +double l = 1.0; +double dt = 0.02; + +double t = t0; +double phi = phi0; +double omega = omega0; + +gboolean draw (GtkWidget *widget, cairo_t *c, gpointer data) +{ + GdkRGBA blue = { 0.0, 0.5, 1.0, 1.0 }; + + int x = width / 2 + visual_length * sin (phi); + int y = gap + visual_length * cos (phi); + + gdk_cairo_set_source_rgba (c, &blue); + cairo_move_to (c, width / 2, gap); + cairo_line_to (c, x, y); + cairo_stroke (c); + cairo_arc (c, x, y, r, 0, 2 * G_PI); + cairo_fill (c); + + return FALSE; /* TRUE to stop other handlers from being invoked for the event. + FALSE to propagate the event further. */ +} + +gboolean timer (GtkWidget *widget) +{ + t += dt; + phi += omega * dt; + omega += - dt * g / l * sin (phi); + + gtk_widget_queue_draw_area (widget, 0, 0, width, height); + g_timeout_add (50, (GSourceFunc) timer, widget); + return FALSE; +} + +int main (int argc, char **argv) +{ + gtk_init (&argc, &argv); + + GtkWidget *window = gtk_window_new (GTK_WINDOW_TOPLEVEL); + gtk_widget_show (window); + gtk_window_set_title (GTK_WINDOW (window), "Hello"); + g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL); + + GtkWidget *drawing_area = gtk_drawing_area_new (); + gtk_widget_show (drawing_area); + gtk_container_add (GTK_CONTAINER (window), drawing_area); + gtk_widget_set_size_request (drawing_area, width, height); + g_signal_connect (drawing_area, "draw", G_CALLBACK (draw), NULL); + + g_timeout_add (50, (GSourceFunc) timer, drawing_area); + + gtk_main (); + return 0; +} diff --git a/20191205/pendulum-1.c b/20191205/pendulum-1.c new file mode 100644 index 0000000000000000000000000000000000000000..44f4c4fde79d96ed604882b0d5ce22a9035e1400 --- /dev/null +++ b/20191205/pendulum-1.c @@ -0,0 +1,69 @@ +#include <gtk/gtk.h> +#include <math.h> + +#define WIDTH 320 +#define HEIGHT 240 +#define GAP (HEIGHT / 20) +#define r GAP +#define visual_length (HEIGHT - 2 * GAP - r) + +#define phi0 (-0.5) +#define omega0 0.0 +#define t0 0.0 +#define g 9.81 +#define l 1.0 +#define dt 0.02 + +double t = t0; +double phi = phi0; +double omega = omega0; + +gboolean draw (GtkWidget *widget, cairo_t *c, gpointer data) +{ + GdkRGBA blue = { 0.0, 0.5, 1.0, 1.0 }; + + int x = WIDTH / 2 + visual_length * sin (phi); + int y = GAP + visual_length * cos (phi); + + gdk_cairo_set_source_rgba (c, &blue); + cairo_move_to (c, WIDTH / 2, 10); + cairo_line_to (c, x, y); + cairo_stroke (c); + cairo_arc (c, x, y, r, 0, 2 * G_PI); + cairo_fill (c); + + return FALSE; /* TRUE to stop other handlers from being invoked for the event. + FALSE to propagate the event further. */ +} + +gboolean timer (GtkWidget *widget) +{ + t += dt; + phi += omega * dt; + omega += - dt * g / l * sin (phi); + + gtk_widget_queue_draw_area (widget, 0, 0, WIDTH, HEIGHT); + g_timeout_add (50, (GSourceFunc) timer, widget); + return FALSE; +} + +int main (int argc, char **argv) +{ + gtk_init (&argc, &argv); + + GtkWidget *window = gtk_window_new (GTK_WINDOW_TOPLEVEL); + gtk_widget_show (window); + gtk_window_set_title (GTK_WINDOW (window), "Hello"); + g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL); + + GtkWidget *drawing_area = gtk_drawing_area_new (); + gtk_widget_show (drawing_area); + gtk_container_add (GTK_CONTAINER (window), drawing_area); + gtk_widget_set_size_request (drawing_area, WIDTH, HEIGHT); + g_signal_connect (drawing_area, "draw", G_CALLBACK (draw), NULL); + + g_timeout_add (50, (GSourceFunc) timer, drawing_area); + + gtk_main (); + return 0; +} diff --git a/20191205/pendulum-2.c b/20191205/pendulum-2.c new file mode 100644 index 0000000000000000000000000000000000000000..4fbad9a50cc595e92fbb1075dd01405a08e84001 --- /dev/null +++ b/20191205/pendulum-2.c @@ -0,0 +1,69 @@ +#include <gtk/gtk.h> +#include <math.h> + +#define WIDTH 320 +#define HEIGHT 240 +#define GAP (HEIGHT / 20) +#define r GAP +#define visual_length (HEIGHT - 2 * GAP - r) + +#define phi0 (-0.5) +#define omega0 0.0 +#define t0 0.0 +#define g 9.81 +#define l 1.0 +#define dt 0.02 + +double t = t0; +double phi = phi0; +double omega = omega0; + +gboolean draw (GtkWidget *widget, cairo_t *c, gpointer data) +{ + GdkRGBA orange = { 1.0, 0.5, 0.0, 1.0 }; + + int x = WIDTH / 2 + visual_length * sin (phi); + int y = GAP + visual_length * cos (phi); + + gdk_cairo_set_source_rgba (c, &orange); + cairo_move_to (c, WIDTH / 2, 10); + cairo_line_to (c, x, y); + cairo_stroke (c); + cairo_arc (c, x, y, r, 0, 2 * G_PI); + cairo_fill (c); + + return FALSE; /* TRUE to stop other handlers from being invoked for the event. + FALSE to propagate the event further. */ +} + +gboolean timer (GtkWidget *widget) +{ + t += dt; + phi += omega * dt; + omega += - dt * g / l * phi; + + gtk_widget_queue_draw_area (widget, 0, 0, WIDTH, HEIGHT); + g_timeout_add (50, (GSourceFunc) timer, widget); + return FALSE; +} + +int main (int argc, char **argv) +{ + gtk_init (&argc, &argv); + + GtkWidget *window = gtk_window_new (GTK_WINDOW_TOPLEVEL); + gtk_widget_show (window); + gtk_window_set_title (GTK_WINDOW (window), "Hello"); + g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL); + + GtkWidget *drawing_area = gtk_drawing_area_new (); + gtk_widget_show (drawing_area); + gtk_container_add (GTK_CONTAINER (window), drawing_area); + gtk_widget_set_size_request (drawing_area, WIDTH, HEIGHT); + g_signal_connect (drawing_area, "draw", G_CALLBACK (draw), NULL); + + g_timeout_add (50, (GSourceFunc) timer, drawing_area); + + gtk_main (); + return 0; +} diff --git a/20191205/pendulum-3.c b/20191205/pendulum-3.c new file mode 100644 index 0000000000000000000000000000000000000000..35b0b67aeb0d11aa2a81aba204c97ca96504ad89 --- /dev/null +++ b/20191205/pendulum-3.c @@ -0,0 +1,86 @@ +#include <gtk/gtk.h> +#include <math.h> + +#define WIDTH 320 +#define HEIGHT 240 +#define GAP (HEIGHT / 20) +#define r GAP +#define visual_length (HEIGHT - 2 * GAP - r) + +#define phi0 (-0.5) +#define omega0 0.0 +#define t0 0.0 +#define g 9.81 +#define l 1.0 +#define dt 0.02 + +double t = t0; +double phi_with_sin = phi0; +double omega_with_sin= omega0; +double phi_without_sin = phi0; +double omega_without_sin= omega0; + +void draw_pendulum (cairo_t *c, double phi, GdkRGBA *colour) +{ + int x = WIDTH / 2 + visual_length * sin (phi); + int y = GAP + visual_length * cos (phi); + + gdk_cairo_set_source_rgba (c, colour); + cairo_move_to (c, WIDTH / 2, 10); + cairo_line_to (c, x, y); + cairo_stroke (c); + cairo_arc (c, x, y, r, 0, 2 * G_PI); + cairo_fill (c); +} + +gboolean draw (GtkWidget *widget, cairo_t *c, gpointer data) +{ + GdkRGBA blue = { 0.0, 0.5, 1.0, 1.0 }; + GdkRGBA orange = { 1.0, 0.5, 0.0, 1.0 }; + GdkRGBA green = { 0.0, 0.5, 0.0, 1.0 }; + + double A = phi0; + double B = 0.5 * M_PI; /* 90° */ + double phi_analytic = A * sin (sqrt (g / l) * t + B); + + draw_pendulum (c, phi_with_sin, &blue); + draw_pendulum (c, phi_without_sin, &orange); + draw_pendulum (c, phi_analytic, &green); + + return FALSE; /* TRUE to stop other handlers from being invoked for the event. + FALSE to propagate the event further. */ +} + +gboolean timer (GtkWidget *widget) +{ + t += dt; + phi_with_sin += omega_with_sin * dt; + omega_with_sin += - dt * g / l * sin (phi_with_sin); + phi_without_sin += omega_without_sin * dt; + omega_without_sin += - dt * g / l * phi_without_sin; + + gtk_widget_queue_draw_area (widget, 0, 0, WIDTH, HEIGHT); + g_timeout_add (50, (GSourceFunc) timer, widget); + return FALSE; +} + +int main (int argc, char **argv) +{ + gtk_init (&argc, &argv); + + GtkWidget *window = gtk_window_new (GTK_WINDOW_TOPLEVEL); + gtk_widget_show (window); + gtk_window_set_title (GTK_WINDOW (window), "Hello"); + g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL); + + GtkWidget *drawing_area = gtk_drawing_area_new (); + gtk_widget_show (drawing_area); + gtk_container_add (GTK_CONTAINER (window), drawing_area); + gtk_widget_set_size_request (drawing_area, WIDTH, HEIGHT); + g_signal_connect (drawing_area, "draw", G_CALLBACK (draw), NULL); + + g_timeout_add (50, (GSourceFunc) timer, drawing_area); + + gtk_main (); + return 0; +} diff --git a/20191205/pendulum.pdf b/20191205/pendulum.pdf new file mode 120000 index 0000000000000000000000000000000000000000..7d1d87305cdb8840a248ff2207538d758464f452 --- /dev/null +++ b/20191205/pendulum.pdf @@ -0,0 +1 @@ +../common/pendulum.pdf \ No newline at end of file diff --git a/20191205/pgscript.sty b/20191205/pgscript.sty new file mode 120000 index 0000000000000000000000000000000000000000..95c888478c99ea7fda0fd11ccf669ae91be7178b --- /dev/null +++ b/20191205/pgscript.sty @@ -0,0 +1 @@ +../common/pgscript.sty \ No newline at end of file diff --git a/20191205/pgslides.sty b/20191205/pgslides.sty new file mode 120000 index 0000000000000000000000000000000000000000..5be1416f4216f076aa268901f52a15d775e43f64 --- /dev/null +++ b/20191205/pgslides.sty @@ -0,0 +1 @@ +../common/pgslides.sty \ No newline at end of file