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 7837773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T14:50:40+00:00 2026-06-02T14:50:40+00:00

I am having trouble figuring out the correct order of transformations to apply on

  • 0

I am having trouble figuring out the correct order of transformations to apply on elements in my app. The app has elements which have parent/child relations and each element has a transformation (each element is drawn in local space and then transformed).

What i want to archive is that if i transform the parent all its child should also get transformed (so if you rotate the parent, the child should rotate around the parent).

The code below does just that but the problem is when i rotate the parent and then want to move the child. It moves in the wrong direction (due to its parent transformation ). I’ve tried changing the sequence of the transformations but no luck (i know i should first translate and then transform but then the child is rotated around its own axis – instead of parents).

The code:

 Element e;
 AffineTransform t = new AffineTransform();
 AffineTransform t3 = new AffineTransform();

 for (i = 0; i < el.size(); i++)
 {
    e = el.get(i);
    t3.setToIdentity();
    t.setToIdentity();
    tmp = e.getParent();
    while(tmp != null)
    {
         t.translate(tmp.getX(), tmp.getY());
         t3.rotate(Math.toRadians(tmp.getAngle()),tmp.getAnchorX(), tmp.getAnchorY());
         tmp = tmp.getParent();
    }

    t.concatenate(t3);
    t.translate(e.getX(), e.getY());

    t.rotate(Math.toRadians(e.getAngle()),e.getAnchorX(), e.getAnchorY());

    e.draw(g2d,t);
}

The problem:
– given two elements (one child of other)
– parent is rotate (by 40 degrees)
– child is then move by 10px
How can i concatenate transformations so that when i move the child it wont move in the rotated direction?

EDIT:
The code that Torious posted (not working yet):

public AffineTransform getTransformTo(Element ancestor) {
    AffineTransform t = (AffineTransform)getAffineTransform().clone();
    Element parent = getParent();
    while (parent != null && parent != ancestor) {
        t.preConcatenate(parent.getAffineTransform());
        parent = parent.getParent();
    }
    return t;
}


public void translateInAncestorSpace(Element ancestor, Point translation) {
    AffineTransform fromAncestor = getTransformTo(ancestor); // to ancestor space
    try
    {
        fromAncestor.invert();
    } catch(Exception e)
    {
       e.printStackTrace();   
    }
    translation = (Point)fromAncestor.transform(translation, new Point());

    Transform t1 = new Transform();
    t1.translate(translation.x,translation.y);
    transform(t1);
}

Output of the code:

 Moving by [x=1,y=1] old position [x=22,y=40]
 Moved by [x=-21,y=-39] new position [x=1,y=1]
 Moving by [x=2,y=2] old position [x=1,y=1]
 Moved by [x=1,y=1] new position [x=2,y=2]
 Moving by [x=4,y=3] old position [x=2,y=2]
 Moved by [x=2,y=1] new position [x=4,y=3]
  • 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-02T14:50:40+00:00Added an answer on June 2, 2026 at 2:50 pm

    Something like this:

    // get composite transform for 'e'
    t.setToIdentity();
    t.translate(e.getX(), e.getY());
    t.rotate(...);
    
    tmp = e.getParent();
    while (tmp != null) {
    
        // get composite transform for parent
        t3.setToIdentity();
        t3.translate(tmp.getX(), tmp.getY());
        t3.rotate(...);
        tmp = tmp.getParent();
    
        t.preConcatenate(t3); // t3 is applied after t
    }
    
    e.draw(g2d, t);
    

    Edit: For moving an element in ‘global space’ while it’s actually a child of another element, I’d suggest transforming the desired move to element space so it can simply be added to its translation:

    (untested code)

    // Element.getTransformTo: get transform to ancestor or root (null)
    public AffineTransform getTransformTo(Element ancestor) {
        AffineTransform t = getCompositeTransform();
        Element parent = getParent();
        while (parent != null && parent != ancestor) {
            t.preConcatenate(parent.getCompositeTransform());
            parent = parent.getParent();
        }
    }
    
    // Element.translateInAncestorSpace
    // ancestor may be null for root/global space
    public void translateInAncestorSpace(Element ancestor, Point translation) {
        AffineTransform fromAncestor = getTransformTo(ancestor); // to ancestor space
        fromAncestor.invert(); // from ancestor space
    
        // [NEW] re-apply element rotation since translation occurs after rotation
        fromAncestor.rotate(Math.toRadians(angle), anchor.x, anchor.y);
    
        translation = fromAncestor.deltaTransform(translation, new Point());
    
        // translation is now in element space; add to existing translation
        element.setX(element.getX() + translation.x);
        element.setY(element.getY() + translation.y);
    }
    
    // example usage:
    // (this should move the element 10px to the right, regardless
    // of parent transforms)
    element.translateInAncestorSpace(null, new Point(10, 0));
    

    Also consider implementing an AffineTransform Element.getCompositeTransform() method, which will make your life easier in the end.

    Also consider rendering your ‘element tree’ from the root node to the leaf nodes, so you’d get something like this:

    public void render(Element node, AffineTransform transform) {
    
        AffineTransform nodeTransform = node.getCompositeTransform();
        nodeTransform.preConcatenate(transform);
    
        node.draw(g2d, nodeTransform);
    
        for (Element child : node.children())
            render(child, nodeTransform);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having trouble figuring out what the elements are in parsed xml input,
I having trouble figuring out the correct architecture for this kind of application: it's
I'm having trouble figuring out the correct syntax to integrate an emdash into a
I'm working on my homework and am having trouble figuring out the correct syntax
I'm having trouble figuring out the best way to have a delphi function operate
I'm having some trouble figuring this out. I have a script that checks a
I having trouble figuring out NSBundle & DocumentDirectory data, I have a Camera Picture
I'm having some trouble figuring out the correct syntax for mapping a many-to-many relationship
im having trouble figuring out how to bind mouseout() to my entire nav bar
I've having trouble figuring out how to select based on a list in a

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.