public Bitmap DDA(int x1, int y1, int x2, int y2, Boolean apagavel, Bitmap map) { int width = pbGraficos.Width; int height = pbGraficos.Height; //lock dados bitmap origem BitmapData data = map.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); unsafe { int Length, I; double X, Y, Xinc, Yinc; Desenho reta = new Reta(); Length = Math.Abs(x2 - x1); if (Math.Abs(y2 - y1) > Length) { Length = Math.Abs(y2 - y1); } Xinc = (double)(x2 - x1) / Length; Yinc = (double)(y2 - y1) / Length; X = x1; Y = y1; if (X < x2) { while (X < x2) { PrintaPixel((int)Math.Round(X), (int)Math.Round(Y), data); if (!apagavel) { reta.add(new Point((int)Math.Round(X), (int)Math.Round(Y))); } X = X + Xinc; Y = Y + Yinc; } } else { while (X > x2) { PrintaPixel((int)Math.Round(X), (int)Math.Round(Y), data); if (!apagavel) { reta.add(new Point((int)Math.Round(X), (int)Math.Round(Y))); } X = X + Xinc; Y = Y + Yinc; } } if (reta.getPixels().Count > 0) { desenho.Add(reta); } } map.UnlockBits(data); return(map); }
Bitmap bresenham(int x1, int y1, int x2, int y2, Boolean apagavel, BitmapData data) { unsafe { int declive; int dx, dy, incE, incNE, d, x, y; dx = x2 - x1; dy = y2 - y1; if (dx < 0) { return(bresenham(x2, y2, x1, y1, apagavel, data)); } if (dy < 0) { declive = -1; } else { declive = 1; } Desenho reta = new Reta(cor); x = x1; y = y1; PrintaPixel(x, y, data); if (!apagavel) { reta.add(new Point(x, y)); } if (dx > declive * dy) { if (dy < 0) { d = 2 * dy + dx; while (x < x2) { if (d < 0) { d += 2 * (dy + dx); x++; y--; } else { d += 2 * dy; x++; } PrintaPixel(x, y, data); if (!apagavel) { reta.add(new Point(x, y)); } } } else { d = 2 * dy - dx; while (x < x2) { if (d < 0) { // escolhido é o I d += 2 * dy; x++; // varia apenas no eixo x } else { // escolhido é o S d += 2 * (dy - dx); x++; y++; } PrintaPixel(x, y, data); if (!apagavel) { reta.add(new Point(x, y)); } } } } else { if (dy < 0) { // caso y2<y1 d = dy + 2 * dx; while (y > y2) { if (d < 0) { d += 2 * dx; y--; // varia apenas no eixo y } else { d += 2 * (dy + dx); x++; y--; } PrintaPixel(x, y, data); if (!apagavel) { reta.add(new Point(x, y)); } } } else { // caso y1<y2 d = dy - 2 * dx; while (y < y2) { if (d < 0) { d += 2 * (dy - dx); x++; y++; } else { d += -2 * dx; y++; // varia apenas no eixo y } PrintaPixel(x, y, data); if (!apagavel) { reta.add(new Point(x, y)); } } } } PrintaPixel(x, y, data); if (!apagavel) { reta.add(new Point(x, y)); } } map.UnlockBits(data); return(map); /* * // Constante de Bresenham * incE = 2 * dy; * incNE = 2 * dy - 2 * dx; * d = 2 * dy - dx; * y = y1; * for (x = x1; x <= x2; x++) * { * PrintaPixel(x, y, data); * if (d <= 0) * { * d += incE; * } * else * { * d += incNE; * y += declive; * } * }*/ }
public Bitmap eqReta(int x1, int y1, int x2, int y2, Boolean apagavel, Bitmap map) { int width = pbGraficos.Width; int height = pbGraficos.Height; //lock dados bitmap origem BitmapData data = map.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); unsafe { Boolean isX; double dy = y2 - y1; double dx = x2 - x1; double m = dx != 0 && dy != 0 ? dy / dx : 0; int y; int x; if (dx < dy) { isX = false; } else { isX = true; } int inicio, fim; Desenho reta = new Reta(cor); if (isX) { if (x1 > x2) { inicio = x2; fim = x1; } else { inicio = x1; fim = x2; } for (x = inicio; x <= fim; x++) { y = Convert.ToInt32(y1 + m * (x - x1)); PrintaPixel(x, y, data); if (!apagavel) { reta.add(new Point(x, y)); } } } else { if (y1 > y2) { inicio = y2; fim = y1; } else { inicio = y1; fim = y2; } for (y = inicio; y <= fim; y++) { x = Convert.ToInt32(x1 + ((y - y1) / m)); PrintaPixel(x, y, data); if (!apagavel) { reta.add(new Point(x, y)); } } } if (reta.getPixels().Count > 0) { desenho.Add(reta); } } //unlock imagem origem map.UnlockBits(data); return(map); }