How do I project a map from the plane to a sphere?
1 Comment
Answers (4)
Hi @Christopher Scott,
To achieve the projection of points from a 2-D plane onto a 3-D unit sphere in MATLAB, you can utilize spherical coordinates. The projection can be defined mathematically, where each point in the 2-D plane is transformed into a point on the surface of the sphere. The following steps outline the approach, followed by the updated MATLAB code. The projection of a point ((x, y)) in the 2-D plane onto a unit sphere can be expressed using the following equations:
Convert Cartesian to Spherical Coordinates: The radius (r) of the sphere is 1 (unit sphere).
The azimuthal angle (\theta) can be derived from the x-coordinate: [ theta = frac{x}{sqrt{x^2 + y^2 + 1}} ]
The polar angle (phi) can be derived from the y-coordinate: [ phi = frac{y}{sqrt{x^2 + y^2 + 1}} ]
The z-coordinate is given by: [ z = sqrt{1 - x^2 - y^2} ]
Mapping to the Sphere: The final coordinates on the sphere can be represented as: [ X = r cdot sin(phi) cdot cos(theta) ] [ Y = r cdot \sin(phi) cdot sin(theta) ] [ Z = r cdot cos(phi) ]
Below is the updated MATLAB code that includes the projection of the fractal data onto a 3-D unit sphere. The code retains the original fractal generation logic and adds the projection functionality.
% Fractal generation parameters res = 0.05; x = -2:res:2; y = x'; depth = 32; grid = zeros(length(x), length(y)); map = zeros(length(x), length(y)); c = 0.30 + 0.5*1i;
% Generate the fractal for i = 1:length(x) for j = 1:length(y) grid(i,j) = x(i) + y(j)*1i; end end
for i = 1:length(x) for j = 1:length(y) for n = 1:depth if abs(grid(i,j)) > 2 map(i,j) = n; break; else grid(i,j) = grid(i,j)^2 + c; end end end end
map(map==0) = depth;
% Projection onto the 3D unit sphere [X, Y, Z] = sphere(50); % Create a unit sphere hold on; colormap(flipud(jet(depth)));
% Normalize map values to fit the sphere for i = 1:length(x) for j = 1:length(y) % Normalize the coordinates norm_factor = sqrt(x(i)^2 + y(j)^2 + 1); theta = x(i) / norm_factor; phi = y(j) / norm_factor;
% Calculate the 3D coordinates sphere_x = sin(atan2(phi, sqrt(1 - theta^2))) * cos(atan2(theta, sqrt(1 - phi^2))); sphere_y = sin(atan2(phi, sqrt(1 - theta^2))) * sin(atan2(theta, sqrt(1 - phi^2))); sphere_z = cos(atan2(phi, sqrt(1 - theta^2)));
% Plot the points on the sphere scatter3(sphere_x, sphere_y, sphere_z, 36, map(i,j), 'filled'); end end
% Set the view and axis properties axis equal; view(3); xlabel('X-axis'); ylabel('Y-axis'); zlabel('Z-axis'); title('Projection of Fractal Data onto a 3D Unit Sphere'); hold off;
Please see attached.


The above code provides a comprehensive approach to project points from a 2-D plane onto a 3-D unit sphere in MATLAB. By utilizing spherical coordinates and the built-in plotting functions, you can visualize the fractal data effectively.
1. Fractal Generation: The initial part of the code generates the fractal and stores the results in the map matrix. 2. Sphere Creation: The sphere function creates a unit sphere for visualization. 3. Normalization and Projection: The nested loops iterate through the map matrix, normalizing the coordinates and calculating their corresponding positions on the sphere. 4. Visualization: The scatter3 function is used to plot the projected points on the sphere, with colors corresponding to the values in the map.
If you have further questions or need additional modifications, feel free to ask!
2 Comments
3 Comments
Hi @ Christopher Scott,
Please let me know how can I help you further or help you clarify any issues.
5 Comments

Hi @Christopher Scott,
I went through analysis of my recent code and your code again, it retains your original fractal generation logic while adding functionality for projecting these points onto the sphere.
clear all clc clf
% Parameters res = 0.05; % Resolution x = -2:res:2; % X range y = x'; % Y range (transpose) depth = 32; % Iteration depth grid = zeros(length(x), length(y)); map = zeros(length(x), length(y)); c = 0.30 + 0.5*1i; % Complex parameter
% Generate grid of complex numbers for i = 1:length(x) for j = 1:length(y) grid(i,j) = x(i) + y(j)*1i; end end
% Fractal generation using iteration for i = 1:length(x) for j = 1:length(y) for n = 1:depth if abs(grid(i,j)) > 2 map(i,j) = n; break; else grid(i,j) = grid(i,j)^2 + c; end end end end
map(map==0) = depth; % Set unassigned points to max depth
% Projection onto a 3D unit sphere [x_sphere, y_sphere, z_sphere] = sphere(50); % Create a unit sphere mesh
% Prepare figure for fractal and sphere projection figure;
% Plotting the fractal image on a flat surface subplot(1, 2, 1); image(map); axis image; colormap(flipud(jet(depth))); title('Fractal Image');
% Plotting the projected points on the sphere subplot(1, 2, 2); hold on;
% Map each point from the fractal to the sphere for i = 1:length(x) for j = 1:length(y) if map(i,j) < depth % Only plot points that were iterated over r = sqrt(x(i)^2 + y(j)^2); theta = atan2(y(j), x(i)); phi = acos(r / sqrt(r^2 + 1)); % Calculate angle from vertical
% Spherical coordinates conversion to Cartesian coordinates X = sin(phi) * cos(theta); Y = sin(phi) * sin(theta); Z = cos(phi);
plot3(X, Y, Z, '.', 'MarkerSize', map(i,j)/depth*10); % Scale marker size by depth end end end
% Add sphere surface for reference surf(x_sphere, y_sphere, z_sphere, 'FaceAlpha', 0.3); % Transparent sphere surface
axis equal; title('Projection onto Unit Sphere'); xlabel('X-axis'); ylabel('Y-axis'); zlabel('Z-axis'); grid on; hold off;
Please see attached.


Visualization: This updated code provides side-by-side visualization of both the fractal image and its projection onto a unit sphere. This helps in understanding how complex plane data maps into three dimensions.
This approach should provide you with a solid foundation for projecting your fractal data onto a sphere and visualizing it effectively in MATLAB.
Hope this helps now.

1 Comment
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!