Quantcast
Channel: VBForums - CodeBank - Visual Basic 6 and earlier
Viewing all articles
Browse latest Browse all 1529

Improved circle drawing

$
0
0
Here's my code for drawing a circle, that has significant improvements over the internal VB6 circle drawing commands.
Code:

Private Sub DrawCircle(ByVal X0 As Long, ByVal Y0 As Long, ByVal Radius As Long, ByVal Color As Long)
    Dim xMax As Long
    Dim Y As Long
    Dim X As Long
    For Y = -Radius To Radius
        xMax = Int(Sqr(Radius * Radius - Y * Y))
        For X = -xMax To xMax
            PSet (X0 + X, Y0 + Y), Color
        Next X
    Next Y
End Sub

The built in DrawWidth property makes a PSet dot bigger, so you can try to draw a circle with it, but it is not even close to being a perfectly symmetrical circle, until it reaches quite large sizes.

The builtin Circle command allows you to make a perfect circle, but the color you set using the color number in the Circle command only effects the circle's outline. To set the interior color of the circle, you have to set the FillColor property in a separate command, and furthermore you need to set the FillStyle to even make the interior of the circle visible (otherwise it's invisible/transparent). So you need to set 2 properties before even running the Circle command, and every time you want to change the color, you need to change the FillColor property.

This DrawCircle method that I created though, makes drawing a perfectly symmetric circle as easy as running one line of code, the code to call the method. All 4 parameters needed to draw the circle are specified at the time of calling the method.

The below sample code shows how to use this method in a MouseDown event in Form1, to make it draw a green circle of radius 10 (center pixel plus 10 pixels out from the center, which some people might call radius 11). The center of the circle will be wherever you click the mouse. Note that ScaleMode property of Form1 should be Pixel (not the default Twip), and that AutoRedraw should be set to True.
Code:

Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    DrawCircle X, Y, 10, &HFF00&
End Sub


Viewing all articles
Browse latest Browse all 1529

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>