How to remove lines' segments located outside square?

조회 수: 8 (최근 30일)
Eman S
Eman S 2018년 9월 10일
편집: Eman S 2020년 2월 2일
Hi all, The following code is used to draw multiple lines and square of area (1x1);
%%PLOT lines
deg = 0 : 5 : 90;
r=2;
x_ax = r*[zeros(size(deg(:))) cosd(deg(:))];
y_ax = r*[zeros(size(deg(:))) sind(deg(:))];
plot(x_ax', y_ax', 'LineWidth',1)
hold on
%%Plot square
x1=0;
x2=1;
y1=0;
y2=1;
x = [x1, x2, x2, x1, x1];
y = [y1, y1, y2, y2, y1];
plot(x, y, 'b-', 'LineWidth', 2.5);
grid on
grid minor
axis equal
xlim([-0.2,1.2])
ylim([-0.2,1.2])
The output would be as the following image;
My question is;
How to remove the multiple lines' segments that are located outside the (1x1) square?
So, the output should be like that;
Any suggestions for code to do that?
  댓글 수: 1
Image Analyst
Image Analyst 2020년 2월 1일
Original question by Erman, in case he also deletes this one:
How to remove the multiple lines' segments that are located outside the (1x1) square?
Hi all, The following code is used to draw multiple lines and square of area (1x1);
%%PLOT lines
deg = 0 : 5 : 90;
r=2;
x_ax = r*[zeros(size(deg(:))) cosd(deg(:))];
y_ax = r*[zeros(size(deg(:))) sind(deg(:))];
plot(x_ax', y_ax', 'LineWidth',1)
hold on
%%Plot square
x1=0;
x2=1;
y1=0;
y2=1;
x = [x1, x2, x2, x1, x1];
y = [y1, y1, y2, y2, y1];
plot(x, y, 'b-', 'LineWidth', 2.5);
grid on
grid minor
axis equal
xlim([-0.2,1.2])
ylim([-0.2,1.2])
The output would be as the following image;
My question is;
How to remove the multiple lines' segments that are located outside the (1x1) square?
So, the output should be like that;
Any suggestions for code to do that?

댓글을 달려면 로그인하십시오.

채택된 답변

KSSV
KSSV 2018년 9월 11일
clc; clear all ;
%%PLOT lines
deg = 0 : 5 : 90;
r=2;
x_ax = r*[zeros(size(deg(:))) cosd(deg(:))];
y_ax = r*[zeros(size(deg(:))) sind(deg(:))];
plot(x_ax', y_ax', 'LineWidth',1)
hold on
%%Plot square
x1=0;
x2=1;
y1=0;
y2=1;
x = [x1, x2, x2, x1, x1];
y = [y1, y1, y2, y2, y1];
plot(x, y, 'b-', 'LineWidth', 2.5);
grid on
grid minor
axis equal
xlim([-0.2,1.2])
ylim([-0.2,1.2])
%%Get points lying inisde square
figure
hold on
plot(x, y, 'b-', 'LineWidth', 2.5);
grid on
grid minor
axis equal
xlim([-0.2,1.2])
ylim([-0.2,1.2])
t = linspace(0,1,1000)' ;
for i = 1:size(x_ax,1)
xi = (1-t)*x_ax(i,1)+t*x_ax(i,2) ;
yi = (1-t)*y_ax(i,1)+t*y_ax(i,2) ;
%
idx = inpolygon(xi, yi,x', y') ;
plot(xi(idx), yi(idx))
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Grid Lines, Tick Values, and Labels에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by