How to trim a surface intersecting with another
조회 수: 11 (최근 30일)
이전 댓글 표시
Hi all, I have two surfaces Z1 and Z2 (a surface and an extruded line) that intersect with each other, in point format with two different grids. What I want to accomplish is to trim the left part of the Z1 surface after its point of intrersection with Z2 as shown in the example below, so that I can obtain the trimmed surface (lets call it Z1t ) in its new point format, as a series of three matrices.
Below is the example code the creates the above (without the trimming part obviously). The reason that I am struggling to accomplish that, is because the surfaces that I am given do not share the same grid, as I also demonstrate in my code below. So I do not have the exact points where they intersect. Moreover, when I attempt to place those surfaces on the same grid using griddata , I am getting a matrix full of NaN.
clear ; clc
n = 20 ;
x1 = linspace(1,10,n) ;
y1 = linspace(-25,25,n) ;
x2 = linspace(1,10,n) ;
y2 = linspace(-10,10,n) ;
[X1, Y1] = meshgrid(x1, y1) ; % Grid 1
[X2, Y2] = meshgrid(x2, y2) ; % Grid 2
Z1 = sin(X1) + cos(Y1) ; % Surface
Z2 = X2.*sin(X2) ; % Line (extruded for plotting)
z2 = x2.*sin(x2) ; % Line (not extruded)
% Match grid surface to line
[~, ~, Z2new] = griddata(x2, y2, z2, x1, y1') ;
figure
surf(X1,Y1,Z1)
hold on
surf(X2,Z2,Y2)
P.S. I have seen other posts with similar subjects, however they don't address this problem exactly. There is this post which seems to offer a valuable hint, however I don't get how to implement the method. Also, there is this post that straight-up doesn't work for me when dealing with different grid sizes. Finally, this post seems to get the closes, however I can't obtain the new plane in point format.
Thanks for your help in advance
댓글 수: 0
채택된 답변
Star Strider
2021년 5월 1일
Try this—
n = 50 ;
x1 = linspace(1,10,n) ;
y1 = linspace(-25,25,n) ;
x2 = linspace(1,10,n) ;
y2 = linspace(-10,10,n) ;
[X1, Y1] = meshgrid(x1, y1) ; % Grid 1
[X2, Y2] = meshgrid(x2, y2) ; % Grid 2
Z1 = sin(X1) + cos(Y1) ; % Surface
Z2 = X2.*sin(X2) ; % Line (extruded for plotting)
z2 = x2.*sin(x2) ; % Line (not extruded)
% Match grid surface to line
[~, ~, Z2new] = griddata(x2, y2, z2, x1, y1') ;
Lv = nan(size(X1)); % Create 'NaN' Matrix
Lv(Y1<=Z2) = 1; % Set Selected Elements TO 'true'
figure
surf(X1.*Lv,Y1.*Lv,Z1.*Lv) % Multiply Matrices By 'Lv' To Approximate Desired Result
hold on
surf(X2,Z2,Y2)
hold off
xlabel('X')
ylabel('Y')
view(-37.5,30)
grid on
figure
surf(X1.*Lv,Y1.*Lv,Z1.*Lv)
hold on
surf(X2,Z2,Y2)
hold off
xlabel('X')
ylabel('Y')
view(150.5,60)
grid on
title('Rotated To See Result')
.
댓글 수: 4
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!