Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 901543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:34:16+00:00 2026-05-15T15:34:16+00:00

Given a canvas, let’s say 10×10, and given 3 rectangles/squares. Canvas = 10×10 Rectangle

  • 0

Given a canvas, let’s say 10×10, and given 3 rectangles/squares.

Canvas = 10×10

Rectangle 1 = 2×2
Rectangle 2 = 3×3
Rectangle 3 = 2×4

I’ve created a recursive function that loops every position of every rectangle on the canvas, and it works fine. (I’ve included the function below incase anyone wants to see it but I don’t think it’s necessary).

We can see that rectangle 1 and 2 are non rotatable, IE, if you rotate either of them 90 degrees essentially they are the same shape. However rectangle 3 is rotatable.

How do I change/construct the loop/recurisve function so that it loops every position of every rectangle, along with every possible rotation?

The aim is to loop through every possible fitting of the shapes on the canvas.

Thanks for any help!

Sub recurse(ByVal startPoint As Integer)

    Dim x As Integer
    Dim y As Integer
    Dim validSolution As Boolean = isSolutionValid()
    Dim loopXTo As Integer
    Dim loopYTo As Integer
    Dim solutionRating As Integer

    'If parent nodes create invalid solution, we can skip (375 iterations from 1,600 iterations saving)
    If validSolution = True Then

        If (startPoint = 0) Then
            loopXTo = Math.Floor((canvasCols - squareObjects(startPoint).sqRows()) / 2)    '576 iterations from 1,680 iterations
            loopYTo = Math.Floor((canvasRows - squareObjects(startPoint).sqCols) / 2)       '31,104 iterations from 90,720 iterations
        Else
            loopXTo = canvasCols - squareObjects(startPoint).sqRows
            loopYTo = canvasRows - squareObjects(startPoint).sqCols

        End If

        'Loop all positions on canvas
        For x = 0 To loopXTo
            For y = 0 To loopYTo

                'Set coords of square
                squareObjects(startPoint).setSquareCords(x, y)

                'Phyiscally place it in canvas
                placeSquareOnCanvas(x, y, squareObjects(startPoint).sqRows, squareObjects(startPoint).sqCols)

                'Recursive, get next square
                If (startPoint + 1 < totalSquares) Then
                    recurse(startPoint + 1)
                Else
                    validSolution = isSolutionValid()

                    'Is solution valud
                    If (validSolution = True) Then
                        solutions = solutions + 1
                    End If

                    iterations = iterations + 1

                    'Response.Write("<br /><b>Iteration " & iterations & "</b>")
                    If (validSolution) Then

                        'Rate solution, record if best
                        solutionRating = rateSolution()
                        If solutionRating > bestCellSaving Then
                            bestCellSaving = solutionRating
                            copySolution()
                        End If
                        'Response.Write(" <span style='color:green'> <B>VALID SOLUTION</B></span> (" & rateSolution() & ")")
                    End If
                    'printCanvas(canvas)

                End If

                squareObjects(startPoint).removeSquare(canvas)

            Next
        Next
    End If

End Sub
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T15:34:17+00:00Added an answer on May 15, 2026 at 3:34 pm

    If you extract the loops in a separate routine the solution emerges relatively easily.

    I have changed the validSolution logic a bit to make the code shorter – now we don’t call recurse if the solution is invalid and we don’t need to check for isSolutionValid() at the beginning of recurse(). These changes make counting the iterations harder so I removed that code, but it should be possible to add it later.

    The recurse() routine without the last “If” statement should behave exactly as your code. The last “If” statement essentially performs the loops for a rotated rectangle.

    I am not sure how removeSquare() is implemented, but it may need to know the orientation to be able to work correctly.

    Sub recurse(ByVal startPoint As Integer)
        Dim loopXTo As Integer
        Dim loopYTo As Integer
        If (startPoint = 0) Then
            loopXTo = Math.Floor((canvasCols - squareObjects(startPoint).sqRows) / 2)
            loopYTo = Math.Floor((canvasRows - squareObjects(startPoint).sqCols) / 2)
        Else
            loopXTo = canvasCols - squareObjects(startPoint).sqRows
            loopYTo = canvasRows - squareObjects(startPoint).sqCols
        End If
        fitSqare(loopXTo, loopYTo, False)
        If (squareObjects(startPoint).sqCols <> squareObjects(startPoint).sqRows) Then
            fitSqare(loopYTo, loopXTo, True)
        End If
    End Sub
    
    Sub fitSquare(ByVal loopXTo As Integer, ByVal loopYTo As Integer, ByVal rotate As Boolean)
        Dim x As Integer
        Dim y As Integer
        Dim solutionRating As Integer
        Dim validSolution As Boolean
    
        'Loop all positions on canvas
        For x = 0 To loopXTo
            For y = 0 To loopYTo
                'Set coords of square
                squareObjects(startPoint).setSquareCords(x, y)
    
                'Phyiscally place it in canvas
                If (rotate) Then
                    placeSquareOnCanvas(x, y, squareObjects(startPoint).sqCols, squareObjects(startPoint).sqRows)
                Else
                    placeSquareOnCanvas(x, y, squareObjects(startPoint).sqRows, squareObjects(startPoint).sqCols)
                End If
    
                validSolution = isSolutionValid()
                'Is solution valud
                If (validSolution) Then
                    'Recursive, get next square
                    If (startPoint + 1 < totalSquares) Then
                        recurse(startPoint + 1)
                    Else
                        solutions = solutions + 1
                        'Rate solution, record if best
                        solutionRating = rateSolution()
                        If solutionRating > bestCellSaving Then
                            bestCellSaving = solutionRating
                            copySolution()
                        End If
                    End If
                End If
                squareObjects(startPoint).removeSquare(canvas) 'removeSquare may require additional work to handle rotated state
            Next
        Next
    End Sub
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.