Thread Subject: Calculating overlap areas

Subject: Calculating overlap areas

From: Steve Bachmeier

Date: 9 Jul, 2007 18:16:35

Message: 1 of 18

I have the cartesian coordinates of the four corners of several
stationary rectangles (windows and doors on a wall) as well as a
large set of corners of a changing rectangle (representing a
simplified shadow on the same wall). Is there a way to calculate the
area of shade on each part of the wall? For example, for each point
in time that I have calculated the four points of the shadow, I would
like to calculate the area of that shadow that covers a window (which
has been defined by four stationary points). I need to be able to
output the full range of coverage (from zero to the area of the
window). Any ideas? Thanks in advance.

Subject: Calculating overlap areas

From: ellieandrogerxyzzy@mindspring.com.invalid (Roger Stafford)

Date: 10 Jul, 2007 05:20:45

Message: 2 of 18

In article <ef5ceb5.-1@webcrossing.raydaftYaTP>, "Steve Bachmeier"
<steve.bachmeier@yahoo.com> wrote:

> I have the cartesian coordinates of the four corners of several
> stationary rectangles (windows and doors on a wall) as well as a
> large set of corners of a changing rectangle (representing a
> simplified shadow on the same wall). Is there a way to calculate the
> area of shade on each part of the wall? For example, for each point
> in time that I have calculated the four points of the shadow, I would
> like to calculate the area of that shadow that covers a window (which
> has been defined by four stationary points). I need to be able to
> output the full range of coverage (from zero to the area of the
> window). Any ideas? Thanks in advance.
-----------------------
  You've got a somewhat messy problem there. We might as well generalize
it to finding the area of intersection of two convex polygons. (I presume
your shadows are convex.) I would proceed as follows. Find all points
which are either 1) the intersection of two line segments of the two
respective polygons, or 2) are vertices of one polygon that lie on or
inside the other polygon. Then take the convex hull of all these points
and find the area of its polygon.

  For 1), if (x1,y1) and (x2,y2) are the endpoints of one segment, and
(x3,y3) and (x4,y4) the endpoints of another segment, then the segments
intersect if and only if

 det([x1,x2,x3;y1,y2,y3,1,1,1])*det(x1,x2,x4;y1,y2,y4,1,1,1]) < 0 and
 det([x1,x3,x4;y1,y3,y4,1,1,1])*det(x2,x3,x4;y2,y3,y4,1,1,1]) < 0.

and their point of intersection is at:

 x = ((x4-x3)*(x2*y1-x1*y2)+(x2-x1)*(x3*y4-x4*y3)) / ...
      ((x2-x1)*(y4-y3)-(x4-x3)*(y2-y1));
 y = ((y4-y3)*(x2*y1-x1*y2)+(y2-y1)*(x3*y4-x4*y3)) / ...
      ((x2-x1)*(y4-y3)-(x4-x3)*(y2-y1));

  For 2) you can use 'inpolygon'. To get the convex hull, use
'convhull'. The area of the final polygon (X,Y) is given by

 X1 = [X,X(1)]; Y1 = [Y,Y(1)]; % Make sure polygon is closed
 X2 = [X1(2:end);X1(1)]; Y2 = [Y1(2:end);Y1(1)];
 area = 1/2*abs(sum(X1.*Y2-X2.*Y1));

Roger Stafford

Subject: Calculating overlap areas

From: Steve Bachmeier

Date: 10 Jul, 2007 11:09:14

Message: 3 of 18

I really appreciate the human mind's ability to visually solve these
types of problems...I was really hoping there was already a script
for this! Thank you very much for your suggestions. I've considered
an approach similar to yours (although I didn't know about
'inpolygon' which should help tremendously). A problem I'm having is
that, unfortunately, windows and doors have parallel and
perpendicular lines and determinants don't like those. I suppose I
can cheat and just make them really close to parallel and
perpendicular. I'll see what I can pull together.

Steve

 Roger Stafford wrote:
