Define n spheres such that no 2 spheres will overlap each other - matlab

I have a matlab/math question,
I want to define n spheres (each sphere should be defined by a single 3D point and a radius) such that no two spheres will overlap each other.
The spheres should be completely random (as long as they don't overlap each other).
Is there an efficient way of doing so?
I know there's an option to define each time a sphere that is farther than the current farthest sphere (you can define how far the sphere is by norm(center)+radius). but that way, we miss many possible options (like defining two spheres that are on the same distance from the origin and still don't overlap each other).
anyone has a smarter way?
Thanks

It is impossible to generate "completely random" data, some limitations always do exist.
You can generate random points, then build Voronoi diagram for this point set and make spheres centered in these points and with radii not larger than distance to the closest Voronoi cell face

Related

Matlab circle packing on a sphere

I am trying to pack same-radi circles on a sphere using matlab. I have figured out how to pack circles and spheres in a box or a cube but just can't figure out where to start when packing it on the surface of a sphere. I would love any hints or ideas in doing this.
Wish you all the best of luck
This is a really hard problem, not sure if there exists a generic solution.
To obtain perfectly regular distribution of circles, take any regular polyhedron, and put a circle at each vertex. The largest regular polyhedron, the dodecahedron has 20 vertices, so you would be able to fit 20 circles on a sphere with this method. If you are willing to have a tiling that is not perfectly regular, you can use shapes such as that of a buckyball (same as soccer ball).
Other ways to distribute points evenly around a sphere have been proposed.
A friend of mine proposed a way that allowed him to easily index each point. It was based on the icosahedron (dual of the dodecahedron, it has 20 faces), and tiled each triangular face. See his paper for more details.
That paper has references to some other methods known at that time. I think the most useful one would be the one where the sphere is cut vertically into slices of equal width (the surface of the sphere section has equal height, the cuts themselves are not spaced evenly), and each section is then a strip that can be tiled by circles evenly. This leads to one circle on the top and one on the bottom, and in between rows of circles. The distribution is not even, and the spacing depends on the ratio of the circle size and sphere size.

Intersection between two 3D surfaces

I have two independent 3D shapes; one is a square and another is a cone.
Let's assume the cone lies inside the square. How can I find out that the surface of the cone touches the square's surface when I move the cone in any direction?
It will be helpful if anyone can suggest an algorithm to check whether the surface touches another shape.
I am working with MATLAB, but the underlying logic will be appreciated in any language will be appriciated.
https://in.mathworks.com/matlabcentral/answers/367565-findout-surface-to-surface-intersection-between-two-3d-shapes
There is a relatively easy solution, thanks to the fact that the truncated cone is a convex shape, and finding its AABB is not so hard.
First rotate space so that the cube become axis aligned (and the cone in arbitrary position). Then to find the AABB of the base, is suffices to get the maxima an minima of the coordinates, using the parametric equation, C + R cos t + R' sin t, where C is the position vector of the center, and R, R' two orthogonal radii. You find the limit angles by canceling the derivative.
After finding the extrema on the three coordinates, the global bounding box is the one that surrounds these six points plus the apex.
By comparing the AABB to the cube, you can tell what distance remains before a collision, in any direction.

Draws the intersecting volume of two spheres in MATLAB?

I have the equations of two spheres in MATLAB, like
(x-x0)^2+(y-y0)^2+(z-z0)^2=R0^2
(x-x1)^2+(y-y1)^2+(z-z1)^2=R1^2
They have an intersecting part (I find a similar picture which could explain my meaning on the web like below)
Now I would like to only draw the spatial intersecting volume of these two spheres (like the 3D part between the curved faces with red lines in the sample image above)
Maybe I could use the command 'surface' in MATLAB to draw out those two curved faces which make up the intersecting volume? I do not know how to do it....
Moreover, I also would like to draw these two spheres after erasing out those intersecting parts
That is, my goal: drawing out the intersecting and non-intersecting part of these two spheres individually.
Could anyone help me?
Thank you!
The answer is given in great detail at Wolfram. In brief, rather than copypaste all the equations:
1) calculate the circle of intersection of the two sphere's surfaces.
2) determine the location of that circle on each sphere.
3) Calculate the volume of each sphere's spherical cap for that circle and add the two volumes to get the "lens."
I am recommending this be moved to Math.SE because it is a math problem, and coding it in Matlab is trivial.

