transparency in Pict

43 views
Skip to first unread message

Hendrik Boom

unread,
Sep 9, 2019, 2:20:22 PM9/9/19
to Racket Users
I'm wondering how to cut a transparent hole in something.

Say I have a rectangle and I want to make part of it transparent so
that I cn see what's behind it.
Drawing a transparent rectangle on top of it won't workm because it'll
just reveal the original rectangle.
The only way I cn see it to draw the original rectangle in pieces,
careful avoiding the area I want to make transparent. (that may be
tricky if the transparent area is, say, a circle).

Is there some convenient operation in Pict that can accomplish this
more directly? Kind of the union and intersection and complementation
on constructive solid geometry, but now 2D.

My guess is no. I haven't found it. So I ask.

Maybe I'll need some other drawing tool than Pict. Suggestions welcome.

-- hendrik

Jens Axel Søgaard

unread,
Sep 9, 2019, 3:15:19 PM9/9/19
to Racket Users
You can use a path with an even-odd-fill to cut out parts.

An example:

#lang racket
(require metapict metapict/polygons)

(define (cutout p x y r)
  (defv (w h) (pict-size p))
  (with-window (window 0 w 0 h)
    (brushstipple (pict->bitmap p)
                  (eofill (rectangle (pt 0 0) (pt w h))
                          (circle x y r)))))

(set-curve-pict-size 400 400)
(def p (brushcolor "red" (fill (regular-polygon 5))))
(cutout p 200 200 50)


/Jens Axel

The result (the yellowish color is the background color in my editor):

image.png


--
You received this message because you are subscribed to the Google Groups "Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to racket-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/20190909182018.74o322qfwehrmqaz%40topoi.pooq.com.


--
--
Jens Axel Søgaard

Robby Findler

unread,
Sep 10, 2019, 8:12:57 AM9/10/19
to Jens Axel Søgaard, Racket Users
Another trick I use is to crop the picture that is nominally below the image and then just draw it again, but this time on top. 

Robby

George Neuner

unread,
Sep 10, 2019, 8:54:11 AM9/10/19
to Racket Users

Or create a region with a hole through it and use it to clip the drawing.
George

Jens Axel Søgaard

unread,
Sep 10, 2019, 9:23:59 AM9/10/19
to George Neuner, Racket Users
Den tir. 10. sep. 2019 kl. 14.54 skrev George Neuner <gneu...@comcast.net>:

Or create a region with a hole through it and use it to clip the drawing.

Great suggestion. That's much better than using pict->bitmap.

I have extended "clipped" to handle regions defined by more than one path. 
Now (clipped c ... p) will make a region from the curves c ... and use
the region to clip the pict p. Most common uses:

   (clipped c p)          the part of p inside c
   (clipped c1 c2 p)   the part of p between c1 and c2 (if c2 is inside c1)

The example now becomes:

#lang racket
(require metapict metapict/polygons)

(define (cutout p x y r)
  (defv (w h) (pict-size p))
  (with-window (window 0 w 0 h)
    (clipped (rectangle (pt 0 0) (pt w h))
             (circle (pt x y) r)
             p)))


(set-curve-pict-size 400 400)
(def p (brushcolor "red" (fill (regular-polygon 5))))
(cutout p 200 200 50)

Before only one curve was supported and the result of clipped was the interior,
so the old clipped couldn't cut holes.

/Jens Axel
 

Hendrik Boom

unread,
Sep 10, 2019, 11:04:40 AM9/10/19
to Racket Users
On Tue, Sep 10, 2019 at 03:23:43PM +0200, Jens Axel Søgaard wrote:
> Den tir. 10. sep. 2019 kl. 14.54 skrev George Neuner <gneu...@comcast.net>:
>
> >
> > Or create a region with a hole through it and use it to clip the drawing.
> >
>
> Great suggestion. That's much better than using pict->bitmap.
>
> I have extended "clipped" to handle regions defined by more than one path.
> Now (clipped c ... p) will make a region from the curves c ... and use
> the region to clip the pict p. Most common uses:
>
> (clipped c p) the part of p inside c
> (clipped c1 c2 p) the part of p between c1 and c2 (if c2 is inside c1)

Does that mean the part of p that is inside c1 and outside c2? (which
would be meaningful even if c1 and c2 intersect? That might happen
because of numerical instability.)

Jens Axel Søgaard

unread,
Sep 10, 2019, 11:24:02 AM9/10/19
to Racket Users
Den tir. 10. sep. 2019 kl. 17.04 skrev Hendrik Boom <hen...@topoi.pooq.com>:
On Tue, Sep 10, 2019 at 03:23:43PM +0200, Jens Axel Søgaard wrote:
 
> I have extended "clipped" to handle regions defined by more than one path.
> Now (clipped c ... p) will make a region from the curves c ... and use
> the region to clip the pict p. Most common uses:
>
>    (clipped c p)          the part of p inside c
>    (clipped c1 c2 p)   the part of p between c1 and c2 (if c2 is inside c1)

Does that mean the part of p that is inside c1 and outside c2? (which
would be meaningful even if c1 and c2 intersect?  That might happen
because of numerical instability.)

Whether is point is inside or outside the region given by the
two curves is decided by the "winding rule". See the
discussion on fill (which also uses the winding rule):

https://docs.racket-lang.org/metapict/index.html?q=metapict#%28def._%28%28lib._metapict%2Fdraw..rkt%29._fill%29%29

For filling with the even-odd rule I have an eofill, but I haven't written an eoclipped yet.

/Jens Axel
 

George Neuner

unread,
Sep 10, 2019, 12:04:31 PM9/10/19
to racket users
Just a couple of points relevant to using regions in general:

 - any region can be selected as the clipping region for drawing.

 - a region can be created from any closed figure: a rectangle, an ellipse, a looping point path, etc.

 - a region can be created by combining other regions using union, subtraction, and intersection.

 - a single region can cover multiple disjoint areas.

And a hint:
If you are attempting to "shade" objects by painting them with a partly transparent brush, you may (and probably will) end up with Moire patterns where objects overlay / intersect each other.  The way to apply shade without worrying about Moire is to create a single region that simultaneously covers all the to-be-shaded areas, and then to fill/paint that region with your chosen shading brush.

YMMV,
George
Reply all
Reply to author
Forward
0 new messages