Fork me on GitHub
#quil
<
2023-03-07
>
dabrazhe13:03:23

`quil/rotate` rotates around [0 0] point, right? How can I rotate eg a rectangle in-place, i.e, around/relative to it's center?

genmeblog13:03:20

translate to the center of the rectangle, rotate and translate back

craftybones15:03:40

there are two ways

craftybones15:03:56

One is to calculate the points yourself and draw, which is painful

craftybones15:03:59

But here is a useful snippet

craftybones15:03:30

``````(q/with-translation [(+ x half-width) (+ y half-height)]
(q/rotate angle)
(q/translate (- half-width) (- half-height))
(q/rect 0 0 width height))``````

dabrazhe16:03:12

Thank you all. It seems to be quite a chore for such a simple operation ) Is there anything standard in the core quil lib? I guess might others have the same question

genmeblog16:03:10

Get used to it :) It works this way in most of such libraries. With rotate/translate/scale/shear you build affine transformation of your plane. This way shapes and tranformations are separated and can be composed.

craftybones18:03:49

I highly recommend drawing by hand until you get a hang of it, it becomes a lot easier then

craftybones18:03:51

Alternately, you can use the `center` rect-mode and it’ll save you one translate call…

dabrazhe10:03:19

The problem with translation is that I need to know the actual coordinates of a primitive, before rotation, every time. This is not helpful when drawing within `mapv` or similar AND it does not work for ellipse..

dabrazhe10:03:20

@U8VE0UBBR Can you elaborate on the center mode, can't find it in the api

genmeblog11:03:26

It controls how arguments to rect are interpreted. It doesn't affect the rotation point though.

dabrazhe11:03:14

Great, that helps a bit. What about other shapes, like triangle and vertex?

genmeblog11:03:04

Only ellipse (`ellipse-mode`) is supported. Regardings triangle, there are more than 70k of definitions of triangle https://faculty.evansville.edu/ck6/encyclopedia/ETC.html... The same probably applies to any other (not symmetrical) shape.

genmeblog11:03:22

Generally you may want to find a center of the shape enclosing circle. But it's not trivial in general.

dabrazhe12:03:14

The simplest solution so far for me is to use `(q/with-translation [] (q/with-rotation []` combo, with the :center mode. But the triangles are off as expected, as you can see on the image, returned by a mapv sequence.

dabrazhe12:03:56

Here it's more clear : )

genmeblog12:03:27

Depends what you expect. And how you define the center of the triangle.

dabrazhe12:03:34

What about inducing the size of the rectangle surrounding a shape and rotating it/the whole canvas in closure?

genmeblog12:03:26

Doable. If you are on JDK, take a look at PShape which knows enclosing box size.

dabrazhe12:03:26

If I understand you correctly, I can use Pshape with quil via interop, right?

dabrazhe12:03:13

I am not bit on java interop, how can I access it?

genmeblog12:03:26

Hmmm... In Processing you cal call `createShape` I don't see such function in `quil`. It's explained here: https://github.com/quil/quil/issues/165

👍 2
genmeblog12:03:58

Maybe the easiest way is to calculate the centroid of used vertices and rotate around it. Centroid is just an average of point coordinates (x and y separately).

2
dabrazhe21:03:16

Thanks everyone for help!