Compose.jl icon indicating copy to clipboard operation
Compose.jl copied to clipboard

How do you create a square (without using absolute measurements)?

Open habemus-papadum opened this issue 9 years ago • 5 comments

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!

habemus-papadum avatar Mar 17 '15 12:03 habemus-papadum

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?

habemus-papadum avatar Mar 17 '15 13:03 habemus-papadum

The default dimensions in IJulia are rather rectangular.

The easiest way is to change that: Compose.set_default_graphic_size(6inch, 6inch).

shashi avatar Mar 17 '15 14:03 shashi

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.

habemus-papadum avatar Mar 17 '15 14:03 habemus-papadum

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.

shashi avatar Mar 17 '15 14:03 shashi

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() )

mkborregaard avatar Oct 01 '15 17:10 mkborregaard