Find out a general expression for the coordinates of a point in a square based on certain distances and an...












5












$begingroup$


my problem appears to be a very simple one, but I just can't seem to figure it out, maybe I am just overlooking something…



figure



The problem is defined as the following (refer to the figure for better understanding): I have a square of side L and I have a variable point O inside the square. The distances d1, d2, d3, and d4 are known and the angle between d1 and the horizontal, which I call $alpha$, is also known. The line segments d1, d3 and d2, d4 make the lines AB and CD respectively, which are perpendicular to each other. My aim is to find the coordinates of the point O when the origin of the 2d plane is the bottom left corner of the square (denoted as P) and the X axis is defined by the bottom side of the square, and the Y axis is defined by the left side of the square. In the figure 1, this can be calculated very easily: the coordinate $X=L-d1*cos(alpha)$ and $Y=L-d4*cos(alpha)$.



The issue arises when I try and find a general equation to give the coordinates of O. For example in figure 2: $X$ is no longer $L-d1*cos(alpha)$ but rather is $X=d4*cos(alpha)$ and $Y$ is no longer $L-d4*cos(alpha)$ but is $Y=L- d1*sin(alpha)$. In other words, the expressions for X and Y keep changing based on certain conditions. Ideally, I would want to have a general expression for X and Y regardless of the situation, but I am okay with having a few conditions.



To put this math problem into context, my point O is actually a robotic car, the square represents a boundary that the car moves about in and the distances d1,d2,d3 and d4 are distances to the boundary that are measured from distance sensors that are attached to the car, the angle is measured using an onboard accelerometer and gyro. And I am trying to use these sensor inputs to position the robot on a 2d cartesian plane.



Update: I added another figure (figure 3) to clarify certain doubts.










share|cite|improve this question











