Geometry, Two Rectangles Overlapping, Sharing Boundaries, Distinct











up vote
2
down vote

favorite












So I have this program that needs to test two rectangles and check:




  1. If the test rectangle is within the reference rectangle

  2. If the test rectangle is overlapping the reference rectangle

  3. If the test rectangle is only sharing a border with the reference rectangle

  4. If the test rectangle and reference rectangle are distinct


Both the reference and test rectangles are defined with their center coordinates (x,y) and their width and height.



I believe I have the first check coded correctly, but I cannot figure out the math for the last three checks of overlapping, sharing boundary, and being totally distinct.



I understand this isn't a programming site but my issue is purely math related not programming related. Whenever it says this.variable it is referring to the reference rectangle's value. r.variable is referring to the rectangle being tested against the reference rectangle.



Here is my code for the four checks so far:



   //returns true if the specified rectangle is inside this rectangle
public boolean contains(MyRectangle2D r){
if(this.x > r.x + r.width && x + width < r.x && y > r.y +r.height && y + height < r.y){
return true;
}
else{
return false;
}
}

//returns true if the specified rectangle overlaps with this rectangle
public boolean overlaps(MyRectangle2D r) {
if (this.x < r.x + r.width && x + width > r.x && y < r.y + r.height && y + height > r.y){
return true;
}
else{
return false;
}
}

//returns true if only the boundaries touch
public boolean abut(MyRectangle2D r) {
if(this.x = r.x + r.width && x + width = r.x || y = r.y +r.height && y + height = r.y){
return true;
}
else{
return false;
}
}

//returns true if the rectangles are not touching at all
public boolean distinct(MyRectangle2D r) {

}


Any help is greatly appreciated, thank you!










share|cite|improve this question
















bumped to the homepage by Community 2 days ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.











  • 1




    Was this assigned to you from school? Unless it was, I would personally define a rectangle by a lower left and a upper right coordinate (notice this creates a rectangle) mainly because it is much easier to if there is an intersection using the x and y values.
    – Ian Limarta
    Aug 6 '16 at 4:20










  • @EugenCovaci AFAIK, the Java language doesn’t treat rectangles at all. For the Java awt Rectangle class, its coordinates are those of the top left corner. In the code included in this question, the rectangles are of class MyRectangle2D, which looks pretty nonstandard to me.
    – amd
    Aug 6 '16 at 7:22












  • @amd By Java language I mean everything Java SDK has to offer. AWT is part of Java standard and the class java.awt.geom.Rectangle2D is abstract, ready to be extended, witch I suppose the OP did. Also JavaFX has javafx.geometry.Rectangle2D a concrete class having some methods like "boolean contains(Point2D p), boolean intersects(double x, double y, double w, double h) " that OP might be interested with. Both classes are part of Java Standard and both treat rectangles the way OD did. BTW, I'm Java certified programmer since 2007.
    – user261263
    Aug 6 '16 at 7:41










  • @EugenCovaci Are you sure about that? Take, for example docs.oracle.com/javase/8/javafx/api/javafx/geometry/…, which describes the rectangle’s location (as used in the constructor) as “minX, minY.” That doesn’t sound like its center to me, nor do any of the methods documented there return a center point.
    – amd
    Aug 6 '16 at 7:52






  • 1




    A detail: It is implicit, but it should be said explicitly in your question that all rectangles have their sides parallel to one of the coordinate's axes.
    – Jean Marie
    Aug 6 '16 at 10:52















up vote
2
down vote

favorite












So I have this program that needs to test two rectangles and check:




  1. If the test rectangle is within the reference rectangle

  2. If the test rectangle is overlapping the reference rectangle

  3. If the test rectangle is only sharing a border with the reference rectangle

  4. If the test rectangle and reference rectangle are distinct


Both the reference and test rectangles are defined with their center coordinates (x,y) and their width and height.



I believe I have the first check coded correctly, but I cannot figure out the math for the last three checks of overlapping, sharing boundary, and being totally distinct.



I understand this isn't a programming site but my issue is purely math related not programming related. Whenever it says this.variable it is referring to the reference rectangle's value. r.variable is referring to the rectangle being tested against the reference rectangle.



