How to create a series of dimples over a plane
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Johnny Dessoulavy
le 7 Fév 2022
Commenté : William Rose
le 10 Fév 2022
I am trying to create a plane which mimics the appearance of a golf ball. Where the dimples have a controllable radius and depth. My problem currently is that I have a limit of 200x200 grid where these dimples must be placed on, but I want to be able to control the number of dimples I have in the x and y directions. I also want the 200x200 grid that these dimples to be placed on to have an x and y value range from 0:2000.
Every time I have created dimples (currently I am just using the equation of a hemisphere) they have a grid size which is considerably larger than that of the 200x200 max grid size. So, I have been using imgresize to scale these matrices down, but they have become so distorted and the original parameters (In the code I have below I want a radius of 25 and a depth of 25) no longer hold after resizing.
I want to be able to have these dimples oscillate IE the plane should look like:
XOXOX
OXOXO
XOXOX
where the X's are dimples out of the page and the O's are dimples into the page.
I have my code below which has the dimples that have scaled incorrectly (much larger than they should be). I’d appreciate any help people have that would tackle this problem in another fashion or help fix the code I have already written. I’m happy to clarify further if people are also confused on what I am asking.
clc, clear all, close all
nrow = 200; ncol = 200; angle = 20; xsize = 2000; ysize = 2000; radius =25; dim_size =25;
%where dim size controls the image resize of the dimples, currently the way to control the amount of dimples
dx = xsize/ncol; dy = ysize/nrow;
angle = angle*pi/180;
z_flat=zeros(nrow,ncol);
for j=1:nrow
for i=1:ncol
z_flat(j,i) = (i-1)*dx*sin(angle); %sets the inclination plane on which the dimples will be placed
end
end
%% Setting the rows and columns into a meshed matrix to generate x and y coordinates
dx_mat = linspace(0,xsize,length(z_flat));
dy_mat = linspace(0,ysize,height(z_flat));
[x y] = meshgrid(dx_mat, dy_mat);
%%
[X,Y] = meshgrid(-radius:.01:radius);
Z = (-sqrt(radius.^2 - X.^2 - Y.^2));
Z(imag(Z) ~= 0) = 0; % turns from complex to double
Z = imresize(Z,[dim_size,dim_size]);
z_matrix = zeros(nrow, ncol);
Q =1;
kk =0;
count = 1;
for i = 1:length(z_flat)
W = 1;
for j = 1:length(z_flat)
if W == length(Z)
if j < j-5
z_matrix(:,j:j+5) = z_flat(:,j:j+5);
W = 1;
if kk ==1
kk = 0;
else
kk = 1;
end
else
z_matrix(i,j) = z_flat(i,j);
W = 1;
end
end
if W <= length(Z) && kk == 0
z_matrix(i,j) = z_flat(i,j) + Z(Q,W);
elseif W <= length(Z) && kk == 1
z_matrix(i,j) = z_flat(i,j) - Z(Q,W);
end
W = W+1;
end
if Q == length(Z)
if i < i-5
z_matrix(i:i+5,:) = z_flat(i:i+5,:);
Q = 1;
if kk == 0
kk = 1;
else
kk = 0;
end
else
z_matrix(i,j) = z_flat(i,j);
Q = 1;
if kk == 0
kk = 1;
else
kk =0;
end
end
else
end
Q = Q+1;
end
z_matrix(:,end-6:end) = z_flat(:,end-6:end);
surf(x,y,z_matrix);
view(-52,22)
axis tight
xlabel 'Planar Depth (m)', ylabel 'Planar Width (m)',zlabel 'Planar Height'
As you can see the dimples are considerably larger than the intended size of depth of 25 and radius of 25. Furthermore, the patterning is in columns when they should follow the described matrix above.
Any help would be appreciated!
0 commentaires
Réponse acceptée
William Rose
le 8 Fév 2022
Here's a script that makes a surface with up and down dimples. I plot the surface twice - zoomed in in the seocond case, so the dimples are easier to see. You can adjust the spacing, positioning, radius, height, etc.
Experiment with code above.
2 commentaires
Plus de réponses (1)
Benjamin Thompson
le 7 Fév 2022
Not sure what you are doing overwriting the Y variable output from meshgrid and then using it as a counter later on. Try simplifying this down to producing a single dimple in a flat plane, then use repmat to increase the size of the plane for more dimples.
2 commentaires
Benjamin Thompson
le 7 Fév 2022
Scale up your problem complexity slowly. Try one dimple, then two dimples, then a two by two arrangement, etc.
Voir également
Catégories
En savoir plus sur Resizing and Reshaping Matrices dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!