1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| #pragma once #include <cmath> #include <iostream>
void drawPixel(int x, int y, int color) { }
void dDALine(int x0, int y0, int x1, int y1, int color) { int x; float dx, dy, y, k; dx = x1 - x0; dy = y1 - y0; k = dy / dx; y = y0; for (x = x0; x <= x1; x++) { drawPixel(x, round(y + 0.5), color); y = y + k; } }
void middlePointLine(int x0, int y0, int x1, int y1, int color) { int a, b, d1, d2, d, x, y; a = y0 - y1; b = x1 - x0; d = 2 * a + b; d1 = 2 * a; d2 = 2 * (a + b); x = x0; y = y0; drawPixel(x, y, color); while (x < x1) { if (d < 0) { x++; y++; d += d2; } else { x++; d += d1; } drawPixel(x, y, color); } }
void bresenhamLine(int x0, int y0, int x1, int y1, int color) { int x, y, dx, dy, e; dx = x1 - x0; dy = y1 - y0; e = -dx; x = x0; y = y0; for (int i = 0; i <= dx; i++) { drawPixel(x, y, color); x++; e = e + 2 * dy; if (e >= 0) { y++; e = e - 2 * dx; } } }
|