$endgroup$

















    5












    $begingroup$


    my problem appears to be a very simple one, but I just can't seem to figure it out, maybe I am just overlooking something…



    figure



    The problem is defined as the following (refer to the figure for better understanding): I have a square of side L and I have a variable point O inside the square. The distances d1, d2, d3, and d4 are known and the angle between d1 and the horizontal, which I call $alpha$, is also known. The line segments d1, d3 and d2, d4 make the lines AB and CD respectively, which are perpendicular to each other. My aim is to find the coordinates of the point O when the origin of the 2d plane is the bottom left corner of the square (denoted as P) and the X axis is defined by the bottom side of the square, and the Y axis is defined by the left side of the square. In the figure 1, this can be calculated very easily: the coordinate $X=L-d1*cos(alpha)$ and $Y=L-d4*cos(alpha)$.



    The issue arises when I try and find a general equation to give the coordinates of O. For example in figure 2: $X$ is no longer $L-d1*cos(alpha)$ but rather is $X=d4*cos(alpha)$ and $Y$ is no longer $L-d4*cos(alpha)$ but is $Y=L- d1*sin(alpha)$. In other words, the expressions for X and Y keep changing based on certain conditions. Ideally, I would want to have a general expression for X and Y regardless of the situation, but I am okay with having a few conditions.



    To put this math problem into context, my point O is actually a robotic car, the square represents a boundary that the car moves about in and the distances d1,d2,d3 and d4 are distances to the boundary that are measured from distance sensors that are attached to the car, the angle is measured using an onboard accelerometer and gyro. And I am trying to use these sensor inputs to position the robot on a 2d cartesian plane.



    Update: I added another figure (figure 3) to clarify certain doubts.










    share|cite|improve this question











    $endgroup$















      5












      5








      5


      2



      $begingroup$


      my problem appears to be a very simple one, but I just can't seem to figure it out, maybe I am just overlooking something…



      figure



      The problem is defined as the following (refer to the figure for better understanding): I have a square of side L and I have a variable point O inside the square. The distances d1, d2, d3, and d4 are known and the angle between d1 and the horizontal, which I call $alpha$, is also known. The line segments d1, d3 and d2, d4 make the lines AB and CD respectively, which are perpendicular to each other. My aim is to find the coordinates of the point O when the origin of the 2d plane is the bottom left corner of the square (denoted as P) and the X axis is defined by the bottom side of the square, and the Y axis is defined by the left side of the square. In the figure 1, this can be calculated very easily: the coordinate $X=L-d1*cos(alpha)$ and $Y=L-d4*cos(alpha)$.



      The issue arises when I try and find a general equation to give the coordinates of O. For example in figure 2: $X$ is no longer $L-d1*cos(alpha)$ but rather is $X=d4*cos(alpha)$ and $Y$ is no longer $L-d4*cos(alpha)$ but is $Y=L- d1*sin(alpha)$. In other words, the expressions for X and Y keep changing based on certain conditions. Ideally, I would want to have a general expression for X and Y regardless of the situation, but I am okay with having a few conditions.



      To put this math problem into context, my point O is actually a robotic car, the square represents a boundary that the car moves about in and the distances d1,d2,d3 and d4 are distances to the boundary that are measured from distance sensors that are attached to the car, the angle is measured using an onboard accelerometer and gyro. And I am trying to use these sensor inputs to position the robot on a 2d cartesian plane.



      Update: I added another figure (figure 3) to clarify certain doubts.










      share|cite|improve this question











      $endgroup$




      my problem appears to be a very simple one, but I just can't seem to figure it out, maybe I am just overlooking something…



      figure



      The problem is defined as the following (refer to the figure for better understanding): I have a square of side L and I have a variable point O inside the square. The distances d1, d2, d3, and d4 are known and the angle between d1 and the horizontal, which I call $alpha$, is also known. The line segments d1, d3 and d2, d4 make the lines AB and CD respectively, which are perpendicular to each other. My aim is to find the coordinates of the point O when the origin of the 2d plane is the bottom left corner of the square (denoted as P) and the X axis is defined by the bottom side of the square, and the Y axis is defined by the left side of the square. In the figure 1, this can be calculated very easily: the coordinate $X=L-d1*cos(alpha)$ and $Y=L-d4*cos(alpha)$.



      The issue arises when I try and find a general equation to give the coordinates of O. For example in figure 2: $X$ is no longer $L-d1*cos(alpha)$ but rather is $X=d4*cos(alpha)$ and $Y$ is no longer $L-d4*cos(alpha)$ but is $Y=L- d1*sin(alpha)$. In other words, the expressions for X and Y keep changing based on certain conditions. Ideally, I would want to have a general expression for X and Y regardless of the situation, but I am okay with having a few conditions.



      To put this math problem into context, my point O is actually a robotic car, the square represents a boundary that the car moves about in and the distances d1,d2,d3 and d4 are distances to the boundary that are measured from distance sensors that are attached to the car, the angle is measured using an onboard accelerometer and gyro. And I am trying to use these sensor inputs to position the robot on a 2d cartesian plane.



      Update: I added another figure (figure 3) to clarify certain doubts.







      geometry trigonometry robotics






      share|cite|improve this question















      share|cite|improve this question













      share|cite|improve this question




      share|cite|improve this question








      edited Jan 15 at 15:25







      kousheek

















      asked Jan 15 at 7:47









      kousheekkousheek

      286




      286






















          1 Answer
          1






          active

          oldest

          votes


















          0












          $begingroup$

          enter image description here



          We are going to introduce a new coordinate system $x'Oy'$ like shown in Fig. 1. If we are able to calculate coordinates of point $P(x'_P, y'_P)$ in that coordinate systen - we are done because:



          $$x_O=-x'_P, y_O=-y'_Ptag{1}$$



          Step 1:



          Find coordinates of points $A,B,C,D$ in $x'Oy'$ coordinate system:



          $$A: x'_1=d_1cosalpha, y'_1=d_1sinalpha$$



          $$D: x'_2=d_4cos(alpha+90^circ), y'_2=d_4sin(alpha+90^circ)$$



          $$B: x'_3=d_3cos(alpha+180^circ), y'_3=d_3sin(alpha+180^circ)$$



          $$C: x'_4=d_2cos(alpha+270^circ), y'_4=d_2sin(alpha+270^circ)$$



          Step 2:



          Calculate the following values:



          $$x'_{min}=min(x_1, x_2, x_3, x_4)$$
          $$x'_{max}=max(x_1, x_2, x_3, x_4)$$
          $$y'_{min}=min(y_1, y_2, y_3, y_4)$$
          $$y'_{max}=max(y_1, y_2, y_3, y_4)$$



          Step 3:



          Find the bottom left corner $P'(x'_{P'}, y'_{P'})$ of the bounding rectangle (shown in blue color) and calculate the width $w$ and height $h$ of the bounding rectangle:



          $$x'_{P'}=x'_min$$
          $$y'_{P'}=y'_min$$
          $$w=x'_max-x'_min$$
          $$h=y'_max-y'_min$$



          Step 4:



          If $h=w=L$ we are done (see Fig. 2) because the blue rectangle is actually our bounding square of length $L$, the points $P$ and $P'$ are identical and $x'_{P}=x'_{P'}$, $y'_{P}=y'_{P'}$. Just apply (1) and you have $x_O, y_O$.



          Step 5:



          Needed only if $w<L$. In this case your blue rectangle is narrower than the square and it is possible that points $P$ and $P'$ are not identical. In other words, you have to expand the blue rectangle either to the left or to the right to match the original square.



          If the blue rectangle has to be expanded to the right, it won't impact the position of point $P'$ and in this case:



          $$x'_P=x'_{P'}$$



          The point $P'$ moves only if you have to expand your rectangle to the left. Such situation is presented in Fig. 3. Obviously:



          $$x'_P=x'_{P'}-(L-w)$$



          Now apply (1) and you have $x_O$.



          How to recognize that expansion to the left is needed? Take a look at Fig. 3 again. You cannot move the right side of the blue rectangle more to the right because that side has points $A$ and $C$ on it and that points are not corners. If you moved that side to the right, endpoints of $d_1$ and $d_2$ would be no longer "attached" to the right side. On the contrary, there is no non-corner point on the left side of the blue rectangle so you can freely move the left side further to the left and still have endpoints $A,B,C,D$ "attached" to sides of the square.



          You can detect if there is a point $(x_i, y_i)$ on the left side of the blue rectangle that is blocking expansion to the left with the following condition:



          $$x_i=x_min land y_i ne y_min land y_i ne y_max$$



          Expansion to the left is not possible if this condition is satisfied for any $i=1,2,3,4$



          Step 6:



          Needed only if $h<L$. In this case your blue rectangle is shorter than the square and it is possible that points $P$ and $P'$ are not identical. In other words, you have to expand the blue rectangle either upwards or downwards to match the original square.



          If the blue rectangle has to be expanded upwards, it won't impact the position of point $P'$:



          $$y'_P=y'_{P'}$$



          The point $P'$ moves only if you have to expand your rectangle downwards. Such situation is presented in Fig. 1. Obviously:



          $$y'_P=y'_{P'}-(L-h)$$



          Now apply (1) and you have $y_O$.



          How to recognize that expansion downwards is needed? Take a look at Fig. 1 again. You cannot move the top side of the blue rectangle upwards because that side has point $D$ on it and that point is not a corner. If you moved that side upwards, endpoint of $d_4$ would be no longer "attached" to the top side. On the contrary, there is no non-corner point on the bottom side of the blue rectangle so you can freely move the bottom side further down and still have endpoints $A,B,C,D$ "attached" to sides of the square.



          You can detect if there is a point $(x_i, y_i)$ on the bottom side that is blocking expansion downwards with the following condition:



          $$y_i=y_min land x_i ne x_min land x_i ne x_max$$



          Expansion downwards is not possible if this condition is satisfied for any $i=1,2,3,4$



          Important note



          Steps shown above can be transformed into a fairly simple and efficient software code. However, have in mind that your sensors have limited precision so something that is strictly equal in mathematics (like $L=w$) is just approximately equal in the real world. In the real world, $L$ will be always different from $w,h$ because we calculate the last two values from sensor readings.



          So if you want to check if $L=w$, your code should actually test for:



          $$|L-w|<delta$$



          ...where $delta$ is some small value adjusted for the precision of your sensors.






          share|cite|improve this answer











          $endgroup$













          • $begingroup$
            Thank you so much for your time and effort...This method seems to take care of all possible cases, but if I encounter a case that doesn't work, I'll surely get back to you
            $endgroup$
            – kousheek
            Jan 17 at 14:02










          • $begingroup$
            @kousheek Sure, be my guest!
            $endgroup$
            – Oldboy
            Jan 17 at 15:12












          • $begingroup$
            what does the ^ mean in "xi=xmin ∧ yi≠ymin ∧ yi ≠ymax"?
            $endgroup$
            – kousheek
            Jan 19 at 6:24










          • $begingroup$
            @kousheek It’s “and” in Python or “&&” in almost all other programming languages.
            $endgroup$
            – Oldboy
            Jan 19 at 6:30












          • $begingroup$
            ohh okay thank you so much!
            $endgroup$
            – kousheek
            Jan 19 at 6:40











          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',
          autoActivateHeartbeat: false,
          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%2f3074168%2ffind-out-a-general-expression-for-the-coordinates-of-a-point-in-a-square-based-o%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0












          $begingroup$

          enter image description here



          We are going to introduce a new coordinate system $x'Oy'$ like shown in Fig. 1. If we are able to calculate coordinates of point $P(x'_P, y'_P)$ in that coordinate systen - we are done because:



          $$x_O=-x'_P, y_O=-y'_Ptag{1}$$



          Step 1:



          Find coordinates of points $A,B,C,D$ in $x'Oy'$ coordinate system:



          $$A: x'_1=d_1cosalpha, y'_1=d_1sinalpha$$



          $$D: x'_2=d_4cos(alpha+90^circ), y'_2=d_4sin(alpha+90^circ)$$



          $$B: x'_3=d_3cos(alpha+180^circ), y'_3=d_3sin(alpha+180^circ)$$



          $$C: x'_4=d_2cos(alpha+270^circ), y'_4=d_2sin(alpha+270^circ)$$



          Step 2:



          Calculate the following values:



          $$x'_{min}=min(x_1, x_2, x_3, x_4)$$
          $$x'_{max}=max(x_1, x_2, x_3, x_4)$$
          $$y'_{min}=min(y_1, y_2, y_3, y_4)$$
          $$y'_{max}=max(y_1, y_2, y_3, y_4)$$



          Step 3:



          Find the bottom left corner $P'(x'_{P'}, y'_{P'})$ of the bounding rectangle (shown in blue color) and calculate the width $w$ and height $h$ of the bounding rectangle:



          $$x'_{P'}=x'_min$$
          $$y'_{P'}=y'_min$$
          $$w=x'_max-x'_min$$
          $$h=y'_max-y'_min$$



          Step 4:



          If $h=w=L$ we are done (see Fig. 2) because the blue rectangle is actually our bounding square of length $L$, the points $P$ and $P'$ are identical and $x'_{P}=x'_{P'}$, $y'_{P}=y'_{P'}$. Just apply (1) and you have $x_O, y_O$.



          Step 5:



          Needed only if $w<L$. In this case your blue rectangle is narrower than the square and it is possible that points $P$ and $P'$ are not identical. In other words, you have to expand the blue rectangle either to the left or to the right to match the original square.



          If the blue rectangle has to be expanded to the right, it won't impact the position of point $P'$ and in this case:



          $$x'_P=x'_{P'}$$



          The point $P'$ moves only if you have to expand your rectangle to the left. Such situation is presented in Fig. 3. Obviously:



          $$x'_P=x'_{P'}-(L-w)$$



          Now apply (1) and you have $x_O$.



          How to recognize that expansion to the left is needed? Take a look at Fig. 3 again. You cannot move the right side of the blue rectangle more to the right because that side has points $A$ and $C$ on it and that points are not corners. If you moved that side to the right, endpoints of $d_1$ and $d_2$ would be no longer "attached" to the right side. On the contrary, there is no non-corner point on the left side of the blue rectangle so you can freely move the left side further to the left and still have endpoints $A,B,C,D$ "attached" to sides of the square.



          You can detect if there is a point $(x_i, y_i)$ on the left side of the blue rectangle that is blocking expansion to the left with the following condition:



          $$x_i=x_min land y_i ne y_min land y_i ne y_max$$



          Expansion to the left is not possible if this condition is satisfied for any $i=1,2,3,4$



          Step 6:



          Needed only if $h<L$. In this case your blue rectangle is shorter than the square and it is possible that points $P$ and $P'$ are not identical. In other words, you have to expand the blue rectangle either upwards or downwards to match the original square.



          If the blue rectangle has to be expanded upwards, it won't impact the position of point $P'$:



          $$y'_P=y'_{P'}$$



          The point $P'$ moves only if you have to expand your rectangle downwards. Such situation is presented in Fig. 1. Obviously:



          $$y'_P=y'_{P'}-(L-h)$$



          Now apply (1) and you have $y_O$.



          How to recognize that expansion downwards is needed? Take a look at Fig. 1 again. You cannot move the top side of the blue rectangle upwards because that side has point $D$ on it and that point is not a corner. If you moved that side upwards, endpoint of $d_4$ would be no longer "attached" to the top side. On the contrary, there is no non-corner point on the bottom side of the blue rectangle so you can freely move the bottom side further down and still have endpoints $A,B,C,D$ "attached" to sides of the square.



          You can detect if there is a point $(x_i, y_i)$ on the bottom side that is blocking expansion downwards with the following condition:



          $$y_i=y_min land x_i ne x_min land x_i ne x_max$$



          Expansion downwards is not possible if this condition is satisfied for any $i=1,2,3,4$



          Important note



          Steps shown above can be transformed into a fairly simple and efficient software code. However, have in mind that your sensors have limited precision so something that is strictly equal in mathematics (like $L=w$) is just approximately equal in the real world. In the real world, $L$ will be always different from $w,h$ because we calculate the last two values from sensor readings.



          So if you want to check if $L=w$, your code should actually test for:



          $$|L-w|<delta$$



          ...where $delta$ is some small value adjusted for the precision of your sensors.






          share|cite|improve this answer











          $endgroup$













          • $begingroup$
            Thank you so much for your time and effort...This method seems to take care of all possible cases, but if I encounter a case that doesn't work, I'll surely get back to you
            $endgroup$
            – kousheek
            Jan 17 at 14:02










          • $begingroup$
            @kousheek Sure, be my guest!
            $endgroup$
            – Oldboy
            Jan 17 at 15:12












          • $begingroup$
            what does the ^ mean in "xi=xmin ∧ yi≠ymin ∧ yi ≠ymax"?
            $endgroup$
            – kousheek
            Jan 19 at 6:24










          • $begingroup$
            @kousheek It’s “and” in Python or “&&” in almost all other programming languages.
            $endgroup$
            – Oldboy
            Jan 19 at 6:30












          • $begingroup$
            ohh okay thank you so much!
            $endgroup$
            – kousheek
            Jan 19 at 6:40
















          0












          $begingroup$

          enter image description here



          We are going to introduce a new coordinate system $x'Oy'$ like shown in Fig. 1. If we are able to calculate coordinates of point $P(x'_P, y'_P)$ in that coordinate systen - we are done because:



          $$x_O=-x'_P, y_O=-y'_Ptag{1}$$



          Step 1:



          Find coordinates of points $A,B,C,D$ in $x'Oy'$ coordinate system:



          $$A: x'_1=d_1cosalpha, y'_1=d_1sinalpha$$



          $$D: x'_2=d_4cos(alpha+90^circ), y'_2=d_4sin(alpha+90^circ)$$



          $$B: x'_3=d_3cos(alpha+180^circ), y'_3=d_3sin(alpha+180^circ)$$



          $$C: x'_4=d_2cos(alpha+270^circ), y'_4=d_2sin(alpha+270^circ)$$



          Step 2:



          Calculate the following values:



          $$x'_{min}=min(x_1, x_2, x_3, x_4)$$
          $$x'_{max}=max(x_1, x_2, x_3, x_4)$$
          $$y'_{min}=min(y_1, y_2, y_3, y_4)$$
          $$y'_{max}=max(y_1, y_2, y_3, y_4)$$



          Step 3:



          Find the bottom left corner $P'(x'_{P'}, y'_{P'})$ of the bounding rectangle (shown in blue color) and calculate the width $w$ and height $h$ of the bounding rectangle:



          $$x'_{P'}=x'_min$$
          $$y'_{P'}=y'_min$$
          $$w=x'_max-x'_min$$
          $$h=y'_max-y'_min$$



          Step 4:



          If $h=w=L$ we are done (see Fig. 2) because the blue rectangle is actually our bounding square of length $L$, the points $P$ and $P'$ are identical and $x'_{P}=x'_{P'}$, $y'_{P}=y'_{P'}$. Just apply (1) and you have $x_O, y_O$.



          Step 5:



          Needed only if $w<L$. In this case your blue rectangle is narrower than the square and it is possible that points $P$ and $P'$ are not identical. In other words, you have to expand the blue rectangle either to the left or to the right to match the original square.



          If the blue rectangle has to be expanded to the right, it won't impact the position of point $P'$ and in this case:



          $$x'_P=x'_{P'}$$



          The point $P'$ moves only if you have to expand your rectangle to the left. Such situation is presented in Fig. 3. Obviously:



          $$x'_P=x'_{P'}-(L-w)$$



          Now apply (1) and you have $x_O$.



          How to recognize that expansion to the left is needed? Take a look at Fig. 3 again. You cannot move the right side of the blue rectangle more to the right because that side has points $A$ and $C$ on it and that points are not corners. If you moved that side to the right, endpoints of $d_1$ and $d_2$ would be no longer "attached" to the right side. On the contrary, there is no non-corner point on the left side of the blue rectangle so you can freely move the left side further to the left and still have endpoints $A,B,C,D$ "attached" to sides of the square.



          You can detect if there is a point $(x_i, y_i)$ on the left side of the blue rectangle that is blocking expansion to the left with the following condition:



          $$x_i=x_min land y_i ne y_min land y_i ne y_max$$



          Expansion to the left is not possible if this condition is satisfied for any $i=1,2,3,4$



          Step 6:



          Needed only if $h<L$. In this case your blue rectangle is shorter than the square and it is possible that points $P$ and $P'$ are not identical. In other words, you have to expand the blue rectangle either upwards or downwards to match the original square.



          If the blue rectangle has to be expanded upwards, it won't impact the position of point $P'$:



          $$y'_P=y'_{P'}$$



          The point $P'$ moves only if you have to expand your rectangle downwards. Such situation is presented in Fig. 1. Obviously:



          $$y'_P=y'_{P'}-(L-h)$$



          Now apply (1) and you have $y_O$.



          How to recognize that expansion downwards is needed? Take a look at Fig. 1 again. You cannot move the top side of the blue rectangle upwards because that side has point $D$ on it and that point is not a corner. If you moved that side upwards, endpoint of $d_4$ would be no longer "attached" to the top side. On the contrary, there is no non-corner point on the bottom side of the blue rectangle so you can freely move the bottom side further down and still have endpoints $A,B,C,D$ "attached" to sides of the square.



          You can detect if there is a point $(x_i, y_i)$ on the bottom side that is blocking expansion downwards with the following condition:



          $$y_i=y_min land x_i ne x_min land x_i ne x_max$$



          Expansion downwards is not possible if this condition is satisfied for any $i=1,2,3,4$



          Important note



          Steps shown above can be transformed into a fairly simple and efficient software code. However, have in mind that your sensors have limited precision so something that is strictly equal in mathematics (like $L=w$) is just approximately equal in the real world. In the real world, $L$ will be always different from $w,h$ because we calculate the last two values from sensor readings.



          So if you want to check if $L=w$, your code should actually test for:



          $$|L-w|<delta$$



          ...where $delta$ is some small value adjusted for the precision of your sensors.






          share|cite|improve this answer











          $endgroup$













          • $begingroup$
            Thank you so much for your time and effort...This method seems to take care of all possible cases, but if I encounter a case that doesn't work, I'll surely get back to you
            $endgroup$
            – kousheek
            Jan 17 at 14:02










          • $begingroup$
            @kousheek Sure, be my guest!
            $endgroup$
            – Oldboy
            Jan 17 at 15:12












          • $begingroup$
            what does the ^ mean in "xi=xmin ∧ yi≠ymin ∧ yi ≠ymax"?
            $endgroup$
            – kousheek
            Jan 19 at 6:24










          • $begingroup$
            @kousheek It’s “and” in Python or “&&” in almost all other programming languages.
            $endgroup$
            – Oldboy
            Jan 19 at 6:30












          • $begingroup$
            ohh okay thank you so much!
            $endgroup$
            – kousheek
            Jan 19 at 6:40














          0












          0








          0





          $begingroup$

          enter image description here



          We are going to introduce a new coordinate system $x'Oy'$ like shown in Fig. 1. If we are able to calculate coordinates of point $P(x'_P, y'_P)$ in that coordinate systen - we are done because:



          $$x_O=-x'_P, y_O=-y'_Ptag{1}$$



          Step 1:



          Find coordinates of points $A,B,C,D$ in $x'Oy'$ coordinate system:



          $$A: x'_1=d_1cosalpha, y'_1=d_1sinalpha$$



          $$D: x'_2=d_4cos(alpha+90^circ), y'_2=d_4sin(alpha+90^circ)$$



          $$B: x'_3=d_3cos(alpha+180^circ), y'_3=d_3sin(alpha+180^circ)$$



          $$C: x'_4=d_2cos(alpha+270^circ), y'_4=d_2sin(alpha+270^circ)$$



          Step 2:



          Calculate the following values:



          $$x'_{min}=min(x_1, x_2, x_3, x_4)$$
          $$x'_{max}=max(x_1, x_2, x_3, x_4)$$
          $$y'_{min}=min(y_1, y_2, y_3, y_4)$$
          $$y'_{max}=max(y_1, y_2, y_3, y_4)$$



          Step 3:



          Find the bottom left corner $P'(x'_{P'}, y'_{P'})$ of the bounding rectangle (shown in blue color) and calculate the width $w$ and height $h$ of the bounding rectangle:



          $$x'_{P'}=x'_min$$
          $$y'_{P'}=y'_min$$
          $$w=x'_max-x'_min$$
          $$h=y'_max-y'_min$$



          Step 4:



          If $h=w=L$ we are done (see Fig. 2) because the blue rectangle is actually our bounding square of length $L$, the points $P$ and $P'$ are identical and $x'_{P}=x'_{P'}$, $y'_{P}=y'_{P'}$. Just apply (1) and you have $x_O, y_O$.



          Step 5:



          Needed only if $w<L$. In this case your blue rectangle is narrower than the square and it is possible that points $P$ and $P'$ are not identical. In other words, you have to expand the blue rectangle either to the left or to the right to match the original square.



          If the blue rectangle has to be expanded to the right, it won't impact the position of point $P'$ and in this case:



          $$x'_P=x'_{P'}$$



          The point $P'$ moves only if you have to expand your rectangle to the left. Such situation is presented in Fig. 3. Obviously:



          $$x'_P=x'_{P'}-(L-w)$$



          Now apply (1) and you have $x_O$.



          How to recognize that expansion to the left is needed? Take a look at Fig. 3 again. You cannot move the right side of the blue rectangle more to the right because that side has points $A$ and $C$ on it and that points are not corners. If you moved that side to the right, endpoints of $d_1$ and $d_2$ would be no longer "attached" to the right side. On the contrary, there is no non-corner point on the left side of the blue rectangle so you can freely move the left side further to the left and still have endpoints $A,B,C,D$ "attached" to sides of the square.



          You can detect if there is a point $(x_i, y_i)$ on the left side of the blue rectangle that is blocking expansion to the left with the following condition:



          $$x_i=x_min land y_i ne y_min land y_i ne y_max$$



          Expansion to the left is not possible if this condition is satisfied for any $i=1,2,3,4$



          Step 6:



          Needed only if $h<L$. In this case your blue rectangle is shorter than the square and it is possible that points $P$ and $P'$ are not identical. In other words, you have to expand the blue rectangle either upwards or downwards to match the original square.



          If the blue rectangle has to be expanded upwards, it won't impact the position of point $P'$:



          $$y'_P=y'_{P'}$$



          The point $P'$ moves only if you have to expand your rectangle downwards. Such situation is presented in Fig. 1. Obviously:



          $$y'_P=y'_{P'}-(L-h)$$



          Now apply (1) and you have $y_O$.



          How to recognize that expansion downwards is needed? Take a look at Fig. 1 again. You cannot move the top side of the blue rectangle upwards because that side has point $D$ on it and that point is not a corner. If you moved that side upwards, endpoint of $d_4$ would be no longer "attached" to the top side. On the contrary, there is no non-corner point on the bottom side of the blue rectangle so you can freely move the bottom side further down and still have endpoints $A,B,C,D$ "attached" to sides of the square.



          You can detect if there is a point $(x_i, y_i)$ on the bottom side that is blocking expansion downwards with the following condition:



          $$y_i=y_min land x_i ne x_min land x_i ne x_max$$



          Expansion downwards is not possible if this condition is satisfied for any $i=1,2,3,4$



          Important note



          Steps shown above can be transformed into a fairly simple and efficient software code. However, have in mind that your sensors have limited precision so something that is strictly equal in mathematics (like $L=w$) is just approximately equal in the real world. In the real world, $L$ will be always different from $w,h$ because we calculate the last two values from sensor readings.



          So if you want to check if $L=w$, your code should actually test for:



          $$|L-w|<delta$$



          ...where $delta$ is some small value adjusted for the precision of your sensors.






          share|cite|improve this answer











          $endgroup$



          enter image description here



          We are going to introduce a new coordinate system $x'Oy'$ like shown in Fig. 1. If we are able to calculate coordinates of point $P(x'_P, y'_P)$ in that coordinate systen - we are done because:



          $$x_O=-x'_P, y_O=-y'_Ptag{1}$$



          Step 1:



          Find coordinates of points $A,B,C,D$ in $x'Oy'$ coordinate system:



          $$A: x'_1=d_1cosalpha, y'_1=d_1sinalpha$$



          $$D: x'_2=d_4cos(alpha+90^circ), y'_2=d_4sin(alpha+90^circ)$$



          $$B: x'_3=d_3cos(alpha+180^circ), y'_3=d_3sin(alpha+180^circ)$$



          $$C: x'_4=d_2cos(alpha+270^circ), y'_4=d_2sin(alpha+270^circ)$$



          Step 2:



          Calculate the following values:



          $$x'_{min}=min(x_1, x_2, x_3, x_4)$$
          $$x'_{max}=max(x_1, x_2, x_3, x_4)$$
          $$y'_{min}=min(y_1, y_2, y_3, y_4)$$
          $$y'_{max}=max(y_1, y_2, y_3, y_4)$$



          Step 3:



          Find the bottom left corner $P'(x'_{P'}, y'_{P'})$ of the bounding rectangle (shown in blue color) and calculate the width $w$ and height $h$ of the bounding rectangle:



          $$x'_{P'}=x'_min$$
          $$y'_{P'}=y'_min$$
          $$w=x'_max-x'_min$$
          $$h=y'_max-y'_min$$



          Step 4:



          If $h=w=L$ we are done (see Fig. 2) because the blue rectangle is actually our bounding square of length $L$, the points $P$ and $P'$ are identical and $x'_{P}=x'_{P'}$, $y'_{P}=y'_{P'}$. Just apply (1) and you have $x_O, y_O$.



          Step 5:



          Needed only if $w<L$. In this case your blue rectangle is narrower than the square and it is possible that points $P$ and $P'$ are not identical. In other words, you have to expand the blue rectangle either to the left or to the right to match the original square.



          If the blue rectangle has to be expanded to the right, it won't impact the position of point $P'$ and in this case:



          $$x'_P=x'_{P'}$$



          The point $P'$ moves only if you have to expand your rectangle to the left. Such situation is presented in Fig. 3. Obviously:



          $$x'_P=x'_{P'}-(L-w)$$



          Now apply (1) and you have $x_O$.



          How to recognize that expansion to the left is needed? Take a look at Fig. 3 again. You cannot move the right side of the blue rectangle more to the right because that side has points $A$ and $C$ on it and that points are not corners. If you moved that side to the right, endpoints of $d_1$ and $d_2$ would be no longer "attached" to the right side. On the contrary, there is no non-corner point on the left side of the blue rectangle so you can freely move the left side further to the left and still have endpoints $A,B,C,D$ "attached" to sides of the square.



          You can detect if there is a point $(x_i, y_i)$ on the left side of the blue rectangle that is blocking expansion to the left with the following condition:



          $$x_i=x_min land y_i ne y_min land y_i ne y_max$$



          Expansion to the left is not possible if this condition is satisfied for any $i=1,2,3,4$



          Step 6:



          Needed only if $h<L$. In this case your blue rectangle is shorter than the square and it is possible that points $P$ and $P'$ are not identical. In other words, you have to expand the blue rectangle either upwards or downwards to match the original square.



          If the blue rectangle has to be expanded upwards, it won't impact the position of point $P'$:



          $$y'_P=y'_{P'}$$



          The point $P'$ moves only if you have to expand your rectangle downwards. Such situation is presented in Fig. 1. Obviously:



          $$y'_P=y'_{P'}-(L-h)$$



          Now apply (1) and you have $y_O$.



          How to recognize that expansion downwards is needed? Take a look at Fig. 1 again. You cannot move the top side of the blue rectangle upwards because that side has point $D$ on it and that point is not a corner. If you moved that side upwards, endpoint of $d_4$ would be no longer "attached" to the top side. On the contrary, there is no non-corner point on the bottom side of the blue rectangle so you can freely move the bottom side further down and still have endpoints $A,B,C,D$ "attached" to sides of the square.



          You can detect if there is a point $(x_i, y_i)$ on the bottom side that is blocking expansion downwards with the following condition:



          $$y_i=y_min land x_i ne x_min land x_i ne x_max$$



          Expansion downwards is not possible if this condition is satisfied for any $i=1,2,3,4$



          Important note



          Steps shown above can be transformed into a fairly simple and efficient software code. However, have in mind that your sensors have limited precision so something that is strictly equal in mathematics (like $L=w$) is just approximately equal in the real world. In the real world, $L$ will be always different from $w,h$ because we calculate the last two values from sensor readings.



          So if you want to check if $L=w$, your code should actually test for:



          $$|L-w|<delta$$



          ...where $delta$ is some small value adjusted for the precision of your sensors.







          share|cite|improve this answer














          share|cite|improve this answer



          share|cite|improve this answer








          edited Jan 16 at 14:27

























          answered Jan 16 at 14:18









          OldboyOldboy

          7,9201935




          7,9201935












          • $begingroup$
            Thank you so much for your time and effort...This method seems to take care of all possible cases, but if I encounter a case that doesn't work, I'll surely get back to you
            $endgroup$
            – kousheek
            Jan 17 at 14:02










          • $begingroup$
            @kousheek Sure, be my guest!
            $endgroup$
            – Oldboy
            Jan 17 at 15:12












          • $begingroup$
            what does the ^ mean in "xi=xmin ∧ yi≠ymin ∧ yi ≠ymax"?
            $endgroup$
            – kousheek
            Jan 19 at 6:24










          • $begingroup$
            @kousheek It’s “and” in Python or “&&” in almost all other programming languages.
            $endgroup$
            – Oldboy
            Jan 19 at 6:30












          • $begingroup$
            ohh okay thank you so much!
            $endgroup$
            – kousheek
            Jan 19 at 6:40


















          • $begingroup$
            Thank you so much for your time and effort...This method seems to take care of all possible cases, but if I encounter a case that doesn't work, I'll surely get back to you
            $endgroup$
            – kousheek
            Jan 17 at 14:02










          • $begingroup$
            @kousheek Sure, be my guest!
            $endgroup$
            – Oldboy
            Jan 17 at 15:12












          • $begingroup$
            what does the ^ mean in "xi=xmin ∧ yi≠ymin ∧ yi ≠ymax"?
            $endgroup$
            – kousheek
            Jan 19 at 6:24










          • $begingroup$
            @kousheek It’s “and” in Python or “&&” in almost all other programming languages.
            $endgroup$
            – Oldboy
            Jan 19 at 6:30












          • $begingroup$
            ohh okay thank you so much!
            $endgroup$
            – kousheek
            Jan 19 at 6:40
















          $begingroup$
          Thank you so much for your time and effort...This method seems to take care of all possible cases, but if I encounter a case that doesn't work, I'll surely get back to you
          $endgroup$
          – kousheek
          Jan 17 at 14:02




          $begingroup$
          Thank you so much for your time and effort...This method seems to take care of all possible cases, but if I encounter a case that doesn't work, I'll surely get back to you
          $endgroup$
          – kousheek
          Jan 17 at 14:02












          $begingroup$
          @kousheek Sure, be my guest!
          $endgroup$
          – Oldboy
          Jan 17 at 15:12






          $begingroup$
          @kousheek Sure, be my guest!
          $endgroup$
          – Oldboy
          Jan 17 at 15:12














          $begingroup$
          what does the ^ mean in "xi=xmin ∧ yi≠ymin ∧ yi ≠ymax"?
          $endgroup$
          – kousheek
          Jan 19 at 6:24




          $begingroup$
          what does the ^ mean in "xi=xmin ∧ yi≠ymin ∧ yi ≠ymax"?
          $endgroup$
          – kousheek
          Jan 19 at 6:24












          $begingroup$
          @kousheek It’s “and” in Python or “&&” in almost all other programming languages.
          $endgroup$
          – Oldboy
          Jan 19 at 6:30






          $begingroup$
          @kousheek It’s “and” in Python or “&&” in almost all other programming languages.
          $endgroup$
          – Oldboy
          Jan 19 at 6:30














          $begingroup$
          ohh okay thank you so much!
          $endgroup$
          – kousheek
          Jan 19 at 6:40




          $begingroup$
          ohh okay thank you so much!
          $endgroup$
          – kousheek
          Jan 19 at 6:40


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Mathematics Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3074168%2ffind-out-a-general-expression-for-the-coordinates-of-a-point-in-a-square-based-o%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

          Mario Kart Wii

          What does “Dominus providebit” mean?

          Antonio Litta Visconti Arese