Plot 3 D cube around interest point in MATLAB

I want to plot 3D cube of size 2x2x5 (x,y,z) around an interest point to get the nearest points to it and inside the cube. The interest point may be at the center of cube.
How can I get the nearest points to the interest point?
There are several techniques for drawing cubes here. You will need to choose one that lets you specify size and origin. The sizes will be 2,2,5, and the origin will be the coordinates of the interest point.

How to represent a polygon with hole(s)?

It's usually popular to work with polygons with their vertices sorted CW or CCW in vectors(2*1 or 1*2 matrices). However, how to state polygons with holes in vectors?
I'm going to apply various process on these polygons, so I want a way of representing with which I could work easily or efficiently.(i.e how to state that kind of polygons in my program in order to ease my algorithms?)
polygons are 2D and I'm programming in MATLAB.
EDIT 1 : I'm going to calculate visibility graph of these polygons(with or without holes).
As others have mentioned, a polygon with holes can be represented as an exterior boundary, plus zero or more interior boundaries, all of which are mutually nonoverlapping*. If you use nonzero winding number to determine inside/outside, be sure to specify your interior boundaries in the opposite direction as the exterior boundaries (counterclockwise for exterior and clockwise for interior, or vice-versa) so that the contour integrals are zero inside the holes.
FYI, tis kind of definition/representation has been formalized in the OpenGIS Simple Features Specification (PDF).
As far as representation:
I'd probably have a cell array of K Nx2 matrices, where the first element in the cell array is the exterior boundary, and the remaining elements (if any) in the cell array are the interior boundaries. I would use a cell array because there may not be the same number of points on each boundary.
*nonoverlapping = except at individual points, e.g. a diamond inside a square:
You can break a polygon with a hole in it into two shapes without a hole. When you're doing contour integration in a complex plane, you can create a "cut" from one edge of the polygon that brings you to the edge of the hole; integrate around one side of the hole and back; then traverse around the other side for the second polygon. You end up with two path integrals along each cut that cancel each other out.
"visibility graph" - is this for a radiation view factor calculation with shading? Or a ray-tracing graphics algorithm?
A polygon, plus a list of polygonal holes. Just be sure the various polygons don't intersect.
What do you plan to do with this thing?
It sounds like each hole is just a polygon inside the polygon itself. Perhaps you could store a vector like you describe for the outer polygon, then a vector of more polygon vectors for the holes.
Presumably you'll want to have a tree structure if you want this to be as generic as possible (i.e. polygons with polygonal holes that have polygons inside them with holes inside that, ...). Matlab isn't really great at representing tree structures efficiently, but here's one idea...
Have a struct-array of polygons.
Each polygon is a struct with two fields, 'corners', and 'children'.
The 'corners' field contains a matrix of (x,y) coordinates of the corners, accessed as "data{polyIdx}.corners(:,cornerIdx)".
The 'children' field is a struct-array of polygons.
Here's an example of some code to make a triangle with bogus children that are holes (they aren't really valid though because they will likely overlap:
polygon = struct;
npoints = 3;
polygon.corners = rand(2,npoints);
polygon.children = struct;
nchildren = 5;
for c=1:nchildren
polygon.children(c).corners = rand(2,npoints);
polygon.children(c).children = struct;
end
You could continue to recursively define children that alternate between creating holes and filling them.
What exactly do you mean under "a visibility graph" ?
Two "full" poligons, two states possible, either +1 or -1.
If you're representing a hole, you've got one with state +1 and one with state -1, which represents a hole, resulting in state 0.
If you've got overlapping polygons, you'll end up with resultant state >1. Then you can calculate the borders of a new polygon.
If you've got two polygons with holes that intersect, then first calculate the state of a new polygon which consists of outer borders of the two old ones, then deal with holes.
Anyways, ... I think you get the general principle.
Have no idea how to do it in matlab, I used it only marginally so far, and even that for very simple things.