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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:47:40+00:00 2026-05-31T03:47:40+00:00

I have a list of objects like this: [ Rectangle(20, 30, 100, 200), //

  • 0

I have a list of objects like this:

[
  Rectangle(20, 30, 100, 200), // x, y, width, height
  Rectangle(50, 40, 50, 50),
  Text(60, 50, 'Text')
]

For the sake of this example, the first Rectangle is the bottom-most object, Text is top-most. Based on the position/size, the second Rectangle should be a child of the first as it is entirely contained within the first and on top. The text object should be a child of the second Rectangle for the same reasons.

I’m looking for discussion or a general pointer toward an algorithm to efficiently do this. I’m building my own, but there is likely something clever I’m missing.

EDIT: Here is my implementation in coffeescript:

obj = 
  _isParent: (parent, child) ->
    pb = parent.getBounds() # this returns a rectangle
    return pb.contains(child.getBounds())

  generateHiearchy: (object, index, objects, cache) ->
    return cache[object._id] if cache[object._id]

    exObject = new Node(object)
    cache[object._id] = exObject

    for i in [index+1...objects.length]
      potentialParent = objects[i]
      if @_isParent(potentialParent, object)
        exParent = @generateHiearchy(potentialParent, i, objects, cache)
        exParent.addChild(exObject)
        break

    exObject

  generateTree: (objects) ->
    return [] unless objects and _.size(objects)

    cache = {}
    objects = @sort(objects)

    for i in [0...objects.length]
      object = objects[i]
      exobj = @generateHiearchy(object, i, objects, cache)

    (val for k, val of cache when not val.parent)

Basically, I sort it so the top-most is at the array head, then walk the array. On each iteration, I recurse from the current node all the way to the top of the hierarchy, caching nodes along the way.

  • 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-31T03:47:41+00:00Added an answer on May 31, 2026 at 3:47 am

    You might want to look at R-trees. This data structure keeps rectangles in a tree hierarchy. Basically, if a rectangle node R1 has a child node rectangle R2, it means R2 is contained within R1. Have a look at the illustration on Wikipedia, it’s very self-explanatory.

    To tailor this data structure to your needs, you can find a way to represent Text objects as a Rectangle (or some other multi-dimensional object compatible with R-trees).

    It should be easy to find an open-source implementation. Hope this suits your needs!

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

Sidebar

Related Questions

For example, if I have list of ids values like this: values = list(set(Handler.objects.filter(blog=1).values_list('entry_id',
I have List objects which are shown like this: www.mysite.com/lists/123 Where 123 is the
I have a list of objects that I've created manually, like this: rand1 <-
I have a list of objects like this: ArrayList<Phone> list = new ArrayList(); list.add(new
I have a list of objects returned from getJdbcTemplate().query that look like this object(test,test,test,1)
Q: I have a list of objects like this: List<object> entities = GetallEntities(); The
I have a List of Objects like List<Object> p .I want to sort this
I have a list of many Python objects like this: class RangeClass(object): def __init__(self,address,size):
I have a large list of objects inside JSON like this: var data =
I have a list of objects MyObject and that looks like this: var Obj1

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.