Nadesłany przez Andrzej Borucki, 10 stycznia 2012 19:23
Kod przedstawiony poniżej przedstawia główną część rozwiązania problemu.Pobierz pełne rozwiązanie.
Jeżeli nie odpowiada Ci sposób formatowania kodu przez autora skorzystaj z pretty printer'a i dostosuj go automatycznie do siebie.
BresenhamLine/WriteableBitmapDrawer.cs:
//Andrzej Borucki
//www.algorytm.org
//Algorytm kreslenia odcinkow
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media.Imaging;
namespace BresenhamLine
{
class WriteableBitmapDrawer
{
WriteableBitmap writeableBitmap;
IntPtr backBuffer;
public WriteableBitmapDrawer(WriteableBitmap writeableBitmap)
{
this.writeableBitmap = writeableBitmap;
backBuffer = writeableBitmap.BackBuffer;
}
public unsafe void SetPixel(int x, int y, int color)
{
byte* p = (byte*)backBuffer + y * writeableBitmap.BackBufferStride+ x*4;
*((int*)p) = color;
}
//http://pl.wikipedia.org/wiki/Algorytm_Bresenhama
unsafe public void FirstBresenham(int x1, int y1, int x2, int y2, int color)
{
int d, dx, dy, ai, bi, xi, yi;
int x = x1, y = y1;
// determining the direction of drawing
if (x1 < x2)
{
xi = 1;
dx = x2 - x1;
}
else
{
xi = -1;
dx = x1 - x2;
}
// determining the direction of drawing
if (y1 < y2)
{
yi = 1;
dy = y2 - y1;
}
else
{
yi = -1;
dy = y1 - y2;
}
// first pixel
SetPixel(x, y, color);
// the leading axis OX
if (dx > dy)
{
ai = (dy - dx) * 2;
bi = dy * 2;
d = bi - dx;
// loop for x
while (x != x2)
{
// factor test
if (d >= 0)
{
x += xi;
y += yi;
d += ai;
}
else
{
d += bi;
x += xi;
}
SetPixel(x, y, color);
}
}
// the leading axis OY
else
{
ai = ( dx - dy ) * 2;
bi = dx * 2;
d = bi - dy;
// loop for y
while (y != y2)
{
// factor test
if (d >= 0)
{
x += xi;
y += yi;
d += ai;
}
else
{
d += bi;
y += yi;
}
SetPixel(x, y, color);
}
}
}
//poprzednia wersja po optymalizacji
unsafe public void Bresenham(int x1, int y1, int x2, int y2, int color)
{
int d, dx, dy, ai, bi, xi, yi;
int pxi, pyi;
int x = x1, y = y1;
// determining the direction of drawing
if (x1 < x2)
{
xi = 1;
pxi = 4;
dx = x2 - x1;
}
else
{
xi = -1;
pxi = -4;
dx = x1 - x2;
}
// determining the direction of drawing
if (y1 < y2)
{
yi = 1;
pyi = writeableBitmap.BackBufferStride;
dy = y2 - y1;
}
else
{
yi = -1;
pyi = -writeableBitmap.BackBufferStride;
dy = y1 - y2;
}
// first pixel
//SetPixel(x, y, color);
byte* p = (byte*)backBuffer + y * writeableBitmap.BackBufferStride + x * 4;
*((int*)p) = color;
// the leading axis OX
if (dx > dy)
{
ai = (dy - dx) * 2;
bi = dy * 2;
d = bi - dx;
// loop for x
while (x != x2)
{
// factor test
if (d >= 0)
{
x += xi;
p += pxi;
y += yi;
p += pyi;
d += ai;
}
else
{
d += bi;
x += xi;
p += pxi;
}
//SetPixel(x, y, color);
*((int*)p) = color;
}
}
// the leading axis OY
else
{
ai = (dx - dy) * 2;
bi = dx * 2;
d = bi - dy;
// loop for y
while (y != y2)
{
// factor test
if (d >= 0)
{
x += xi;
p += pxi;
y += yi;
p += pyi;
d += ai;
}
else
{
d += bi;
y += yi;
p += pyi;
}
//SetPixel(x, y, color);
*((int*)p) = color;
}
}
}
}
}