>
>
> In article <ef5ceb5.-1@webcrossing.raydaftYaTP>, "Steve
Bachmeier"
> <steve.bachmeier@yahoo.com> wrote:
>
>> I have the cartesian coordinates of the four corners of several
>> stationary rectangles (windows and doors on a wall) as well as
a
>> large set of corners of a changing rectangle (representing a
>> simplified shadow on the same wall). Is there a way to
calculate
> the
>> area of shade on each part of the wall? For example, for each
> point
>> in time that I have calculated the four points of the shadow, I
> would
>> like to calculate the area of that shadow that covers a window
> (which
>> has been defined by four stationary points). I need to be able
to
>> output the full range of coverage (from zero to the area of the
>> window). Any ideas? Thanks in advance.
> -----------------------
> You've got a somewhat messy problem there. We might as well
> generalize
> it to finding the area of intersection of two convex polygons. (I
> presume
> your shadows are convex.) I would proceed as follows. Find all
> points
> which are either 1) the intersection of two line segments of the
> two
> respective polygons, or 2) are vertices of one polygon that lie on
> or
> inside the other polygon. Then take the convex hull of all these
> points
> and find the area of its polygon.
>
> For 1), if (x1,y1) and (x2,y2) are the endpoints of one segment,
> and
> (x3,y3) and (x4,y4) the endpoints of another segment, then the
> segments
> intersect if and only if
>
> det([x1,x2,x3;y1,y2,y3,1,1,1])*det(x1,x2,x4;y1,y2,y4,1,1,1]) < 0
> and
> det([x1,x3,x4;y1,y3,y4,1,1,1])*det(x2,x3,x4;y2,y3,y4,1,1,1]) <
0.
>
> and their point of intersection is at:
>
> x = ((x4-x3)*(x2*y1-x1*y2)+(x2-x1)*(x3*y4-x4*y3)) / ...
> ((x2-x1)*(y4-y3)-(x4-x3)*(y2-y1));
> y = ((y4-y3)*(x2*y1-x1*y2)+(y2-y1)*(x3*y4-x4*y3)) / ...
> ((x2-x1)*(y4-y3)-(x4-x3)*(y2-y1));
>
> For 2) you can use 'inpolygon'. To get the convex hull, use
> 'convhull'. The area of the final polygon (X,Y) is given by
>
> X1 = [X,X(1)]; Y1 = [Y,Y(1)]; % Make sure polygon is closed
> X2 = [X1(2:end);X1(1)]; Y2 = [Y1(2:end);Y1(1)];
> area = 1/2*abs(sum(X1.*Y2-X2.*Y1));
>
> Roger Stafford
>

Subject: Calculating overlap areas

From: Steve Bachmeier

Date: 10 Jul, 2007 11:27:10

Message: 4 of 18

Clarification: it's not the horizontal and vertical lines themselves
determinants "don't like." But because I'm assuming rectangular
shadows for sanity's sake, that is creating only parallel and
perpendicular sets of line segments. That is the what may blow up.

-Steve

