Matlab - polygon and line intersection - matlab

Based on Matlab - Draw angles lines over circle and get the intersecting points
I tried the following code that should find the intersecting points and mark them. Unfortunately, it does not work. Do you have an idea how I can solve it?
Script:
clc;
clear;
close all;
r=1000;
nCircle = 1000;
t = linspace(0,2*pi,nCircle);
xCircle = 0+ r*sin(t);
yCircle = 0+ r*cos(t);
pgon = polyshape(xCircle,yCircle );
line(xCircle,yCircle );
axis equal;
hold on;
nAngles = 45;
lineLength = r+50;
for angle = 0:nAngles:359
xLine(1) = 0;
yLine(1) = 0;
xLine(2) = xLine(1) + lineLength * cosd(angle);
yLine(2) = yLine(1) + lineLength * sind(angle);
plot(xLine, yLine);
lineseg = [xLine; yLine];
intersectCoord = intersect(pgon,lineseg);
scatter(intersectCoord(2,2),intersectCoord(1,2));
end
The update script only check the last generated line but it still does not work with intersect:
clc;
clear;
close all;
r=1000;
nCircle = 1000;
t = linspace(0,2*pi,nCircle);
xCircle = 0+ r*sin(t);
yCircle = 0+ r*cos(t);
objectCircle = rot90([xCircle; yCircle]);
line(xCircle,yCircle);
axis equal;
hold on;
nAngles = 35;
lineLength = r+5;
for angle = 0:nAngles:360
disp(angle);
xLine(1) = 0;
yLine(1) = 0;
xLine(2) = xLine(1) + lineLength * cosd(angle);
yLine(2) = yLine(1) + lineLength * sind(angle);
plot(xLine, yLine);
lineseg = [xLine; yLine];
end
coefficients = polyfit([xLine(1), xLine(2)], [ yLine(1), yLine(2)], 1);
a = coefficients (1);
b = coefficients (2);
for xLineCoord = 0:lineLength
i=xLineCoord+1;
yLineCoord=a*xLineCoord +b ;
yLineCoordArray(i, :) = yLineCoord;
xLineCoordArray(i, :) = i;
end
objectLine = [xLineCoordArray, yLineCoordArray];
C = intersect(objectLine,objectCircle);

Related

add parameters to the figure in Matlab

