Plotting a circle and finding intersections
6 次查看(过去 30 天)
显示 更早的评论
I am trying to plot a circle using existing data I have and find the points where the circle intersect grid lines. I randomly picked a set of coordinates from the data to be used as the center point of the circle. However, the code I have isn't displaying the circle. When I run the code, it says there is an error in the line "plot(Q(1,:),Q(2,:),'.b')". Can anyone solve this problem? Thanks in advance!
nc = 1; % number of circle
r = 10; % radius
th = linspace(0,2*pi);
iwant = cell(nc,1);
for i = i:nc
% get center of circle
x = center(:,2); % x coordinate
y = center(:,3); % y coordinate
% circle coordinates
cx = x+r*cos(th);
cy = y+r*sin(th);
plot(cx,cy,'k');
% get intersection
Q = cell(1,[]) ;
count = 0 ;
% loop along lines parallel to x-axes
for j = 1:N
P = InterX([cx ;cy],[X(j,:) ; Y(j,:)]) ;
if ~isempty(P)
count = count+1 ;
Q{count} = P ;
end
end
% loop along lines parallel to y-axes
for k = 1:N
P = InterX([cx ;cy],[X(:,k)' ; Y(:,k)']) ;
if ~isempty(P)
count = count+1 ;
Q{count} = P ;
end
end
Q = cell2mat(Q) ;
plot(Q(1,:),Q(2,:),'.b')
iwant{i} = Q ;
end
3 个评论
回答(1 个)
TED MOSBY
2024-8-25
To find the points of intersection of a circle with the gridlines of the plot area you can use the “InterX” function of MATLAB.
Here is an example code to find the points of intersection of the circle with the grid lines:
% Example dataset: Replace this with your actual data
data = [1, 2; 3, 4; 5, 6; 7, 8; 9, 10]; % Each row is a point [x, y]
% Randomly select a point from the dataset to be the center
numPoints = size(data, 1); % Number of points in the dataset
randomIndex = randi(numPoints); % Randomly select an index
center = data(randomIndex, :); % Get the [x, y] coordinates of the center
% Display the chosen center
disp(['Chosen center: (', num2str(center(1)), ', ', num2str(center(2)), ')']);
% Define circle parameters
nc = 1; % number of circles
r = 10; % radius
th = linspace(0, 2*pi);
iwant = cell(nc, 1);
% Define grid
gridSize = 20; % Size of the grid
N = 10; % Number of grid lines
xGrid = linspace(-gridSize, gridSize, N);
yGrid = linspace(-gridSize, gridSize, N);
[X, Y] = meshgrid(xGrid, yGrid);
figure; % Create a new figure
hold on; % Ensure all plots are on the same figure
% Plot grid lines
for j = 1:N
plot([xGrid(1), xGrid(end)], [yGrid(j), yGrid(j)], 'g--'); % Horizontal lines
plot([xGrid(j), xGrid(j)], [yGrid(1), yGrid(end)], 'g--'); % Vertical lines
end
for i = 1:nc
% Get center of circle
x = center(1); % x coordinate
y = center(2); % y coordinate
% Circle coordinates
cx = x + r * cos(th);
cy = y + r * sin(th);
plot(cx, cy, 'k');
% Get intersection
Q = cell(1, []);
count = 0;
% Loop along lines parallel to x-axes
for j = 1:N
P = InterX([cx; cy], [X(j, :); Y(j, :)]);
if ~isempty(P)
count = count + 1;
Q{count} = P;
end
end
% Loop along lines parallel to y-axes
for k = 1:N
P = InterX([cx; cy], [X(:, k)'; Y(:, k)']);
if ~isempty(P)
count = count + 1;
Q{count} = P;
end
end
Q = cell2mat(Q);
plot(Q(1, :), Q(2, :), '.b');
iwant{i} = Q;
end
hold off; % Release the hold on the plot
% Display intersection points
disp('Intersection points:');
disp(Q);
Below is the output of the above code in my MATLAB R2023b:
Here is the documentation on the "interX" function:
Hope your query is resolved!
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Polar Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!