Steve Bachmeier wrote:
>
>
> I really appreciate the human mind's ability to visually solve
> these
> types of problems...I was really hoping there was already a script
> for this! Thank you very much for your suggestions. I've
> considered
> an approach similar to yours (although I didn't know about
> 'inpolygon' which should help tremendously). A problem I'm having
> is
> that, unfortunately, windows and doors have parallel and
> perpendicular lines and determinants don't like those. I suppose I
> can cheat and just make them really close to parallel and
> perpendicular. I'll see what I can pull together.
>
> Steve
>
> Roger Stafford wrote:
>>
>>
>> In article <ef5ceb5.-1@webcrossing.raydaftYaTP>, "Steve
> Bachmeier"
>> <steve.bachmeier@yahoo.com> wrote:
>>
>>> I have the cartesian coordinates of the four corners of
> several
>>> stationary rectangles (windows and doors on a wall) as well
> as
> a
>>> large set of corners of a changing rectangle (representing
a
>>> simplified shadow on the same wall). Is there a way to
> calculate
>> the
>>> area of shade on each part of the wall? For example, for
each
>> point
>>> in time that I have calculated the four points of the
shadow,
> I
>> would
>>> like to calculate the area of that shadow that covers a
> window
>> (which
>>> has been defined by four stationary points). I need to be
> able
> to
>>> output the full range of coverage (from zero to the area of
> the
>>> window). Any ideas? Thanks in advance.
>> -----------------------
>> You've got a somewhat messy problem there. We might as well
>> generalize
>> it to finding the area of intersection of two convex polygons.
> (I
>> presume
>> your shadows are convex.) I would proceed as follows. Find
all
>> points
>> which are either 1) the intersection of two line segments of
the
>> two
>> respective polygons, or 2) are vertices of one polygon that lie
> on
>> or
>> inside the other polygon. Then take the convex hull of all
these
>> points
>> and find the area of its polygon.
>>
>> For 1), if (x1,y1) and (x2,y2) are the endpoints of one
segment,
>> and
>> (x3,y3) and (x4,y4) the endpoints of another segment, then the
>> segments
>> intersect if and only if
>>
>> det([x1,x2,x3;y1,y2,y3,1,1,1])*det(x1,x2,x4;y1,y2,y4,1,1,1])
<
> 0
>> and
>> det([x1,x3,x4;y1,y3,y4,1,1,1])*det(x2,x3,x4;y2,y3,y4,1,1,1])
<
> 0.
>>
>> and their point of intersection is at:
>>
>> x = ((x4-x3)*(x2*y1-x1*y2)+(x2-x1)*(x3*y4-x4*y3)) / ...
>> ((x2-x1)*(y4-y3)-(x4-x3)*(y2-y1));
>> y = ((y4-y3)*(x2*y1-x1*y2)+(y2-y1)*(x3*y4-x4*y3)) / ...
>> ((x2-x1)*(y4-y3)-(x4-x3)*(y2-y1));
>>
>> For 2) you can use 'inpolygon'. To get the convex hull, use
>> 'convhull'. The area of the final polygon (X,Y) is given by
>>
>> X1 = [X,X(1)]; Y1 = [Y,Y(1)]; % Make sure polygon is closed
>> X2 = [X1(2:end);X1(1)]; Y2 = [Y1(2:end);Y1(1)];
>> area = 1/2*abs(sum(X1.*Y2-X2.*Y1));
>>
>> Roger Stafford
>>

Subject: Calculating overlap areas

From: ellieandrogerxyzzy@mindspring.com.invalid (Roger Stafford)

Date: 10 Jul, 2007 17:35:49

Message: 5 of 18

In article <ef5ceb5.2@webcrossing.raydaftYaTP>, "Steve Bachmeier"
<steve.bachmeier@yahoo.com> wrote:

> Clarification: it's not the horizontal and vertical lines themselves
> determinants "don't like." But because I'm assuming rectangular
> shadows for sanity's sake, that is creating only parallel and
> perpendicular sets of line segments. That is the what may blow up.
>
> Steve Bachmeier wrote:
> >
> > I really appreciate the human mind's ability to visually solve
> > these
> > types of problems...I was really hoping there was already a script
> > for this! Thank you very much for your suggestions. I've
> > considered
> > an approach similar to yours (although I didn't know about
> > 'inpolygon' which should help tremendously). A problem I'm having
> > is
> > that, unfortunately, windows and doors have parallel and
> > perpendicular lines and determinants don't like those. I suppose I
> > can cheat and just make them really close to parallel and
> > perpendicular. I'll see what I can pull together.
-------------------
  The test with the four determinants I mentioned should keep you out of
trouble with parallel sides. If the test fails for a pair of segments,
just don't attempt to find their intersection.

  The test should really be modified to allow for all cases. Rather than
insisting on negative products, it should require that,

 sign(det([x1,x2,x3;y1,y2,y3,1,1,1])) ~= sign(det(x1,x2,x4;y1,y2,y4,1,1,1]))

and

 sign(det([x1,x3,x4;y1,y3,y4,1,1,1])) ~= sign(det(x2,x3,x4;y2,y3,y4,1,1,1])),

where "sign" is the matlab function, 'sign'. This is the equivalent of
insisting that at least one of each pair of determinants should be
nonzero, and if both are nonzero they should be of opposite signs.

  With parallel segments, one of these pairs of determinants will be equal
and are therefore bound to fail the test.

  I cannot see that parallel sides would lead to any trouble with
'inpolygon' or 'convhull', and certainly not with finding the area.

Roger Stafford

Subject: Calculating overlap areas

From: Kelly

Date: 10 Jul, 2007 14:33:45

Message: 6 of 18

Steve Bachmeier wrote:

