<br />
<b>Warning</b>:  Undefined array key "global_protection_id" in <b>/home/wikitechy/public_html/interview-questions/wp-content/plugins/content-protector/inc/class-ps-rest-handler.php</b> on line <b>51</b><br />
{"id":647,"date":"2021-07-13T23:34:06","date_gmt":"2021-07-13T23:34:06","guid":{"rendered":"https:\/\/www.wikitechy.com\/interview-questions\/?p=647"},"modified":"2021-09-11T11:19:34","modified_gmt":"2021-09-11T11:19:34","slug":"how-to-check-if-two-rectangles-intersect-or-not","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/interview-questions\/java\/how-to-check-if-two-rectangles-intersect-or-not\/","title":{"rendered":"How to check if two rectangles intersect or not ?"},"content":{"rendered":"<div class=\"TextHeading\">\n<div class=\"hddn\">\n<h2 id=\"how-to-check-if-two-rectangles-intersect-or-not\" class=\"color-pink\" style=\"text-align: justify;\">How to check if two rectangles intersect or not ?<\/h2>\n<\/div>\n<\/div>\n<div class=\"Content\" style=\"text-align: justify;\">\n<div class=\"hddn\">\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li>The following standard Java coding practices to solve this problem.<\/li>\n<li>We have an encapsulated two co-ordinates in a Point class and have Rectangle has two Point\u00a0<a href=\"https:\/\/www.wikitechy.com\/tutorials\/ruby-on-rails\/avoid-instance-variables-use-getters-and-setters\" target=\"_blank\" rel=\"noopener\">instance variable<\/a>\u00a0and an instance method like\u00a0<a href=\"https:\/\/www.wikitechy.com\/tutorials\/java\/java-strings-equals\" target=\"_blank\" rel=\"noopener\">equals()<\/a>\u00a0to check if another rectangle is overlapping or not.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<div class=\"ImageContent\">\n<div class=\"hddn\"><img decoding=\"async\" class=\"img-responsive center-block aligncenter\" src=\"https:\/\/cdn.wikitechy.com\/interview-questions\/java\/how-to-check-if-two-rectangles-intersect-or-not.jpg\" alt=\" To Check Rectangle Intersects or Not\" \/><\/div>\n<\/div>\n<ul>\n<li>A rectangle can be represented by two coordinates, top left, and bottom right. As part of the problem you will be given four coordinates L1, R1 and L2, R2, top left and bottom right coordinate of two rectangles and you need to write a function isOverlapping() which should return true if rectangles are overlapping or false if they are not.<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<div class=\"text-center row\" style=\"text-align: justify;\"><\/div>\n<div class=\"TextHeading\">\n<div class=\"hddn\">\n<h2 id=\"sample-code-in-java\" class=\"color-blue\" style=\"text-align: justify;\">Sample Code in Java:<\/h2>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\">public class Main {<br\/><br\/>  public static void main(String[] args) {<br\/>  Point l1 = new Point(0, 10);<br\/>  Point r1 = new Point(10, 0);<br\/>  Point l2 = new Point(5, 5);<br\/>  Point r2 = new Point(15, 0);<br\/><br\/>  Rectangle first = new Rectangle(l1, r1);<br\/>  Rectangle second = new Rectangle(l2, r2);<br\/><br\/>  if (first.isOverLapping(second)) {<br\/>  System.out<br\/>  .println(&quot;Yes, two rectangles are intersecting with each other&quot;);<br\/>  } else {<br\/>  System.out<br\/>  .println(&quot;No, two rectangles are not overlapping with each other&quot;);<br\/>  }<br\/>  }<br\/><br\/>}<br\/><br\/>class Point {<br\/>  int x;<br\/>  int y;<br\/><br\/>  public Point(int x, int y) {<br\/>  super();<br\/>  this.x = x;<br\/>  this.y = y;<br\/>  }<br\/>}<br\/><br\/>class Rectangle {<br\/><br\/>  private final Point topLeft;<br\/>  private final Point bottomRight;<br\/><br\/>  public Rectangle(Point topLeft, Point bottomRight) {<br\/>  this.topLeft = topLeft;<br\/>  this.bottomRight = bottomRight;<br\/>  }<br\/><br\/>  public boolean isOverLapping(Rectangle other) {<br\/>  if (this.topLeft.x &gt; other.bottomRight.x \/\/ R1 is right to R2<br\/>  || this.bottomRight.x &lt; other.topLeft.x \/\/ R1 is left to R2<br\/>  || this.topLeft.y &lt; other.bottomRight.y \/\/ R1 is above R2<br\/>  || this.bottomRight.y &gt; other.topLeft.y) { \/\/ R1 is below R1<br\/>  return false;<br\/>  }<br\/>  return true;<br\/>  }<br\/><br\/>}<\/code><\/pre> <\/div>\n<div class=\"TextHeading\" style=\"text-align: justify;\">\n<div class=\"hddn\">\n<h2 id=\"output\" class=\"color-blue\">Output<\/h2>\n<\/div>\n<\/div>\n<div class=\"Output\">\n<div class=\"hddn\">\n<figure class=\"highlight\" style=\"text-align: justify;\">\n<pre><code class=\"hljs javascript\" data-lang=\"\"><span class=\"nt\">Yes, two rectangles are intersecting <span class=\"hljs-keyword\">with<\/span> each other<\/span><\/code><\/pre>\n<\/figure>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Answer : The following standard Java coding practices to solve&#8230;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"passster_activate_protection":false,"passster_protect_child_pages":"","passster_protection_type":"password","passster_password":"","passster_activate_overwrite_defaults":"","passster_headline":"","passster_instruction":"","passster_placeholder":"","passster_button":"","passster_id":"","passster_activate_misc_settings":"","passster_redirect_url":"","passster_hide":"no","passster_area_shortcode":"","gtb_hide_title":false,"gtb_wrap_title":false,"gtb_class_title":"","gtb_remove_headerfooter":false,"footnotes":""},"categories":[4004],"tags":[195,491,4190,4195,4194,4193,360,203,199,214,4191,198,363,209,205,4188,4192,2936,222,15934,4199,196,212,213,4201,4189,286,207,366,204,4186,217,282,4023,4187,4197,4196,4198,483,206,200,4185,197,280,364,968,4200],"class_list":["post-647","post","type-post","status-publish","format-standard","hentry","category-java","tag-accenture-interview-questions-and-answers","tag-applied-materials-interview-questions-and-answers","tag-area-of-intersection-of-two-rectangles","tag-area-of-intersection-of-two-rectangles-python","tag-area-of-overlapping-rectangles-algorithm","tag-area-of-overlapping-rectangles-java","tag-atos-interview-questions-and-answers","tag-capgemini-interview-questions-and-answers","tag-casting-networks-india-pvt-limited-interview-questions-and-answers","tag-cgi-group-inc-interview-questions-and-answers","tag-check-if-two-rotated-rectangles-intersect","tag-chetu-interview-questions-and-answers","tag-ciena-corporation-interview-questions-and-answers","tag-collabera-technologies-interview-questions-and-answers","tag-dell-international-services-india-pvt-ltd-interview-questions-and-answers","tag-determine-if-two-rectangles-overlap-python","tag-find-the-intersection-of-two-rectangles-c","tag-fis-global-business-solutions-india-pvt-ltd-interview-questions-and-answers","tag-flipkart-interview-questions-and-answers","tag-geekyants-interview-questions-and-answers","tag-how-to-find-intersection-of-two-rectangles-in-c","tag-ibm-interview-questions-and-answers","tag-indecomm-global-services-interview-questions-and-answers","tag-infosys-technologies-interview-questions-and-answers","tag-intersection-of-rectangles-in-c","tag-intersection-of-two-rectangles-in-c","tag-lt-infotech-interview-questions-and-answers","tag-mphasis-interview-questions-and-answers","tag-netapp-interview-questions-and-answers","tag-oracle-corporation-interview-questions-and-answers","tag-overlapping-rectangles","tag-peoplestrong-interview-questions-and-answers","tag-persistent-systems-interview-questions-and-answers","tag-rbs-india-de-interview-questions-and-answers","tag-rectangle-intersection-algorithm","tag-rectangle-overlap-area-c","tag-rectangle-overlap-leetcode","tag-rectangle-overlap-lintcode","tag-reliance-industries-ltd-interview-questions-and-answers","tag-sap-labs-india-pvt-ltd-interview-questions-and-answers","tag-tech-mahindra-interview-questions-and-answers","tag-to-check-if-two-rectangles-are-intersecting-or-not","tag-unitedhealth-group-interview-questions-and-answers","tag-virtusa-consulting-services-pvt-ltd-interview-questions-and-answers","tag-wells-fargo-interview-questions-and-answers","tag-wipro-infotech-interview-questions-and-answers","tag-write-a-c-program-to-find-the-intersection-of-the-two-given-rectangles"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v22.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to check if two rectangles intersect or not ? - Java<\/title>\n<meta name=\"description\" content=\"How to check if two rectangles intersect or not ? - The following standard Java coding practices to solve this problem.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wikitechy.com\/interview-questions\/java\/how-to-check-if-two-rectangles-intersect-or-not\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to check if two rectangles intersect or not ? - Java\" \/>\n<meta property=\"og:description\" content=\"How to check if two rectangles intersect or not ? - The following standard Java coding practices to solve this problem.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wikitechy.com\/interview-questions\/java\/how-to-check-if-two-rectangles-intersect-or-not\/\" \/>\n<meta property=\"og:site_name\" content=\"Wikitechy\" \/>\n<meta property=\"article:published_time\" content=\"2021-07-13T23:34:06+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-09-11T11:19:34+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cdn.wikitechy.com\/interview-questions\/java\/how-to-check-if-two-rectangles-intersect-or-not.jpg\" \/>\n<meta name=\"author\" content=\"Editor\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Editor\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/java\/how-to-check-if-two-rectangles-intersect-or-not\/\",\"url\":\"https:\/\/www.wikitechy.com\/interview-questions\/java\/how-to-check-if-two-rectangles-intersect-or-not\/\",\"name\":\"How to check if two rectangles intersect or not ? - Java\",\"isPartOf\":{\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/java\/how-to-check-if-two-rectangles-intersect-or-not\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/java\/how-to-check-if-two-rectangles-intersect-or-not\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/cdn.wikitechy.com\/interview-questions\/java\/how-to-check-if-two-rectangles-intersect-or-not.jpg\",\"datePublished\":\"2021-07-13T23:34:06+00:00\",\"dateModified\":\"2021-09-11T11:19:34+00:00\",\"author\":{\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/4d5a581fb5470d1560324bddc5e8b757\"},\"description\":\"How to check if two rectangles intersect or not ? - The following standard Java coding practices to solve this problem.\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wikitechy.com\/interview-questions\/java\/how-to-check-if-two-rectangles-intersect-or-not\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/java\/how-to-check-if-two-rectangles-intersect-or-not\/#primaryimage\",\"url\":\"https:\/\/cdn.wikitechy.com\/interview-questions\/java\/how-to-check-if-two-rectangles-intersect-or-not.jpg\",\"contentUrl\":\"https:\/\/cdn.wikitechy.com\/interview-questions\/java\/how-to-check-if-two-rectangles-intersect-or-not.jpg\"},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#website\",\"url\":\"https:\/\/www.wikitechy.com\/interview-questions\/\",\"name\":\"Wikitechy\",\"description\":\"Interview Questions\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wikitechy.com\/interview-questions\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/4d5a581fb5470d1560324bddc5e8b757\",\"name\":\"Editor\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/e9531079fe7e07841b7b156c04d65e5f39d4adfd18b6ffe3edfff8ca5aab85b5?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/e9531079fe7e07841b7b156c04d65e5f39d4adfd18b6ffe3edfff8ca5aab85b5?s=96&d=mm&r=g\",\"caption\":\"Editor\"},\"url\":\"https:\/\/www.wikitechy.com\/interview-questions\/author\/editor\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to check if two rectangles intersect or not ? - Java","description":"How to check if two rectangles intersect or not ? - The following standard Java coding practices to solve this problem.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wikitechy.com\/interview-questions\/java\/how-to-check-if-two-rectangles-intersect-or-not\/","og_locale":"en_US","og_type":"article","og_title":"How to check if two rectangles intersect or not ? - Java","og_description":"How to check if two rectangles intersect or not ? - The following standard Java coding practices to solve this problem.","og_url":"https:\/\/www.wikitechy.com\/interview-questions\/java\/how-to-check-if-two-rectangles-intersect-or-not\/","og_site_name":"Wikitechy","article_published_time":"2021-07-13T23:34:06+00:00","article_modified_time":"2021-09-11T11:19:34+00:00","og_image":[{"url":"https:\/\/cdn.wikitechy.com\/interview-questions\/java\/how-to-check-if-two-rectangles-intersect-or-not.jpg"}],"author":"Editor","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Editor","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wikitechy.com\/interview-questions\/java\/how-to-check-if-two-rectangles-intersect-or-not\/","url":"https:\/\/www.wikitechy.com\/interview-questions\/java\/how-to-check-if-two-rectangles-intersect-or-not\/","name":"How to check if two rectangles intersect or not ? - Java","isPartOf":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/java\/how-to-check-if-two-rectangles-intersect-or-not\/#primaryimage"},"image":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/java\/how-to-check-if-two-rectangles-intersect-or-not\/#primaryimage"},"thumbnailUrl":"https:\/\/cdn.wikitechy.com\/interview-questions\/java\/how-to-check-if-two-rectangles-intersect-or-not.jpg","datePublished":"2021-07-13T23:34:06+00:00","dateModified":"2021-09-11T11:19:34+00:00","author":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/4d5a581fb5470d1560324bddc5e8b757"},"description":"How to check if two rectangles intersect or not ? - The following standard Java coding practices to solve this problem.","inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wikitechy.com\/interview-questions\/java\/how-to-check-if-two-rectangles-intersect-or-not\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.wikitechy.com\/interview-questions\/java\/how-to-check-if-two-rectangles-intersect-or-not\/#primaryimage","url":"https:\/\/cdn.wikitechy.com\/interview-questions\/java\/how-to-check-if-two-rectangles-intersect-or-not.jpg","contentUrl":"https:\/\/cdn.wikitechy.com\/interview-questions\/java\/how-to-check-if-two-rectangles-intersect-or-not.jpg"},{"@type":"WebSite","@id":"https:\/\/www.wikitechy.com\/interview-questions\/#website","url":"https:\/\/www.wikitechy.com\/interview-questions\/","name":"Wikitechy","description":"Interview Questions","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wikitechy.com\/interview-questions\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/4d5a581fb5470d1560324bddc5e8b757","name":"Editor","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/e9531079fe7e07841b7b156c04d65e5f39d4adfd18b6ffe3edfff8ca5aab85b5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/e9531079fe7e07841b7b156c04d65e5f39d4adfd18b6ffe3edfff8ca5aab85b5?s=96&d=mm&r=g","caption":"Editor"},"url":"https:\/\/www.wikitechy.com\/interview-questions\/author\/editor\/"}]}},"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts\/647","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/comments?post=647"}],"version-history":[{"count":3,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts\/647\/revisions"}],"predecessor-version":[{"id":3408,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts\/647\/revisions\/3408"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/media?parent=647"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/categories?post=647"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/tags?post=647"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}