如何在 LockBits 数组中使用 DrawEllipse(如何生成一组形成椭圆的像素)

How to use DrawEllipse in a LockBits array (How to generate a set of pixels that forms an ellipse)

我正在使用此 class 根据 LockBits 函数填充位图的像素:

Imports System.Drawing.Imaging
Imports System.Runtime.InteropServices.Marshal

Public Class Fill

    Public Shared Function Process(ByVal b As Bitmap) As Bitmap

    Dim bmd As BitmapData = _
    b.LockBits(New Rectangle(0, 0, b.Width, b.Height), _
    System.Drawing.Imaging.ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb)

    Dim scan0 As IntPtr = bmd.Scan0
    Dim stride As Integer = bmd.Stride

    ' Here's the speedier method.
    ' define an array to store each pixels color as an int32 
    Dim pixels(b.Width * b.Height - 1) As Integer

    ' this is system.runtime.interopservices.marshall.copy
    Copy(scan0, pixels, 0, pixels.Length)

    ' loop through all pixels and fill

    For i As Integer = 0 To pixels.Length - 1
        pixels(i) = Color.Red.ToArgb
    Next

    ' Copy the data back from the array to the locked memory
    Copy(pixels, 0, scan0, pixels.Length)

    ' finally we unlock the bits. 
    b.UnlockBits(bmd)
    Return b
    End Function
End Class

现在,我不需要填充所有像素,而是需要填充一个椭圆(实际上会有很多椭圆,这就是我使用 LockBits 的原因),所以我在 google 上搜索了一种逐像素绘制椭圆的方法,使用某种公式,但我没有找到太多帮助,而且我也不擅长这些数学知识。 所以,我的问题是:如何创建形成填充椭圆的像素数组?谢谢

.

补充(随意忽略):

我会准确解释我想做什么,所以它可能会帮助你了解我的情况.. 实际上,我正在研究一个函数,该函数应该在位图的特定区域上生成具有随机宽度和高度(在特定范围内)的填充椭圆,而填充像素必须占该区域像素总数的百分比,这就是为什么我需要逐个像素(或使用像素数组)绘制椭圆以跟踪填充像素的数量。

椭圆内所有点的公式为:

(x - h) * (x - h) / a * a + (y - k) * (y - k) / b * b <= 1

哪里
x,y are the coordinates of any point on the ellipse,
a, b are the radius on the x and y axes respectively
h,k the coordinates of the center

所以代码:

Dim h, k, a, b, x, y As Integer
Dim res As Double

'arbitrary values of ellipse
h = 200
k = 200
a = 80
b = 60

For x = h - a To h + a
    For y = k - b To k + b
        res = CDbl((x - h) * (x - h)) / CDbl(a * a) + CDbl((y - k) * (y - k)) / CDbl(b * b)

        If res <= 1.0 Then
            'the point (x, y) is inside
        End If

    Next
Next