Page 1 of 1

Plot a straight line

Posted: Sun Sep 27, 2015 4:06 am
by WilliamL
Ok, I googled this and saw that this was 8th grade math. I haven't been in 8th grade for 60 years and the explanation makes my head hurt. :evil:

Can somebody give me the equation that gives me the points to plot between the two end points.. in PureBasic?

I'll wait.. and take a pain reliever...

Re: Plot a straight line

Posted: Sun Sep 27, 2015 4:40 am
by Demivec
I dug up some of my old code from 2008 that is based on older code from 1987. It uses the Bresenhem line drawing method.

Code: Select all

Procedure draw(x1, y1, x2, y2)
  Protected dy = Abs(y2 - y1), yInc = Sign(y2 - y1)
  Protected dx = Abs(x2 - x1), xInc = Sign(x2 - x1)
  Protected rowAc, ColAc
  Protected y = y1, x = x1
  
  err = dx
  If dy > dx
    err = dy
  EndIf 
  endPt = err
  If err = dx
    rowAc = endPt / 2
  Else 
    ColAc = endPt / 2
  EndIf 
  While err > 0
    rowAc + dy
    If rowAc >= endPt
      rowAc - endPt
      y + yInc
    EndIf 
    ColAc + dx
    If ColAc >= endPt
      ColAc - endPt
      x + xInc
    EndIf 
    Plot(x, y)
    err - 1
  Wend 
EndProcedure

OpenWindow(0, 0, 0, 500, 500, "Bresenhem line drawing method", #PB_Window_SystemMenu)

Define event, quit
Repeat
  StartDrawing(WindowOutput(0))
    FrontColor(Random($FFFFFF))
    draw(Random(500 - 1), Random(500 - 1), Random(500 - 1), Random(500 - 1)) ;stay in screen boundaries
  StopDrawing()

  Repeat
    event = WindowEvent()
    If event = #PB_Event_CloseWindow
      quit = 1
    EndIf 
  Until event = 0 Or quit = 1
  Delay(10)
Until quit = 1
There are no bounds checking in the above code, so it goes without saying, don't draw outside the bounds of the drawing surface. :wink:

Re: Plot a straight line

Posted: Sun Sep 27, 2015 5:36 am
by WilliamL
Hey Demivec!

Boy, I'm glad I asked. (and quick too!) That is very interesting. I'm going to look at that for a while...

I tried it and it draws lines. It's just what I need.

Thanks!

Re: Plot a straight line

Posted: Sun Sep 27, 2015 6:27 am
by Demivec
Your welcome. :)

It's strange you posted this in the Off Topic forum as it is squarely a PureBasic programming question. Coding Questions or General Discussion would probably have been a better forum to post in.


P.S. As an addendum to my timeline, the code I translated from was written in 1977 and as I understand it Jack Bresenham developed the code in 1962 and published it in 1965. Which was still 20+ years after you were in 8th grade. :)

I happy to help explain any portions if needed. There are great explanations to be found elsewhere on the web as well.

Re: Plot a straight line

Posted: Sun Sep 27, 2015 6:52 am
by WilliamL
Demivec.

Hey, I got it working with my code!

Thanks for the background. Now I don't feel quite so dumb seeing that a formula had to be developed!

Yeah, maybe 'General Discussion' would have been better. I suppose trying to figure this out addled my brain. :wink:

Re: Plot a straight line

Posted: Sun Sep 27, 2015 8:31 am
by Danilo
If you search the forum for 'Bresenham' you will also find codes to draw lines, circles, ellipses, arc's, pie's, with anti-aliasing, etc.

Re: Plot a straight line

Posted: Sun Sep 27, 2015 4:05 pm
by WilliamL
ok, now I DO feel that dumb! Over 3 pages of links, in the forum, about drawing straight lines but I needed to know the key was 'Bresenham'! Isn't that always the case with a search?

Well, any question, or programming block, that gets solved is good!

I'll be reading for days...

Re: Plot a straight line

Posted: Mon Sep 28, 2015 5:11 am
by Danilo
WilliamL wrote:ok, now I DO feel that dumb!
Don't do that! Just wanted to say that you find more interesting stuff when searching for 'Bresenham'. That's all.

Re: Plot a straight line

Posted: Mon Sep 28, 2015 5:21 am
by tj1010

Code: Select all

LineXY(x1, y1, x2, y2 [, Color])
same level of alias and already in PB