Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Line Algorihtm

This thread is locked; no one can reply to it. rss feed Print
Line Algorihtm
fatboy2
Member #8,239
January 2007

I need help with writing a line drawing algorithm. I want the line to start from a fixed point and then draw to different points. For example, the line starts from a fixed point and then draws to where the mouse is. I want the line to change each time I move the mouse. i want the line to be drawn like in Ms Paint.

Matthew Leverton
Supreme Loser
January 1999
avatar

fatboy2
Member #8,239
January 2007

But that would draw only one line, i want the line to constantly change shape as i pull the mouse, like MsPaint
I want a constantly changing line. not many lines like this routine would give me

Matthew Leverton
Supreme Loser
January 1999
avatar

Your problem is so simple, you really should try to think about how to solve it yourself. If you cannot, then you really should get a new hobby.

Mr. Big
Member #6,196
September 2005

A line is a line.
If you want the line to "change shape", what you need is animation.

fatboy2
Member #8,239
January 2007

I do not need animation, I want to be able to drag the line with the mouse like in Paint in real time. The routine that you gave me would draw lots of lines instead of 1.

PS If my problem is so simple why do not you just give the answer?

Mr. Big
Member #6,196
September 2005

Programmers these days aren't what they used to be, are they? ::)

P.S:
Fatboy, of course this is an excuse.
Your problem is too difficult for us. ;D

Evert
Member #794
November 2000
avatar

Quote:

If my problem is so simple why do not you just give the answer?

Because you should really be able to think of it yourself if you ever want to do any serious programming, that's why. Definately not because we don't know how.

Anyway, here's a hint to get you started: you draw a line from (x, y) to the location of the mouse. Then the mouse moves, and you draw a new line to its new position. Your problem? The old line is still there. You figure it out from here.

By the way,

Quote:

like in Paint

This is not as clear a reference as you might think it is.

fatboy2
Member #8,239
January 2007

I knew that the problem is the old present without you having to tell me. I do not know how to get rid of the old line. I think that it is possible to remove the old lines by plotting individual pixels along them. The thing is also that I want to be able to do that on various backgrounds, so I need to take the color of each point on the line and then plot pixels with the colors that these pixels were previously.

PS I think that you are all just making up excuses for not helping

Matthew Leverton
Supreme Loser
January 1999
avatar

Have you ever heard of double buffering? You're supposed to draw from all of your sources to an intermediate bitmap. Then you copy that bitmap to the screen. Once you do that, your problem is trivial.

Mr. Big
Member #6,196
September 2005

Basically, if you're writing a paint program, you'll need two bitmaps. (Not counting 'screen'.)
One of them is the actual drawing, that is, you don't draw the "changing" line on it, only the final line.
On the second, you first draw the first bitmap (the one with the drawing), then draw the "changing" line on top of it, then wait for a certain amount of time and repeat.
That bitmap you draw on the actual 'screen' bitmap.
That's double-buffered animation.

Evert
Member #794
November 2000
avatar

Quote:

The thing is also that I want to be able to do that on various backgrounds, so I need to take the color of each point on the line and then plot pixels with the colors that these pixels were previously.

That's already a better question to ask, because it at least makes it clear that you've actually thought about the problem. This isn't clear in your original message.

Quote:

I think that you are all just making up excuses for not helping

Right. On that note, don't expect help from me in the future.

gnolam
Member #2,030
March 2002
avatar

Quote:

PS I think that you are all just making up excuses for not helping

Matthew already told you what that "excuse" is. If you can't figure this out after the information you've already been given, programming just isn't for you. Seriously.
Even a cursory glance at either the manual or the examples should give you all the information you need to solve this...

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

Johan Halmén
Member #1,550
September 2001

What we have here is a difficulty to exactly know the level of abstraction. I have a strange BASIC tool on my old Mac (68k). It had this funny sprite data type. You could define a variable as a sprite and attach it to a bitmap resource. Then you could perform some draw_sprite function that place the sprite on the screen. And doing draw_sprite again with other coordinates moved the sprite to its new place. So the sprite could be seen only on one place on the screen at a time. A bit like the line drawing thing fatboy2 is asking for.

Programming with Allegro is on a lower abstraction level. "Line writing algorithm" is in the first place wrong terminology for fatboy2's problem. As Matthew said, this is a very simple thing. Only that no one would expect such a function to be incorporated in Allegro's set of drawing primitives functions.

That said, *click*

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Years of thorough research have revealed that the red "x" that closes a window, really isn't red, but white on red background.

Years of thorough research have revealed that what people find beautiful about the Mandelbrot set is not the set itself, but all the rest.

kentl
Member #2,905
November 2002

fatboy2:
First you can start out with this algorithm given in pseudo-code:

  1. Wait until the user clicks on a position. Store this as origoX and origoY.

  2. Erase everything on the screen.

  3. Draw the line from origoX,origoY to wherever the mouse pointer is.

  4. Go to [2] until end of program.

Translate it all to C and Allegro API calls. You will get a program that does the above, however you probably won't be satisfied as the line flickers terribly. Now it's time to add double buffering to it to fix the flicker.

Come back and post the code you've got for the little algorithm above without double buffering. Then we'll talk about double buffering to get rid of the flicker and to have different backgrounds to get it as you want it to be.

Johan Halmén
Member #1,550
September 2001

[branestorming]
On a 1024*768 screen, any line is at its most 1024 pixel, when drawn with do_line(). One could think of actually saving each pixel colour in a 1024 by 2 array.

When line gets first time drawn:
For each pixel in the line, save previous colour in the array and draw the line colour on the pixel. Save the start and end coordinates of the line.

When next line gets drawn:
Redraw the previous line using the saved coordinates for the said line and using the colours saved in the array. After that,
for each pixel in the new line, save previous colour in the array and draw the line colour on the pixel. Save the start and end coordinates of the line.

This might be faster than the double buffering and blitting.
[/branestorming]

  • aching head*

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Years of thorough research have revealed that the red "x" that closes a window, really isn't red, but white on red background.

Years of thorough research have revealed that what people find beautiful about the Mandelbrot set is not the set itself, but all the rest.

Neil Black
Member #7,867
October 2006
avatar

I used the same method as Mr. Big when I was making a sprite editor. Of course, that project died when I found Allegro Sprite Editor ^-^, but the line function worked great.

Go to: