1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
|
function p_handle = hatching(f,g,xx,option)
% Hatching Area between two graphics
% f,g : functions
% xx : graphic support
% option : ploting options
% See 'DEMO MODE' for exemples
% Hassan Ijtihadi
% ijtihah6 at gmail dot com
% v1.0 9/04/2008
if nargin < 4
% CHANGE OPTIONS HERE
option.angle = 0.25*pi; % 45°
option.size = 40;
option.margin = 0.1;
option.color = 'red';
end
if nargin < 3
%---------------------------
% DEMO MODE
p = floor(3*rand()-eps)+1;
switch p
case 1
f = @cos;
g = @sin;
xx = 0:0.05:2*pi;
case 2
f = @(x)(x.^1.5);
g = @sin;
xx = 0:0.05:2*pi;
case 3
f = @(x)(ones(1,length(x)));
g = @(x)(zeros(1,length(x)));
xx = 0:0.05:2*pi;
end
end
% Bounds & variables
d = [cos(option.angle) sin(option.angle)];
FMin = min([f(xx) ; g(xx)]);
FMax = max([f(xx) ; g(xx)]);
dxx = max(xx) - min(xx);
m = min(xx);
% hatching
Xmin = linspace(min(FMin)-(d(2)/d(1))*dxx,max(FMax),option.size);
Xmax = linspace(min(FMin),max(FMax)+(d(2)/d(1))*dxx,option.size);
% Local Functions
DFMIN = @(x,x0) (min([f(x+m);g(x+m)])-(d(2)/d(1))*x-x0);
DFMAX = @(x,x0) (max([f(x+m);g(x+m)])-(d(2)/d(1))*x-x0);
ZF = @(x)(find(x(1:end-1).*x(2:end)<=0));
H = [];
XX = xx-m;
Xstart = Xmin >= FMin(1) & Xmin <= FMax(1);
Xend = Xmax >= FMin(end) & Xmax <= FMax(end);
%------------------------------------
% MAIN LOOP
for i=1:length(Xmin)
x_min = DFMIN(XX, Xmin(i));
x_max = DFMAX(XX, Xmin(i));
zx_min = ZF(x_min);
zx_max = ZF(x_max);
Xtmp = [xx(zx_min)' min([f(xx(zx_min));g(xx(zx_min))])'];
Xtmp = [Xtmp; xx(zx_max)' max([f(xx(zx_max));g(xx(zx_max))])'];
[tmp ,IX] = sort(Xtmp(:,1));
Xtmp = Xtmp(IX,:);
% Add Start Points
if Xstart(i)
Xtmp = [xx(1) Xmin(i); Xtmp];
end
% Add End Points
if Xend(i)
Xtmp = [Xtmp; xx(end) Xmax(i)];
end
for j=1:2:(size(Xtmp,1)-mod(size(Xtmp,1),2))
H = [H; Xtmp(j,:) Xtmp(j+1,:)];
end
end
%-----------------------------
% Plot Lines
m = option.margin;
p_handle = plot(xx,f(xx),xx,g(xx));
xlim([min(xx)-m*(max(xx)-min(xx)) max(xx)+m*(max(xx)-min(xx))])
ylim([min(FMin)-m*(max(FMax)-min(FMin)) max(FMax)+m*(max(FMax)-min(FMin))])
hold on
for i=1:size(H,1)
plot(H(i,[1 3]),H(i,[2 4]),'color',option.color)
end |
Partager