I have a plot in Matlab as following, I want to add some parameters to the figure like the figure which I have added. Could you help me with how to solve that? Thank you so much.
delta = 0.5;
I1=2;
I2 =4;
T = 5;
tau = 0.5;
b = 8;
a = 6;
c = 8;
d = 4;
t = [0:0.001:5];
y =(a-b*exp(-tau*t)).*(t>=0 & t<delta*T )+(-d+c*exp(-tau*(t-delta*T))).*(t>=delta*T & t<=T);
plot(t,y , 'b', 'LineWidth',1.8)
hold on
x = [0:0.01:12];
max = 4*ones(1,1201);
min = -2*ones(1,1201);
z = 0*ones(1,1201);
plot(x,max , '--g', 'LineWidth',1)
hold on
plot(x,min , '--g', 'LineWidth',1)
plot(2,min , '--g', 'LineWidth',1)
plot(x,z , 'k', 'LineWidth',1.7)
axis([0 6 -4 6])
I think this code might generate something close for what you are looking. It is important to say that the arrows for x and y can be added using the same "annotation command".
delta = 0.5;
I1=2;
I2 =4;
T = 5;
tau = 0.5;
b = 8;
a = 6;
c = 8;
d = 4;
t = [0:0.001:5];
y =(a-b*exp(-tau*t)).*(t>=0 & t<delta*T )+(-d+c*exp(-tau*(t-delta*T))).*(t>=delta*T & t<=T);
plot(t,y , 'b', 'LineWidth',1.8,'DisplayName',"Data")
% Change the y limits
ylim([-4 6])
hold on
% %% Set the horizontal lines and its name in the y axis
% MAX LINE - Create a yline Cte along the plot
hLine = yline(4);
hLine.LineWidth = 1;
hLine.Color = "g";
hLine.LineStyle = "--";
hLine.DisplayName = "Max Line";
% MIN LINE - Create a yline Cte along the plot
hLine = yline(-2);
hLine.LineWidth = 1;
hLine.Color = "g";
hLine.LineStyle = "--";
hLine.DisplayName = "Min Line";
% Zero LINE - Create a yline Cte along the plot
hLine = yline(0);
hLine.LineWidth = 1.7;
hLine.Color = "k";
hLine.LineStyle = "-";
% Change the "name" in the y axis
hAxis = gca;
% Get index
% - when y is equal to "Max" in this case 4
% - when y is equal to "MIN" in this case -2
idx = hAxis.YTick == 4;
hAxis.YTickLabel{idx} = 'X';
idx = hAxis.YTick == -2;
hAxis.YTickLabel{idx} = 'Y';
% %% Set the vertical lines and its name in the x axis
% Delta LINE - Create a xline Cte along the plot
hLine = xline(2.5);
hLine.LineWidth = 1;
hLine.Color = "k";
hLine.LineStyle = "-";
% MIN LINE - Create a yline Cte along the plot
hLine = xline(4);
hLine.LineWidth = 1;
hLine.Color = "k";
hLine.LineStyle = "-";
% Add the notes
annotation('textbox',[.55 .2 .3 .3],"String","Delta",...
'FitBoxToText','on','LineStyle','none')
annotation('textbox',[0.8 0.2 .3 .3],"String","A",...
'FitBoxToText','on','LineStyle','none')
The previous code will generate the next figure.

saved movie in matlab cannot be played

I have created a movie in matlab and I want to save it. I wrote below code to do that, it shows the movie when the code is running correctly but the file which have been saved, can not be played. The error is: "could not determine type of stream"
clear all
close all
l = 27;
r = 4; noise_t = 0.02; v0 = 0.1; nn = 300;
x = rand(1,nn).*l; y = rand(1,nn).*l; vx = 0.1.*(rand(1,nn)-0.5.*ones(1,nn)); vy = 0.1.*(rand(1,nn)-0.5.*ones(1,nn));
figure('Color',[0 0 0])
axis([0 115 0 40])
axis('square')
hold on
vidObj = VideoWriter('che.avi');
open(vidObj);
time = 900;
neigh=[];
eigh=[];
for t = 1:time;
eig=[];
for number = 1:nn;
distance(1:nn) = (ones(1,nn).*x(number)-x).^2 + (ones(1,nn).*y(number)-y).^2;
neighbour_s = distance < r^2;
eig(1,nn) = sum(neighbour_s);
a = dot(double(neighbour_s),vx)/(sum(neighbour_s));
b = dot(double(neighbour_s),vy)/(sum(neighbour_s));
vx1(number) = a/norm([b,a]); vy1(number) = b/norm([b,a]); mvx(number) = vx1(number); mvy(number) = vy1(number);
end
vx_center = noise_t .*(rand(1,nn)-0.5); vy_center = noise_t .*(rand(1,nn)-0.5);
vx = mvx + vx_center; vy = mvy + vy_center;
x = x + vx; y = y + vy;
index = (x < 0); x(index) = rem(x(index),l) + l;
index = (y < 0); y(index) = rem(y(index),l) + l;
index = (x > l); x(index) = rem(x(index),l);
index = (y > l); y(index) = rem(y(index),l);
eigh = [eigh;eig];
cla
vecc=eigh(:);
vecc= vecc(find(vecc>0));
hist(vecc,80)
drawnow
currFrame = getframe;
writeVideo(vidObj,currFrame);
drawnow
end
It is necessary for you to explicitly close the VideoWriter object using the close method when you are done writing new frames to it. This will finalize the video and make it playable.
% At the end of your script
close(vidObj)

