# Rotation Transformation via Two Vectors

**URL:** <https://forums.sketchup.com/t/rotation-transformation-via-two-vectors/125678>\
**Category:** Ruby API\
**Created:** [May 17, 2020, 10:10am UTC](https://forums.sketchup.com/t/rotation-transformation-via-two-vectors/125678 "2020-05-17T10:10:40Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![medeek](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/medeek/32/10675_2.png) [@medeek](https://forums.sketchup.com/u/medeek)\
**Post date:** [May 17, 2020, 10:10am UTC](https://forums.sketchup.com/t/rotation-transformation-via-two-vectors/125678/1 "2020-05-17T10:10:40Z")

</div>

This a bit of a geometric problem that I can probably solve in a clunky manner but I figured I would put it out on the forum to see if anyone else has encountered the same problem has a more simple and elegant solution than the one I have come up with.

I have an initial vector, lets just say [1,0,0] for simplicity. I now have another vector [5,3,2]. Both vectors are at the origin. I want my initial vector to be aligned with the second vector, what would be the single rotational transformation to get me there.

---

<div class="post-metadata">

**Author:** ![medeek](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/medeek/32/10675_2.png) [@medeek](https://forums.sketchup.com/u/medeek)\
**Post date:** [May 17, 2020, 10:15am UTC](https://forums.sketchup.com/t/rotation-transformation-via-two-vectors/125678/2 "2020-05-17T10:15:50Z")

</div>

Since a rotation transformation is given by a point, vector and angle. We would need to solve for the vector and the angle since we can assume the point is at the origin.

We could use the angle\_between method to compute the angle between vector 1 and vector 2

We could then use the cross method to computer the perpendicular vector for the rotation.

This seems like the easiest way to come up with the solution but I might be missing something here.

---

<div class="post-metadata">

**Author:** ![jiminybillybob](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/jiminybillybob/32/197139_2.png) [@jiminybillybob](https://forums.sketchup.com/u/jiminybillybob)\
**Post date:** [May 17, 2020, 11:03am UTC](https://forums.sketchup.com/t/rotation-transformation-via-two-vectors/125678/3 "2020-05-17T11:03:41Z")

</div>

That sounds about right. Have you tried it? 🙂

---

<div class="post-metadata">

**Author:** ![medeek](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/medeek/32/10675_2.png) [@medeek](https://forums.sketchup.com/u/medeek)\
**Post date:** [May 17, 2020, 12:34pm UTC](https://forums.sketchup.com/t/rotation-transformation-via-two-vectors/125678/4 "2020-05-17T12:34:21Z")

</div>

I have but its not giving me exactly the results expected.

---

<div class="post-metadata">

**Author:** ![jiminybillybob](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/jiminybillybob/32/197139_2.png) [@jiminybillybob](https://forums.sketchup.com/u/jiminybillybob)\
**Post date:** [May 17, 2020, 3:27pm UTC](https://forums.sketchup.com/t/rotation-transformation-via-two-vectors/125678/5 "2020-05-17T15:27:53Z")

</div>

Are the vectors normalized?

---

<div class="post-metadata">

**Author:** ![medeek](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/medeek/32/10675_2.png) [@medeek](https://forums.sketchup.com/u/medeek)\
**Post date:** [May 17, 2020, 3:34pm UTC](https://forums.sketchup.com/t/rotation-transformation-via-two-vectors/125678/6 "2020-05-17T15:34:07Z")

</div>

It didn’t do what I needed it to do. I will post up my alternate solution shortly.

---

<div class="post-metadata">

**Author:** ![eneroth3](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/eneroth3/32/1222_2.png) [@eneroth3](https://forums.sketchup.com/u/eneroth3)\
**Post date:** [May 18, 2020, 7:34am UTC](https://forums.sketchup.com/t/rotation-transformation-via-two-vectors/125678/7 "2020-05-18T07:34:44Z")

</div>

angle\_between can only measure angles between 0 and 180 degrees; it can’t detect what direction the rotation is going.

With something like this you can get an angle in a plane (defined by the cross product as normal).

```ruby
# Counter-clockwise angle from vector2 to vector1, as seen from normal.
def planar_angle(vector1, vector2, normal = Z_AXIS)
  Math.atan2((vector2 * vector1) % normal, vector1 % vector2)
end

```

---

<div class="post-metadata">

**Author:** ![Sasquatch](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/sasquatch/32/87863_2.png) [@Sasquatch](https://forums.sketchup.com/u/Sasquatch)\
**Post date:** [May 18, 2020, 11:46am UTC](https://forums.sketchup.com/t/rotation-transformation-via-two-vectors/125678/8 "2020-05-18T11:46:28Z")

</div>

Just remember that if you use the _cross product_ of the two given vectors to find the normal to the common plane, you must then _ **normalize** _ the result (which resizes it to a _unit_ vector) before using it as _ **normal** _ in the method given by @eneroth3

Try this instead to always get the true planar angle:

```ruby
def planar_angle(vector1, vector2)
  normal = (vector1 * vector2).normalize
  Math.atan2((vector2 * vector1) % normal, vector1 % vector2)
end

```

However, calculating (and _normalizing_) the _cross product_ externally (and prior) to _ **planar\_angle** _ yourself would be more efficient since you will also be using that cross product in additional methods after calling @eneroth3’s version.

---

<div class="post-metadata">

**Author:** ![Fredo6](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/fredo6/32/77024_2.png) [@Fredo6](https://forums.sketchup.com/u/Fredo6)\
**Post date:** [May 18, 2020, 12:30pm UTC](https://forums.sketchup.com/t/rotation-transformation-via-two-vectors/125678/9 "2020-05-18T12:30:25Z")

</div>

It seems to me that, if you have 2 vectors `v1` and `v2`, the following transformation will bring `v1` aligned with `v2`.

`t = Geom::Transformation.rotation ORIGIN, v1*v2, v1.angle_between(v2)`

This means that

- `(t * v1).samedirection?(v2)` will be `true`
- `(t.inverse * v2).samedirection?(v1)` will be `true` too

---

<div class="post-metadata">

**Author:** ![medeek](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/medeek/32/10675_2.png) [@medeek](https://forums.sketchup.com/u/medeek)\
**Post date:** [May 18, 2020, 12:50pm UTC](https://forums.sketchup.com/t/rotation-transformation-via-two-vectors/125678/10 "2020-05-18T12:50:20Z")

</div>

I’m not entirely sure why but the solution Fredo has shown is what I initially ended up with but ultimately it did not work. Instead I ended up doing this:

```
@vecdir = @pts[1] - @pts[0]

vecdir3 = @vecdir.clone
zvalue = vecdir3.z
vecdir3.z = 0.0
		
if zvalue < 0.0
	phi = (0 - asin((vecdir3.length)/(@vecdir.length)) + 90.degrees) * -1
else
	phi = 0 - asin((vecdir3.length)/(@vecdir.length)) + 90.degrees
end

theta = atan2(vecdir3.y, vecdir3.x)
		
tr1 = Geom::Transformation.rotation(ORIGIN, [0, 1, 0], -phi)
tr2 = Geom::Transformation.rotation(ORIGIN, [0, 0, 1], theta)

tot_trans = tr2 * tr1

```

---

<div class="post-metadata">

**Author:** ![Sasquatch](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/sasquatch/32/87863_2.png) [@Sasquatch](https://forums.sketchup.com/u/Sasquatch)\
**Post date:** [May 18, 2020, 9:56pm UTC](https://forums.sketchup.com/t/rotation-transformation-via-two-vectors/125678/11 "2020-05-18T21:56:16Z")

</div>

@medeek I am not sure what the goal is here, but when I use your code with inputs of _**[1,0,0]**_ and _**[5,3,2]**_, then apply the resulting _ **tot\_trans** _ to _**[1,0,0]**_, I get a result of _**[0.742781, 0.8557086, 0.371391]**_ which is neither the second vector, not any multiple of it (i.e. it does _not_ lie along _[5,3,2]_)

The following method however works for any two input vectors (with credit to @eneroth3 for her **planar\_angle** method which I incorporated):

```ruby
def find_rot_trans (vec1, vec2)
  norm = (vec1 * vec2).normalize
  rot_angle = Math.atan2((vec1 * vec2) % norm, vec1 % vec2)
  tr = Geom::Transformation.rotation(ORIGIN, norm, rot_angle)
  return tr
end

```

A test for this method is:

```ruby
def test_rotation (vec1, vec2)
  tr = find_rot_trans vec1, vec2 # Find rotation transformation
  vec3 = vec1.transform tr # Test by applying to 'vec1'
  if vec3.samedirection? vec2
    puts "This works!" # Resulting transformed vector is parallel to original 'vec2'
  else
    puts "This did NOT work"
  end
end

```

There is a possibility I misinterpreted your problem, in which case it would be great if you express it as a test method as I have done.
