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?
  • 有 4 个点用于创建 b 样条曲线
  • 还有画圆曲线
  • 交集函数用于识别b样条曲线和圆曲线之间是否有任何交集
  • 如果发生,该程序将 P2 和 P3 的轴增加 0.01,以确保不发生相交

% 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

谢谢你的慷慨!

4

1 回答 1

1

我不明白你为什么写:

abc = isempty (intersect);

while abc == 0

在循环开始时,并且:

    if abc == 1;
        break
    end

end

接近尾声。写起来会更清楚

while isempty(intersect)
    <loop contents>
end

不会吗?也就是说,我认为这不会导致您的循环运行太久。curveintersect如果曲线不相交,你的函数会返回什么?请记住,

isempty([0]) ~= isempty([])

如果这不是您的问题,请更具体地说明您的问题是什么。什么是“太长”?代码,还是运行代码所需的时间?

于 2012-04-05T09:43:34.403 回答