# Planes Intersection

## Packages:

``````

In [ ]:

import plotly.offline as py
import plotly.graph_objs as go
py.init_notebook_mode(connected=True)
import numpy as np

``````

## Utilities:

``````

In [ ]:

def mesh2D(xlim, ylim, n=5): #Set up Meshgrid
if isinstance(n, int):
x = np.linspace(xlim[0],xlim[1],n)
y = np.linspace(ylim[0],ylim[1],n)
elif isinstance(n, list):
x = np.linspace(xlim[0],xlim[1],n[0])
y = np.linspace(ylim[0],ylim[1],n[1])
else:
raise Exception("Invalid Parameter")

return np.meshgrid(x, y, sparse=True)

``````
``````

In [ ]:

def normalize(v): #for Evec, Eval
magnitude = np.sqrt(v[0]**2 + v[1]**2 + v[2]**2)
if magnitude==0:
raise ValueError("Zero vector cannot be normalized.")
else:
return v/magnitude

``````

## Objects:

``````

In [ ]:

class Line:
def __init__(self, vec, offset):
self.vec = vec

t = np.linspace(0,1,6) #parameter
self.x = offset[0] + t*vec[0] #x points
self.y = offset[1] + t*vec[1] #y points
self.z = offset[2] + t*vec[2] #z points

def gObject(self, layout=None):
lineObject = go.Scatter3d(mode="lines",
x=self.x,
y=self.y,
z=self.z,
line=dict(color=('rgb(210,64,0)'),
width=7)
)
return lineObject

``````
``````

In [ ]:

class Sphere:
def __init__(self, radius=5, center=[0, 0, 0]):
self.center = center
meshSize = 20
theta = np.linspace(0,2*np.pi,meshSize)
phi = np.linspace(0,np.pi,meshSize)

def gObject(self):
sphere = go.Surface(name='Sphere',
x=self.x,
y=self.y,
z=self.z,
showscale=False,
opacity=0.5,
colorscale=[[0.0, 'rgb(2.137.59)'],
[1.0, 'rgb(0,62,116)']
]
)
return sphere

``````
``````

In [ ]:

class Point:
def __init__(self, position):
self.position = np.array(position)

def gObject(self):
point = go.Scatter3d(mode="markers",
x=[self.position[0]],
y=[self.position[1]],
z=[self.position[2]],
marker=dict(color='rgb(12,161,205)',
size=7
)
)
return point

``````

### 3D Visualization

``````

In [ ]:

line1 = Line([1, 0, 0], [0, 0, 0])
line2 = Line([0, 1, 0], [0, 0, 0])
line3 = Line([0, 0, 1], [0, 0, 0])
sphere1 = Sphere(2)
point1 = Point([2,0,0])

data = [line1.gObject(),
line2.gObject(),
line3.gObject(),
sphere1.gObject(),
point1.gObject()
]

``````
``````

In [ ]:

#fig=go.Figure(data=data)
#py.plot(fig)

``````
``````

In [ ]:

theta1 = np.pi/2
theta2 = -np.pi/2
t1 = np.linspace(0, theta1, 10)
t2 = np.linspace(0, theta2, 10)

Rz = np.matrix([[np.cos(theta1), -np.sin(theta1), 0], [np.sin(theta1), np.cos(theta1), 0], [0, 0 ,1]])
Rx = np.matrix([[1, 0, 0], [0, np.cos(theta1), -np.sin(theta1)], [0, np.sin(theta1), np.cos(theta1)]])
init = np.matrix([[2], [0], [0]])

p1 = Rz*init
p2 = Rx*p1

traP1 = np.reshape(p1, (1,3))
traP2 = np.reshape(p2, (1,3))
traP1list = traP1.tolist()[0]
traP2list = traP2.tolist()[0]

# p3 = Rz*init
# p4 = Rx*p3

# traP3 = np.reshape(p3, (1,3))
# traP4 = np.reshape(p4, (1,3))
# traP3 = traP3.tolist()[0]
# traP4 = traP4.tolist()[0]

``````
``````

In [ ]:

point2 = Point(traP1list)
point3 = Point(traP2list)
data.append(point2.gObject())
data.append(point3.gObject())

``````
``````

In [ ]:

fig=go.Figure(data=data)
py.plot(fig)

``````
``````

In [ ]:

``````