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 8071791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T13:48:38+00:00 2026-06-05T13:48:38+00:00

Given a contiguous drawing of arbitrary pixels (e.g. on an HTML5 Canvas) is there

  • 0

Given a contiguous drawing of arbitrary pixels (e.g. on an HTML5 Canvas) is there any algorithm for finding the axis-aligned bounding box that is more efficient than simply looking at every pixel and recording the min/max x/y values?

  • 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-06-05T13:48:40+00:00Added an answer on June 5, 2026 at 1:48 pm

    Just scanline from top left to right and down to get y top,and similar algorithm with different directions for the rest.


    Edit by Phrogz:

    Here’s a pseudo-code implementation. An included optimization ensures that each scan line does not look at pixels covered by an earlier pass:

    function boundingBox()
      w = getWidth()            # Assuming graphics address goes from [0,w)
      h = getHeight()           # Assuming graphics address goes from [0,h)
      for y=h-1 to 0 by -1      # Iterate from last row upwards
        for x=w-1 to 0 by -1    # Iterate across the entire row
          if pxAt(x,y) then
            maxY=y
            break               # Break out of both loops
    
      if maxY===undefined then  # No pixels, no bounding box
        return               
    
      for x=w-1 to 0 by -1      # Iterate from last column to first
        for y=0 to maxY         # Iterate down the column, up to maxY
          if pxAt(x,y) then
            maxX=x
            break               # Break out of both loops
    
      for x=0 to maxX           # Iterate from first column to maxX
        for y=0 to maxY         # Iterate down the column, up to maxY
          if pxAt(x,y) then
            minX=x
            break               # Break out of both loops
    
      for y=0 to maxY           # Iterate down the rows, up to maxY
        for x=0 to maxX         # Iterate across the row, up to maxX
          if pxAt(x,y) then
            minY=y
            break               # Break out of both loops
    
      return minX, minY, maxX, maxY
    

    The result (in practice) performs about the same as the brute-force algorithm for a single pixel, and significantly better as the object gets larger.

    Demo: http://phrogz.net/tmp/canvas_bounding_box2.html

    For fun, here’s a visual representation of how this algorithm works:

            enter image description here
            enter image description here
            enter image description here
            enter image description here
            enter image description here

    It doesn’t matter in what order you choose to do the sides, you just have to make sure that you take the previous results into account so that you are not double-scanning the corners.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to instantiate a pointer using (unsafe) C# at any given memory
Given an array of length N. How will you find the minimum length contiguous
I want to find a nonempty, contiguous subarray for a given input array of
Given a set of real numbers drawn from a unknown continuous univariate distribution (let's
Given this markup: // Calendar.html?date=1/2/2003 <script> $(function() { $('.inlinedatepicker').datepicker(); }); </script> ... <div class=inlinedatepicker
Given a package, how can I automatically find all its sub-packages?
Given these two queries: Select t1.id, t2.companyName from table1 t1 INNER JOIN table2 t2
Given a date range how to calculate the number of weekends partially or wholly
Given an interface or interfaces, what is the best way to generate an class
Given: unsigned int a, b, c, d; I want: d = a * b

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.