Bresenham画圆算法

这个代码是偶然在网上找到的,保存在这里方便以后参考

Option Explicit

Private m_lCX As Long 'Center X-coordinate of the screen.
Private m_lCY As Long 'Center Y-coordinate of the screen.
Private m_lR As Long 'Radius of the circle to draw.

Private Sub Form_Load()
'Keeps the drawing on the form from "disappearing".
Me.AutoRedraw = True

'Points are plotted in pixels.
Me.ScaleMode = vbPixels
End Sub

'Re-draw the circle each time the form is resized.
Private Sub Form_Resize()
'Calculate the screen's centerpoint.
m_lCX = Me.ScaleWidth / 2
m_lCY = Me.ScaleHeight / 2

'Make the radius 90% of the shorter side.
m_lR = IIf(m_lCX > m_lCY, m_lCY, m_lCX) * 0.9

'Clear any previously drawn circle.
Me.Cls

Draw2
End Sub

Private Sub DrawPoint(X As Long, Y As Long)
'Of course, using PSet slows things down. I'd suggest
' using an API call like SetPixel or SetPixelV.
Me.PSet (X, Y), vbBlack
End Sub

Private Sub DrawOctants(X As Long, Y As Long)
DrawPoint m_lCX + X, m_lCY + Y
DrawPoint m_lCX + X, m_lCY - Y
DrawPoint m_lCX - X, m_lCY + Y
DrawPoint m_lCX - X, m_lCY - Y
DrawPoint m_lCX + Y, m_lCY + X
DrawPoint m_lCX + Y, m_lCY - X
DrawPoint m_lCX - Y, m_lCY + X
DrawPoint m_lCX - Y, m_lCY - X
End Sub

Private Sub Draw2()
Dim X As Long
Dim Y As Long
Dim d As Long

'Start the coordinates at the top of the circle (90 degrees)
X = 0
Y = m_lR
d = 1 - m_lR

'Move the x-coordinate to the right, until the x- and
'y-coordinates meet (signifying the point at 45 degrees).
Do While Y >= X
DrawOctants X, Y

'I don't understand how these calculations decide when
'to decrement the y-coordinate, but they certainly seem
'to work well! :-)
If d < 0 Then
d = d + (2 * X) + 3
Else
d = d + 2 * (X - Y) + 5
Y = Y - 1
End If

'Move to the next x-coordinate
X = X + 1
Loop
End Sub

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值