> I have the cartesian coordinates of the four corners of several
> stationary rectangles (windows and doors on a wall) as well as a
> large set of corners of a changing rectangle (representing a
> simplified shadow on the same wall). Is there a way to calculate
> the
> area of shade on each part of the wall? For example, for each
> point
> in time that I have calculated the four points of the shadow, I
> would
> like to calculate the area of that shadow that covers a window
> (which
> has been defined by four stationary points). I need to be able to
> output the full range of coverage (from zero to the area of the
> window). Any ideas? Thanks in advance.

If you have the Mapping Toolbox, take a look at the polybool
function.

-Kelly

Subject: Calculating overlap areas

From: Steve Bachmeier

Date: 10 Jul, 2007 16:02:10

Message: 7 of 18

Well this looks promising as a quick solution and a check to what I
develop (I'd rather not rely on a possibly not-installed toolbox on
other computers trying to run this model). It seems to work great
(as the examples show) with circles, but what about rectangles and
other polygons? Can I pass the 'rectangle' function in as inputs?
Thanks both of you!

-Steve

Kelly wrote:
>
>
> Steve Bachmeier wrote:
>
>> I have the cartesian coordinates of the four corners of several
>> stationary rectangles (windows and doors on a wall) as well as
a
>> large set of corners of a changing rectangle (representing a
>> simplified shadow on the same wall). Is there a way to
calculate
>> the
>> area of shade on each part of the wall? For example, for each
>> point
>> in time that I have calculated the four points of the shadow, I
>> would
>> like to calculate the area of that shadow that covers a window
>> (which
>> has been defined by four stationary points). I need to be able
> to
>> output the full range of coverage (from zero to the area of the
>> window). Any ideas? Thanks in advance.
>
> If you have the Mapping Toolbox, take a look at the polybool
> function.
>
> -Kelly

Subject: Calculating overlap areas

From: Kelly Kearney

Date: 11 Jul, 2007 16:57:52

Message: 8 of 18

Yes, polybool will accept any polygons of any shape. The only thing you have to be careful about is the direction of the vertices. Clockwise order indicates a polygon contour, while counterclockwise is interpreted as a hole contour.

So a rectangle with the lower left corner at the origin, a height of 2, and a width of 3 would be defined by

x = [0 0 3 3 0];
y = [0 2 2 0 0];

-Kelly

Subject: Calculating overlap areas

From: Steve Bachmeier

Date: 11 Jul, 2007 17:05:33

Message: 9 of 18

In trying a quick test of the 'polybool' function, I get the error
"Subscripted assignment dimension mismatch" in the last line. Why is
this? If I create [ishadow_win jshadow_win] one step at a time
outside of the for loop it works... Thanks.

% Define the window
window_left=8;
window_right=19;
window_high=6;
window_low=4;

for t=1:4
    iwin_left(t)=window_left;
    iwin_right(t)=window_right;
    jwin_high(t)=window_high;
    jwin_low(t)=window_low;
    iwin=[iwin_left, iwin_left, iwin_right, iwin_right, iwin_left];
    jwin=[jwin_low, jwin_high, jwin_high, jwin_low, jwin_low];
    
    % Define the changing shadow
    ishadow_left(t)=-3+t;
    ishadow_right(t)=26-4*t;
    jshadow_high(t)=13;
    jshadow_low(t)=-4+3*t;
    ishadow=[ishadow_left, ishadow_left, ishadow_right,
ishadow_right, ishadow_left];
    jshadow=[jshadow_low, jshadow_high, jshadow_high, jshadow_low,
jshadow_low];

    % Determine the shade covering the window
    [ishadow_win(t,:)
jshadow_win(t,:)]=polybool('intersection',iwin(t,:),jwin(t,:),ishadow(
t,:),jshadow(t,:));
end

Kelly wrote:
>
>
> Steve Bachmeier wrote:
>
>> I have the cartesian coordinates of the four corners of several
>> stationary rectangles (windows and doors on a wall) as well as
a
>> large set of corners of a changing rectangle (representing a
>> simplified shadow on the same wall). Is there a way to
calculate
>> the
>> area of shade on each part of the wall? For example, for each
>> point
>> in time that I have calculated the four points of the shadow, I
>> would
>> like to calculate the area of that shadow that covers a window
>> (which
>> has been defined by four stationary points). I need to be able
> to
>> output the full range of coverage (from zero to the area of the
>> window). Any ideas? Thanks in advance.
>
> If you have the Mapping Toolbox, take a look at the polybool
> function.
>
> -Kelly

