Draw a 2d plane with scatterplot3d - r

I am trying to plot a plane with scatterplot3d that is perpendicular to a direction vector described by two angles, say theta and phi. The points are described by the (xyz)-coordinates satisfying the following equation, where R is the distance from the origin.
x cos(theta)cos(phi) + y sin(theta) cos(phi) + z sin(phi) = R
I guess I should use plane3d, but I can't figure out how to get this plane right based on my description. Can anyone help?
In other words, I am trying to plot the plane perpendicular to the blue line at distance R from the origin in this figure.
I assume this should be straightforward, but cannot figure it out.

Using plane3d and calculating the intercept and coefficients, this turned out to be quite straightforward:
spl$plane3d(Intercept, x.coeff, y.coeff, col=5, draw_polygon=T, lty=NULL)
The Intercept would just be R/sin(phi), and the x- and y-coefficients are the coefficients in front of X and Y: x.coeff = cos(theta)/tan(phi) and y.coeff = sin(theta)/tan(phi).
This gives the plane, as desired.

Related

Fitting an ellipsoid given 2D contours

I have coordinates corresponding to a set of 2D contours, each corresponding to different heights. These contours do not draw out a perfect ellipsoid in 3D, and instead what I would like to do is to find the best fitting ellipsoid. I do not have any knowledge on the origin of this ellipsoid.
My first thought was to incorporate some type of least squares algorithm, where I find the ellipsoid parameters that minimize the distance between points. I imagine this would be quite expensive and not too far from a brute force approach. I am convinced there is a more elegant and efficient way of doing this. If there is an existing library that handles this (preferably in Python) that would be even better.
I have already seen a related question (Fitting an ellipsoid to 3D data points), but figured I would ask again as it has been over a decade since that post.
So you have a set of (x,y) values for each contour, which describe a portion of an ellipse (blue dots below).
The best fit ellipse is described by the general equation
A x^2 + B y^2 + 2C x y + 2D x + 2E y = 1
and once the coefficients (A,B,C,D,E) are found, the ellipse of fully described. See below in how to find the the curve coordinates (x,y) from the coefficients and a parameter t=0 .. 1.
To find the coefficients of the ellipse, form 5 vectors, each a column of a n×5 matrix Q
for i = 1 to n
Q(i,1) = x(i)^2
Q(i,2) = y(i)^2
Q(i,3) = 2*x(i)*y(i)
Q(i,4) = 2*x(i)
Q(i,5) = 2*y(i)
next i
and a vector K filled with 1 for the right-hand side
for i = 1 to n
K(i) = 1.0
next i
Find the coefficients using a least-squares fit with some linear algebra
[A,B,C,D,E] = inv(tr(Q)*Q)*tr(Q)*K
where tr(Q) is the transpose of Q and * is matrix/vector product
Now we need to extract the geometric properties of the ellipse from the coefficient. I want to have a the semi-major axis, b the semi-minor axis, φ the rotation angle, xc the x-axis center, yc the y-axis center.
xc = -(B*D-C*E)/(A*B-(C^2))
yc = -(A*E-C*D)/(A*B-(C^2))
φ = atan( 2*C/(A-B) )/2
a = SQRT(2*(A*(B+E^2)+B*D^2-C*(C+2*D*E))/((A*B-C^2)*(A+B-SQRT((A-B)^2+4*C^2))))
b = SQRT(2*(A*(B+E^2)+B*D^2-C*(C+2*D*E))/((A*B-C^2)*(A+B+SQRT((A-B)^2+4*C^2))))
Finally to plot the ellipse you need to generate a set of points (x,y) from the curve parameter t=0..1 using the above 5 coefficients.
Generate the centered aligned coordinates (u,v) with
u = a*cos(2*π*t)
v = b*sin(2*π*t)
Generate the centered rotated coordinates (x',y') with
x' = u*cos(φ) - v*sin(φ)
y' = u*sin(φ) + v*cos(φ)
Generate the ellipse coordinates (x,y) with
x = x' + xc
y = y' + yc
The result is observed above in the first picture.
Now for the total solution, each 2D slice would have its own ellipse. But all the slices would not generate an ellipsoid this way.
Extending the above into 3D coordinates (x,y,z) is doable, but the math is quite involved and I feel [SO] is not a good place to develop such an algorithm. You can hack it together, by finding the average center for each slice (weighted by the ellipse area π*a*b). Additionally, the rotation angle should be the same for all contours, and so another averaging is needed. Finally, the major and minor axis values would fall on an elliptical curve along the z-axis and it would require another least-fit solution. This one is driven by the equation
(x/a)^2 + (y/b)^2 + (z/c)^2 = 1
but rather in the aligned coordinates (u,v,w)
(u/a)^2 + (v/b)^2 + (w/c)^2 = 1

The plane formed by the base of a cone transformed to x-y coordinates

