EDN Admin
Well-known member
ok, so all you c++ wizards who are also vb wizards, I am hoping to translate a small c++ function to vb...
I have done my best, can someone who knows both languages give me some assistance?
heres the c++ code(I just need the bresenham function):
<pre class="prettyprint
//**************************************
// Name: Bresenham Line Algorithm
// Description:Efficient LIne Drawing Algorithm
// By: Sunny Pal Singh
//
//This code is copyrighted and has// limited warranties.Please see http://www.PlanetSourceCode.com/vb/scripts/ShowCode.asp?txtCodeId=3800&lngWId=10//for details.//**************************************
//Efficient Line Drawing Algorithm(Bresenham)
# include<iostream.h>
# include<conio.h>
# include<graphics.h>
# include<math.h>
# include<dos.h>
void bresenham(int x1,int y1,int x2,int y2)
{
int x,y,dx,dy,p,twody,twodydx,xend;
dx = abs(x2-x1);
dy = abs(y2-y1);
p = (2*dy) - dx;
twody = 2 * dy;
twodydx = 2 * (dy-dx);
if(x1>x2)
{
x = x2;
y = y2;
xend = x1;
}
else
{
x = x1;
y = y1;
xend = x2;
}
putpixel(x,y,5);
while(x<xend)
{
x++;
if(p<0)
p += twody;
else
{
y++;
p += twodydx;
}
delay(10);
putpixel(x,y,5);
}
}
int main()
{
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode,"c:\tc\");
int x1,y1,x2,y2;
cout<<"nEnter two points : n";
cout<<"nFirst point : ";
cin>>x1>>y1;
cout<<"nSecond point : ";
cin>>x2>>y2;
bresenham(x1,y1,x2,y2);
cout<<"nPress any key to exit...!";
getch();
return 0;
}[/code]
<br/>
Heres what I have translated...
<pre class="prettyprint lang-vb Public Sub Bresenham(ByVal x1 As Integer, ByVal y1 As Integer, ByVal x2 As Integer, ByVal y2 As Integer)
Dim Dx As Double = Math.Abs(x2 - x1)
Dim Dy As Double = Math.Abs(y2 - y1)
Dim x As Integer
Dim y As Integer
Dim xend As Double
Dim P As Double = (2 * Dy) - Dx
Dim twody As Double = 2 * Dy
Dim twodydx As Integer = 2 * (Dy - Dx)
If x1 > x2 Then
x = x2
y = y2
xend = x1
Else
x = x1
y = y1
xend = x2
End If
DrawPixel(x, y, Color.Black)
Do While x < xend
x = x + 1
If P < 0 Then
P = P + twody
Else
y = y + 1
P = P + twodydx
End If
delay(10)
DrawPixel(x, y, Color.Black)
Loop
End Sub[/code]
<pre class="prettyprint lang-vb Public Sub delay(ByVal milliseconds As Integer)
Dim a As New Stopwatch
a.Reset()
a.Start()
Do
Loop Until a.ElapsedMilliseconds >= milliseconds
a.Stop()
End Sub
Public Sub DrawPixel(ByVal X As Integer, ByVal Y As Integer, ByVal Color As Color)
Dim G As Graphics
G = Form1.CreateGraphics
Dim bm As New Bitmap(1, 1)
bm.SetPixel(0, 0, Color)
G.DrawImageUnscaled(bm, X, Y)
End Sub[/code]
<br/>
Please if I have translated this incorrectly, help me get it right =) <hr class="sig If you want something youve never had, you need to do something youve never done. If you believe something to be true, then one day you will be called upon to demonstrate that truth.
View the full article
I have done my best, can someone who knows both languages give me some assistance?
heres the c++ code(I just need the bresenham function):
<pre class="prettyprint
//**************************************
// Name: Bresenham Line Algorithm
// Description:Efficient LIne Drawing Algorithm
// By: Sunny Pal Singh
//
//This code is copyrighted and has// limited warranties.Please see http://www.PlanetSourceCode.com/vb/scripts/ShowCode.asp?txtCodeId=3800&lngWId=10//for details.//**************************************
//Efficient Line Drawing Algorithm(Bresenham)
# include<iostream.h>
# include<conio.h>
# include<graphics.h>
# include<math.h>
# include<dos.h>
void bresenham(int x1,int y1,int x2,int y2)
{
int x,y,dx,dy,p,twody,twodydx,xend;
dx = abs(x2-x1);
dy = abs(y2-y1);
p = (2*dy) - dx;
twody = 2 * dy;
twodydx = 2 * (dy-dx);
if(x1>x2)
{
x = x2;
y = y2;
xend = x1;
}
else
{
x = x1;
y = y1;
xend = x2;
}
putpixel(x,y,5);
while(x<xend)
{
x++;
if(p<0)
p += twody;
else
{
y++;
p += twodydx;
}
delay(10);
putpixel(x,y,5);
}
}
int main()
{
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode,"c:\tc\");
int x1,y1,x2,y2;
cout<<"nEnter two points : n";
cout<<"nFirst point : ";
cin>>x1>>y1;
cout<<"nSecond point : ";
cin>>x2>>y2;
bresenham(x1,y1,x2,y2);
cout<<"nPress any key to exit...!";
getch();
return 0;
}[/code]
<br/>
Heres what I have translated...
<pre class="prettyprint lang-vb Public Sub Bresenham(ByVal x1 As Integer, ByVal y1 As Integer, ByVal x2 As Integer, ByVal y2 As Integer)
Dim Dx As Double = Math.Abs(x2 - x1)
Dim Dy As Double = Math.Abs(y2 - y1)
Dim x As Integer
Dim y As Integer
Dim xend As Double
Dim P As Double = (2 * Dy) - Dx
Dim twody As Double = 2 * Dy
Dim twodydx As Integer = 2 * (Dy - Dx)
If x1 > x2 Then
x = x2
y = y2
xend = x1
Else
x = x1
y = y1
xend = x2
End If
DrawPixel(x, y, Color.Black)
Do While x < xend
x = x + 1
If P < 0 Then
P = P + twody
Else
y = y + 1
P = P + twodydx
End If
delay(10)
DrawPixel(x, y, Color.Black)
Loop
End Sub[/code]
<pre class="prettyprint lang-vb Public Sub delay(ByVal milliseconds As Integer)
Dim a As New Stopwatch
a.Reset()
a.Start()
Do
Loop Until a.ElapsedMilliseconds >= milliseconds
a.Stop()
End Sub
Public Sub DrawPixel(ByVal X As Integer, ByVal Y As Integer, ByVal Color As Color)
Dim G As Graphics
G = Form1.CreateGraphics
Dim bm As New Bitmap(1, 1)
bm.SetPixel(0, 0, Color)
G.DrawImageUnscaled(bm, X, Y)
End Sub[/code]
<br/>
Please if I have translated this incorrectly, help me get it right =) <hr class="sig If you want something youve never had, you need to do something youve never done. If you believe something to be true, then one day you will be called upon to demonstrate that truth.
View the full article