Difference of Rectangles

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Difference of Rectangles

Jerome Peace
Difference of Rectangles

Hi JIm,

The following is some workspace code (copy into a
workspace.
Select and do all of it. Then recolor and move the
rectangles around.
And select and do the last bit.

note: #flipRotated:  is new as of 3.9. I don't know
how people got along without it.


aLessB :=
[ :a :b | | overlap |
        overlap := a intersect: b .
        overlap area  = 0
                ifTrue: [ a corners ]
                ifFalse: [ overlap area < a area
                                        ifFalse: [ a corners ]
                                        ifTrue: [oCorners := overlap corners .
                                                        verts := OrderedCollection new .
                                                        a corners withIndexDo:  [  :each :eachIndex |
                                                                        ((oCorners at: eachIndex) dist:  each) < 1
                                                                        ifTrue: [ verts
                                                                                                addAll: ( oCorners
                                                                                                                        flipRotated: eachIndex negated * 2  
                                                                                                                                + 1)
                                                                                                                allButLast ]
                                                                        ifFalse: [ verts add: each ] ] . verts ] ] ].
                                                                       
                                                                       
               
aRect := RectangleMorph new openCenteredInWorld .
bRect := RectangleMorph new openCenteredInWorld .
poly := PolygonMorph new openCenteredInWorld .

polyVerts :=
aLessB value: aRect bounds
            value: bRect bounds .
poly setVertices: polyVerts; changed .

I had done something similar for puzzle pieces. And I
had fun working this one out.

Yours in curiosity and service, --Jerome Peace




>  Jim Rosenberg jr at amanue.com
>  Sun May 6 20:12:03 UTC 2007

>  
>  Consider two rectangles, a and b. The only
constraint I
>  will put on them is
>  that neither contains the other. I wish to create a
>  PolygonMorph
>  programmatically consisting of a merge: b with b
>  removed but all of a
>  retained in the case a and b overlap. I think I see
my

>  way to how to do
>  this, but if anyone knows of any code I can look at
>  where something like
>  this is already implemented I'd be much obliged.
>  
>  My idea for how to go about this is to use "turtle
>  logic" more or less as
>  follows: Start with the first vertex of a merge: b
>  which is not inside of
>  b. Proceed clockwise along the bounds of a merge:
b; if
>  you hit b turn
>  left, if you hit a turn right. But this may be
doing
>  things the hard way.
>  
>  -Thanks, Jim



 
____________________________________________________________________________________
The fish are biting.
Get more visitors on your site using Yahoo! Search Marketing.
http://searchmarketing.yahoo.com/arp/sponsoredsearch_v2.php

Reply | Threaded
Open this post in threaded view
|

Re: Difference of Rectangles

Jerome Peace
Hi Dan.

Yep

I hadn't thought of that case. Also missed the case
where the two rectanges share a width or a height and
overlap like playing cards on a solitare layout.

Back to the drawing board. Worthy problems prove their
worth by fighting back. :-)

Yours in curiosity and service, --Jerome Peace

--- Dan Ingalls <[hidden email]> wrote:

> Hi, Jerome -
>
> Your code is interesting, but it did not seem to
> work for me in the
> case of, eg...
>
> AAAA
> ABBA
>   BB
>
> But I haven't got anything better, either ;-).
>
> - Dan
> ------------------------------------
> >Difference of Rectangles
> >
> >Hi JIm,
> >
> >The following is some workspace code (copy into a
> >workspace.
> >Select and do all of it. Then recolor and move the
> >rectangles around.
> >And select and do the last bit.
> >
> >note: #flipRotated:  is new as of 3.9. I don't know
> >how people got along without it.
> >
> >aLessB :=
> >[ :a :b | | overlap |
> > overlap := a intersect: b .
> > overlap area  = 0
> > ifTrue: [ a corners ]
> > ifFalse: [ overlap area < a area
> > ifFalse: [ a corners ]
> > ifTrue: [oCorners := overlap corners .
> > verts :=
> >OrderedCollection new .
> > a corners
> >withIndexDo:  [  :each :eachIndex |
> >
> > ((oCorners at: eachIndex) dist:  each) < 1
> >
> > ifTrue: [ verts
> >
> > addAll: ( oCorners
> >
> > flipRotated:
> >eachIndex negated * 2  
> >
> > + 1)
> >
> > allButLast ]
> >
> > ifFalse: [ verts add: each ] ] . verts ] ] ].
> >
> >
> >
> >aRect := RectangleMorph new openCenteredInWorld .
> >bRect := RectangleMorph new openCenteredInWorld .
> >poly := PolygonMorph new openCenteredInWorld .
> >
> >polyVerts :=
> >aLessB value: aRect bounds
> >    value: bRect bounds .
> >poly setVertices: polyVerts; changed .
> >
> >I had done something similar for puzzle pieces. And
> I
> >had fun working this one out.
> >
> >Yours in curiosity and service, --Jerome Peace
> >
> >
> >
> >
> >>   Jim Rosenberg jr at amanue.com
> >>   Sun May 6 20:12:03 UTC 2007
> >
> >>
> >>   Consider two rectangles, a and b. The only
> >constraint I
> >>   will put on them is
> >>   that neither contains the other. I wish to
> create a
> >>   PolygonMorph
> >>   programmatically consisting of a merge: b with
> b
> >>   removed but all of a
> >>   retained in the case a and b overlap. I think I
> see
> >my
> >>   way to how to do
> >>   this, but if anyone knows of any code I can
> look at
> >>   where something like
> >>   this is already implemented I'd be much
> obliged.
> >>
> >>   My idea for how to go about this is to use
> "turtle
> >>   logic" more or less as
> >>   follows: Start with the first vertex of a
> merge: b
> >>   which is not inside of
> >>   b. Proceed clockwise along the bounds of a
> merge:
> >b; if
> >>   you hit b turn
> >>   left, if you hit a turn right. But this may be
> >doing
> >>   things the hard way.
> >>
> >>   -Thanks, Jim
> >
> >
> >
> >
>
>____________________________________________________________________________________
> >The fish are biting.
> >Get more visitors on your site using Yahoo! Search
> Marketing.
>
>http://searchmarketing.yahoo.com/arp/sponsoredsearch_v2.php
>



 
____________________________________________________________________________________
Food fight? Enjoy some healthy debate
in the Yahoo! Answers Food & Drink Q&A.
http://answers.yahoo.com/dir/?link=list&sid=396545367