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

  • Home
  • SEARCH
  • 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 7938181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:43:57+00:00 2026-06-03T22:43:57+00:00

I have a grid on which users can draw rectangles (actually rooms in a

  • 0

I have a grid on which users can draw rectangles (actually rooms in a house). Because rooms cannot cross each other, I try to check for conflicts before drawing the second room. So far I managed to check if the ‘to-point’ of the second rectangle is inside the first rectangle (=> return false). But the code has to ‘return: false’ also when the boundaries of the room cross another room.

I have set up an example page here

To try it, click anywhere in the grid and click somewhere else to create the first room. Then click outside this box and move the cursor to see the problem…

In the source code I have marked the concerning code (starts on line 175).

Thanks in advance!

  • 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-03T22:43:59+00:00Added an answer on June 3, 2026 at 10:43 pm

    You return false in the anonymous function called by the each statement 🙂 the checkConflict function always return true ! Look :

    function checkConflict(fromx, fromy, tox, toy) {
        $.each(rooms, function() {
            var left1 = fromx;
            var left2 = $(this)[0];
            var right1 = tox;
            var right2 = $(this)[2];
            var top1 = fromy;
            var top2 = $(this)[1];
            var bottom1 = toy;
            var bottom2 = $(this)[3];
    
            if (bottom1 < top2) { return false; }
            if (top1 > bottom2) { return false; }
            if (right1 < left2) { return false; }
            if (left1 > right2) { return false; }
        });  
        return true;
    }
    

    This is easy to fix but I give you the updated code because your tests are not correct too ( you want to test if you are INSIDE the box, your previous code would always return false ) So this is probably something like this (you have to improve this code because it won’t work perfectly either) :

      function checkConflict(fromx, fromy, tox, toy) {
        var returnValue = true;
        $.each(rooms, function() {
           if (tox > this[0] &&
               tox < this[2] && 
               toy > this[1] && 
               toy < this[3]) 
            { returnValue = false; return false; }
    
            if (fromx > this[0] && 
                fromx < this[2] &&
                fromy > this[1] && 
                fromy < this[3]) 
            { returnValue = false; return false; }
    
        });
        return returnValue;
    }
    

    EDIT:
    I wrote the correct code. I thought it would be easier but, a promise is a promise… There are probably ways to refactor the code but the headache is enough for now 😀

    I made a JSFiddle so you can see the result : http://jsfiddle.net/T4ta9/2/

    function checkConflict(fromx, fromy, tox, toy) {
        var returnValue = true;
        $.each(rooms, function() {
    
             var squareLeft = Math.min( parseInt(this[0]) ,parseInt(this[2])) , 
                squareRight = Math.max(parseInt(this[0]) ,parseInt(this[2]))  , 
                squareTop = Math.min(parseInt(this[1]) ,parseInt(this[3])) ,
                squareBot = Math.max(parseInt(this[1]) ,parseInt(this[3])) ;
    
             //drawing inside a shape
             if ((fromx > squareLeft && fromx < squareRight && fromy > squareTop && fromy < squareBot) 
             && (tox > squareRight || tox < squareLeft || toy > squareBot || toy < squareTop)) {
            returnValue = false;
            return false;
        }
    
            // meet the bottom of the current square ?
            if (fromy >= squareBot && // we are below this square 
                    ( 
                    (toy < squareBot) &&  // and our destination is above the bottom of this square
                        ( 
                            (
                                fromx < squareRight && // we are drawing on the inner left side of this square
                                (tox > squareLeft || fromx > squareLeft)  // and our destination is on the left of this square, or we started to draw on the right part of this square
                            ) 
                            || 
                            (
                                fromx > squareLeft && // we are drawing on the inner right side of this square
                                (tox < squareRight || fromx < squareRight)  // and our destination is on 
                            )
                        )
                    )
                ) 
            {
                returnValue = false;
                return false;
            }
    
            // meet the top of a square ?
            if (fromy <= squareTop && 
                    ( 
                    (toy > squareTop) &&  
                        ( 
                            (
                                fromx < squareRight && 
                                (tox > squareLeft || fromx > squareLeft) 
                            ) 
                            || 
                            (
                                fromx > squareLeft && 
                                (tox < squareRight || fromx < squareRight)  
                            )
                        )
                    )
                ) 
            {
                returnValue = false;
                return false;
            }
    
            // meet the left of a square ?
            if (fromx <= squareLeft && 
                    ( 
                    (tox > squareLeft) &&  
                        ( 
                            (
                                fromy < squareBot && 
                                (toy > squareTop || fromy > squareTop) 
                            ) 
                            || 
                            (
                                fromy > squareTop && 
                                (toy < squareBot || fromy < squareBot)  
                            )
                        )
                    )
                ) 
            {
                returnValue = false;
                return false;
            }
    
            // meet the right of a square ?
            if (fromx >= squareRight && 
                    ( 
                    (tox < squareRight) &&  
                        ( 
                            (
                                fromy < squareBot && 
                                (toy > squareTop || fromy > squareTop) 
                            ) 
                            || 
                            (
                                fromy > squareTop && 
                                (toy < squareBot || fromy < squareBot)  
                            )
                        )
                    )
                ) 
            {
                returnValue = false;
                return false;
            }
    
        });
        return returnValue;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a page which contains a dynamically generated grid, where a user can
I have grid which displays users information. I have commandColumn with edit, delete commands.
I want to make a WPF Grid in which users can drag and drop
I have a grid with multiple columns and users can sort based on any
I have a Grid which contains an Image in one of its columns. The
I have a grid view which contains a button in a template field. I
I have a grid bound to a BindingSource which is bound to DataContext table,
I have a grid with one TemplateField which is a checkbox and say 2
I have a grid, which I want to add a button at the top
I have a grid (DevExpress XtraGrid, if that matters) which is bound to a

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.