Results 1 to 7 of 7

Thread: Line algorithm

  1. #1

    Thread Starter
    Member
    Join Date
    May 2002
    Posts
    37

    Line algorithm

    Hi,

    This is the best place where I can post this, I think

    Say, I have a subroutine called drawline, which accepts as parameters:
    x1, y1, x2, y2

    We have a screen, and on point (x1,y1) we start drawing our line to (x2,y2). Each pixel on the line should be determined by an algorithm, and than colored, say gree. Does anybody know an algorithm?

  2. #2
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    Most graphics systems have support the LineTo verb since PostScript came out with it in 1984. Windows version of drawing a line with LineTo:

    Code:
    'This project needs:
    '- picture box
    
    Private Type POINTAPI
        X As Long
        Y As Long
    End Type
    
    Private Declare Function MoveToEx Lib "gdi32" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long, lpPoint As POINTAPI) As Long
    Private Declare Function LineTo Lib "gdi32" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long) As Long
    
    
    Const Yellow = &HFFFF&
    Private Sub Form_Load()
        Dim Cnt1 As Byte, Cnt2 As Byte, Point As POINTAPI
        Me.AutoRedraw = True
        Me.ScaleMode = vbPixels
        Picture1.ForeColor = Yellow
        Picture1.ScaleMode = vbPixels
        Picture1.AutoRedraw = True
               Cnt1=100
               Cnt2=100
                'Set the start-point's coördinates
                Point.X = Cnt1: Point.Y = Cnt2
                'Move the active point
                MoveToEx Picture1.hDC.hdc, Cnt1, Cnt2, Point
                'Draw a line from the active point to the given point
                LineTo Picture1.hdc, 200, 200
                Picture1.Refresh
    End Sub

  3. #3
    Hyperactive Member
    Join Date
    Jul 2002
    Location
    WGTN, New Zealand
    Posts
    338
    I think he wanted to know how LineTo drew the line, and how it knows which pixels to change.

  4. #4

    Thread Starter
    Member
    Join Date
    May 2002
    Posts
    37
    Yes, that was the point.
    It is, however, for a C project, but VB code is nice too, I know both.

  5. #5
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    Ok. My bad.

    You asked. So- It's called Bresnham's Algorithm. It's all over the net.

    Here is a pretty good explanation, if you can do basic algebra:

    http://www.cs.helsinki.fi/group/goa/...s/bresenh.html

  6. #6

    Thread Starter
    Member
    Join Date
    May 2002
    Posts
    37
    Kinda nice stuff for the math lessons

  7. #7
    Hyperactive Member DavidHooper's Avatar
    Join Date
    Apr 2001
    Posts
    357
    Yer, thanx for that link Jim.
    There are 10 types of people in the world - those that understand binary, and those that don't.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width