How to remove lines' segments located outside square?

4 views (last 30 days)
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 Comment
Image Analyst
Image Analyst on 1 Feb 2020
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?

Sign in to comment.

Accepted Answer

KSSV
KSSV on 11 Sep 2018
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

More Answers (0)

Categories

Find more on Graphics Object Properties in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!