Subject: Calculating overlap areas

From: Steve Bachmeier

Date: 11 Jul, 2007 17:26:18

Message: 10 of 18

OK, the problem is when there is no intersection. 'polybool' seems
to pump out [] in that case. I tried preallocating the matrices to
zeros, but that didn't help. How can I make it so if there is no
intersection, it still adds a row of, say, zeros? Thanks.

 Steve Bachmeier wrote:
>
>
> In trying a quick test of the 'polybool' function, I get the error
> "Subscripted assignment dimension mismatch" in the last line. Why
> is
> this? If I create [ishadow_win jshadow_win] one step at a time
> outside of the for loop it works... Thanks.
>
> % Define the window
> window_left=8;
> window_right=19;
> window_high=6;
> window_low=4;
>
> for t=1:4
> iwin_left(t)=window_left;
> iwin_right(t)=window_right;
> jwin_high(t)=window_high;
> jwin_low(t)=window_low;
> iwin=[iwin_left, iwin_left, iwin_right, iwin_right, iwin_left];
> jwin=[jwin_low, jwin_high, jwin_high, jwin_low, jwin_low];
>
> % Define the changing shadow
> ishadow_left(t)=-3+t;
> ishadow_right(t)=26-4*t;
> jshadow_high(t)=13;
> jshadow_low(t)=-4+3*t;
> ishadow=[ishadow_left, ishadow_left, ishadow_right,
> ishadow_right, ishadow_left];
> jshadow=[jshadow_low, jshadow_high, jshadow_high, jshadow_low,
> jshadow_low];
>
> % Determine the shade covering the window
> [ishadow_win(t,:)
>
jshadow_win(t,:)]=polybool('intersection',iwin(t,:),jwin(t,:),ishado
> w(
> t,:),jshadow(t,:));
> end
>
> Kelly wrote:
>>
>>
>> Steve Bachmeier wrote:
>>
>>> I have the cartesian coordinates of the four corners of
> several
>>> stationary rectangles (windows and doors on a wall) as well
> as
> a
>>> large set of corners of a changing rectangle (representing
a
>>> simplified shadow on the same wall). Is there a way to
> calculate
>>> the
>>> area of shade on each part of the wall? For example, for
> each
>>> point
>>> in time that I have calculated the four points of the
shadow,
> I
>>> would
>>> like to calculate the area of that shadow that covers a
> window
>>> (which
>>> has been defined by four stationary points). I need to be
> able
>> to
>>> output the full range of coverage (from zero to the area of
> the
>>> window). Any ideas? Thanks in advance.
>>
>> If you have the Mapping Toolbox, take a look at the polybool
>> function.
>>
>> -Kelly

Subject: Calculating overlap areas

From: Kelly

Date: 11 Jul, 2007 22:18:39

Message: 11 of 18

Steve wrote:

> OK, the problem is when there is no intersection. 'polybool' seems
> to pump out [] in that case. I tried preallocating the matrices to
> zeros, but that didn't help. How can I make it so if there is no
> intersection, it still adds a row of, say, zeros? Thanks.

I'd just use a temporary variable and then check if it's empty. If
not, assign it to your output array.

win_x = [8 8 19 19 8];
win_y = [4 6 6 4 4];

t = (1:4)';
shadow_x = [-3+t -3+t 26-4*t 26-4*t -3+t];
shadow_y = [-4+3*t 13+t*0 13+t*0 -4+3*t -4+3*t];

[overlap_x, overlap_y] = deal(zeros(size(shadow_x)));
for it = t'
    [x,y] = polybool('&', win_x, win_y, shadow_x(it,:),
shadow_y(it,:));
    if ~isempty(x)
        overlap_x(it,:) = x;
        overlap_y(it,:) = y;
    end
    p(it) = subplot(2,2,it);
    patch(win_x, win_y, 'b');
    hold on;
    patch(shadow_x(it,:), shadow_y(it,:), 'r');
    patch(overlap_x(it,:), overlap_y(it,:), 'g');
