# Draw circle within square

dear sir,

I made a code in VB6 and it's work fine to draw multiple square in selected area.

The form contain a picture box use for draw my idea.
the form name is "tasfrms"
The picture box is "vieform"

What I need is to caculate the free space which not been used for drawing, and draw a Ellipse inside each sqaure.

The free space will also include the (square space-circle space)

Global C As Double
Global v1 As Double
Global v2 As Double
Global X As Double
Global Y As Double
Global a As Double
Global b As Double
Global d As Double

'xa is the x of filled square
'ya is the y of filled square
'ha is the hieght of the space(picture)
'wa is the width of the space (picture)
's is flag used to change the arrange of filling the total space to try used maximum space as can
Sub COMPUTE_TOTAL44(xa As Double, ya As Double, ha As Double, wa As Double, s As Integer)

On Error GoTo Checks
'If IsNull(tasfrms.Text13.Text) = False And za <> "0" Then

tasfrms.vieform.Cls

tasfrms.vieform.Line (0, 0)-(wa + 3, ha + 3), QBColor(12), BF
tasfrms.vieform.Line (0, 0)-(wa, ha), QBColor(3), BF

a = xa
b = ya
Y = 0
C = 0
Y1 = b
Do While Y1 <= ha
X = 0
X1 = a

Do While X1 <= wa
Call SHOWBOX(X, Y, X1, Y1)
X = X1
X1 = X1 + a
C = C + 1
Loop
Y = Y1
Y1 = Y1 + b
Loop
If s = 0 Then
v1 = X
C = C + COMPUTE_TOTAL15(ya, xa, ha, wa, v1, X, 0)
Else
v2 = Y
C = C + COMPUTE_TOTAL211(ya, xa, ha, wa, v2, 0, Y)
End If
' Call SHOWBOX(X, Y, X1, Y1)

'   tasfrms.vieform.Text1.Text = C

tasfrms.Text3.Text = C

'tasfrms.Text3.Text = c
d = za

Checks:
Exit Sub

End Sub

Function COMPUTE_TOTAL15(xa As Double, ya As Double, ha As Double, wa As Double, V As Double, n1 As Double, n2 As Double) As Integer
Dim C As Integer
tasfrms.vieform.Line (V, n2)-(wa, ha), QBColor(12), BF
tasfrms.vieform.Line (V, n2)-(wa, ha), QBColor(3), BF
a = xa
b = ya
Y = 0
C = 0
Y1 = b

Do While Y1 <= ha
X = V
X1 = V + a

Do While X1 <= wa
Call SHOWBOX(X, Y, X1, Y1)
C = C + 1
X = X1
X1 = X1 + a
Loop
Y = Y1
Y1 = Y1 + b
Loop

COMPUTE_TOTAL15 = C
End Function

Function COMPUTE_TOTAL211(xa As Double, ya As Double, ha As Double, wa As Double, V As Double, n1 As Double, n2 As Double) As Integer
Dim C As Integer
tasfrms.vieform.Line (n1, V)-(wa, ha), QBColor(12), BF
tasfrms.vieform.Line (n1, V)-(wa, ha), QBColor(3), BF
a = xa
b = ya
Y = V
C = 0
Y1 = V + b

Do While Y1 <= ha
X = 0
X1 = a

Do While X1 <= wa
Call SHOWBOX(X, Y, X1, Y1)
C = C + 1
X = X1
X1 = X1 + a
Loop
Y = Y1
Y1 = Y1 + b
Loop
COMPUTE_TOTAL211 = C
End Function

Sub SHOWBOX(ByVal X As Double, ByVal Y As Double, ByVal X1 As Double, ByVal Y1 As Double)
tasfrms.vieform.Line (X, Y)-(X1, Y1), QBColor(8), BF
tasfrms.vieform.Line (X, Y)-(X1, Y1), QBColor(0), B
tasfrms.vieform.Line (X + 3, Y + 3)-(X1 - 3, Y1 - 3), QBColor(7), BF
tasfrms.vieform.Line (X + 3, Y + 3)-(X1 - 3, Y1 - 3), QBColor(0), B
tasfrms.vieform.Line (X, Y)-(X + 3, Y + 3), QBColor(0)
tasfrms.vieform.Line (X1, Y1)-(X1 - 3, Y1 - 3), QBColor(0)
tasfrms.vieform.Line (X, Y1)-(X + 3, Y1 - 3), QBColor(0)
tasfrms.vieform.Line (X1, Y)-(X1 - 3, Y + 3), QBColor(0)
End Sub

to execute the code

'first option
Private Sub Command1_Click()

Call COMPUTE_TOTAL44(tasfrms.Text1.Text, tasfrms.Text2.Text, tasfrms.Text4.Text, tasfrms.Text5.Text, 2)
end sub

