Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8583157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:28:01+00:00 2026-06-11T21:28:01+00:00

I have a javascript Matrix class for affine transforms, and a function to set

  • 0

I have a javascript Matrix class for affine transforms, and a function to set the rotation to an absolute number of radians, around a given center:

this.setRotation = function (radians, center) {
    var cos = Math.cos(radians);
    var sin = Math.sin(radians);
    this.a = cos;
    this.b = sin;
    this.c = -sin;
    this.d = cos;
    this.tx += center.x - center.x * cos + center.y * sin;
    this.ty += center.y - center.x * sin - center.y * cos;
}

I’m trying to rotate around the center of the object itself, so I’m passing in a center point of half the object’s width, and half its height, so for a 100 x 100 object I’m passing in 50, 50.

If my object starts from a rotation of zero, this works fine:

Before After

… but if I rotate the shape again, or start with a rotation of other than zero, the tx and ty values end up wrong:

Position moved

What’s wrong with my formula above? Setting the rotation seems to be accurate, but not the tx and ty.

I have seen a few other questions on this subject, in particular this one, but nothing that has helped.

Update To add some numbers to this:

If I begin with a 100×100 rectangle, positioned at 100,100, then my initial matrix is: {Matrix: [a:1, b:0, c:0, d:1, tx:100, ty:100]}

To rotate this clockwise 45 degrees, I feed the above function 0.7853981633974483 (45 degrees in radians), and the center: {Point: [x:50, y: 50]}

This produces the following matrix:
{Matrix: [a:0.7071067812, b:0.7071067812, c:-0.7071067812, d:0.7071067812, tx:150, ty:79.28932188]} which is exactly right.

But if I then start with that matrix, and try to return it to zero, by feeding the function arguments of 0 and {Point: [x:70.71067812000001, y: 70.71067812000001]} (the center of the new, rotated shape), then the output is {Matrix: [a:1, b:0, c:0, d:1, tx:150, ty:79.28932188]}, which is the correct rotation but not the correct translation. I’m expecting to get {Matrix: [a:1, b:0, c:0, d:1, tx:100, ty:100]}

I’ve tried several other variants of the function, including using the center of the rectangle in the parent coordinate space 150,150 instead of the local center of 50,50, and replacing += with = as suggested below, but nothing seems to help. If I comment out the tx calculation then my shape rotates beautifully around it’s origin, so the issue must be either with the tx/ty calculation, or with the center point that I’m passing in.

Does anyone have any further ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T21:28:03+00:00Added an answer on June 11, 2026 at 9:28 pm

    My problem was to do with getting the object back to the origin – removing the top left coordinates as well as half the height and width before rotating, then adding them back again.

    I found it easier to use a rotate function than a setRotate function, and ended up with the following code:

    // Matrix class functions:
    this.rotate = function (radians) {
        var cos = parseFloat(Math.cos(radians).toFixed(10));
        var sin = parseFloat(Math.sin(radians).toFixed(10));
        a = this.a,
        b = this.b,
        c = this.c,
        d = this.d,
        tx = this.tx,
        ty = this.ty;
        this.a = a * cos - b * sin;
        this.b = a * sin + b * cos;
        this.c = c * cos - d * sin;
        this.d = c * sin + d * cos;
        this.tx = tx * cos - ty * sin;
        this.ty = tx * sin + ty * cos;
    }
    
    this.setRotation = function (radians) {
        var rotation = this.rotation();
        this.rotate(radians - rotation);
    }
    
    this.translate = function (tx, ty) {
        this.tx += tx;
        this.ty += ty;
    }
    
    // Called from outside the class
    var transformedBounds = pe.Model.ReadTransformedBounds(selection[0]);
    var itemTransform = pe.Model.ReadItemTransform(selection[0]);
    
    // Cache the coordinates before translating them away:
    var ox = transformedBounds.Left + (transformedBounds.Width / 2); 
    var oy = transformedBounds.Top + (transformedBounds.Height / 2);
    itemTransform.translate(-ox, -oy);
    
    // Rotate:
    itemTransform.setRotation(radians);
    
    // Restore the translation:
    itemTransform.translate(ox, oy);
    

    All of which is probably pretty obvious if you can actually do sums, but I post it here in case anyone has a day as dim as mine…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a 4x3 matrix where the class is set to blank (white background).
I have javascript like and i want to set time delay to this function
I have Javascript function that is adding css class current to selected link <a>
I have javascript snippet: <div id=store_stock_display><p>Stock: <span class=store_product_stock></span></p></div> <script type=text/javascript> $(function() { $('.store_product_stock').change(function() {
I have Javascript function defined in the main index.html file. One part of this
I have JavaScript form validation functions like so: function validate_name(field) { if (field ==
I have JavaScript of ajax uploader as below; $(document).ready(function(){ var qwerty = $('#qwerty').val(); $('#demo1').ajaxupload({
'i have JavaScript associative array name 'options' <script> options[dynamickey] = Number; </script> and i
I have JavaScript method which acts when a particular class (mcb) of forms are
I have javascript function that automatically adds input fields together, but adding numbers like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.