| Zeile 39: |
Zeile 39: |
| | | | |
| | == Nützliches für die GUI-Programmierung == | | == Nützliches für die GUI-Programmierung == |
| − | Wer nicht gleich ein Library portieren will um schiefe Linien zu zeichnen kann diesen Beispielcode verwenden. | + | Wer nicht gleich ein ganze Graphik-Library portieren will um schiefe Linien zu zeichnen, sollte sich den [http://de.wikipedia.org/wiki/Bresenham-Algorithmus Bresenham-Algorithmus] mal genauer Anschauen. |
| − | '''Der Beispielcode setzt voraus das es die Funktion setpixel(int x,int y, unsigned char color) gibt.(die Funktion sollte einen Pixel zeichnen)'''
| |
| − | <c>
| |
| − | #define sgn(x) ((x<0)?-1:((x>0)?1:0))
| |
| − | void line(int x1, int y1, int x2, int y2, unsigned char color) {
| |
| − | int dx,dy,sdx,sdy,px,py,dxabs,dyabs,i;
| |
| − | int slope;
| |
| − | | |
| − | dx = x2-x1;
| |
| − | dy = y2-y1;
| |
| − | dxabs = abs(dx);
| |
| − | dyabs = abs(dy);
| |
| − | sdx = sgn(dx);
| |
| − | sdy = sgn(dy);
| |
| − | if(dxabs >= dyabs) {
| |
| − | slope = dy / dx;
| |
| − | for(i=0;i!=dx;i+=sdx) {
| |
| − | px = i + x1;
| |
| − | py = slope * i + y1;
| |
| − | setpixel(px, py, color);
| |
| − | }
| |
| − | } else {
| |
| − | slope = dx / dy;
| |
| − | for(i=0;i!=dy;i+=sdy) {
| |
| − | px = slope * i + x1;
| |
| − | py = i + y1;
| |
| − | setpixel(px, py, color);
| |
| − | }
| |
| − | }
| |
| − | }
| |
| − | </c>
| |
| − | Hat man diese Funktion ist vieles getan.<br />
| |
| − | Eine leichte Übung wäre jetzt eine Funktion um ein Rechteck zu zeichnen:
| |
| − | <c>
| |
| − | void rectangle(int x, int y, int w, int h, unsigned char color) {
| |
| − | line(x, y, x + h, y, color);
| |
| − | line(x, y, x, y + w, color);
| |
| − | line(x, y + w, x + h, y + w, color);
| |
| − | line(x + h, y, x + h, y + w, color);
| |
| − | }
| |
| − | </c>
| |
| | | | |
| | == Links == | | == Links == |