php画圆 锯齿,抗锯齿方式画圆! - 小志的日志 - 电子工程世界-论坛

Private Declare Function GetPixel Lib "gdi32" (ByVal hDC As Long, ByVal X As Long, ByVal Y As Long) As Long

Private Declare Function SetPixel Lib "gdi32" (ByVal hDC As Long, ByVal X As Long, ByVal Y As Long, ByVal crColor As Long) As Long

'

Private Const PI = 3.1415926 / 180

Private Sub PsetEx(Pic As PictureBox, X As Single, Y As Single, Optional Color As Long = 0)

Dim CenterX As Long, CenterY As Long, CenterColor As Long, CenterAlpha As Single

Dim CenterRX As Long, CenterRY As Long, CenterRColor As Long, CenterRAlpha As Single

Dim CenterBX As Long, CenterBy As Long, CenterBColor As Long, CenterBAlpha As Single

Dim CenterRBX As Long, CenterRBY As Long, CenterRBColor As Long, CenterRBAlpha As Single

Dim hDC As Long

hDC = Pic.hDC

CenterX = Int(X)

CenterY = Int(Y)

CenterColor = GetPixel(hDC, CenterX, CenterY)

CenterAlpha = (CenterX - X + 1) * (CenterY - Y + 1)

CenterRX = CenterX + 1

CenterRY = Int(Y)

CenterRColor = GetPixel(hDC, CenterRX, CenterRY)

CenterRAlpha = (X - CenterRX + 1) * (CenterY - Y + 1)

CenterBX = CenterX

CenterBy = CenterY + 1

CenterBColor = GetPixel(hDC, CenterBX, CenterBy)

CenterBAlpha = (CenterX - X + 1) * (Y - CenterBy + 1)

CenterRBX = CenterX + 1

CenterRBY = CenterY + 1

CenterRBColor = GetPixel(hDC, CenterRBX, CenterRBY)

CenterRBAlpha = (X - CenterRX + 1) * (Y - CenterBy + 1)

SetPixel hDC, CenterX, CenterY, BlendColor(CenterColor, Color, CenterAlpha)

SetPixel hDC, CenterRX, CenterRY, BlendColor(CenterRColor, Color, CenterRAlpha)

SetPixel hDC, CenterBX, CenterBy, BlendColor(CenterBColor, Color, CenterBAlpha)

SetPixel hDC, CenterRBX, CenterRBY, BlendColor(CenterRBColor, Color, CenterRBAlpha)

End Sub

'

Private Sub GetRGB(Color As Long, Red As Long, Green As Long, Blue As Long)

If Color <> 0 Then

Red = Color And 255&

Green = Color \ 256 And 255

Blue = Color \ 65536

End If

End Sub

Private Function BlendColor(ColorBase As Long, ColorBlend As Long, Alpha As Single) As Long

Dim BaseR As Long, BaseG As Long, BaseB As Long

Dim BlendR As Long, BlendG As Long, BlendB As Long

GetRGB ColorBase, BaseR, BaseG, BaseB

GetRGB ColorBlend, BlendR, BlendG, BlendB

If Alpha > 1 Then Alpha = 1

BaseR = Alpha * (BlendR - BaseR) + BaseR

BaseG = Alpha * (BlendG - BaseG) + BaseG

BaseB = Alpha * (BlendB - BaseB) + BaseB

BlendColor = RGB(BaseR, BaseG, BaseB)

End Function

'

Private Sub CircleEx(Pic As PictureBox, CenterX As Long, CenterY As Long, Radius As Long, Optional Color As Long = vbRed)

Dim i As Single

Dim X As Single, Y As Single

For i = 0 To 360 Step 60 / Radius

X = Cos(i * PI)

Y = Sin(i * PI)

X = X * Radius + CenterX

Y = Y * Radius + CenterY

PsetEx Pic, X, Y, Color

Next

End Sub

Private Sub Command1_Click()

CircleEx Pic, 100, 100, 50, vbRed

Pic.Refresh

End Sub

以这种方式画抗锯齿的直线时要注意对于斜率大于和小于1的情况要分开处理,这里不赘述!

如果你觉得画出来的圆颜色和指定的颜色有所区别,请适当修改PsetEx函数的最后四行的SetPixel的最后一个透明度的参数,比如都乘2!.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值