- https://mathworks.com/help/matlab/ref/polyshape.html
- https://mathworks.com/help/matlab/ref/polyshape.plot.html
simulation of the target
조회 수: 36 (최근 30일)
이전 댓글 표시
Hello everyone,
I'm working on a simulation of the following target using MATLAB. Fortunately, I succeeded in generating the profile, but there is one thing left that I'm still trying to achieve. I need to have a smooth transition between the circles and the flat section, as shown in the picture.
Please, if you could provide me with any information, I would be very grateful.
Thank you in advance.
댓글 수: 0
답변 (2개)
Madheswaran
2024년 11월 20일 11:28
Hello @bouchra turki
To achieve a seamless transition between the 'rectangular' and 'circular' sections of your plot, consider plotting an arc to bridge them. The following code, inspired by a MATLAB Central discussion, demonstrates how to create such an arc: https://mathworks.com/matlabcentral/answers/367126-plot-an-arc-on-a-2d-grid-by-given-radius-and-end-points
Once the arcs are constructed, you can utilize the polyshape function to connect the top and bottom arcs, allowing you to fill the area between them. An added benefit of using polyshape is its ability to translate a polygon for reuse on other sides.
Here is the implementation for the described approach:
function [x, y] = bottomArc(startPoint, endPoint, radius)
d = norm(endPoint-startPoint);
C = (endPoint+startPoint)/2+sqrt(radius^2-d^2/4)/d*[0,-1;1,0]*(endPoint-startPoint);
a = atan2(startPoint(2)-C(2),startPoint(1)-C(1));
b = atan2(endPoint(2)-C(2),endPoint(1)-C(1));
b = mod(b-a,2*pi)+a; % Ensure that arc moves counterclockwise
t = linspace(a,b,1000);
x = C(1)+radius*cos(t);
y = C(2)+radius*sin(t);
end
function [x, y] = topArc(startPoint, endPoint, radius)
d = norm(endPoint-startPoint);
C = (endPoint+startPoint)/2+sqrt(radius^2-d^2/4)/d*[0,-1;1,0]*(endPoint-startPoint);
a = atan2(startPoint(2)-C(2),startPoint(1)-C(1));
b = atan2(endPoint(2)-C(2),endPoint(1)-C(1));
b = mod(b-a,2*pi)+a; % Ensure that arc moves counterclockwise
t = linspace(a,b,1000);
x = C(1)+radius*cos(t);
y = C(2)+radius*sin(t);
end
figure;
rectangle("Position", [3, -1, 20, 2], "FaceColor", [0, 0, 1], "EdgeColor", "none");
hold on
rectangle("Position", [0, -2, 4, 4], "FaceColor", [0, 0, 1], "Curvature", [1 1], "EdgeColor", "none");
rectangle("Position", [21, -2, 4, 4], "FaceColor", [0, 0, 1], "Curvature", [1 1], "EdgeColor", "none");
[xtop, ytop] = topArc([2.8;1.8], [8; 1], 15);
[xbot, ybot] = bottomArc([8; -1], [2.8;-1.8], 15);
x_arc = [xtop xbot];
y_arc = [ytop ybot];
left = polyshape(x_arc, y_arc);
right = translate(left, 21, 0);
right = rotate(right, 180, [23, 0]);
plot(left, "FaceColor", [0 0 1], "FaceAlpha", 1, "EdgeColor", "none");
plot(right, "FaceColor", [0 0 1], "FaceAlpha", 1, "EdgeColor", "none");
axis equal
xlim([-5 30])
ylim([-2.5 2.5])
xlabel('X (\mum)')
ylabel('Y (\mum)')
grid on;
You can modify the starting point, ending point and radius to change the look and feel of the arcs.
For more information, refer to the following MathWorks documentations:
Hope this solution is helpful to you!
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Elementary Polygons에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!