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?
  • There have 4 points are using to create b-spline curve
  • Also have drawing the circle curve
  • Intersection function was using to identify there have any intersection between b-spline curve and circle curve or not
  • If it occur, this program will increase the axisy of P2 and P3 with 0.01 to ensure there have no intersection occur

% 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

Thank you for your generousity!

1 Answer 1

1

I don't understand why you have written:

abc = isempty (intersect);

while abc == 0

at the start of your loop and:

    if abc == 1;
        break
    end

end

towards its end. It would be clearer to write

while isempty(intersect)
    <loop contents>
end

wouldn't it ? That said, I don't think that's causing your loop to run for too long. What does your function curveintersect return if the curves do not intersect ? Bear in mind that

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

If that's not your problem, be more specific about what your problem is. What's 'too long' ? The code, or the time it takes to run the code ?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.