Here is my code for the four checks so far:



   //returns true if the specified rectangle is inside this rectangle
public boolean contains(MyRectangle2D r){
if(this.x > r.x + r.width && x + width < r.x && y > r.y +r.height && y + height < r.y){
return true;
}
else{
return false;
}
}

//returns true if the specified rectangle overlaps with this rectangle
public boolean overlaps(MyRectangle2D r) {
if (this.x < r.x + r.width && x + width > r.x && y < r.y + r.height && y + height > r.y){
return true;
}
else{
return false;
}
}

//returns true if only the boundaries touch
public boolean abut(MyRectangle2D r) {
if(this.x = r.x + r.width && x + width = r.x || y = r.y +r.height && y + height = r.y){
return true;
}
else{
return false;
}
}

//returns true if the rectangles are not touching at all
public boolean distinct(MyRectangle2D r) {

}


Any help is greatly appreciated, thank you!










share|cite|improve this question
















bumped to the homepage by Community 2 days ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.











  • 1




    Was this assigned to you from school? Unless it was, I would personally define a rectangle by a lower left and a upper right coordinate (notice this creates a rectangle) mainly because it is much easier to if there is an intersection using the x and y values.
    – Ian Limarta
    Aug 6 '16 at 4:20










  • @EugenCovaci AFAIK, the Java language doesn’t treat rectangles at all. For the Java awt Rectangle class, its coordinates are those of the top left corner. In the code included in this question, the rectangles are of class MyRectangle2D, which looks pretty nonstandard to me.
    – amd
    Aug 6 '16 at 7:22












  • @amd By Java language I mean everything Java SDK has to offer. AWT is part of Java standard and the class java.awt.geom.Rectangle2D is abstract, ready to be extended, witch I suppose the OP did. Also JavaFX has javafx.geometry.Rectangle2D a concrete class having some methods like "boolean contains(Point2D p), boolean intersects(double x, double y, double w, double h) " that OP might be interested with. Both classes are part of Java Standard and both treat rectangles the way OD did. BTW, I'm Java certified programmer since 2007.
    – user261263
    Aug 6 '16 at 7:41










  • @EugenCovaci Are you sure about that? Take, for example docs.oracle.com/javase/8/javafx/api/javafx/geometry/…, which describes the rectangle’s location (as used in the constructor) as “minX, minY.” That doesn’t sound like its center to me, nor do any of the methods documented there return a center point.
    – amd
    Aug 6 '16 at 7:52






  • 1




    A detail: It is implicit, but it should be said explicitly in your question that all rectangles have their sides parallel to one of the coordinate's axes.
    – Jean Marie
    Aug 6 '16 at 10:52













up vote
2
down vote

favorite









up vote
2
down vote

favorite











So I have this program that needs to test two rectangles and check:




  1. If the test rectangle is within the reference rectangle

  2. If the test rectangle is overlapping the reference rectangle

  3. If the test rectangle is only sharing a border with the reference rectangle

  4. If the test rectangle and reference rectangle are distinct


Both the reference and test rectangles are defined with their center coordinates (x,y) and their width and height.



I believe I have the first check coded correctly, but I cannot figure out the math for the last three checks of overlapping, sharing boundary, and being totally distinct.



I understand this isn't a programming site but my issue is purely math related not programming related. Whenever it says this.variable it is referring to the reference rectangle's value. r.variable is referring to the rectangle being tested against the reference rectangle.



Here is my code for the four checks so far:



   //returns true if the specified rectangle is inside this rectangle
public boolean contains(MyRectangle2D r){
if(this.x > r.x + r.width && x + width < r.x && y > r.y +r.height && y + height < r.y){
return true;
}
else{
return false;
}
}

//returns true if the specified rectangle overlaps with this rectangle
public boolean overlaps(MyRectangle2D r) {
if (this.x < r.x + r.width && x + width > r.x && y < r.y + r.height && y + height > r.y){
return true;
}
else{
return false;
}
}

//returns true if only the boundaries touch
public boolean abut(MyRectangle2D r) {
if(this.x = r.x + r.width && x + width = r.x || y = r.y +r.height && y + height = r.y){
return true;
}
else{
return false;
}
}