I have a random 3-d walk where you can go either positive x,y,z by one step. After 1000 steps you most likely end up on concordant 333,333,333. I have a visual of 2000 trials. I want to plot the ending points onto a 3-d histogram. I believe it should look like a 3-d Gaussian. My problem is that if you take the ending x-y coordinates directly and plot them, you get an oval shape. I think that's kind of expected. These ending points lie on the plane formed by the end of the random walks. Here is a picture of the histogram Here is a picture of a 2-d plot of the x-y ending points. How to I transform the ending points from my "cone" to accurate x-y coordinates. I think the 2-d would look like a circle. Thank you
If you want a graph that looks more like a circle, consider plotting against the ending plane (which you describe) instead of x and y. If you transform your results to use coordinates like an isometric projection:
x' = x - (y + z)/2
y' = y - (x + z)/2
z' = z - (x + y)/2
e.g.:
z'
|
|
/ \
/ \
x' y'
then convert that to a 2d version that pleases you.
x'' = sqrt(3) * (y' - x')/2
y'' = z' - (x' + y')/2
If you want to understand how this was accomplished, just think of looking directly down from the vector (k, k, k) to the origin from infinitely far away, with 'up' pointed towards the z-axis. You would see (1,0,0), (0,1,0), and (0,0,1) forming an equilateral triangle, and simple geometry can give you the coordinates. You can even skip the first xyz -> xyz' steps, that was to attempt to make it easier to understand, but might make it seem more confusing.
You can also search for "isometric projection", or take a look at this chart:
and this online calculator:
https://planetcalc.com/8316/

From line in cartesian coordinates to polar coordinates with youth style

I have line like in 2D defined by ax+by+c = 0 so (a,b,c).
I need to compute a polar representation of this line like Hough approach with rho an theta.
How to do this?
A line in cartesian coordinates is not as easily represented in polar coordinates.
You can simply substitute x,y with their respective polar equivalents, r*cos(theta), r*sin(theta), giving you
a*r*cos(theta) + b*r*sin(theta) + c = 0
This implicit equation is not as easy to figure out, however. But, if you first convert your implicit line equation to a parametric vector equation of the form (x,y) = R(t) = R0 + t*V, where R0,V are cartesian vectors which you can derive from a,b,c, you can then write
(r*cos(theta), r*sin(theta)) = R0 + t*V
and solve this system of equations for r and theta in terms of t.
However, polar coordinates are not the same as the Hough transform.
In the Hough system, the line is defined by the length rho of a perpendicular line that crosses (0,0) , which is theta = atan(b/a). Figuring out rho seems more difficult at first, but this tutorial explains it.

find 3d point on a circle given the angle and radius

http://i.stack.imgur.com/7InNo.png
I am trying to find the green points using the angle ,radius and center of the circle.
I am using this image that was posted by another member.
I wish to find the green points, but in a 3d space instead.
I am able to get the x and y value but i am unable to get the z.
r = radius
X = r * cos(angle)
Y = r * sin(angle)
How can i get the value for z-axis?
In the case of 3 dimensions you need 2 angles. Basically what you are doing is converting from spherical coordinates to cartesian coordinates. So your formulas can be found here

Converting from spherical coordinates to cartesian around arbitrary vector N

So if I'm given an arbitrary unit vector N and another vector V defined in spherical coordinates theta (polar angle between N and V) and phi (azimuthal angle) and r = 1. How do I convert vector V into cartesian coordinates?
Now, I know that in general the conversion from spherical to cartesian is as follows:
x = r * sin theta * cos phi
y = r * sin theta * sin phi
z = r * cos theta
However, since the angles theta and phi are defined respective to the vector N and not the axes, the above conversion wouldn't work, yes? So how would I go about modifying the conversion?
I feel that this is simply not possible given the information you have to hand.
You cannot have a vector V with spherical polar components defined relative to another vector. In a standard spherical polar coordinate system, the coordinates of a point P are given by (r,theta,phi) where theta is the polar angle, phi azimuthal angle, and r the Euclidean distance from the origin. The polar angle is the angle between the z-axis and the line joining the origin to the point P. The azimuthal angle is defined as the angle between the x-axis and the line that joins the origin to the orthogonal projection of P onto the xy plane.
Sometimes the definitions of these two angles are reversed. The above is clearly illustrated at the wiki page http://en.wikipedia.org/wiki/Spherical_polars
The point here is that the angles are defined relative to two mutually orthogonal axes - z and x in this case. Thus you cannot have both of your polar and azimuthal angles defined relative to a single vector N - You can have ONE of them measured relative to N but not both.
As it stands, your problem cannot be solved without providing another vector orthogonal to your N that provides the axis to which the other angle (either polar or azimuthal) is measured.
Your description of N indicates that this is the z-axis of some rotated coordinate system that V takes its polar angle relative to. You need another vector that gives the x-axis of the same rotated coordinate system that V takes measures its azimuthal angle relative to. With that information you can obtain the rotation matrix that maps your rotated coordinate system axes onto the cartesian coordinate axes - from there you will have sufficient information to obtain the cartesian coordinates of V that you require.
Look at this one: http://www.ewerksinc.com/refdocs/coordinate%20and%20unit%20vector.pdf
On page 7 you'll find the conversion formulas between spherical and cartesian vectors.

Resources