Difference of Rectangles
Jerome Peace
peace_the_dreamer at yahoo.com
Mon May 7 07:20:59 UTC 2007
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
More information about the Squeak-dev
mailing list
|