//returns true if the rectangles are not touching at all
public boolean distinct(MyRectangle2D r) {

}


Any help is greatly appreciated, thank you!










share|cite|improve this question















So I have this program that needs to test two rectangles and check:




  1. If the test rectangle is within the reference rectangle

  2. If the test rectangle is overlapping the reference rectangle

  3. If the test rectangle is only sharing a border with the reference rectangle

  4. If the test rectangle and reference rectangle are distinct


Both the reference and test rectangles are defined with their center coordinates (x,y) and their width and height.



I believe I have the first check coded correctly, but I cannot figure out the math for the last three checks of overlapping, sharing boundary, and being totally distinct.



I understand this isn't a programming site but my issue is purely math related not programming related. Whenever it says this.variable it is referring to the reference rectangle's value. r.variable is referring to the rectangle being tested against the reference rectangle.



Here is my code for the four checks so far:



   //returns true if the specified rectangle is inside this rectangle
public boolean contains(MyRectangle2D r){
if(this.x > r.x + r.width && x + width < r.x && y > r.y +r.height && y + height < r.y){
return true;
}
else{
return false;
}
}

//returns true if the specified rectangle overlaps with this rectangle
public boolean overlaps(MyRectangle2D r) {
if (this.x < r.x + r.width && x + width > r.x && y < r.y + r.height && y + height > r.y){
return true;
}
else{
return false;
}
}

//returns true if only the boundaries touch
public boolean abut(MyRectangle2D r) {
if(this.x = r.x + r.width && x + width = r.x || y = r.y +r.height && y + height = r.y){
return true;
}
else{
return false;
}
}

//returns true if the rectangles are not touching at all
public boolean distinct(MyRectangle2D r) {

}


Any help is greatly appreciated, thank you!







geometry






share|cite|improve this question















share|cite|improve this question













share|cite|improve this question




share|cite|improve this question








edited Aug 6 '16 at 4:12









MathTrain

659215




659215










asked Aug 6 '16 at 4:01









user359637

112




112





bumped to the homepage by Community 2 days ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







bumped to the homepage by Community 2 days ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.










  • 1




    Was this assigned to you from school? Unless it was, I would personally define a rectangle by a lower left and a upper right coordinate (notice this creates a rectangle) mainly because it is much easier to if there is an intersection using the x and y values.
    – Ian Limarta
    Aug 6 '16 at 4:20










  • @EugenCovaci AFAIK, the Java language doesn’t treat rectangles at all. For the Java awt Rectangle class, its coordinates are those of the top left corner. In the code included in this question, the rectangles are of class MyRectangle2D, which looks pretty nonstandard to me.
    – amd
    Aug 6 '16 at 7:22












  • @amd By Java language I mean everything Java SDK has to offer. AWT is part of Java standard and the class java.awt.geom.Rectangle2D is abstract, ready to be extended, witch I suppose the OP did. Also JavaFX has javafx.geometry.Rectangle2D a concrete class having some methods like "boolean contains(Point2D p), boolean intersects(double x, double y, double w, double h) " that OP might be interested with. Both classes are part of Java Standard and both treat rectangles the way OD did. BTW, I'm Java certified programmer since 2007.
    – user261263
    Aug 6 '16 at 7:41










  • @EugenCovaci Are you sure about that? Take, for example docs.oracle.com/javase/8/javafx/api/javafx/geometry/…, which describes the rectangle’s location (as used in the constructor) as “minX, minY.” That doesn’t sound like its center to me, nor do any of the methods documented there return a center point.
    – amd
    Aug 6 '16 at 7:52






  • 1




    A detail: It is implicit, but it should be said explicitly in your question that all rectangles have their sides parallel to one of the coordinate's axes.
    – Jean Marie
    Aug 6 '16 at 10:52














  • 1




    Was this assigned to you from school? Unless it was, I would personally define a rectangle by a lower left and a upper right coordinate (notice this creates a rectangle) mainly because it is much easier to if there is an intersection using the x and y values.
    – Ian Limarta
    Aug 6 '16 at 4:20










  • @EugenCovaci AFAIK, the Java language doesn’t treat rectangles at all. For the Java awt Rectangle class, its coordinates are those of the top left corner. In the code included in this question, the rectangles are of class MyRectangle2D, which looks pretty nonstandard to me.
    – amd
    Aug 6 '16 at 7:22












  • @amd By Java language I mean everything Java SDK has to offer. AWT is part of Java standard and the class java.awt.geom.Rectangle2D is abstract, ready to be extended, witch I suppose the OP did. Also JavaFX has javafx.geometry.Rectangle2D a concrete class having some methods like "boolean contains(Point2D p), boolean intersects(double x, double y, double w, double h) " that OP might be interested with. Both classes are part of Java Standard and both treat rectangles the way OD did. BTW, I'm Java certified programmer since 2007.
    – user261263
    Aug 6 '16 at 7:41










  • @EugenCovaci Are you sure about that? Take, for example docs.oracle.com/javase/8/javafx/api/javafx/geometry/…, which describes the rectangle’s location (as used in the constructor) as “minX, minY.” That doesn’t sound like its center to me, nor do any of the methods documented there return a center point.
    – amd
    Aug 6 '16 at 7:52






  • 1




    A detail: It is implicit, but it should be said explicitly in your question that all rectangles have their sides parallel to one of the coordinate's axes.
    – Jean Marie
    Aug 6 '16 at 10:52








