How to normilize rotation to zero











up vote
-2
down vote

favorite












I have a rotated rectangle on a 2d surface



enter image description here



Known values are:




  • x,y (upper left corner)

  • width/height of rectangle

  • rotation


How can I rotate the rectangle back to zero with the point of origin being the center of the rectangle...



... and then get the new x,y values of the upper left rectangle corner?



enter image description here



example php function:



    function getPositionWithoutRotation(
float $rotation,
float $x,
float $y,
float $width,
float $height
) {
$angleRadian = ($rotation * pi()) / 180;

$xRelativeToCenter = ($width / 2) * cos($angleRadian) - ($height / 2) * sin($angleRadian);
$yRelativeToCenter = ($width / 2) * sin($angleRadian) + ($height / 2) * cos($angleRadian);

$cx = $x - $xRelativeToCenter;
$cy = $y - $yRelativeToCenter;

$x0 = $cx + ($width / 2);
$y0 = $cy + ($height / 2);

return [
'x0' => $x0,
'y0' => $y0,
];
}


result:



enter image description here



data:



x = 453
y = 244
w = 139
h = 139
rotation = 16

angleRadian = 0.27925268031909

xRelativeToCenter = 47.650891638432
yRelativeToCenter = 85.964484096995

cx = 405.34910836157
cy = 158.03551590301

x0 = 474.84910836157
y0 = 227.53551590301









