![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/985935/image.png)
How to map a point inside a circle to circle area in MATLAB?
2 次查看(过去 30 天)
显示 更早的评论
Hi I have a point inside a circle in MATLAB. I want to map it to circle environment. I did this but it is not always useful!
function [xr,yr] = FindSurroundingPoint(xc,yc,x2,y2,r)
x=x2-xc;
y=y2-yc;
angle=atan(y/x);
if(x<0 && y<0)||(x<0&&y>0)
angle = angle + pi;
end
xr=cos(angle)*r + xc;
yr=sin(angle)*r + yc;
end
0 个评论
回答(1 个)
Image Analyst
2022-5-3
Not sure what you're after, but perhaps this will help. It looks like you're trying to find where the line between two points intersects a circle around the first point.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
markerSize = 40;
% Define parameters
xc=2;
yc=2;
x2=3;
y2=4;
r=1;
% Plot diagram of points and circle.
plot(xc, yc, 'b.', 'MarkerSize', markerSize)
grid on;
hold on;
plot(x2, y2, 'b.', 'MarkerSize', markerSize)
viscircles([xc, yc], r);
axis equal
xlim([0, 4]);
ylim([0, 5]);
% Find the point where the circle intersects
% the line going between the two points.
[xr, yr] = FindSurroundingPoint(xc,yc,x2,y2,r)
% Put line at yr to see where line intersects the circle.
yline(yr, 'Color', 'r', 'LineWidth', 2);
% Put line at xr to see where line intersects the circle.
xline(xr, 'Color', 'r', 'LineWidth', 2);
caption = sprintf('(xr, yr) = (%.3f, %.3f)', xr, yr);
title(caption, 'FontSize',fontSize);
%===================================================================
function [xr, yr] = FindSurroundingPoint(xc,yc,x2,y2,r)
deltax = x2 - xc
deltay = y2 - yc
line([xc, x2], [yc, y2], 'Color', 'b')
angle = atand(deltay/deltax)
if(deltax<0 && deltay<0) || (deltax<0&&deltay>0)
angle = angle + 180
end
xr = cosd(angle) * r + xc
yr = sind(angle) * r + yc
end
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/985935/image.png)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Object Programming 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!