# Metapict questions on filling polygons from points

**URL:** https://racket.discourse.group/t/metapict-questions-on-filling-polygons-from-points/1449
**Category:** Questions & Answers
**Created:** [November 9, 2022, 3:18pm UTC](https://racket.discourse.group/t/metapict-questions-on-filling-polygons-from-points/1449 "2022-11-09T15:18:33Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![pmatos](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/pmatos/32/26_2.png) [@pmatos](https://racket.discourse.group/u/pmatos)
#### Post date: [November 9, 2022, 3:18pm UTC](https://racket.discourse.group/t/metapict-questions-on-filling-polygons-from-points/1449/1 "2022-11-09T15:18:33Z")

</div>

I am trying to draw something in metapict and I am having trouble simplifying code.

Lets say I have 2 rectangles side by side, roughly:

```scheme
A B C
x------------x------------x
| | |
x------------x------------x
D E F

```

and I want to draw them. I can do:

```scheme
(curve A -- B -- C -- F -- E -- D -- cycle)
(curve B -- E)

```

But then I run into trouble if I want to fill the left side and right rides with different colours.  
So I need to do in other to fill both parts

```scheme
(curve A -- B -- E -- D -- cycle)
(curve B -- C -- F -- E -- cycle)

```

But this is not great because it redraws `B -- E`. Is there a better way to do this?

Another issue is... Lets say I define a bunch of point in a list. Like

```scheme
(define left (list A B E D))
(define right (list B C F E))

```

How do I draw the above from these lists?  
The obvious `(apply curve left)`... of course doesn't work.

---

<div class="post-metadata">

### Author: ![soegaard](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/soegaard/32/19_2.png) [@soegaard](https://racket.discourse.group/u/soegaard)
#### Post date: [November 9, 2022, 7:39pm UTC](https://racket.discourse.group/t/metapict-questions-on-filling-polygons-from-points/1449/2 "2022-11-09T19:39:43Z")

</div>

If you want to avoid redrawing, you can draw the background (the two filled rectangles) first,  
then draw the lines on top. See below.

But why is redrawing a problem in the first place - do you have an example of what it looks like?  
[I get the best results, when I save as svg.]

> The obvious `(apply curve left)` ... of course doesn't work.  
> I think `(curve* left)` is what you are looking for.

```scheme
#lang racket
(require metapict)

;; A B C
;; x------------x------------x
;; | | |
;; x------------x------------x
;; D E F

(def A (pt 0 2))
(def B (pt 2 2))
(def C (pt 4 2))

(def D (pt 0 0))
(def E (pt 2 0))
(def F (pt 4 0))

(def ACFD (curve A -- C -- F -- D -- cycle))
(def LEFT (curve A -- B -- E -- D -- cycle))
(def RIGHT (curve B -- C -- F -- E -- cycle))

(def BE (curve B -- E))

(define win (window -1 5 -1 5))

(set-curve-pict-size 401 401)
(with-window win
  (draw (color "red" (fill LEFT))
        (color "blue" (fill RIGHT))
        ACFD
        BE))

```

![image](https://global.discourse-cdn.com/free1/uploads/racket/original/1X/8821a1da741569ce79b87e3e0054cf77608590c8.jpeg)

---

<div class="post-metadata">

### Author: ![soegaard](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/soegaard/32/19_2.png) [@soegaard](https://racket.discourse.group/u/soegaard)
#### Post date: [November 9, 2022, 8:14pm UTC](https://racket.discourse.group/t/metapict-questions-on-filling-polygons-from-points/1449/3 "2022-11-09T20:14:54Z")

</div>

I forgot to use `rectangle`. It takes two corners (or a corner and a vector) and  
makes a rectangular path. The corners become a diagonal.

```scheme
(def AF (rectangle A F)) ; A -- C -- F -- D
(def LEFT (rectangle A E)) ; A -- B -- E -- D
(def RIGHT (rectangle B F)) ; B -- C -- F -- E

```

---

<div class="post-metadata">

### Author: ![pmatos](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/pmatos/32/26_2.png) [@pmatos](https://racket.discourse.group/u/pmatos)
#### Post date: [November 10, 2022, 2:59am UTC](https://racket.discourse.group/t/metapict-questions-on-filling-polygons-from-points/1449/4 "2022-11-10T02:59:47Z")

</div>

Cool thanks.

Thanks for all the explanation. Somehow I missed `curve*`, and I noticed now that it's because it's _undocumented_.

The `rectangle` stuff is cool but unneeded since this was just a simple example. What I am currently doing needs no rectangles. Once I get this right, I will post here what I came up with. 🙂

Paulo

---

<div class="post-metadata">

### Author: ![soegaard](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/soegaard/32/19_2.png) [@soegaard](https://racket.discourse.group/u/soegaard)
#### Post date: [November 10, 2022, 10:34am UTC](https://racket.discourse.group/t/metapict-questions-on-filling-polygons-from-points/1449/5 "2022-11-10T10:34:45Z")

</div>

About `curve*` and `curve*`: I forgot to say that you need "path joins" between the points.

For example: `(curve* (add-between (list (pt 1 0) (pt 2 3) (pt 3 -1)) ..))`

I did an experiment with redrawing the middle vertical line.  
Both look fine when svg is used.

If you save the images as "png" you can see the difference.

```scheme
(set-curve-pict-size 150 150)
(with-window win
  (draw (filldraw LEFT "red")
        (filldraw RIGHT "blue")))

(set-curve-pict-size 151 151)
(with-window win
  (draw (filldraw LEFT "red")
        (filldraw RIGHT "blue")))

```

![image](https://global.discourse-cdn.com/free1/uploads/racket/original/1X/7f078c5bfad963ea5c1d0234c60a6c8f04efce62.png)

The effect is explained in the documentation of racket/draw.  
Basically the first version where the width is 150 draws between the pixels  
and the second one draws perfectly in the middle of the pixels.

 ![image](https://global.discourse-cdn.com/free1/uploads/racket/original/1X/a88a880961420b104044a724ed6bda39662936a4.png)

The reason is due to the model used in Cairo:  
[http://stevehanov.ca/blog/?id=28](http://stevehanov.ca/blog/?id=28)