share|improve this question




























    up vote
    -2
    down vote

    favorite












    I have a rotated rectangle on a 2d surface



    enter image description here



    Known values are:




    • x,y (upper left corner)

    • width/height of rectangle

    • rotation


    How can I rotate the rectangle back to zero with the point of origin being the center of the rectangle...



    ... and then get the new x,y values of the upper left rectangle corner?



    enter image description here



    example php function:



        function getPositionWithoutRotation(
    float $rotation,
    float $x,
    float $y,
    float $width,
    float $height
    ) {
    $angleRadian = ($rotation * pi()) / 180;

    $xRelativeToCenter = ($width / 2) * cos($angleRadian) - ($height / 2) * sin($angleRadian);
    $yRelativeToCenter = ($width / 2) * sin($angleRadian) + ($height / 2) * cos($angleRadian);

    $cx = $x - $xRelativeToCenter;
    $cy = $y - $yRelativeToCenter;

    $x0 = $cx + ($width / 2);
    $y0 = $cy + ($height / 2);

    return [
    'x0' => $x0,
    'y0' => $y0,
    ];
    }


    result:



    enter image description here



    data:



    x = 453
    y = 244
    w = 139
    h = 139
    rotation = 16

    angleRadian = 0.27925268031909

    xRelativeToCenter = 47.650891638432
    yRelativeToCenter = 85.964484096995

    cx = 405.34910836157
    cy = 158.03551590301

    x0 = 474.84910836157
    y0 = 227.53551590301









    share|improve this question


























      up vote
      -2
      down vote

      favorite









      up vote
      -2
      down vote

      favorite











      I have a rotated rectangle on a 2d surface



      enter image description here



      Known values are:




      • x,y (upper left corner)

      • width/height of rectangle

      • rotation


      How can I rotate the rectangle back to zero with the point of origin being the center of the rectangle...



      ... and then get the new x,y values of the upper left rectangle corner?



      enter image description here



      example php function:



          function getPositionWithoutRotation(
      float $rotation,
      float $x,
      float $y,
      float $width,
      float $height
      ) {
      $angleRadian = ($rotation * pi()) / 180;

      $xRelativeToCenter = ($width / 2) * cos($angleRadian) - ($height / 2) * sin($angleRadian);
      $yRelativeToCenter = ($width / 2) * sin($angleRadian) + ($height / 2) * cos($angleRadian);

      $cx = $x - $xRelativeToCenter;
      $cy = $y - $yRelativeToCenter;

      $x0 = $cx + ($width / 2);
      $y0 = $cy + ($height / 2);

      return [
      'x0' => $x0,
      'y0' => $y0,
      ];
      }


      result:



      enter image description here



      data:



      x = 453
      y = 244
      w = 139
      h = 139
      rotation = 16

      angleRadian = 0.27925268031909

      xRelativeToCenter = 47.650891638432
      yRelativeToCenter = 85.964484096995

      cx = 405.34910836157
      cy = 158.03551590301

      x0 = 474.84910836157
      y0 = 227.53551590301









      share|improve this question















      I have a rotated rectangle on a 2d surface



      enter image description here



      Known values are:




      • x,y (upper left corner)

      • width/height of rectangle

      • rotation


      How can I rotate the rectangle back to zero with the point of origin being the center of the rectangle...



      ... and then get the new x,y values of the upper left rectangle corner?



      enter image description here



      example php function:



          function getPositionWithoutRotation(
      float $rotation,
      float $x,
      float $y,
      float $width,
      float $height
      ) {
      $angleRadian = ($rotation * pi()) / 180;

      $xRelativeToCenter = ($width / 2) * cos($angleRadian) - ($height / 2) * sin($angleRadian);
      $yRelativeToCenter = ($width / 2) * sin($angleRadian) + ($height / 2) * cos($angleRadian);

      $cx = $x - $xRelativeToCenter;
      $cy = $y - $yRelativeToCenter;

      $x0 = $cx + ($width / 2);
      $y0 = $cy + ($height / 2);

      return [
      'x0' => $x0,
      'y0' => $y0,
      ];
      }


      result:



      enter image description here



      data:



      x = 453
      y = 244
      w = 139
      h = 139
      rotation = 16

      angleRadian = 0.27925268031909

      xRelativeToCenter = 47.650891638432
      yRelativeToCenter = 85.964484096995

      cx = 405.34910836157
      cy = 158.03551590301

      x0 = 474.84910836157
      y0 = 227.53551590301






      php math geometry






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 at 10:59

























      asked Nov 12 at 7:53









      Adnan Mujkanovic

      781112




      781112
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          Edit: rechecked signs for left top corner



          Essentially you need to determine rectangle (and rotation) center.



          Let half-width of rectangle is w, half-height is h. So corner coordinates relative to center after rotation by angle fi are



          x' =  -w * cos(fi) - h * sin(fi)
          y' = -w * sin(fi) + h * cos(fi)


          and center is



           cx = x - x'
          cy = y - y'


          And corner coordinates without rotation:



          x0 = cx - w
          y0 = cy + h


          Python code:



          import math
          def getPositionWithoutRotation(rotation, x, y, width, height):
          angleRadian = (rotation * math.pi) / 180

          xRelativeToCenter = - (width / 2) * math.cos(angleRadian) - (height / 2) * math.sin(angleRadian)
          yRelativeToCenter = -(width / 2) * math.sin(angleRadian) + (height / 2) * math.cos(angleRadian)

          cx = x - xRelativeToCenter
          cy = y - yRelativeToCenter

          x0 = cx - (width / 2)
          y0 = cy + (height / 2)

          return x0, y0

          print(getPositionWithoutRotation(-30, -0.37, 1.37, 2, 2))
          print(getPositionWithoutRotation(16, 453, 244, 139, 139))

          (-1.0039745962155613, 1.0039745962155615) //OK
          (469.4644840969946, 265.84910836156826) // ????





          share|improve this answer























          • actually the center part with the double equal signs is a bit confusing. can the center equation be simplified to single equal signs?
            – Adnan Mujkanovic
            Nov 12 at 10:01












          • I just subsituted x' from the first equation, so cx = x - x and cx = x - (w * cos(fi) - h * sin(fi)) are the same
            – MBo
            Nov 12 at 10:26












          • i am not sure what im doing wrong but im not getting the result i wanted. link
            – Adnan Mujkanovic
            Nov 12 at 10:44












          • Could you show example input data and code used. I might make mistake in signs (depends on coordinate system)
            – MBo
            Nov 12 at 10:49










          • I added a code example and result example in the question
            – Adnan Mujkanovic
            Nov 12 at 10:53













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          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
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53257863%2fhow-to-normilize-rotation-to-zero%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








          up vote
          0
          down vote













          Edit: rechecked signs for left top corner



          Essentially you need to determine rectangle (and rotation) center.



          Let half-width of rectangle is w, half-height is h. So corner coordinates relative to center after rotation by angle fi are



          x' =  -w * cos(fi) - h * sin(fi)
          y' = -w * sin(fi) + h * cos(fi)


          and center is



           cx = x - x'
          cy = y - y'


          And corner coordinates without rotation:



          x0 = cx - w
          y0 = cy + h


          Python code:



          import math
          def getPositionWithoutRotation(rotation, x, y, width, height):
          angleRadian = (rotation * math.pi) / 180

          xRelativeToCenter = - (width / 2) * math.cos(angleRadian) - (height / 2) * math.sin(angleRadian)
          yRelativeToCenter = -(width / 2) * math.sin(angleRadian) + (height / 2) * math.cos(angleRadian)

          cx = x - xRelativeToCenter
          cy = y - yRelativeToCenter

          x0 = cx - (width / 2)
          y0 = cy + (height / 2)

          return x0, y0

          print(getPositionWithoutRotation(-30, -0.37, 1.37, 2, 2))
          print(getPositionWithoutRotation(16, 453, 244, 139, 139))

          (-1.0039745962155613, 1.0039745962155615) //OK
          (469.4644840969946, 265.84910836156826) // ????





          share|improve this answer























          • actually the center part with the double equal signs is a bit confusing. can the center equation be simplified to single equal signs?
            – Adnan Mujkanovic
            Nov 12 at 10:01












          • I just subsituted x' from the first equation, so cx = x - x and cx = x - (w * cos(fi) - h * sin(fi)) are the same
            – MBo
            Nov 12 at 10:26












          • i am not sure what im doing wrong but im not getting the result i wanted. link
            – Adnan Mujkanovic
            Nov 12 at 10:44












          • Could you show example input data and code used. I might make mistake in signs (depends on coordinate system)
            – MBo
            Nov 12 at 10:49










          • I added a code example and result example in the question
            – Adnan Mujkanovic
            Nov 12 at 10:53

















          up vote
          0
          down vote













          Edit: rechecked signs for left top corner



          Essentially you need to determine rectangle (and rotation) center.



          Let half-width of rectangle is w, half-height is h. So corner coordinates relative to center after rotation by angle fi are



          x' =  -w * cos(fi) - h * sin(fi)
          y' = -w * sin(fi) + h * cos(fi)


          and center is



           cx = x - x'
          cy = y - y'


          And corner coordinates without rotation:



          x0 = cx - w
          y0 = cy + h


          Python code:



          import math
          def getPositionWithoutRotation(rotation, x, y, width, height):
          angleRadian = (rotation * math.pi) / 180

          xRelativeToCenter = - (width / 2) * math.cos(angleRadian) - (height / 2) * math.sin(angleRadian)
          yRelativeToCenter = -(width / 2) * math.sin(angleRadian) + (height / 2) * math.cos(angleRadian)

          cx = x - xRelativeToCenter
          cy = y - yRelativeToCenter

          x0 = cx - (width / 2)
          y0 = cy + (height / 2)

          return x0, y0

          print(getPositionWithoutRotation(-30, -0.37, 1.37, 2, 2))
          print(getPositionWithoutRotation(16, 453, 244, 139, 139))

          (-1.0039745962155613, 1.0039745962155615) //OK
          (469.4644840969946, 265.84910836156826) // ????





          share|improve this answer























          • actually the center part with the double equal signs is a bit confusing. can the center equation be simplified to single equal signs?
            – Adnan Mujkanovic
            Nov 12 at 10:01












          • I just subsituted x' from the first equation, so cx = x - x and cx = x - (w * cos(fi) - h * sin(fi)) are the same
            – MBo
            Nov 12 at 10:26












          • i am not sure what im doing wrong but im not getting the result i wanted. link
            – Adnan Mujkanovic
            Nov 12 at 10:44












          • Could you show example input data and code used. I might make mistake in signs (depends on coordinate system)
            – MBo
            Nov 12 at 10:49










          • I added a code example and result example in the question
            – Adnan Mujkanovic
            Nov 12 at 10:53















          up vote
          0
          down vote










          up vote
          0
          down vote









          Edit: rechecked signs for left top corner



          Essentially you need to determine rectangle (and rotation) center.



          Let half-width of rectangle is w, half-height is h. So corner coordinates relative to center after rotation by angle fi are



          x' =  -w * cos(fi) - h * sin(fi)
          y' = -w * sin(fi) + h * cos(fi)


          and center is



           cx = x - x'
          cy = y - y'


          And corner coordinates without rotation:



          x0 = cx - w
          y0 = cy + h


          Python code:



          import math
          def getPositionWithoutRotation(rotation, x, y, width, height):
          angleRadian = (rotation * math.pi) / 180

          xRelativeToCenter = - (width / 2) * math.cos(angleRadian) - (height / 2) * math.sin(angleRadian)
          yRelativeToCenter = -(width / 2) * math.sin(angleRadian) + (height / 2) * math.cos(angleRadian)

          cx = x - xRelativeToCenter
          cy = y - yRelativeToCenter

          x0 = cx - (width / 2)
          y0 = cy + (height / 2)

          return x0, y0

          print(getPositionWithoutRotation(-30, -0.37, 1.37, 2, 2))
          print(getPositionWithoutRotation(16, 453, 244, 139, 139))

          (-1.0039745962155613, 1.0039745962155615) //OK
          (469.4644840969946, 265.84910836156826) // ????





          share|improve this answer














          Edit: rechecked signs for left top corner



          Essentially you need to determine rectangle (and rotation) center.



          Let half-width of rectangle is w, half-height is h. So corner coordinates relative to center after rotation by angle fi are



          x' =  -w * cos(fi) - h * sin(fi)
          y' = -w * sin(fi) + h * cos(fi)


          and center is



           cx = x - x'
          cy = y - y'


          And corner coordinates without rotation:



          x0 = cx - w
          y0 = cy + h


          Python code:



          import math
          def getPositionWithoutRotation(rotation, x, y, width, height):
          angleRadian = (rotation * math.pi) / 180

          xRelativeToCenter = - (width / 2) * math.cos(angleRadian) - (height / 2) * math.sin(angleRadian)
          yRelativeToCenter = -(width / 2) * math.sin(angleRadian) + (height / 2) * math.cos(angleRadian)

          cx = x - xRelativeToCenter
          cy = y - yRelativeToCenter

          x0 = cx - (width / 2)
          y0 = cy + (height / 2)

          return x0, y0

          print(getPositionWithoutRotation(-30, -0.37, 1.37, 2, 2))
          print(getPositionWithoutRotation(16, 453, 244, 139, 139))

          (-1.0039745962155613, 1.0039745962155615) //OK
          (469.4644840969946, 265.84910836156826) // ????






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 12 at 11:42

























          answered Nov 12 at 8:05









          MBo

          46.5k22848




          46.5k22848












          • actually the center part with the double equal signs is a bit confusing. can the center equation be simplified to single equal signs?
            – Adnan Mujkanovic
            Nov 12 at 10:01












          • I just subsituted x' from the first equation, so cx = x - x and cx = x - (w * cos(fi) - h * sin(fi)) are the same
            – MBo
            Nov 12 at 10:26












          • i am not sure what im doing wrong but im not getting the result i wanted. link
            – Adnan Mujkanovic
            Nov 12 at 10:44












          • Could you show example input data and code used. I might make mistake in signs (depends on coordinate system)
            – MBo
            Nov 12 at 10:49










          • I added a code example and result example in the question
            – Adnan Mujkanovic
            Nov 12 at 10:53




















          • actually the center part with the double equal signs is a bit confusing. can the center equation be simplified to single equal signs?
            – Adnan Mujkanovic
            Nov 12 at 10:01












          • I just subsituted x' from the first equation, so cx = x - x and cx = x - (w * cos(fi) - h * sin(fi)) are the same
            – MBo
            Nov 12 at 10:26












          • i am not sure what im doing wrong but im not getting the result i wanted. link
            – Adnan Mujkanovic
            Nov 12 at 10:44












          • Could you show example input data and code used. I might make mistake in signs (depends on coordinate system)
            – MBo
            Nov 12 at 10:49










          • I added a code example and result example in the question
            – Adnan Mujkanovic
            Nov 12 at 10:53


















          actually the center part with the double equal signs is a bit confusing. can the center equation be simplified to single equal signs?
          – Adnan Mujkanovic
          Nov 12 at 10:01






          actually the center part with the double equal signs is a bit confusing. can the center equation be simplified to single equal signs?
          – Adnan Mujkanovic
          Nov 12 at 10:01














          I just subsituted x' from the first equation, so cx = x - x and cx = x - (w * cos(fi) - h * sin(fi)) are the same
          – MBo
          Nov 12 at 10:26






          I just subsituted x' from the first equation, so cx = x - x and cx = x - (w * cos(fi) - h * sin(fi)) are the same
          – MBo
          Nov 12 at 10:26














          i am not sure what im doing wrong but im not getting the result i wanted. link
          – Adnan Mujkanovic
          Nov 12 at 10:44






          i am not sure what im doing wrong but im not getting the result i wanted. link
          – Adnan Mujkanovic
          Nov 12 at 10:44














          Could you show example input data and code used. I might make mistake in signs (depends on coordinate system)
          – MBo
          Nov 12 at 10:49




          Could you show example input data and code used. I might make mistake in signs (depends on coordinate system)
          – MBo
          Nov 12 at 10:49












          I added a code example and result example in the question
          – Adnan Mujkanovic
          Nov 12 at 10:53






          I added a code example and result example in the question
          – Adnan Mujkanovic
          Nov 12 at 10:53




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • 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.


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





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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.


          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%2fstackoverflow.com%2fquestions%2f53257863%2fhow-to-normilize-rotation-to-zero%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

          Bressuire

          Vorschmack

          Quarantine