end
set(p, 'XLim', [0 20], 'YLim', [0 15]);

-Kelly

Subject: Calculating overlap areas

From: Dan Haeg

Date: 12 Jul, 2007 02:49:18

Message: 12 of 18

look at rectint. I just stumbled upon it and remembered this thread. It looks promising

area = rectint(A,B) returns the area of intersection of the rectangles specified by position vectors A and B.

Subject: Calculating overlap areas

From: Steve Bachmeier

Date: 12 Jul, 2007 09:25:48

Message: 13 of 18

OK, I think that's it. Out of curiousy,

1. Why the '~' before 'isempty'?
2. Why did you only check for x being empty, and not y also?
3. Why did you define 't', only to later change it to 'it'?

Thanks!

 Kelly wrote:
>
>
> Steve wrote:
>
>> OK, the problem is when there is no intersection. 'polybool'
> seems
>> to pump out [] in that case. I tried preallocating the matrices
> to
>> zeros, but that didn't help. How can I make it so if there is
no
>> intersection, it still adds a row of, say, zeros? Thanks.
>
> I'd just use a temporary variable and then check if it's empty. If
> not, assign it to your output array.
>
> win_x = [8 8 19 19 8];
> win_y = [4 6 6 4 4];
>
> t = (1:4)';
> shadow_x = [-3+t -3+t 26-4*t 26-4*t -3+t];
> shadow_y = [-4+3*t 13+t*0 13+t*0 -4+3*t -4+3*t];
>
> [overlap_x, overlap_y] = deal(zeros(size(shadow_x)));
> for it = t'
> [x,y] = polybool('&', win_x, win_y, shadow_x(it,:),
> shadow_y(it,:));
> if ~isempty(x)
> overlap_x(it,:) = x;
> overlap_y(it,:) = y;
> end
> p(it) = subplot(2,2,it);
> patch(win_x, win_y, 'b');
> hold on;
> patch(shadow_x(it,:), shadow_y(it,:), 'r');
> patch(overlap_x(it,:), overlap_y(it,:), 'g');
> end
> set(p, 'XLim', [0 20], 'YLim', [0 15]);
>
> -Kelly
>
>

Subject: Calculating overlap areas

From: Kelly

Date: 12 Jul, 2007 14:35:18

Message: 14 of 18

Steve wrote:

> OK, I think that's it. Out of curiousy,
>
> 1. Why the '~' before 'isempty'?

~ = not. I only want to assign x and y to overlap_x and overlap_y if
x and y are not empty.

> 2. Why did you only check for x being empty, and not y also?

polybool returns to equally sized vectors. If x is empty, so is y, so
there's no need to check both of them.

> 3. Why did you define 't', only to later change it to 'it'?

t is a 4 x 1 vector of all the time values, while it is is my scalar
counter variable. The shadow definitions were easy to pull out of the
loop since each coordinate was just an function of time, but I needed
the entire vector of times to do this. It's just coincidence that in
this case t(1) = 1, t(2) = 2, etc. It will still work if, say, t = [2
5 12 24].

-Kelly

Subject: Calculating overlap areas

From: Steve Bachmeier

Date: 12 Jul, 2007 10:41:24

Message: 15 of 18

Thank you very much.

Kelly wrote:
>
>
> Steve wrote:
>
>> OK, I think that's it. Out of curiousy,
>>
>> 1. Why the '~' before 'isempty'?
>
> ~ = not. I only want to assign x and y to overlap_x and overlap_y
> if
> x and y are not empty.
>
>> 2. Why did you only check for x being empty, and not y also?
>
> polybool returns to equally sized vectors. If x is empty, so is y,
> so
> there's no need to check both of them.
>
>> 3. Why did you define 't', only to later change it to 'it'?
>
> t is a 4 x 1 vector of all the time values, while it is is my
> scalar
> counter variable. The shadow definitions were easy to pull out of
> the
> loop since each coordinate was just an function of time, but I
> needed
> the entire vector of times to do this. It's just coincidence that
> in
> this case t(1) = 1, t(2) = 2, etc. It will still work if, say, t =
> [2
> 5 12 24].
>
> -Kelly
>
>

Subject: Calculating overlap areas

From: Steve Bachmeier

