Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Draw circle within square

Posted on 2010-08-31
Medium Priority
Last Modified: 2012-05-10
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.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
   Y = Y1
   Y1 = Y1 + b
   If s = 0 Then
      v1 = X
      C = C + COMPUTE_TOTAL15(ya, xa, ha, wa, v1, X, 0)
      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

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
   Y = Y1
   Y1 = Y1 + b

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
   Y = Y1
   Y1 = Y1 + b
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

Question by:abdallah1973
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
LVL 76

Expert Comment

ID: 33570753
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.

Author Comment

ID: 33571872
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


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


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  


Author Comment

ID: 33571942
The circle idea

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.


Author Comment

ID: 33571956
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.
LVL 76

Accepted Solution

GrahamSkan earned 1500 total points
ID: 33576579
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 dblRadius As Double 'radius of circle
    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

    dblRadius = 100 'samplesizes
    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
            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
    Debug.Print iMaxCount, iMaxStart
End Sub

Open in new window


Author Closing Comment

ID: 33656007
Fine as calculation

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question