2012-04-05 5 views
0
I have matlab programming below. It too long and repeated when I want to use while loops. So there have any ideas how to simplify this programming? 
  • Es hat 4 Punkte verwenden zu erstellen B-Spline-Kurve
  • Auch haben zeichnen den Kreis Kurve
  • Überschneidung Funktion wurde mit zu identifizieren, da irgendeine Kreuzung vereinfachen zwischen B-Spline-Kurve und Kreis Kurve oder nicht
  • Wenn es dieses Programm auftritt, wird die axisy von P2 und P3 mit 0,01 erhöht es keine Kreuzung, um sicherzustellen, hat auftritt

% CONTROL POINTS OF B-SPLINE CURVE 
P1 = [0,0]; 
P2 = [40,25]; 
P3 = [60,25]; 
P4 = [100,0]; 

% ADDITIONAL CONTROL POINTS FOR COMPLETING THE SEGMENTS 
P0x = (2*P1(1,1))-P2(1,1); 
P0y = (2*P1(1,2))-P2(1,2); 

Z1 = [P0x, P0y]; 

P5x = (2*P4(1,1))-P3(1,1); 
P5y = (2*P4(1,2))-P3(1,2); 

Z2 = [P5x, P5y]; 

global Vx1 Vy1 Vx2 Vy2 Vx3 Vy3 

for cc = 0; 

Vx1 = [Z1(1,1) P1(1,1) P2(1,1) P3(1,1)]'; 
Vy1 = [Z1(1,2) P1(1,2) P2(1,2) P3(1,2)]'; 

[C1 C2] = mybspline (Vx1, Vy1); 

Vx2 = [P1(1,1) P2(1,1) P3(1,1) P4(1,1)]'; 
Vy2 = [P1(1,2) P2(1,2) P3(1,2) P4(1,2)]'; 

[C3 C4] = mybspline (Vx2, Vy2); 

Vx3 = [P2(1,1) P3(1,1) P4(1,1) Z2(1,1)]'; 
Vy3 = [P2(1,2) P3(1,2) P4(1,2) Z2(1,2)]'; 

[C5 C6] = mybspline (Vx3, Vy3);         

end 

% 
A=[C1',C2';C3',C4';C5',C6']; %Red points - curve segment 
B=[Vx1,Vy1;Vx2,Vy2;Vx3,Vy3]; %Blue points - control points 

%-------------------------------------------------------------------------- 
%CIRCLE 

Xc = 50;    %Center for x 
Yc = 0;    %Center for y 
R = 25;    %R, radius of circle 
x = 0:0.01:1;   %x vector 
y = 0:0.01:1;   %y vector 
C = Xc+R*cos(pi*x)'; 
D = Yc+R*sin(pi*y)'; 
Point_circle = [C D]; 

E = A(:,1); % x axis data of B-Spline curve 
F = A(:,2); % y axis data of B-Spline curve 

%-------------------------------------------------------------------------- 
% FIND INTERSECTION BETWEEN CURVE 

[intersect_x,intersect_y] = curveintersect(A(:,1),A(:,2),C,D); 
intersect = [intersect_x, intersect_y]; 
figure(101) 
plot(A(:,1),A(:,2),'k',C,D,'b',intersect_x,intersect_y,'ro') 

abc = isempty (intersect); 

while abc == 0 
    P2(1,2) = P2(1,2)+0.01; 
    P3(1,2) = P3(1,2)+0.01; 

    % ADDITIONAL CONTROL POINTS FOR COMPLETING THE SEGMENTS 
P0x = (2*P1(1,1))-P2(1,1); 
P0y = (2*P1(1,2))-P2(1,2); 

Z1 = [P0x, P0y]; 

P5x = (2*P4(1,1))-P3(1,1); 
P5y = (2*P4(1,2))-P3(1,2); 

Z2 = [P5x, P5y]; 

global Vx1 Vy1 Vx2 Vy2 Vx3 Vy3 

for cc = 0; 

Vx1 = [Z1(1,1) P1(1,1) P2(1,1) P3(1,1)]'; 
Vy1 = [Z1(1,2) P1(1,2) P2(1,2) P3(1,2)]'; 

[C1 C2] = mybspline (Vx1, Vy1); 

Vx2 = [P1(1,1) P2(1,1) P3(1,1) P4(1,1)]'; 
Vy2 = [P1(1,2) P2(1,2) P3(1,2) P4(1,2)]'; 

[C3 C4] = mybspline (Vx2, Vy2); 

Vx3 = [P2(1,1) P3(1,1) P4(1,1) Z2(1,1)]'; 
Vy3 = [P2(1,2) P3(1,2) P4(1,2) Z2(1,2)]'; 

[C5 C6] = mybspline (Vx3, Vy3);         

end 

% 
A=[C1',C2';C3',C4';C5',C6']; %Red points - curve segment 
B=[Vx1,Vy1;Vx2,Vy2;Vx3,Vy3]; %Blue points - control points 

[intersect_x,intersect_y] = curveintersect(A(:,1),A(:,2),C,D); 
intersect = [intersect_x, intersect_y]; 
figure(101) 
plot(A(:,1),A(:,2),'k',C,D,'b',intersect_x,intersect_y,'ro') 

abc = isempty (intersect); 

    if abc == 1; 
     break 
    end 
end 

Vielen Dank für Ihre Großzügigkeit!Wie Matlab-Programmierung für While-Schleife

Antwort

1

Ich verstehe nicht, warum Sie geschrieben haben:

abc = isempty (intersect); 

while abc == 0 

am Anfang der Schleife und:

if abc == 1; 
     break 
    end 

end 

zu seinem Ende. Es wäre klarer zu schreiben

while isempty(intersect) 
    <loop contents> 
end 

nicht wahr? Das heißt, ich glaube nicht, dass Ihre Schleife dadurch zu lange läuft. Was gibt Ihre Funktion curveintersect zurück, wenn sich die Kurven nicht schneiden? Beachten Sie, dass

Wenn das nicht Ihr Problem ist, genauer zu sein, was Ihr Problem ist. Was ist zu lang? Der Code oder die Zeit, die zum Ausführen des Codes benötigt wird?