Reputation: 1
I am attempting to complete a project where, on mouse click, 200 circles are drawn from the clicked location as the center point, growing larger from 1 radius to 200.
I don't need help with the mouse part of this program.
My current roadblock is that it would seem that although there were circle methods in previous releases of VB, VB.NET only uses the System.CreateGraphics.DrawEllipse method to create circles, and this method uses an x and y coordinate as a starting location for the upper lefthand corner of an invisible rectangle and uses two more integers/singles to decide the distance to the right and down (x + and y +) to determine the size and location of the circle.
I'm hoping I am just epically missing a built in way to create a circle/ellipsis using a center point and declaring the radius from that center point in order to complete my program.
Is there a way to draw a circle onto a FORM using a center point as the point of reference then declare the radius?
Upvotes: 0
Views: 6191
Reputation: 39132
I'm hoping I am just epically missing a built in way to create a circle/ellipsis using a center point and declaring the radius from that center point in order to complete my program.
Is there a way to draw a circle onto a FORM using a center point as the point of reference then declare the radius?
Sure, just create a Rectangle() at that point with a size of (1,1) and repeatedly call the Inflate() method:
Public Class Form1
Private Center As Point
Private MouseClicked As Boolean = False
Private Sub Form1_MouseDown(sender As Object, e As MouseEventArgs) Handles MyBase.MouseDown
If e.Button = Windows.Forms.MouseButtons.Left Then
MouseClicked = True
Center = New Point(e.X, e.Y)
Me.Refresh()
End If
End Sub
Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
If MouseClicked Then
Dim rc As New Rectangle(Center, New Size(1, 1))
For i As Integer = 1 To 200
e.Graphics.DrawEllipse(Pens.Black, rc)
rc.Inflate(1, 1)
Next
End If
End Sub
End Class
Upvotes: 1
Reputation: 2119
If you have the center point and radius, then you can calculate corner point easily. The following might help you.
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
Dim centerX, centerY As Integer
Dim cornerX, cornerY As Integer
Dim radius As Integer
centerX = 100
centerY = 100
Dim i As Integer
For i = 20 To 200 Step 20
radius = i
cornerX = centerX - radius / 2
cornerY = centerY - radius / 2
e.Graphics.DrawEllipse(Pens.Black, cornerX, cornerY, radius, radius)
Next
End Sub
Upvotes: 0
Reputation: 158319
Essentially, the only thing you need to do is to is to decrease the X
and Y
values of the point with the radius, and use radius * 2 for the width and height:
graphics.DrawEllipse(
pen, location.X - radius, location.Y - radius, radius * 2, radius * 2)
Given that, it's pretty easy to make an extension method giving you the interface that you are looking for:
Imports System.Runtime.CompilerServices
Module GraphicsExtensions
<Extension()>
Public Sub DrawCircle(ByVal graphics As Graphics, pen As Pen, location As Point, radius As Integer)
graphics.DrawEllipse(
pen, location.X - radius, location.Y - radius, radius * 2, radius * 2)
End Sub
End Module
...and use it in your form:
Dim pos As Point = Me.PointToClient(MousePosition)
Using g As Graphics = Me.CreateGraphics()
g.DrawCircle(Pens.Black, pos, 15)
End Using
Upvotes: 2