フィルターのクリア

How to create a series of dimples over a plane

3 ビュー (過去 30 日間)
Johnny Dessoulavy
Johnny Dessoulavy 2022 年 2 月 7 日
コメント済み: William Rose 2022 年 2 月 10 日
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!

採用された回答

William Rose
William Rose 2022 年 2 月 8 日
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 件のコメント
Johnny Dessoulavy
Johnny Dessoulavy 2022 年 2 月 9 日
Thank you! ive gone through your code and learnt a lot from your method.
William Rose
William Rose 2022 年 2 月 10 日
@Johnny Dessoulavy, thank you, and you're welcome. Good luck.

サインインしてコメントする。

その他の回答 (1 件)

Benjamin Thompson
Benjamin Thompson 2022 年 2 月 7 日
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 件のコメント
Johnny Dessoulavy
Johnny Dessoulavy 2022 年 2 月 7 日
I overwrtote the Y variable becasue it wasnt being used anymore. I have ammended that in the post for clarity.
I have tried using repmat but I ended up with a grid size of 10501X10501, which when scaled down to the final 200x200 grid did not scale correctly and looked like a mess.
Benjamin Thompson
Benjamin Thompson 2022 年 2 月 7 日
Scale up your problem complexity slowly. Try one dimple, then two dimples, then a two by two arrangement, etc.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by