Date: 12 Jul, 2007 14:25:12

Message: 16 of 18

So I figured I'd beat this horse a bit more and try and get more
accurate shadows by not assuming they are all rectangular. The
problem now is that the number of intersecting points (of, say, the
shadow and the window) changes. If, for examply, a shadow falls
entirely on the surface of the window, polybool('and',...) will give
a closed polygon with 4 different intersecting points (the corners of
the shadow). But, with diagonal lines, it is now possible to have as
many as 8 intersection points. How can I build up the time-dependent
overlap matrix when the number of columns is now changing? Thanks!

 Kelly wrote:
>
>
> Steve wrote:
>
>> OK, the problem is when there is no intersection. 'polybool'
> seems
>> to pump out [] in that case. I tried preallocating the matrices
> to
>> zeros, but that didn't help. How can I make it so if there is
no
>> intersection, it still adds a row of, say, zeros? Thanks.
>
> I'd just use a temporary variable and then check if it's empty. If
> not, assign it to your output array.
>
> win_x = [8 8 19 19 8];
> win_y = [4 6 6 4 4];
>
> t = (1:4)';
> shadow_x = [-3+t -3+t 26-4*t 26-4*t -3+t];
> shadow_y = [-4+3*t 13+t*0 13+t*0 -4+3*t -4+3*t];
>
> [overlap_x, overlap_y] = deal(zeros(size(shadow_x)));
> for it = t'
> [x,y] = polybool('&', win_x, win_y, shadow_x(it,:),
> shadow_y(it,:));
> if ~isempty(x)
> overlap_x(it,:) = x;
> overlap_y(it,:) = y;
> end
> p(it) = subplot(2,2,it);
> patch(win_x, win_y, 'b');
> hold on;
> patch(shadow_x(it,:), shadow_y(it,:), 'r');
> patch(overlap_x(it,:), overlap_y(it,:), 'g');
> end
> set(p, 'XLim', [0 20], 'YLim', [0 15]);
>
> -Kelly
>
>

Subject: Calculating overlap areas

From: Kelly

Date: 13 Jul, 2007 14:35:53

Message: 17 of 18

On Jul 12, 2:25 pm, "Steve Bachmeier" <steve.bachme...@yahoo.com>
wrote:

> So I figured I'd beat this horse a bit more and try and get more
> accurate shadows by not assuming they are all rectangular. The
> problem now is that the number of intersecting points (of, say, the
> shadow and the window) changes. If, for examply, a shadow falls
> entirely on the surface of the window, polybool('and',...) will give
> a closed polygon with 4 different intersecting points (the corners of
> the shadow). But, with diagonal lines, it is now possible to have as
> many as 8 intersection points. How can I build up the time-dependent
> overlapmatrix when the number of columns is now changing? Thanks!

Use a cell array.

help cell

or for more info, look in the documentation at MATLAB > Programming >
Data Types > Cell Arrays

-Kelly

Subject: Calculating overlap areas

From: Steve Bachmeier

Date: 13 Jul, 2007 10:56:04

Message: 18 of 18

Thanks again!

 Kelly wrote:
>
>
> On Jul 12, 2:25 pm, "Steve Bachmeier"
<steve.bachme...@yahoo.com>
> wrote:
>
>> So I figured I'd beat this horse a bit more and try and get
more
>> accurate shadows by not assuming they are all rectangular. The
>> problem now is that the number of intersecting points (of, say,
> the
>> shadow and the window) changes. If, for examply, a shadow falls
>> entirely on the surface of the window, polybool('and',...) will
> give
>> a closed polygon with 4 different intersecting points (the
> corners of
>> the shadow). But, with diagonal lines, it is now possible to
have
> as
>> many as 8 intersection points. How can I build up the
> time-dependent
>> overlapmatrix when the number of columns is now changing?
Thanks!
>
> Use a cell array.
>
> help cell
>
> or for more info, look in the documentation at MATLAB >
Programming
>>
> Data Types > Cell Arrays
>
> -Kelly
>
>

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
computational geometry jason 7 Jul, 2008 10:43:03
computational geometry John D'Errico 10 Jul, 2007 06:32:10
rssFeed for this Thread

Public Submission Policy

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.

Contact us at files@mathworks.com