Plotting noncontinuous arc segments
3 次查看(过去 30 天)
显示 更早的评论
Hey all,
I am trying to plot a non-continuous arc of a circle, given the center, radius and angle ranges of the arc(s). For instance, say I want to plot the following angle range:
0,...,0.75*pi,1.25*pi,...,2*pi
There's a gap between 0.75*pi and 1.25*pi. I don't want MATLAB to plot anything "between" these two angles, but it plots a straight vertical line.
This the basic structure of my code:
t=[];
for theta=0:0.01:2*pi
if % irrelevant condition that decides if the arc covers this angle
t=[t theta];
end
end
plot(a1*cos(t),a1*sin(t),'b--')
And here is the figure:
I want to 'skip' the vertical line. Is this possible?
Any kind of assistance would be greatly appreciated!
0 个评论
采纳的回答
John D'Errico
2015-4-15
编辑:John D'Errico
2015-4-15
You ARE plotting 0 to 2*pi. That is 360 degrees. You have an x,y, pair at EVERY one of those points. Then you pass those numbers to plot. How should it know that even though you told it to plot those points, that you don't really mean it?
You CAN buy the mind reading toolbox when it comes out, but I hear that it will be incredibly expensive.
Better is to not create values for t for those points. Simpler is to fill t with NaNs when that test fails.
Finally, BEST is to preallocate your array in advance. This will be seriously faster. And one day if you never learn to preallocate your vectors and arrays in cases like this, you will come back to this forum, and ask why is your code SOOOOO slow!
theta = 0:0.01:2*pi;
t = theta;
for ind = 1:numel(theta)
if % irrelevant condition that decides if the arc does NOT cover this angle
t(ind) = NaN;
end
end
plot(a1*cos(t),a1*sin(t),'b--')
Better yet would be to build t in a vectorized way, but since you have told us nothing more than that the test is irrelevant, I can't help you there. It might be something as simple as this:
t = linspace(-3*pi/4,3*pi/4,100);
a1 = 2;
plot(a1*cos(t),a1*sin(t),'b--')
axis equal
Learn to write code that uses the capabilities of MATLAB in an efficient way.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Performance 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!