1




1




Was this assigned to you from school? Unless it was, I would personally define a rectangle by a lower left and a upper right coordinate (notice this creates a rectangle) mainly because it is much easier to if there is an intersection using the x and y values.
– Ian Limarta
Aug 6 '16 at 4:20




Was this assigned to you from school? Unless it was, I would personally define a rectangle by a lower left and a upper right coordinate (notice this creates a rectangle) mainly because it is much easier to if there is an intersection using the x and y values.
– Ian Limarta
Aug 6 '16 at 4:20












@EugenCovaci AFAIK, the Java language doesn’t treat rectangles at all. For the Java awt Rectangle class, its coordinates are those of the top left corner. In the code included in this question, the rectangles are of class MyRectangle2D, which looks pretty nonstandard to me.
– amd
Aug 6 '16 at 7:22






@EugenCovaci AFAIK, the Java language doesn’t treat rectangles at all. For the Java awt Rectangle class, its coordinates are those of the top left corner. In the code included in this question, the rectangles are of class MyRectangle2D, which looks pretty nonstandard to me.
– amd
Aug 6 '16 at 7:22














@amd By Java language I mean everything Java SDK has to offer. AWT is part of Java standard and the class java.awt.geom.Rectangle2D is abstract, ready to be extended, witch I suppose the OP did. Also JavaFX has javafx.geometry.Rectangle2D a concrete class having some methods like "boolean contains(Point2D p), boolean intersects(double x, double y, double w, double h) " that OP might be interested with. Both classes are part of Java Standard and both treat rectangles the way OD did. BTW, I'm Java certified programmer since 2007.
– user261263
Aug 6 '16 at 7:41




@amd By Java language I mean everything Java SDK has to offer. AWT is part of Java standard and the class java.awt.geom.Rectangle2D is abstract, ready to be extended, witch I suppose the OP did. Also JavaFX has javafx.geometry.Rectangle2D a concrete class having some methods like "boolean contains(Point2D p), boolean intersects(double x, double y, double w, double h) " that OP might be interested with. Both classes are part of Java Standard and both treat rectangles the way OD did. BTW, I'm Java certified programmer since 2007.
– user261263
Aug 6 '16 at 7:41












@EugenCovaci Are you sure about that? Take, for example docs.oracle.com/javase/8/javafx/api/javafx/geometry/…, which describes the rectangle’s location (as used in the constructor) as “minX, minY.” That doesn’t sound like its center to me, nor do any of the methods documented there return a center point.
– amd
Aug 6 '16 at 7:52




@EugenCovaci Are you sure about that? Take, for example docs.oracle.com/javase/8/javafx/api/javafx/geometry/…, which describes the rectangle’s location (as used in the constructor) as “minX, minY.” That doesn’t sound like its center to me, nor do any of the methods documented there return a center point.
– amd
Aug 6 '16 at 7:52




