Tag: Nerd
Generic Bresenham’s Line Algorithm in Visual Basic .NET
by Jason on Nov.18, 2010, under .NET, C#, Code, Code, Programming, VB
' Author: Jason Morley (Source: http://www.morleydev.co.uk/blog/2010/11/18/generic-bresenhams-line-algorithm-in-visual-basic-net/) ' Licence: Public Domain Module Module1 Sub Swap(ByRef X As Long, ByRef Y As Long) Dim t As Long = X X = Y Y = t End Sub ' If the plot function returns true, the bresenham's line algorithm continues. ' if the plot function returns false, the algorithm stops Delegate Function PlotFunction(ByVal x As Long, ByVal y As Long) As Boolean Sub Bresenham(ByVal x1 As Long, ByVal y1 As Long, ByVal x2 As Long, ByVal y2 As Long, ByVal plot As PlotFunction) Dim steep As Boolean = (Math.Abs(y2 - y1) > Math.Abs(x2 - x1)) If (steep) Then Swap(x1, y1) Swap(x2, y2) End If If (x1 > x2) Then Swap(x1, x2) Swap(y1, y2) End If Dim deltaX As Long = x2 - x1 Dim deltaY As Long = y2 - y1 Dim err As Long = deltaX / 2 Dim ystep As Long Dim y As Long = y1 If (y1 < y2) Then ystep = 1 Else ystep = -1 End If For x As Long = x1 To x2 Dim result As Boolean If (steep) Then result = plot(y, x) Else result = plot(x, y) If (Not result) Then Exit Sub err = err - deltaY If (err < 0) Then y = y + ystep err = err + deltaX End If Next End Sub Function plot(ByVal x As Long, ByVal y As Long) As Boolean Console.WriteLine(x.ToString() + " " + y.ToString()) Return True 'This just prints each co-ord End Function Sub Main() ' example Bresenham(1, 1, 10, 15, New PlotFunction(AddressOf plot)) Console.ReadLine() End Sub End Module
I wrote this quickly for someone over on a roguelike forum whose Bresenham’s Line Algorithm code wasn’t working. What makes it generic? It uses delegate so you can plug any plotting function you like in there. This means it could be used for graphics, calculating line of sight, and much more without actually needing to touch the function or do needless position calculations.
C# version:
// Author: Jason Morley (Source: http://www.morleydev.co.uk/blog/2010/11/18/generic-bresenhams-line-algorithm-in-visual-basic-net/)
// Licence: Public Domain
using System;
namespace Bresenhams
{
/// <summary>
/// The Bresenham algorithm collection
/// </summary>
public static class Algorithms
{
private static void Swap<T>(ref T lhs, ref T rhs)
{
T temp;
temp = lhs;
lhs = rhs;
rhs = temp;
}
/// <summary>
/// Plot the line from (x0, y0) to (x1, y1)
/// </summary>
/// <param name="x0">The start x</param>
/// <param name="y0">The start y</param>
/// <param name="x1">The end x</param>
/// <param name="y1">The end y</param>
/// <param name="plot">The plotting function (parameters: x-co-ord, y co-ord. If eturns false, the algorithm stops)</param>
public static void Line(int x0, int y0, int x1, int y1, Func<int, int, bool> plot)
{
bool steep = Math.Abs(y1 - y0) > Math.Abs(x1 - x0);
if (steep)
{
Swap<int>(ref x0, ref y0);
Swap<int>(ref x1, ref y1);
}
if (x0 > x1)
{
Swap<int>(ref x0, ref x1);
Swap<int>(ref y0, ref y1);
}
var dX = (x1 - x0);
var dY = (y1 - y0);
var err = (dX / 2);
var ystep = (y0 < y1 ? 1 : -1);
var y = y0;
for (var x = x0; x <= x1; ++x)
{
if (!(steep ? plot(y, x) : plot(x, y)))
return;
err = err - dY;
if (err < 0)
{
y += ystep;
err += dX;
}
}
}
}
}
Quote of the Day
“It is practically impossible to teach good programming to students that have had a prior exposure to BASIC: as potential programmers they are mentally mutilated beyond hope of regeneration.”
How do we tell truths that might hurt? – Edsger W. Dijkstra
Hello World
by Jason on May.08, 2010, under Alcohol, Life, Technology
‘ello world ^^ So, how are you? I’m good. What’s the etiquette for these early posts? Whatever, I’m just gonna ramble for a bit.
So test time for me, yay…gotta revise sometimes, do mock lessons, all in preperation for the wonderful thing known as “Exams”. Guess what my first exam is? Statistics 1. May 27th. Yaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaay -,,,- No, I’m not looking forward to it.
Oh well, after tests I have a wonderful break though! Glastonbury. Yep, scored tickets (barely) and will be heading down there with two of my friends, Holly and Rianna. Unless I’ve pissed them off to the point they want to kill me by then (which is definitely possible knowing me).
In other news, I finished (pretty much) and handed in my Comp4 coursework. The programming itself was noway near finished, dear lord it wasn’t, but I was able to cleverly combine what was there, ingenious editing and the odd outright lie and should have enough to get a good grade.
Oh, I recently bought the Poets of the Fall album “Twilight Theatre“. It. Is. Amazing. But, then again, what else do you expect from The Poets of the Fall?
Erm, so……..more topics. Well, I have a new personal project I’ve started I’m calling “Project Pauper” for no real reason. When I actually have something almost noteworthy to show, I’ll actually be showing things.
Quote of the Day
“I burn to make you understand
One wrong word and it all may come crashing down
For the fates are devious by heart
They envy you your dreams, so they’ll let you drown”
Heal My Wounds by Poets of the Fall