1

我正在使用这个类来填充基于 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 的原因),所以我在谷歌上搜索了一种使用某种公式逐像素绘制椭圆的方法,但我没有找到太多帮助,而且我对这些数学东西也不擅长。所以,我的问题是:如何创建一个形成填充椭圆的像素数组?谢谢你

.

补充(随意忽略):

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

4

1 回答 1

2

椭圆内所有点的公式为:

(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
于 2015-07-20T18:32:39.010 回答