'second option
Private Sub Command2_Click()
Call COMPUTE_TOTAL44(tasfrms.Text2.Text, tasfrms.Text1.Text, tasfrms.Text4.Text, tasfrms.Text5.Text, 0)

End Sub
'third option
Private Sub Command3_Click()
Call COMPUTE_TOTAL44(tasfrms.Text2.Text, tasfrms.Text1.Text, tasfrms.Text5.Text, tasfrms.Text4.Text, 1)

End Sub

'fourth option
Private Sub Command4_Click()
Call COMPUTE_TOTAL44(tasfrms.Text1.Text, tasfrms.Text2.Text, tasfrms.Text5.Text, tasfrms.Text4.Text, 1)

End Sub

Capture.PNG
Capture.PNG
###### Who is Participating?

RetiredCommented:
Where are you stuck with this?

Obviously the unfilled space is the difference between the area of the circle and the area of the square.

FYI, here is a routine to calculate how many square of a given size will fit inside a circle

``````Sub SquaresInCircle()
Dim dblLength As Double 'length of side
Dim dblTopRow As Double 'distance from centre of top of top row
Dim iTopCount As Integer 'number in top row
Dim iSquareTotal As Integer 'count of squares
Dim dblCurrentHeight As Double
Dim dblRowWidth As Double 'width of row
Dim dblRowCount As Integer
Dim iStartLimit As Integer
Dim iMaxCount As Integer
Dim iMaxStart As Integer

dblLength = 10

iTopCount = 1
iStartLimit = Int(2 * dblRadius / dblLength)
Do Until iTopCount > iStartLimit
dblTopRow = Sqr(dblRadius ^ 2 - ((dblLength / 2) * iTopCount) ^ 2)
dblCurrentHeight = dblTopRow
dblRowCount = 0
iSquareTotal = 0
Do
iSquareTotal = iSquareTotal + dblRowCount
dblCurrentHeight = dblCurrentHeight - dblLength
If dblCurrentHeight ^ 2 > dblRadius ^ 2 Then
Exit Do
End If
dblRowWidth = 2 * Sqr(dblRadius ^ 2 - dblCurrentHeight ^ 2)
dblRowCount = Int(dblRowWidth / dblLength)
If iStartLimit = Int(2 * dblRadius / dblLength) Then
iStartLimit = dblRowCount
End If
Loop Until dblRowCount = 0
If iSquareTotal > iMaxCount Then
iMaxCount = iSquareTotal
iMaxStart = iTopCount
End If
iTopCount = iTopCount + 1

Loop
Debug.Print iMaxCount, iMaxStart
End Sub
``````
0

RetiredCommented:
I have now created a new project. I  copied your code into a form. I have added a command button.  I created a module and transferred the globals there. I have added five textboxes and a command button with default names. I now get a type-mismatch when I click the command button. I know why, but I am reluctant to pursue this.

Please can you supply the full environment or define your problem more closely? Thank you.
0

Author Commented:
The idea of the software is to draw a sqaure and get how many sqaure of size (x and y) we need to fill the (W & H) of space.

when fill the space you with specific x & y size  then you will fill the area space and get how many square you need to cover the space with (W & H).

Which is done in the above code and also it' show you graphically the free space you are not used or not enough to fit with your spcific x & y.

Because of that we give four option for the client to change the way of sort the square as you sea and also the way of space to get maximum number of square with specific x & y to fill the space.

Check this

H=100
W=100
x=15
y=10

you will find that it will need
60 for first option
60 for second option after we rotate the x & y
66 for the third option
60 for the fourth option

Now our job is to get the maxim number square with x & y to fill the area with maximum number of square.

and get the free space as you see in the fourth options.

the client will chose the best design with maximum number of square to reduce or keep the cost as it.

The change of the project that we need to change the square to be circle have the same size of the square

mean it's the same but it will show circle rather thant square.
and the free space will be

Free space as remain after maximum options + (area of square - area or circle)

Because there will be free space after you use circle instead of square then the circle will be inside a invisible square and to get correct free space we need to get the wasted area that consume between circle, and for that we draw a sqaure boundary the circle.

The idea is easy

Square will be (x*y) * number of maximum square we get from the options, e.g 66

(10*15)*66

-
The circle (3.14 * (10^2))*66 as  I remember

Free space we get from the maximum option + total space (square-circle)

and you will see in the third option we rotate the square to get maximum number of square.

in addition, the size of the square will be the same (10 * 10) to get a fix circle will fix R.

The idea is to show circle instead of square but with taken in the mind the free space wasted when you replace with circle.

will be

Capture2.PNG
0

Author Commented:
The circle idea
circle.png
0

Author Commented:
the black space is the free sapce wasted when replace square with circle and will be added to the total free space remain after you get maximum number of square need to fill the free space.
0

Author Commented:
Fine as calculation
0