Filling areas between curves
Show older comments
I'm relatively inexperienced with MATLAB and I'm having a hard time understanding using patch or fill. I would like to shade each of the four areas generated by the intersection of these two curves.

Here's my code:
load 'twopar2gga.dat'
x = twopar2gga;
x1 = x(4001:4137,1);
y1 = x(4001:4137,2);
x2 = x(4138:4201,1);
y2 = x(4138:4201,2);
hold on
plot(x1,y1,'k')
plot(x2,y2,'r')
axis([0 1 0 1])
If someone could expain how to do this that'd be amazing. I've attached my data.
4 Comments
darova
on 19 Feb 2020
Is this correct?

Michael Thomas
on 19 Feb 2020
darova
on 19 Feb 2020
Do you have polyxpoly?
Michael Thomas
on 19 Feb 2020
Accepted Answer
More Answers (1)
darova
on 19 Feb 2020
Example
clc,clear
x1 = linspace(0,1.5);
y1 = sin(x1);
x2 = linspace(0,2);
y2 = cos(x1);
cla
[xc,yc] = polyxpoly(x1,y1,x2,y2); % find intersection point
ii1 = x1 > xc; % indices for the first curve
ii2 = x2 > xc; % indices for the second curve
patch([x1(ii1) flip(x2(ii2))], [y1(ii1) flip(y2(ii2))], 'r'); % fill right side
hold on
patch([x1(ii1) x2(~ii2)], [y1(ii1) y2(~ii2)], 'g'); % fill up/down
patch([x2(ii2) x1(~ii1)], [y2(ii2) y1(~ii1)], 'b'); % fill up/down
patch([x1(~ii1) flip(x2(~ii2))], [y1(~ii1) flip(y2(~ii2))], 'm'); % fill left side
hold off
The result

THe hole inside

Categories
Find more on Polygons in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!