# How to check if 2 transformation are same?

**URL:** <https://forums.sketchup.com/t/how-to-check-if-2-transformation-are-same/168887>\
**Category:** Ruby API\
**Created:** [June 28, 2021, 10:10am UTC](https://forums.sketchup.com/t/how-to-check-if-2-transformation-are-same/168887 "2021-06-28T10:10:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Henry12](https://avatars.discourse-cdn.com/v4/letter/h/b77776/32.png) [@Henry12](https://forums.sketchup.com/u/Henry12)\
**Post date:** [June 28, 2021, 10:10am UTC](https://forums.sketchup.com/t/how-to-check-if-2-transformation-are-same/168887/1 "2021-06-28T10:10:35Z")

</div>

Should I compare 16 elments of transformations to determine if the 2 transformations are actually the same? Or any simpler way to do it?

---

<div class="post-metadata">

**Author:** ![dezmo](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/dezmo/32/51058_2.png) [@dezmo](https://forums.sketchup.com/u/dezmo)\
**Post date:** [June 28, 2021, 10:50am UTC](https://forums.sketchup.com/t/how-to-check-if-2-transformation-are-same/168887/2 "2021-06-28T10:50:54Z")

</div>

Using the [Transformation to\_a-instance\_method](https://ruby.sketchup.com/Geom/Transformation.html#to_a-instance_method) and compare the arrays is can be one way.

The [#identity?](https://ruby.sketchup.com/Geom/Transformation.html#identity%3F-instance_method) method is used to determine if a transformation is the [IDENTITY](https://ruby.sketchup.com/top-level-namespace.html#IDENTITY-constant) transform.

* * *

You can check also [Transformation Inspector | SketchUp Extension Warehouse](https://extensions.sketchup.com/extension/1356b8f5-54c3-4df9-a6ed-28466ff41ede/transformation-inspector) in the file:  
`tt_transformation\vendor\transformation.rb`  
how Thomas did it with his `#same?` method, using SketchUp’s internal precision for Point3d and Vector3d comparison.

---

<div class="post-metadata">

**Author:** ![slbaumgartner](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/slbaumgartner/32/7302_2.png) [@slbaumgartner](https://forums.sketchup.com/u/slbaumgartner)\
**Post date:** [June 28, 2021, 12:24pm UTC](https://forums.sketchup.com/t/how-to-check-if-2-transformation-are-same/168887/3 "2021-06-28T12:24:36Z")

</div>

Whatever approach you take, you need to allow for computer arithmetic limitations.

The contents of a Transformation are usually generated by calculations, particularly if rotations or scaling are involved. Computer calculations are subject to imprecision. So, the only situation you can expect exact equality is if the Transformation was copied from an existing one. But, of course, then you already know the two are the same! In all other situations you must compare to within a tolerance.

There is nothing special to Transformations about this; it is just necessary programming when floating point values are involved.

---

<div class="post-metadata">

**Author:** ![DanRathbun](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/danrathbun/32/500_2.png) [@DanRathbun](https://forums.sketchup.com/u/DanRathbun)\
**Post date:** [June 28, 2021, 4:27pm UTC](https://forums.sketchup.com/t/how-to-check-if-2-transformation-are-same/168887/4 "2021-06-28T16:27:24Z")

</div>

My attempt …

```ruby
    # Build a hash of transformation properties.
    # @param t [Geom::Transformation]
    # @return [Hash] 
    def transform_properties(t)
      vecX = X_AXIS.transform(t)
      vecY = Y_AXIS.transform(t)
      vecZ = Z_AXIS.transform(t)
      Hash[
        :origin, t.origin, # Geom::Point3d
        :xaxis, t.xaxis, # Geom::Vector3d (normalized)
        :yaxis, t.yaxis, # Geom::Vector3d (normalized)
        :zaxis, t.zaxis, # Geom::Vector3d (normalized)
        :xscale, (vecX.length*vecX.normalize.x).to_l, # Length
        :yscale, (vecY.length*vecY.normalize.y).to_l, # Length
        :zscale, (vecZ.length*vecZ.normalize.z).to_l, # Length
      ]
    end

```

```ruby
    # Compare two hashes of transformation properties.
    # Hash#== will use each value's class #== method (in
    # addition to comparing keys and number of members.)
    # @param t1 [Geom::Transformation]
    # @param t2 [Geom::Transformation]
    # @return [Boolean]
    def transforms_equal?(t1,t2)
      transform_properties(t1) == transform_properties(t2)
    end

```

---

<div class="post-metadata">

**Author:** ![ene\_su](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/ene_su/32/103091_2.png) [@ene\_su](https://forums.sketchup.com/u/ene_su)\
**Post date:** [July 30, 2021, 12:02pm UTC](https://forums.sketchup.com/t/how-to-check-if-2-transformation-are-same/168887/5 "2021-07-30T12:02:00Z")

</div>

This is my usual approach. This works from SU 2018 and later, where `Geom::Transformation#identity?` was fixed.

```ruby
(transformation1 * transformation2.inverse).identity?

```
