Attribute VB_Name = "WuLines" ' Wu (anti-aliased) line drawing 1.01 ' Written by Mike D Sutton of EDais ' Microsoft Visual Basic MVP ' ' E-Mail: EDais@mvps.org ' WWW: Http://www.mvps.org/EDais/ ' ' Written: 23/09/2000 ' Last edited: 16/08/2003 ' 'About: ' Anti-aliased line drawing in VB based on Xiaolin Wu's algorithm ' Pretty inefficient translation, but well.. pretty! ' 'Dependencies: ' EDais PixLib 'Version history: ' Version 1.01 (16/08/2003): ' Changed internal point type from PointAPI to WuPoint and ' re-named NewPointAPI() to NewWuPoint() to avoid ' conflicts with other libraries ' A other few minor code changes ' ' Version 1.0 (23/09/2000): ' NewPointAPI() - New wu-line point constrstructor ' WuLine() - Draw's a wu-line between two points 'Disclaimer: ' You use this code at your own risk, I don't accept any ' responsibility for anything nasty it may do to your machine! ' ' Please don't rip my work off... I'm distributing this library ' free of charge because I think it can help other developers, ' this doesn't give you the right to take credit for it. By ' all means use it, yes, but please don't claim it's your own ' work or charge for it. If you do create anything interesting ' with it then feel free to send me it, if I receive any nice ' source code I'll post it on the site (With your permission) ' and of course you'll get full credit for it. ' ' Visit my site for any updates to this an more strange graphics ' related VB code, comments and suggestions always welcome! Private Declare Function GetPixel Lib "gdi32" (ByVal hDC As Long, ByVal X As Long, ByVal Y As Long) As Long Private Declare Function SetPixelV Lib "gdi32" (ByVal hDC As Long, ByVal X As Long, ByVal Y As Long, ByVal crColor As Long) As Long Public Type WuPoint X As Long Y As Long End Type Private Type WuLineInf ' For line drawing routine Start As WuPoint Length As WuPoint End Type Public Function NewWuPoint(ByVal inX As Long, ByVal inY As Long) As WuPoint With NewWuPoint .X = inX .Y = inY End With End Function Public Sub WuLine(ByVal TargDC As Long, ByRef inStart As WuPoint, ByRef inEnd As WuPoint, ByRef inCol As Pixel, ByVal inWidth As Integer) Dim LineData As WuLineInf Dim DrawStep As Integer Dim DrawLine As Integer Dim FillLine As Integer Dim EvalPos As Single Dim PixStrength As Single Dim PlotPos As WuPoint Dim HalfWidth As Integer Dim OtherHalf As Integer If ((inStart.X = inEnd.X) And (inStart.Y = inEnd.Y)) Then Exit Sub HalfWidth = Int(inWidth / 2) OtherHalf = inWidth - HalfWidth If (Abs(inEnd.X - inStart.X) > Abs(inEnd.Y - inStart.Y)) Then ' Line is longer With LineData If (inStart.X < inEnd.X) Then .Start.X = inStart.X .Length.X = inEnd.X - inStart.X .Start.Y = inStart.Y .Length.Y = inEnd.Y - inStart.Y Else .Start.X = inEnd.X .Length.X = inStart.X - inEnd.X .Start.Y = inEnd.Y .Length.Y = inStart.Y - inEnd.Y End If End With If (LineData.Length.X < 0) Then DrawStep = -1 Else DrawStep = 1 For DrawLine = 0 To LineData.Length.X Step DrawStep EvalPos = (DrawLine / LineData.Length.X) * LineData.Length.Y PixStrength = EvalPos - Int(EvalPos) PlotPos.X = DrawLine + LineData.Start.X PlotPos.Y = Int(EvalPos) + LineData.Start.Y Call SetPixelV(TargDC, PlotPos.X, PlotPos.Y - HalfWidth, PixToLong( _ TransPix(inCol, LongToPix(GetPixel(TargDC, PlotPos.X, PlotPos.Y - HalfWidth)), PixStrength))) Call SetPixelV(TargDC, PlotPos.X, PlotPos.Y + OtherHalf, PixToLong( _ TransPix(inCol, LongToPix(GetPixel(TargDC, PlotPos.X, PlotPos.Y + OtherHalf)), 1 - PixStrength))) For FillLine = (PlotPos.Y - (HalfWidth - 1)) To (PlotPos.Y + (OtherHalf - 1)) Call SetPixelV(TargDC, PlotPos.X, FillLine, PixToLong(inCol)) Next FillLine Next DrawLine Else ' Line is higher With LineData If (inStart.Y < inEnd.Y) Then .Start.X = inStart.X .Length.X = inEnd.X - inStart.X .Start.Y = inStart.Y .Length.Y = inEnd.Y - inStart.Y Else .Start.X = inEnd.X .Length.X = inStart.X - inEnd.X .Start.Y = inEnd.Y .Length.Y = inStart.Y - inEnd.Y End If End With If (LineData.Length.Y < 0) Then DrawStep = -1 Else DrawStep = 1 For DrawLine = 0 To LineData.Length.Y Step DrawStep EvalPos = (DrawLine / LineData.Length.Y) * LineData.Length.X PixStrength = EvalPos - Int(EvalPos) PlotPos.X = Int(EvalPos) + LineData.Start.X PlotPos.Y = DrawLine + LineData.Start.Y Call SetPixelV(TargDC, PlotPos.X - HalfWidth, PlotPos.Y, PixToLong( _ TransPix(inCol, LongToPix(GetPixel(TargDC, PlotPos.X - HalfWidth, PlotPos.Y)), PixStrength))) Call SetPixelV(TargDC, PlotPos.X + OtherHalf, PlotPos.Y, PixToLong( _ TransPix(inCol, LongToPix(GetPixel(TargDC, PlotPos.X + OtherHalf, PlotPos.Y)), 1 - PixStrength))) For FillLine = (PlotPos.X - (HalfWidth - 1)) To (PlotPos.X + (OtherHalf - 1)) Call SetPixelV(TargDC, FillLine, PlotPos.Y, PixToLong(inCol)) Next FillLine Next DrawLine End If End Sub