Hello, I'm trying to plot several lines in a circle and need to trim the part of lines which are out of this circle. So how can I do that? Thank you in advance
% plotting random lines
X1 = rand (8,8);
Y1 = rand (8,8);
figure
plot (X1,Y1)
hold on
% plotting the circle
R = 0.5;
angle = linspace(0,2*pi,180);
x= R*cos(angle);
y= R*sin(angle);
plot(x,y,'r')
axis equal

6 Kommentare

Rik
Rik am 14 Mär. 2018
Can you provide an example of what you mean? And what did you try already?
Lisa Smith
Lisa Smith am 14 Mär. 2018
Hi Rik, As shown in the picture, I need to delete all lines that are out of the circle and keep that inside it.
Walter Roberson
Walter Roberson am 14 Mär. 2018
Most of the time there will be a segment of the line in which one point is inside the circle and the other point is outside. Do you want to drop those segments, or do you want to calculate the intersection and draw just to the edge of the circle?
Will there be any line segments where both points are outside the circle but the line crosses the circle?
Lisa Smith
Lisa Smith am 14 Mär. 2018
If a segment of line has one point inside the circle and the other point outside the circle then I want to calculate the intersection and draw just from the point located inside the circle to the circle's circumference. However, if the both points out the circle then I need to delete the whole line.
Walter Roberson
Walter Roberson am 15 Mär. 2018
https://www.mathworks.com/matlabcentral/fileexchange/22441-curve-intersections
https://www.mathworks.com/matlabcentral/fileexchange/11837-fast-and-robust-curve-intersections
https://www.mathworks.com/matlabcentral/fileexchange/8908-curve-intersect-2
Lisa Smith
Lisa Smith am 15 Mär. 2018
Thank you Walter, still even if I get the intersection points how to draw it, because they are totally randomly generated lines.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 15 Mär. 2018

0 Stimmen

Not necessarily the best way, but a way that would work:
Take the coordinates of the random points, and subtract from them the coordinates of the center of the circle. Convert the result to polar coordinates. Now create a logical mask of the points that have r less than the radius of the circle.
Now you want to look at the boundaries between groups of points that are inside or not. Look for every transition between false (outside) and true (inside), and every transition between true (inside) and false (outside)
first_outside_in_group = [false, mask(1:end) & ~mask(2:end-1)];
last_outside_in_group = [mask(2:end) & ~mask(1:end-1), false];
keep = mask | first_outside_in_group | last_outside_in_group;
adjusted_theta = theta;
adjusted_r = r;
adjusted_r(first_outside_in_group) = R_circle;
adjusted_r(last_outside_in_group) = R_circle;
adjusted_theta(~keep) = nan;
adjusted_r(~keep) = nan;
[adjusted_x, adjusted_y] = pol2cart(adjusted_theta, adjusted_r);
adjusted_x = adjusted_x + circle_center_x;
adjusted_y = adjusted_y + circle_center_y;
and now you can plot(adjusted_x, adjusted_y)

Weitere Antworten (0)

Kategorien

Mehr zu Line Plots finden Sie in Hilfe-Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by