1




1




A detail: It is implicit, but it should be said explicitly in your question that all rectangles have their sides parallel to one of the coordinate's axes.
– Jean Marie
Aug 6 '16 at 10:52




A detail: It is implicit, but it should be said explicitly in your question that all rectangles have their sides parallel to one of the coordinate's axes.
– Jean Marie
Aug 6 '16 at 10:52










2 Answers
2






active

oldest

votes

















up vote
0
down vote













I'm not going to write all the code, but mathematically I'll try to give an idea of what to check for.



First I'll define "reach" as half the width (for the x direction) or half the height (for the y). I call it reach because that's how far the rectangle can "reach" from its center (x,y) to other parts of the plane.



In order to check if they overlap, first check if the sum of their "reaches" (half their widths/heights) in the x direction is greater than the x-distance between them. Then do the same for the y direction and y "reach". I'm not going to write the code, but roughly:



if(w1/2+w2/2>abs(x1-x2)){



if(h1/2+h2/2>abs(y1-y2)){



return true.



if the reaches aren't more than the distances, they don't overlap.



If they only share a border, then one of four things could be true:




  • The x coordinate of the first rectangle plus its reach is equal to the x coordinate of the second one minus its reach (x1+w1/2=x2-w2/2).

  • The x coordinate of the second rectangle plus its reach is equal to the x coordinate of the first one minus its reach (x2+w2/2=x1-w1/2).

  • The next two are the same except replace all x's with y's and all w's with h's.


The rectangles can only be totally distinct if there is no border touching and no overlap, so if all of the others return false then they must be totally distinct.






share|cite|improve this answer




























    up vote
    0
    down vote













    First of all, distinct = not contains and not overlap and not boundaries touch.



    I would start by writing two method in MyRectangle2D class like this:




    boolean isPointInside(int x, int y)
    boolean isPointOnBoundary(int x, int y)


    to check when the point (x, y) is inside the current rectangle or on the boundary.



    Then:




    public boolean contains(MyRectangle2D r){
    //all r tips are inside the current rectangle.
    //Use isPointInside function
    }

    public boolean overlaps(MyRectangle2D r) {
    //one and only one of r tips is inside the current rectangle.
    //Use isPointInside function and contains function to check it is not contained
    }

    public boolean abut(MyRectangle2D r) {
    //at least one of r corners is on the current rectangle boundary.
    //Use isPointOnBoundary function
    }
    public boolean distinct(MyRectangle2D r) {
    return ! contains(r) && ! overlaps(r) & ! abut(r);
    }





    share|cite|improve this answer





















      Your Answer





      StackExchange.ifUsing("editor", function () {
      return StackExchange.using("mathjaxEditing", function () {
      StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
      StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
      });
      });
      }, "mathjax-editing");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "69"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      noCode: true, onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














       

      draft saved


      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f1883959%2fgeometry-two-rectangles-overlapping-sharing-boundaries-distinct%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      0
      down vote













      I'm not going to write all the code, but mathematically I'll try to give an idea of what to check for.



      First I'll define "reach" as half the width (for the x direction) or half the height (for the y). I call it reach because that's how far the rectangle can "reach" from its center (x,y) to other parts of the plane.



      In order to check if they overlap, first check if the sum of their "reaches" (half their widths/heights) in the x direction is greater than the x-distance between them. Then do the same for the y direction and y "reach". I'm not going to write the code, but roughly:



      if(w1/2+w2/2>abs(x1-x2)){



      if(h1/2+h2/2>abs(y1-y2)){



      return true.



      if the reaches aren't more than the distances, they don't overlap.



      If they only share a border, then one of four things could be true:




      • The x coordinate of the first rectangle plus its reach is equal to the x coordinate of the second one minus its reach (x1+w1/2=x2-w2/2).

      • The x coordinate of the second rectangle plus its reach is equal to the x coordinate of the first one minus its reach (x2+w2/2=x1-w1/2).

      • The next two are the same except replace all x's with y's and all w's with h's.


      The rectangles can only be totally distinct if there is no border touching and no overlap, so if all of the others return false then they must be totally distinct.






      share|cite|improve this answer

























        up vote
        0
        down vote













        I'm not going to write all the code, but mathematically I'll try to give an idea of what to check for.



        First I'll define "reach" as half the width (for the x direction) or half the height (for the y). I call it reach because that's how far the rectangle can "reach" from its center (x,y) to other parts of the plane.



        In order to check if they overlap, first check if the sum of their "reaches" (half their widths/heights) in the x direction is greater than the x-distance between them. Then do the same for the y direction and y "reach". I'm not going to write the code, but roughly:



        if(w1/2+w2/2>abs(x1-x2)){



        if(h1/2+h2/2>abs(y1-y2)){



        return true.



        if the reaches aren't more than the distances, they don't overlap.



        If they only share a border, then one of four things could be true:




        • The x coordinate of the first rectangle plus its reach is equal to the x coordinate of the second one minus its reach (x1+w1/2=x2-w2/2).

        • The x coordinate of the second rectangle plus its reach is equal to the x coordinate of the first one minus its reach (x2+w2/2=x1-w1/2).

        • The next two are the same except replace all x's with y's and all w's with h's.


        The rectangles can only be totally distinct if there is no border touching and no overlap, so if all of the others return false then they must be totally distinct.






        share|cite|improve this answer























          up vote
          0
          down vote










          up vote
          0
          down vote









          I'm not going to write all the code, but mathematically I'll try to give an idea of what to check for.



          First I'll define "reach" as half the width (for the x direction) or half the height (for the y). I call it reach because that's how far the rectangle can "reach" from its center (x,y) to other parts of the plane.



          In order to check if they overlap, first check if the sum of their "reaches" (half their widths/heights) in the x direction is greater than the x-distance between them. Then do the same for the y direction and y "reach". I'm not going to write the code, but roughly:



          if(w1/2+w2/2>abs(x1-x2)){



          if(h1/2+h2/2>abs(y1-y2)){



          return true.



          if the reaches aren't more than the distances, they don't overlap.



          If they only share a border, then one of four things could be true:




          • The x coordinate of the first rectangle plus its reach is equal to the x coordinate of the second one minus its reach (x1+w1/2=x2-w2/2).

          • The x coordinate of the second rectangle plus its reach is equal to the x coordinate of the first one minus its reach (x2+w2/2=x1-w1/2).

          • The next two are the same except replace all x's with y's and all w's with h's.


          The rectangles can only be totally distinct if there is no border touching and no overlap, so if all of the others return false then they must be totally distinct.






          share|cite|improve this answer












          I'm not going to write all the code, but mathematically I'll try to give an idea of what to check for.



          First I'll define "reach" as half the width (for the x direction) or half the height (for the y). I call it reach because that's how far the rectangle can "reach" from its center (x,y) to other parts of the plane.



          In order to check if they overlap, first check if the sum of their "reaches" (half their widths/heights) in the x direction is greater than the x-distance between them. Then do the same for the y direction and y "reach". I'm not going to write the code, but roughly:



          if(w1/2+w2/2>abs(x1-x2)){



          if(h1/2+h2/2>abs(y1-y2)){



          return true.



          if the reaches aren't more than the distances, they don't overlap.



          If they only share a border, then one of four things could be true:




          • The x coordinate of the first rectangle plus its reach is equal to the x coordinate of the second one minus its reach (x1+w1/2=x2-w2/2).

          • The x coordinate of the second rectangle plus its reach is equal to the x coordinate of the first one minus its reach (x2+w2/2=x1-w1/2).

          • The next two are the same except replace all x's with y's and all w's with h's.


          The rectangles can only be totally distinct if there is no border touching and no overlap, so if all of the others return false then they must be totally distinct.







          share|cite|improve this answer












          share|cite|improve this answer



          share|cite|improve this answer










          answered Aug 6 '16 at 4:29









          MathTrain

          659215




          659215






















              up vote
              0
              down vote













              First of all, distinct = not contains and not overlap and not boundaries touch.



              I would start by writing two method in MyRectangle2D class like this:




              boolean isPointInside(int x, int y)
              boolean isPointOnBoundary(int x, int y)


              to check when the point (x, y) is inside the current rectangle or on the boundary.



              Then:




              public boolean contains(MyRectangle2D r){
              //all r tips are inside the current rectangle.
              //Use isPointInside function
              }

              public boolean overlaps(MyRectangle2D r) {
              //one and only one of r tips is inside the current rectangle.
              //Use isPointInside function and contains function to check it is not contained
              }

              public boolean abut(MyRectangle2D r) {
              //at least one of r corners is on the current rectangle boundary.
              //Use isPointOnBoundary function
              }
              public boolean distinct(MyRectangle2D r) {
              return ! contains(r) && ! overlaps(r) & ! abut(r);
              }





              share|cite|improve this answer

























                up vote
                0
                down vote













                First of all, distinct = not contains and not overlap and not boundaries touch.



                I would start by writing two method in MyRectangle2D class like this:




                boolean isPointInside(int x, int y)
                boolean isPointOnBoundary(int x, int y)


                to check when the point (x, y) is inside the current rectangle or on the boundary.



                Then:




                public boolean contains(MyRectangle2D r){
                //all r tips are inside the current rectangle.
                //Use isPointInside function
                }

                public boolean overlaps(MyRectangle2D r) {
                //one and only one of r tips is inside the current rectangle.
                //Use isPointInside function and contains function to check it is not contained
                }

                public boolean abut(MyRectangle2D r) {
                //at least one of r corners is on the current rectangle boundary.
                //Use isPointOnBoundary function
                }
                public boolean distinct(MyRectangle2D r) {
                return ! contains(r) && ! overlaps(r) & ! abut(r);
                }





                share|cite|improve this answer























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  First of all, distinct = not contains and not overlap and not boundaries touch.



                  I would start by writing two method in MyRectangle2D class like this:




                  boolean isPointInside(int x, int y)
                  boolean isPointOnBoundary(int x, int y)


                  to check when the point (x, y) is inside the current rectangle or on the boundary.



                  Then:




                  public boolean contains(MyRectangle2D r){
                  //all r tips are inside the current rectangle.
                  //Use isPointInside function
                  }

                  public boolean overlaps(MyRectangle2D r) {
                  //one and only one of r tips is inside the current rectangle.
                  //Use isPointInside function and contains function to check it is not contained
                  }

                  public boolean abut(MyRectangle2D r) {
                  //at least one of r corners is on the current rectangle boundary.
                  //Use isPointOnBoundary function
                  }
                  public boolean distinct(MyRectangle2D r) {
                  return ! contains(r) && ! overlaps(r) & ! abut(r);
                  }





                  share|cite|improve this answer












                  First of all, distinct = not contains and not overlap and not boundaries touch.



                  I would start by writing two method in MyRectangle2D class like this:




                  boolean isPointInside(int x, int y)
                  boolean isPointOnBoundary(int x, int y)


                  to check when the point (x, y) is inside the current rectangle or on the boundary.



                  Then:




                  public boolean contains(MyRectangle2D r){
                  //all r tips are inside the current rectangle.
                  //Use isPointInside function
                  }

                  public boolean overlaps(MyRectangle2D r) {
                  //one and only one of r tips is inside the current rectangle.
                  //Use isPointInside function and contains function to check it is not contained
                  }

                  public boolean abut(MyRectangle2D r) {
                  //at least one of r corners is on the current rectangle boundary.
                  //Use isPointOnBoundary function
                  }
                  public boolean distinct(MyRectangle2D r) {
                  return ! contains(r) && ! overlaps(r) & ! abut(r);
                  }






                  share|cite|improve this answer












                  share|cite|improve this answer



                  share|cite|improve this answer










                  answered Aug 6 '16 at 4:46







                  user261263





































                       

                      draft saved


                      draft discarded



















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f1883959%2fgeometry-two-rectangles-overlapping-sharing-boundaries-distinct%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      MongoDB - Not Authorized To Execute Command

                      in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

                      How to fix TextFormField cause rebuild widget in Flutter