Ellipse Volume withMonte Carlo Method - montecarlo

Sorry.I have a homework but I cannot do.I have to write code for between cube and ellipse'volume with Monte Marlo Method.Can you help me? Dimension for cube is 10 and radius for ellipse is 5 and height is 3.
Thank you

I wrote this codes for the intersection of a cylinder and a sphere but I cannot translate for ellipse and cube
%clear the workspace and command window
clc
clear
% find the volume of the intersection of a cylinder and a sphere
RunLength=10000; % run length
Count=0;
%initialize the empty arrays
px=[];
py=[];
pz=[];
qx=[];
qy=[];
qz=[];
d=0.2
%try 10000 points
for i=1:RunLength
x=2*rand()-1; %x coordinate random number between (-1,1)
y=2*rand()-1; %y coordinate random number between (-1,1)
z=2*rand()-1; %z coordinate random number between (-1,1)
if(x^2+y^2+z^2<=1); %if the point is in the big sphere (x^2+y^2+z^2<=1^2)
if (y^2+(z-(1-0.5)^2)>=(0.5)^2);
qx(end+1)=x; %add this point's x coordinate to the qx array
qy(end+1)=y; %add this point's y coordinate to the qy array
qz(end+1)=z; %add this point's z coordinate to the qz array
Count=Count+1; %increase the number of points in the shape by 1
end
else
px(end+1)=x; %add this point's x coordinate to the px array
py(end+1)=y; %add this point's y coordinate to the py array
pz(end+1)=z; %add this point's z coordinate to the pz array
end
end
%ratio is the number of points in the shape/total number of points
disp(8*Count/RunLength); %multiply the ratio with 8 and display
%use plot3 command because it is a 3D plot and use ‘ro’ for showing points as
%red circles in the plot
%use plot3 command because it is a 3D plot and use ‘ro’ for showing points as
red circles in the plot
plot3(qx,qy,qz,'ro');

Related

tangent (y/x) gives values in the range of -180 to +180 degree. Is there a need to change the range?

I am calculating the spherical coordinates of the 3D geometry. I used below formula to find coordinats.
r = np.sqrt(x*x + y*y + z*z) # Radial distance
theta = np.arctan2(y,x)*180/ pi # Polar angle to degrees
phi = np.arccos(z/r)*180/ pi # Azimuth angle to degrees
I got the list of theta values like below.
theta = [-145.75, -164.54, -155.10, -124.70, 146.31, 109.80, 101.56, 77.20, 56.61, 40.76, 11.69, -24.15, -47.82, -72.65, -105.71, -131.19, -52.43, 30.96, 68.20, -145.75, -164.54, -155.10, -124.70, 146.31, 109.80, 101.56, 77.20, 56.61, 40.76, 11.69, -24.15, -47.82, -72.65, -105.71, -131.19, -52.43, 30.96, 68.20]
From these values, I can see that, these values are in between -180 to +180 degree. And also some angles are more than 180 degree and obtained values shows -145.75.
Is there any need to convert the range and values of theta?? How can I do this?
And I have the list of Azimuth angle (Phi) also. May I know that, if there is any need to convert the range of that also?
phi = [99.49, 101.02, 107.10, 121.000000000000, 131.29, 109.67, 101.94, 101.89, 100.22, 103.93, 107.79, 106.64, 102.41, 105.82, 105.95, 102.59, 120.08, 129.17, 109.43, 80.51, 78.98, 72.90, 59.0000000000000, 48.71, 70.33, 78.06, 78.11, 79.78, 76.07, 72.21, 73.36, 77.59, 74.18, 74.05, 77.41, 59.92, 50.83, 70.57]
What I want to do and what is a motive behind asking this question?
I want to project the nodes (coordinate) of geometry to the spherical surface which is created around an geometry. Therefore I am calculating angles of each coordinate available in geometry.
In spherical surface, I have created equal points. I will calculate angles for each points also. My aim is to project geometrical nodes into spherical surface and then I will calculate the number of nodes presented in the each part of spherical surface. Therefore in order to compare the angles of geometry and points of spherical surface, I want polar angles.

How to find the orientation of a plane?

