old:programmieren:c:grafik
Table of Contents
Grafik-Funtionsplotter
Programm 1
#include <stdio.h> #include <unistd.h> #include <X11/Xlib.h> #include <X11/Xutil.h> #include <math.h> int main() { Display *dpy; Window win; if ((dpy = XOpenDisplay(NULL)) == NULL) { printf("Unable to open display\n"); return 1; } win = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0, 400, 300, 0, 0, BlackPixel(dpy, DefaultScreen(dpy))); XMapWindow(dpy, win); GC gc; gc = XCreateGC(dpy, win, 0, 0); XSync(dpy, False); XSetForeground(dpy, gc, WhitePixel(dpy, DefaultScreen(dpy))); XDrawLine (dpy, win, gc, 200, 0, 200, 300); XDrawLine (dpy, win, gc, 0, 150, 400, 150); float x; float y; int z; for (z=0 ; z<=30 ; z++) { for (x=0 ; x<=399 ; x++) { y = -150*exp(-1*((x-200)/80)*((x-200)/80))+(299-5*z); // usleep(10000); XSetForeground(dpy, gc, z*5000); XDrawPoint(dpy, win, gc, x, y); XSync(dpy, False); } } sleep(10); XDestroyWindow(dpy, win); XCloseDisplay(dpy); return 0; }
Compilieren 1
gcc -o xdraw xdraw.c -L/usr/X11R6/lib -lX11 -lm; ./xdraw
Programm 2
#include <stdio.h> #include <stdlib.h> #include <X11/Xlib.h> #include <X11/Xutil.h> #include <math.h> int main (int argc, char *argv[]) { Display *dpy; /* X server connection */ Window win; /* Window ID */ GC gc; /* GC to draw with */ unsigned long fg, bg, bd; /* Pixel values */ unsigned long bw = 5; /* Border width */ XGCValues gcv; /* Struct for creating GC */ XEvent event; /* Event received */ XSizeHints xsh; /* Size hints for window manager */ /* Open X display */ if ((dpy = XOpenDisplay(NULL)) == NULL) { printf("%s: can't open %s\n", argv[0], XDisplayName(NULL)); exit(1); } /* * Select colors for the border, the window background, and the * foreground. */ bd = BlackPixel(dpy, DefaultScreen(dpy)); bg = BlackPixel(dpy, DefaultScreen(dpy)); fg = WhitePixel(dpy, DefaultScreen(dpy)); /* Set original size/position */ xsh.flags = (PPosition | PSize); xsh.height = 300; xsh.width = 400; xsh.x = 200; xsh.y = 150; /* Create the window */ win = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), xsh.x, xsh.y, xsh.width, xsh.height, bw, bd, bg); /* Create a "Graphics context" for drawing */ gcv.foreground = fg; gcv.background = bg; gc = XCreateGC(dpy, win, (GCForeground | GCBackground), &gcv); /* Specify the event types we're interested in */ XSelectInput(dpy, win, ExposureMask); /* Map the window to make it visible */ XMapWindow(dpy, win); /* Main event loop */ while (1) { XNextEvent(dpy, &event); if (event.type == Expose && event.xexpose.count == 0) { XDrawLine (dpy, win, gc, 200, 0, 200, 300); XDrawLine (dpy, win, gc, 0, 150, 400, 150); float x; float y; for (x=0 ; x<=399 ; x++) { /* y=(x-200)*(x-200)*0.01+250; */ /* y = -100*sin(x/50)+150; */ y = -150*exp(-1*((x-200)/80)*((x-200)/80))+250; /* XDrawLine (dpy, win, gc, x, y, x, y); */ XDrawPoint(dpy, win, gc, x, y); } } } return 0; }
Compilieren 2
gcc -o xdraw xdraw.c -L/usr/X11R6/lib -lX11 -lm; ./xdraw
old/programmieren/c/grafik.txt · Last modified: 2023/01/11 20:29 by 127.0.0.1