0

Currently trying to neaten up some code, i want to add the curves into a single array and then simply use that array to plot rather than separate calls, this will also help me fill at a later date. I've got my array but for some reason i'm getting a strange output which is different to what i expect with a random line cutting down the middle.

Tried to change the order the array is populated with curves to no effect

% Outer Spade
% Lower Curve Control Points
p1 = [0, 0, 0.5, 0.6;
     -1, 0, 0.1,  0];
p2 = refy(p1);
% Higher Curve Control Points
p3 = [0.6, 0.75, 0.75 , 0;
      0,  0.15, 0.4 ,   1]; 
p4 = refy(p3);


outer = [];


t = linspace(0,1,100);


C1 = cubic_bezier(p1,t);
C2 = cubic_bezier(p2,t);
C3 = cubic_bezier(p3,t);
C4 = cubic_bezier(p4,t);

outer =[outer C1,C3,C2,C4];

plot(outer(1,:),outer(2,:),'-b')

axis([-2 2 -1 3])






function C = cubic_bezier(P,t)
M = [-1,3,-3,1;
     3,-6,3,0;
    -3,3,0,0;
     1,0,0,0];

T = [t.^3;t.^2;t;t.^0];
C = P * M * T;
end

function r = refy(m)
r = [-1,0;0,1] * m;
end

If i replace the outer and plot outer instead with the following code

hold on
plot(C1(1,:),C1(2,:))
plot(C2(1,:),C2(2,:))
plot(C3(1,:),C3(2,:))
plot(C4(1,:),C4(2,:))

Then i get what i expected, hope this helps. Thank you in advance

Dead_Ling0
  • 171
  • 1
  • 1
  • 13

1 Answers1

3

With C1, C3, C2 and C4 concatenated into a single array, i.e. outer, and when you use plot to plot line between two consecutive points, a previous point gets connected to its next point.

You have outer(:,200) = [-1.1102e-16 ; 1] and outer(:,201) = [0; -1]. When you use plot, a line gets plotted between these two points as well just like the lines are plotted between the other points. This line is what you call "a random line cutting down the middle". If you want to keep a single array and avoid this, you can concatenate a column of NaNs in between like this:

outer = [C1, nan(2,1), C3, nan(2,1), C2, nan(2,1), C4];

which gives:

output

When you plot C1, C2, C3 and C4 separately, the ending point of each have no connection with the starting point of the other and hence the plots are independent of each other and you do not get that line in the middle.

Sardar Usama
  • 19,536
  • 9
  • 36
  • 58
  • Thank you for pointing this out I've noticed when i change this values the line persists though, not sure what I'm doing wrong i feel really stupid at this point :( – Dead_Ling0 Feb 01 '19 at 20:32
  • What change did you do with which the line persisted? – Sardar Usama Feb 01 '19 at 20:33
  • I manually changed the values so that they were the same and correct ! it's strange because i have done the exact same thing for another shape with no issues. – Dead_Ling0 Feb 01 '19 at 20:35
  • Even when i use the nan i still get the line even though you don't have the line :? – Dead_Ling0 Feb 01 '19 at 20:37
  • In another shape, you have the next point in some symmetric continuity to the previous point. In this shape, you have not. The behavior is very expected. – Sardar Usama Feb 01 '19 at 20:37
  • 1
    There is no way you be getting that line with `NaN`s. `clear outer` and initialise it again like I showed and `plot` again – Sardar Usama Feb 01 '19 at 20:38
  • Resolved it now thank you for being patient with me :) so the issue was that plot was just adding an extra point? – Dead_Ling0 Feb 01 '19 at 20:41
  • Ah, there is no extra point added by `plot` itself. The answer to your above comment is in the first two paragraphs of the answer. – Sardar Usama Feb 01 '19 at 20:42