MATLAB 3D lines are invisible

Hello I'am suffered from a problem
As you can see I want a draw 3D graph.
Problem is when I draw sphere lines are invisible.
Here is simple version of my source
clear all; close all; clc
n=1;
n_inner_drone=3;
n_outter_drone=2;
length=100;
initial_d = zeros(1,n);
inner_x = zeros(n_inner_drone,n);
inner_y = zeros(n_inner_drone,n);
inner_z = zeros(n_inner_drone,n);
outter_x = zeros(n_outter_drone,n);
outter_y = zeros(n_outter_drone,n);
outter_z = zeros(n_outter_drone,n);
radius= length;
disp('test');
%%%%%%%%%%%%%%%%%%%%%% Sphere
% figure()
% [x, y, z] = sphere;
% h = surfl(x*length, y*length, z*length);
% hSurf = surf(X,Y,Z,'EdgeColor','none','LineStyle','none','FaceLighting','phong');
% set(h, 'FaceAlpha', 0.05)
% surf(x*length, y*length, z*length,
% shading interp
hold on
%%%%%%%%%%%%%%%%%%%%%%%%%
for i=1:n_inner_drone
k=1;
while 1
x_temp= randi([-length, length], 1, 1);
y_temp= randi([-length, length], 1, 1);
z_temp= randi([-length, length], 1, 1);
dist = sqrt(x_temp^2 + y_temp^2 + z_temp^2);
if dist<radius
if i==1
initial_d(k) = dist;
end
inner_x(i,k) = x_temp;
inner_y(i,k) = y_temp;
inner_z(i,k) = z_temp;
k = k+1;
end
if k == n+1, break, end
end
end
ideal_direction_length = ones(1,n);
ideal_direction_length = ideal_direction_length * length;
ideal_direction_length = ideal_direction_length - initial_d;
k=1;
random_x = inner_x(1,:);
random_y = inner_y(1,:);
random_z = inner_z(1,:);
random_moving_distance = zeros(1,n);
moving_distance = 0;
trigger = 0;
while 1
if trigger == 0
direction = randi([1, 6], 1, 1);
trigger = 1;
end
if direction == 1
random_x(k) = random_x(k) + 1;
elseif direction == 2
random_x(k) = random_x(k) - 1;
elseif direction == 3
random_y(k) = random_y(k) + 1;
elseif direction == 4
random_y(k) = random_y(k) - 1;
elseif direction == 5
random_z(k) = random_z(k) + 1;
elseif direction == 6
random_z(k) = random_z(k) - 1;
end
dist = sqrt(random_x(k)^2 + random_y(k)^2 + random_z(k)^2);
moving_distance = moving_distance+1;
%%%%%%%%%% Line
plot3(random_x(n),random_y(n),random_z(n),'k+')
%%%%%%%%%%%%%%%
if dist>radius
random_moving_distance(k) = moving_distance;
k = k+1;
moving_distance = 0;
trigger = 0;
end
if k == n+1, break, end
end
plot3(inner_x(1,n),inner_y(1,n),inner_z(1,n),'r*')
for k=2:n_inner_drone
plot3(inner_x(k,n),inner_y(k,n),inner_z(k,n),'b*')
end
for k=1:n_outter_drone
plot3(outter_x(k,n),outter_y(k,n),outter_z(k,n),'k*')
end
At the first, I suspected I worngly draw lines, but without sphere I can see lines as fig2.
Those anyone who knows about this problem.
Please answer to me and I will very appericiate about it.
Thanks for reading.
I think it is because:
plot3(gravity_x(n),gravity_y(n),gravity_z(n))
is not a line. Its a single point.
plot3(gravity_x(n:n+1),gravity_y(n:n+1),gravity_z(n:n+1))
is a line.

how to Improve the speed matlab

This is my matlab code. It runs too slow and I had no clue how to improve it.
Could you help me to improve the speed?
What I would like to do is to create some random points and then remove the random points to make them similar to my target points.
syms Dx Dy p q;
a = 0;
num = 10;
x = rand(1,num);
y = rand(1,num);
figure(1)
scatter(x,y,'.','g')
%num_x = xlsread('F:\bin\test_2');% num 1024
%figure(2)
%scatter(num_x(:,1),num_x(:,2),'.','r');
q = 0;
num_q = 10;
x_q = randn(1,num_q);
y_q = randn(1,num_q);
%figure(2)
hold on;
scatter(x_q,y_q,'.','r')
for i = 1:num_q;
for j = 1:num_q;
qx(i,j) = x_q(i) - x_q(j);
qy(i,j) = y_q(i) - y_q(j);
%qx(i,j) = num_x(i,1) - num_x(j,1);
%qy(i,j) = num_x(i,2) - num_x(j,2);
%d~(s(i),s(j))
if ((qx(i,j))^2+(qy(i,j)^2))> 0.01 % find neighbours
qx(i,j) = 0;
qy(i,j) = 0;
end
end
end
for i = 1:num_q;
for j = 1:num_q;
if qx(i,j)>0&&qy(i,j)>0
q = q + exp(-(((Dx - qx(i,j))^2)+((Dy - qy(i,j))^2))/4);%exp(-(((Dx - qx(i,j))^2)+((Dy - qy(i,j))^2))/4);
end
end
end
%I = ones(num,num); % I(s) should from a grayscale image
%r = 1./sqrt(I);
for s = 1:100;
for i = 1:num;
for j = 1:num;
dx(i,j) = x(i) - x(j);
dy(i,j) = y(i) - y(j);
%d~(s(i),s(j))
if ((dx(i,j))^2+(dy(i,j)^2))> 0.05 % delta p, find neighbours
dx(i,j) = 0;
dy(i,j) = 0;
end
end
end
p = 0;
for i = 1:num;
for j = 1:num;
if dx(i,j)>0&&dy(i,j)>0
p = p + exp(-(((Dx - dx(i,j))^2)+((Dy - dy(i,j))^2))/4);
end
end
end
p = p - q;
sum = 0;
for i = 1:num;
for j = 1:num;
if dx(i,j)>0&&dy(i,j)>0;
kx(i,j) = (1/2)*(Dx-dx(i,j))*exp((-(Dx-dx(i,j))^2+(Dy-dy(i,j))^2)/4);
ky(i,j) = (1/2)*(Dy-dy(i,j))*exp((-(Dx-dx(i,j))^2+(Dy-dy(i,j))^2)/4);
end
end
end
sum_x = ones(1,num);% 1行N列0矩阵
sum_y = ones(1,num);
%fx = zeros(1,num);
for i = 1:num;
for j = 1:num;
if dx(i,j)>0&&dy(i,j)>0;
fx(i) = p*kx(i,j);% j is neighbour to i
fy(i) = p*ky(i,j);
%fx(i) = matlabFunction(fx(i));
%fy(i) = matlabFunction(fy(i));
%P =quad2d(#(Dx,Dy) fx,0,0.01,0,0.01);
%fx =quad(#(Dx) fx,0,0.01);
%fx(i) =quad(#(Dy) fx(i),0,0.01);
%Q =quad2d(#(Dx,Dy) fy,0,0.01,0,0.01);
fx(i) = double(int(int(fx(i),Dx,0,0.01),Dy,0,0.01));
fy(i) = double(int(int(fy(i),Dx,0,0.01),Dy,0,0.01));
%fx(i) = vpa(p*kx(i,j));
%fy(i) = vpa(p*ky(i,j));
%fx(i) = dblquad(#(Dx,Dy)fx(i),0,0.01,0,0.01);
%fy(i) = dblquad(#(Dx,Dy)fy(i),0,0.01,0,0.01);
sum_x(i) = sum_x(i) + fx(i);
sum_y(i) = sum_y(i) + fy(i);
end
end
end
for i = 1:num;
sum_x = 4.*sum_x./num;
sum_y = 4.*sum_y./num;
x(i) = x(i) - 0.05*sum_x(i);
y(i) = y(i) - 0.05*sum_y(i);
end
a = a+1
end
hold on;
scatter(x,y,'.','b')
The fast version of your loop should be something like:
qx = bsxfun(#minus, x_q.', x_q);
qy = bsxfun(#minus, y_q.', y_q);
il = (qx.^2 + qy.^2 >= 0.01);
qx(il) = 0;
qy(il) = 0;
il = qx>0 && qy>0;
q = sum(exp(-((Dx-qx(il)).^2 + (Dy-qy(il)).^2)/4));
%// etc. for vectorization of the inner loops

How to show 40 gabor filter in matlab

can someone help me how to show gabor filter in matlab, i can show it but its not what i want. this is my code :
[Gf,gabout] = gaborfilter1(B,sx,sy,f,theta(j));
G{m,n,i,j} = Gf;
and this is gabor filter class:
function [Gf,gabout] = gaborfilter(I,Sx,Sy,f,theta);
if isa(I,'double')~=1
I = double(I);
end
for x = -fix(Sx):fix(Sx)
for y = -fix(Sy):fix(Sy)
xPrime = x * cos(theta) + y * sin(theta);
yPrime = y * cos(theta) - x * sin(theta);
Gf(fix(Sx)+x+1,fix(Sy)+y+1) = exp(-.5*((xPrime/Sx)^2+(yPrime/Sy)^2))*cos(2*pi*f*xPrime);
end
end
Imgabout = conv2(I,double(imag(Gf)),'same');
Regabout = conv2(I,double(real(Gf)),'same');
gabout = sqrt(Imgabout.*Imgabout + Regabout.*Regabout);
Then, I imshow with this code:
imshow(G{m,n,i,j},[]);
and the results :
But i want this result, can someone help me how to slove this?
Use the following function. I hope this is useful.
----------------------------------------------------------------
gfs = GaborFilter(51,0.45,0.05,6,4);
n=0;
for s=1:6
for d=1:4
n=n+1;
subplot(6,4,n)
imshow(real(squeeze(gfs(s,d,:,:))),[])
end
end
Sample Image
----------------------------------------------------------------
function gfs = GaborFilter(winLen,uh,ul,S,D)
% gfs(SCALE, DIRECTION, :, :)
winLen = winLen + mod(winLen, 2) -1;
x0 = (winLen + 1)/2;
y0 = x0;
if S==1
a = 1;
su = uh/sqrt(log(4));
sv = su;
else
a = (uh/ul)^(1/(S-1));
su = (a-1)*uh/((a+1)*sqrt(log(4)));
if D==1
tang = 1;
else
tang = tan(pi/(2*D));
end
sv = tang * (uh - log(4)*su^2/uh)/sqrt(log(4) - (log(4)*su/uh)^2);
end
sx = 1/(2*pi*su);
sy = 1/(2*pi*sv);
coef = 1/(2*pi*sx*sy);
gfs = zeros(S, D, winLen, winLen);
for d = 1:D
theta = (d-1)*pi/D;
for s = 1:S
scale = a^(-(s-1));
gab = zeros(winLen);
for x = 1:winLen
for y = 1:winLen
X = scale * ((x-x0)*cos(theta) + (y-y0)*sin(theta));
Y = scale * (-(x-x0)*sin(theta) + (y-y0)*cos(theta));
gab(x, y) = -0.5 * ( (X/sx).^2 + (Y/sy).^2 ) + (2*pi*1j*uh)*X ;
end
end
gfs(s, d, :, :) = scale * coef * exp(gab);
end
end
Replace the "cos" component by complex part->complex(0, (2*pi*f*xprime)) ans also multiply equation by scaling factor of (1/sqrt(2*Sy*Sx)).