Posts: 650
Threads: 26
Joined: Aug 2004
How do I draw lines? Um, I should probably clarify. Draw a line using PSET. Just like MS.
When I try I screw it up. So how does MS do it when I ask them to draw a line? Thanks.
quote="whitetiger0990"]whitetiger is.. WHITE POWER!!! [/quote]
Here
Posts: 3,368
Threads: 195
Joined: Jan 2003
The "Bresenham" line algorithm, my friend. Can also draw circles if yer smart..
Quote:SUB bline (x, y, x2, y2)
dx = x2 - x
dy = y2 - y
IF dy < 0 THEN dy = -dy: yinc = -1 ELSE xinc = 1
IF dx < 0 THEN dx = -dx: xinc = -1 ELSE yinc = 1
IF dx > dy THEN
d1 = dy + dy - dx - dx
d = d1 + dx
d0 = d + dx
DO
IF x = x2 THEN EXIT DO
x = x + xinc
IF d < 0 THEN d = d + d0 ELSE d = d + d1: y = y + yinc
PSET (x, y)
LOOP
ELSE
d1 = dx + dx - dy - dy
d = d1 + dy
d0 = d + dy
DO
IF y = y2 THEN EXIT DO
y = y + yinc
IF d < 0 THEN d = d + d0 ELSE d = d + d1
x = x + xinc
PSET (x, y)
LOOP
END IF
END SUB
Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."
Visit www.neobasic.net to see rubbish in all its finest.
Posts: 650
Threads: 26
Joined: Aug 2004
:???: same coordinates:
Code: screen 13,,,1
x = 30
y = 45
x2 = 70
y2 = 140
line (30,45)-(70,140),4
dx = x2 - x
dy = y2 - y
IF dy < 0 THEN dy = -dy: yinc = -1 ELSE xinc = 1
IF dx < 0 THEN dx = -dx: xinc = -1 ELSE yinc = 1
IF dx > dy THEN
d1 = dy + dy - dx - dx
d = d1 + dx
d0 = d + dx
DO
IF x = x2 THEN EXIT DO
x = x + xinc
IF d < 0 THEN d = d + d0 ELSE d = d + d1: y = y + yinc
PSET (x, y)
LOOP
ELSE
d1 = dx + dx - dy - dy
d = d1 + dy
d0 = d + dy
DO
IF y = y2 THEN EXIT DO
y = y + yinc
IF d < 0 THEN d = d + d0 ELSE d = d + d1
x = x + xinc
PSET (x, y)
LOOP
END IF
sleep
quote="whitetiger0990"]whitetiger is.. WHITE POWER!!! [/quote]
Here
Posts: 544
Threads: 27
Joined: Jan 2005
A little simpler?
Code: 'create a straight line from P1 to P2
'P1(x1,y1)
'P2(x2,y2)
x1 = 10: y1 = 20
x2 = 100: y2 = 150
dx = 1
dy = dx * (y2 - y1) / (x2 - x1)
y = y1
SCREEN 13
FOR x = x1 TO y1
PSET (x, y), 15
y = y + 1
NEXT x
Ralph, using QuickBASIC 4.5 and Windows XP Home Edition and Service Pack 2, with HP LaserJet 4L printer.
Posts: 650
Threads: 26
Joined: Aug 2004
Alright, look people, itâs not that hard. I wanna know how do draws lines identical to how MS or whoever draw lines. That means I enter the first set of coordinates then the second, and the line gets drawn. Unless the code needed is ten thousand lines long, and a hundred libs involved, it shouldnât be that hard. Just say âI donât knowâ and Iâll stop asking.
I mean, I appreciate the help (in this case, Ralph and Agamemnus) but it just seems like no matter what I ask for or how I articulate it, I never seem to get the results I ask for. âI need code that draws linesâ couldnât be simpler; one of the most basic functions in QBASIC and other crude primitive graphical programming languages and still, no one can give me the right answer. This is why I hardly ask questions, because you people employ sort of passive aggressive attack on my mind. Do you guys go to another forum and mock me, keeping an open log of my mental distress. Placing bets on when Iâll snap. See on the news that some kid from San Diego when on a killing spree.
Again, I appreciate the help, but I feel like Iâm a lab rat in some psychology experiment
Agamemnus, the code you showed me almost worked. And Ralph, all I did was change x1 from 10 to 106. and it rendered your code completely useless. And what did I do? Added one number; A 8-bit change is it? And it didnât even draw a single pixel. I mean, do you guys even test the code that you post? At all?
Look, Iâm not a very good programmer, and I have at least five or six more, different, questions I could ask but I donât for this very reason. But this time I thought, âIâm just asking how to draw lines, there is no way to mess this one up⦠itâs impossible!â but I was wrong.
And this extends to all my other threads in which I ask questions by the way. Not just this one and you guys (both of you)
So if there is anybody that isnât to incredibly pissed at me, what is the code to draw lines?
Thank you for letting me rant my built up frustrations.
quote="whitetiger0990"]whitetiger is.. WHITE POWER!!! [/quote]
Here
Posts: 508
Threads: 49
Joined: May 2002
Here's something I coded not too long ago to draw a line in a sprite editor I am working on. It's in C'ish syntax though...
[syntax="C"]
public void DrawLine(Sprite sprite, int color)
{
double slope = (double)(y2 - y1) / (double)(x2 - x1);
double x = (double)this.x1+0.5;
double y = (double)this.y1+0.5;
if(Math.Abs(slope) > 1)
{
slope = (double)(x2 - x1) / (double)(y2 - y1);
if(this.y1 < this.y2)
{
while((int)y <= this.y2)
{
sprite[(int)x, (int)y] = color;
y += 1;
x += slope;
}
}
else
{
while((int)y >= this.y2)
{
sprite[(int)x, (int)y] = color;
y -= 1;
x -= slope;
}
}
}
else
{
if(this.x1 < this.x2)
{
while((int)x <= this.x2)
{
sprite[(int)x, (int)y] = color;
x += 1;
y += slope;
}
}
else
{
while((int)x >= this.x2)
{
sprite[(int)x, (int)y] = color;
x -= 1;
y -= slope;
}
}
}
}
[/syntax]
You should be able to interpret this to basic. It shouldn't be too hard. If you need help, just ask and I'll do it for you.
Oh, yeah:
this.x1, this.y1 = starting coordinate
this.x2, this.y2 = ending coordinate
Posts: 488
Threads: 19
Joined: Sep 2005
Here's the transalated code with a little example for you:
Code: SUB DrawLine(Sprite AS ANY PTR, colour AS INTEGER, _
x1 as integer, y1 as integer, _
x2 as integer, y2 as integer)
DIM AS DOUBLE slope,x,y
slope = (y2 - y1) / (x2 - x1)
x = x1+0.5
y = y1+0.5
IF (ABS(slope) > 1) THEN
slope = (x2 - x1) / (y2 - y1)
IF (y1 < y2) THEN
WHILE (INT(y) <= y2)
PSET sprite,(INT(x),INT(y)),colour
y += 1
x += slope
WEND
ELSE
WHILE (INT(y) >= y2)
PSET sprite,(INT(x),INT(y)),colour
y -= 1
x -= slope
WEND
END IF
ELSEIF(x1 < x2) THEN
WHILE(INT(x) <= x2)
PSET sprite,(INT(x),INT(y)),colour
x += 1
y += slope
WEND
ELSE
WHILE(INT(x) >= x2)
PSET sprite,(INT(x),INT(y)),colour
x -= 1
y -= slope
WEND
END IF
END SUB
screenres 320,200,32
dim image as any ptr
image = imagecreate(10,10,&h808080)
DrawLine(image,&h125634,1,1,7,7)
put(10,10),image
sleep
Hope it helps, I liked it very much Radical Raccoon, I find it much easier to uinderstand then the "Bresenham" line algorithm.
Posts: 544
Threads: 27
Joined: Jan 2005
axipher:
Is your code in QuickBASIC 4.5? I can't seem to make your
'y += 1 or your 'y -= 1' make any sense in 4.5. Maybe they should be stated as 'y = y + 1' and 'y = y - 1'? Or, maybe, you should cite the progam and version you are using?
Ralph, using QuickBASIC 4.5 and Windows XP Home Edition and Service Pack 2, with HP LaserJet 4L printer.
Posts: 488
Threads: 19
Joined: Sep 2005
Sorry about that, I'm using a new version of HyperBASIC my friend was having me test, it also runs in DOS mode so I figured it should work, I'll fix right now.
Posts: 488
Threads: 19
Joined: Sep 2005
Actually, I don't think I can do this for you, I only used QB for about 1 or 2 months and I never erally got into buffers and such in QB, but I tested this code in FreeBASIC and it works perfectly. Maybe someone else can convert it to QB for you, sorry .
|