# How can I plot a perfect circle in a 2D array using Visual Basic?

• derek101
In summary, the conversation revolved around learning how to plot positions in a 2 dimensional array using Visual Basic. The individual had a question on how to create a circle of 1's in the array and discovered the use of the Pythagorean theorem. They then sought a better way to create a continuous line of points on the circumference and were directed to the midpoint circle algorithm. By implementing the algorithm and using a mirror image, the individual was able to create a perfect circle. They expressed their gratitude for the link and their intention to continue learning from it.

#### derek101

hi,I am learning visual basic as a pastime.
I have a question,I can plot positions in a 2 dimensional array e.g:-
dim test(100,100) as integer
for cox as integer = 40 to 60
for coy as integer = 40 to 60
test(cox,coy)=1
next cox
next coy

My question is how can I plot positions to make a circle of 1's in my array?

ah sussed it I can use Pythagoras.

dim bitmap(400,400) as integer
t=x*x
d=z-t
y=d^(1/2)
bitmap(200+x,200-y)=1
bitmap(200+x,200+y)=1
bitmap(200-x,200-y)=1
bitmap(200-x,200+y)=1
next xonly problem is this does not give a continuous line of points on the circumference as y gets close to zero.
would appreciate better idea?

not quiet sure how I use the algorithm,but see I can make mirror image.

bitmap(200+x,200-y)=1
bitmap(200+x,200+y)=1
bitmap(200-x,200-y)=1
bitmap(200-x,200+y)=1
plus mirror image
bitmap(200+y,200-x)=1
bitmap(200+y,200+x)=1
bitmap(200-y,200-x)=1
bitmap(200-y,200+x)=1for x = 0 to 399
for y = 0 to 399
if bitmap(x,y) = 1 then g.drawline(bluepen,x+100,y+100,x+101,y+100)
next x
next yNow I get a perfect circle.
I shall study the link some more see what else I can learn.THANKS.

Hello,

I would recommend using a mathematical approach to plotting a perfect circle in a 2D array using Visual Basic. One way to do this would be to use the equation of a circle, which is (x - center_x)^2 + (y - center_y)^2 = radius^2.

In your code, you can define the center coordinates and the radius of the circle, and then use a nested loop to iterate through the array and plot the values according to the equation. For example:

Dim test(100,100) As Integer
Dim center_x As Integer = 50
Dim center_y As Integer = 50
Dim radius As Integer = 10
For x As Integer = 0 To 100
For y As Integer = 0 To 100
If (x - center_x)^2 + (y - center_y)^2 = radius^2 Then
test(x,y) = 1
End If
Next y
Next x

This code will plot a perfect circle with a radius of 10 at the center coordinates (50,50). You can adjust the center and radius values to create different sized circles at different positions in the array.

I hope this helps and happy coding!

## 1. How can I draw a circle in visual basic?

To draw a circle in visual basic, you can use the Circle method. This method takes in the coordinates of the center of the circle, as well as the radius, and draws a circle on the screen.

## 2. How do I change the color of a circle in visual basic?

You can change the color of a circle in visual basic by using the ForeColor property. This property allows you to set the color of the circle using various color codes or names.

## 3. Can I make a circle transparent in visual basic?

Yes, you can make a circle transparent in visual basic by setting the BackColor property to the color Transparent. This will make the background of the circle transparent, allowing any elements behind it to show through.

## 4. How do I animate a circle in visual basic?

To animate a circle in visual basic, you can use the Timer control. This control allows you to set a specific interval at which the circle will be redrawn, creating the illusion of movement.

## 5. How can I calculate the area of a circle in visual basic?

To calculate the area of a circle in visual basic, you can use the Math class and its PI property to get the value of pi. Then, you can use the formula area = pi * (radius ^ 2) to calculate the area of the circle.