Plot a straight line

For everything that's not in any way related to PureBasic. General chat etc...
WilliamL
Addict
Addict
Posts: 1253
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Plot a straight line

Post 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...
MacBook Pro-M1 (2021), Sequoia 15.4, PB 6.20
User avatar
Demivec
Addict
Addict
Posts: 4281
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Plot a straight line

Post 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:
WilliamL
Addict
Addict
Posts: 1253
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Re: Plot a straight line

Post 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!
MacBook Pro-M1 (2021), Sequoia 15.4, PB 6.20
User avatar
Demivec
Addict
Addict
Posts: 4281
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Plot a straight line

Post 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.
WilliamL
Addict
Addict
Posts: 1253
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Re: Plot a straight line

Post 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:
MacBook Pro-M1 (2021), Sequoia 15.4, PB 6.20
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Plot a straight line

Post 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.
WilliamL
Addict
Addict
Posts: 1253
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Re: Plot a straight line

Post 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...
MacBook Pro-M1 (2021), Sequoia 15.4, PB 6.20
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Plot a straight line

Post 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.
tj1010
Enthusiast
Enthusiast
Posts: 716
Joined: Mon Feb 25, 2013 5:51 pm

Re: Plot a straight line

Post by tj1010 »

Code: Select all

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