Compose.jl
Compose.jl copied to clipboard
How do you create a square (without using absolute measurements)?
running
compose(context(), fill("tomato"),
(context(0.0, 0.0, 0.5, 0.5), circle(0.5,0.5,0.25)),
(context(0.5, 0.5, 0.5, 0.5), rectangle(0,0,1,1)))
in IJulia creates a pretty picture of a circle and a rectangle, but it's not clear how to modify the example to create a square without using absolute coordinates.
Is there a way to specify you want a square without knowing what the final aspect ratio of the document is going to be (I'll point out that a circle draws a circle not an ellipse, regardless of the global aspect ratio... How does that work?)
thanks!
it looks like:
square = ctxpromise() do drawctx
#dump(drawctx)
dw = drawctx.box.width
dh = drawctx.box.height
w = dw < dh ? 1.0 : dh/dw
h = dw < dh ? dw/dh : 1.0
compose(context(), rectangle(0,0,w,h))
end
compose(context(),
(context(1/2,1/2,1/2,1/2),square, fill("tomato")),
rectangle(),fill("blue"), fillopacity(0.5))
gives a hint as to how to achieve this in general. Does this seem correct?
The default dimensions in IJulia are rather rectangular.
The easiest way is to change that: Compose.set_default_graphic_size(6inch, 6inch)
.
Thanks Shashi,
I actually want to be able to create squares inside rectangular documents... basically, there are times when I want to be able to create a shape with a specific aspect ratio independent of the aspect ratio of the parent context. It looks like the context promise is the best way to achieve this.
Well in that case,
w
, and h
are units in Compose which represent the width and height of the current Context. You can set the width and height of the rectangle both to be a fraction of either w
or h
.
So my understanding is that to get a canvas with a fixed aspect ratio I should do something like this?:
compose(context(0, 0, h > w ? w : h, h > w ? w : h), foo(), bar() )