Urgent.News

What's breaking now, across thousands of outlets.

Tech

Will That Sofa Fit? Validate Your Furniture Layout With JavaScript Rectangles

Check room boundaries, furniture overlaps, and reserved walkways with JavaScript rectangles, then sketch the same layout in SVG.

Will That Sofa Fit? Validate Your Furniture Layout With JavaScript Rectangles

Room layout checks can be performed programmatically using a JavaScript function that treats rooms and furniture as simple rectangles. By representing each room and piece as an x, y coordinate pair along with width and height, you can easily determine if two rectangles overlap or if a rectangle is contained within the room's boundaries. This provides a quick way to compare different layout configurations before making a purchase decision.

To implement this, define a function that validates the input rectangles, ensuring they contain finite, positive values for their coordinates and dimensions. Then, create an overlap function that checks if the horizontal and vertical intervals of two rectangles intersect. A shared edge between rectangles is considered an overlap, but the function only returns true if there is a true overlap, not just touching edges.

Next, develop an inside function to verify if a rectangle is entirely within the room's boundaries. This function also uses the previously defined overlap checks. Once the functions are established, you can model a specific room layout, such as a room measuring 420 by 320 centimetres, and place furniture like a sofa occupying a 200 by 90 rectangle and a table occupying a 100 by 60 rectangle.

If you need to test a change in the layout, such as moving the table, simply update the coordinates and width/height values and re-run the overlap and inside checks. This allows for easy iteration and comparison of multiple layout options. Remember to validate all inputs using the finite check function before performing any comparisons.

Written by urgent.news from HackerNoon's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at hackernoon.com →

More in Tech

More from Thursday 22 October →