I have three non-colinear 3D points, let's say pt1, pt2, pt3. I've computed the plane P using the sympy.Plane. How can I find the orientation of this plane(P) i.e. RPY(euler angles) or in quaternion?
I never used sympy, but you should be able to find a function to get the angle between 2 vectors (your normal vector and the world Y axis.)
theta = yaxis.angle_between(P.normal_vector)
then get the rotation axis, which is the normalized cross product of those same vectors.
axis = yaxis.cross(P.normal_vector).normal()
Then construct a quaternion from the axis and angle
q = Quaternion.from_axis_angle(axis, theta)

Traversing a grid of pixels inside projection plane

Suppose, I have a projection plane of length 2 like the below image:
If I divide the projection plane into a two dimensional
grid of screen_width x screen_height (720*480), then each pixel will be (2/720) distance apart in the x axis and (2/480) distance apart in the y axis and If I add x+(2/720) with my current co-ordinate, keeping the y constant I will move 1 pixel to the right side. Am I right?

Geometry of a radial coordinate to Cartesian with bounding points

I need to find 4 points in Latitude/Longitude format surrounding a given center point and a resulting algorithm (if possible).
Known information:
Equal distances for each "bin" from center of point (Radar) outward.
Example = .54 nautical miles.
1 Degree beam width.
Center point of the "bin"
This image is in Polar coordinates (I think this is similar to Radial coordinates???):
I need to convert from Polar/Radial to Cartesian and I should be able to do that with this formula.
x = r × cos( θ )
y = r × sin( θ )
So now all I need to do is find the "bin" outline coordinates (4 corners) so I can draw a polygon in a Cartesian coordinate space.
I'm using Delphi/Pascal for coding, but I might be able to convert other languages if you have a sample algorithm.
Thanks for any suggestions or sample algorithms.
Regards,
Bryan
You need to convert everything to the same coordinate system and then impose the distance criteria as follows:
Convert your center point from geographic coordinates to polar coordinates to yield (rC, θC)
Convert your center point from polar to Cartesian coordinates using your equations yielding (xC, yC)
The corner points on the right side of the center points (xR, yR) satisfy the equation
(xR - xC)2 + (yR - yC)2 = D2
[rRcos(θC+0.5o) - xC]2 + [rRsin(θC+0.5o) - yC]2 = D2
where D=distance between the center point and corner points
Everything is known in the above equation except rR. This should yield a quadratic equation with two solutions which you can easily solve. Those are your two corner points on the right side.
Repeat step 3 with angle θC-0.5o to get the corner points on the left side.

How to determine which side of a polar line a point lies?

If I have a 2D line given in polar coordinates (i.e. rho distance from the origin and theta angle from the x-axis) how can I determine which on which side of the line a point lies? Specifically, how would I take two points and determine if they are on the same side or opposite sides of this line?
Thanks!
Such line has equation:
-x*cos(theta)+y*sin(theta)-rho=0 [1]
Distance from point (x0, y0) to this line is
Dist = -x0*cos(theta)+y0*sin(theta)-rho [2]
Important thing: sign of Dist depends on which side of the line a point lies (positive when this point and coordinate origin lie on the different sides of line, and negative otherwise).
So it is enough to calc and compare the signs of the [2] expressions for two needed points
Could you take both of the supplied points and calculate their angles respective to theta?
Say for the sake of argument that your 2D line ends at (3,3);
2D Line:
Coord: (3,3)
Radius: 3 * √2
Theta: 0.79 radians
Point 1:
Coord: (3,4)
Radius: 5
Theta: Arcsin(4/5) = 0.92 radians
Point 2:
Coord: (3,1)
Radius: √10
Theta: Arcsin(2/√10) = 0.68 radians
Point 1's Theta is greater than that of the 2D Line; it is on one distinct side. Point 2's is less than that of the 2D line; it is on the other side.
Hope this helps! :)
I understand you have your line given by say rho with is the intersection of your line with the x-axis and theta with is the angle between your line and the x-axis.
An equation for your line then would read
f(x) = (x-rho)*tan(theta)
To determine if a point (x0,y0) is above that line check if
f(x0) = (x0-rho)*tan(theta) > y0
To check if it is under the line check
f(x0) = (x0-rho)*tan(theta) < y0
But note that this method breaks if theta= 90°, 270°. But in that case its easy you just have to check if x0 is larger or smaller then rho.

Resources