# How do I find the rotateX,rotateY, and rotateZ values to make any vector vertical?

**URL:** https://discourse.processing.org/t/how-do-i-find-the-rotatex-rotatey-and-rotatez-values-to-make-any-vector-vertical/29220
**Category:** Processing
**Created:** [April 8, 2021, 1:16pm UTC](https://discourse.processing.org/t/how-do-i-find-the-rotatex-rotatey-and-rotatez-values-to-make-any-vector-vertical/29220 "2021-04-08T13:16:50Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![neel](https://avatars.discourse-cdn.com/v4/letter/n/f475e1/32.png) [@neel](https://discourse.processing.org/u/neel)
#### Post date: [April 8, 2021, 1:16pm UTC](https://discourse.processing.org/t/how-do-i-find-the-rotatex-rotatey-and-rotatez-values-to-make-any-vector-vertical/29220/1 "2021-04-08T13:16:50Z")

</div>

I’ve got two points in 3d space, and I want to draw a box between them that acts like a line. If I can translate to get to the first point, how would I find the rotation values that could make my box aligned so that one of the edges of the box connects from the first point to the second point?

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [April 8, 2021, 1:33pm UTC](https://discourse.processing.org/t/how-do-i-find-the-rotatex-rotatey-and-rotatez-values-to-make-any-vector-vertical/29220/2 "2021-04-08T13:33:53Z")

</div>

Check out line3D function here [Controlling rotational motion in Processing - #2 by Chrisir](https://discourse.processing.org/t/controlling-rotational-motion-in-processing/10629/2)

---

<div class="post-metadata">

### Author: ![micuat](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micuat/32/19407_2.png) [@micuat](https://discourse.processing.org/u/micuat)
#### Post date: [April 8, 2021, 3:41pm UTC](https://discourse.processing.org/t/how-do-i-find-the-rotatex-rotatey-and-rotatez-values-to-make-any-vector-vertical/29220/3 "2021-04-08T15:41:06Z")

</div>

I haven’t tested but perhaps `lookAt` in this article is what you need

> **[3D Rotations in Processing (Vectors, Matrices, Quaternions)](https://behreajj.medium.com/3d-rotations-in-processing-vectors-matrices-quaternions-10e2fed5f0a3)**
>
> This tutorial introduces how to rotate objects in 3D beyond Euler angles; to do this, it looks at the basics of matrices and quaternions…

Basically you need to translate to the midpoint of two points, rotate to `lookAt` one of the points and scale it so that the box reaches both points.

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [April 9, 2021, 7:54pm UTC](https://discourse.processing.org/t/how-do-i-find-the-rotatex-rotatey-and-rotatez-values-to-make-any-vector-vertical/29220/4 "2021-04-09T19:54:30Z")

</div>

simplified of my link above

```auto
// Demo program for a 3D line 

final color RED = color(255, 0, 0); 
final color GREEN = color(0, 255, 0); 
final color BLUE = color(0, 0, 255); 

final float a = 200; 

PVector point0 = new PVector ( -a, -a, -3*a); // point 0
PVector point1 = new PVector ( 133, 10, 2.1*a); // point 1 

// -------------------------------------------------------

void setup() {
  size(1200, 700, P3D);
} // func

void draw() {
  background(0);
  lights();

  translate (width/2, height/2, 0);

  // decoration: Two spheres
  sphere3DPVector(point0, 17, GREEN );
  sphere3DPVector(point1, 17, RED );

  // 3D Line 
  line3DPVector ( point0, point1, 13, BLUE );
} // func 

// ---------------------------------------------------------

void line3DPVector(PVector p1, PVector p2, 
  float weight, 
  color colorLine) {
  // drawLine was programmed by James Carruthers
  // see http://processing.org/discourse/yabb2/YaBB.pl?num=1262458611/0#9 

  PVector v1 = new PVector(p2.x-p1.x, p2.y-p1.y, p2.z-p1.z);
  float rho = sqrt((v1.x * v1.x) + (v1.y * v1.y) + (v1.z * v1.z));
  float phi = acos(v1.z/rho);
  float theta = atan2(v1.y, v1.x);
  v1.mult(0.5);

  pushMatrix();
  translate(p1.x, p1.y, p1.z);
  translate(v1.x, v1.y, v1.z);
  rotateZ(theta);
  rotateY(phi);
  stroke(22); //noStroke();// use noStroke or weak stroke here 
  fill(colorLine);

  box(weight, weight, p1.dist(p2));
  popMatrix();
  //
} // method

//---------------------------------------------------------------------------------------

void sphere3DPVector (PVector p1, 
  float size1, 
  color col ) {
  fill(col);
  noStroke();
  pushMatrix();
  translate(p1.x, p1.y, p1.z);
  sphere(size1);
  popMatrix();
}
//

```

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [April 21, 2021, 5:04pm UTC](https://discourse.processing.org/t/how-do-i-find-the-rotatex-rotatey-and-rotatez-values-to-make-any-vector-vertical/29220/5 "2021-04-21T17:04:47Z")

</div>

Hello,

I did this a few different ways:

- Trigonometry to get the angles and then do 2 rotations.
- Convert the Cartesian co-ordinate to spherical co-ordinates and do 2 rotations.  
It is a worthwhile exercise to do this from scratch from the formulas provided.
- Use PVectors to get the angleBetween() and do 2 rotations.

This will help visualize:

> **[Spherical coordinate system](https://en.wikipedia.org/wiki/Spherical_coordinate_system)**
>
> In mathematics, a spherical coordinate system is a coordinate system for three-dimensional space where the position of a point is specified by three numbers: the radial distance of that point from a fixed origin, its polar angle measured from a fixed zenith direction, and the azimuthal angle of its orthogonal projection on a reference plane that passes through the origin and is orthogonal to the zenith, measured from a fixed reference direction on that plane. It can be seen as the three-dimensi...

`:)`
