[cairo] Qahirah: A High-Level Cairo API Binding For Python 3

Olaf Schmidt ng at vbRichClient.com
Sun Mar 8 21:01:05 PDT 2015


Am 09.03.2015 um 03:27 schrieb Lawrence D'Oliveiro:
> On Mon, 09 Mar 2015 03:04:13 +0100, Olaf Schmidt wrote:
>
>> The code-size is about two/thirds of your python-script here:
>> https://github.com/ldo/qahirah_examples/blob/master/alhambra_tiles
>
> Interesting that you use hard-coded numbers for the shape of the
> curves. Mine does not. You can change the tilt and curve_factor
> parameters on my triwing, or the wing_ratio on my arrowing, to produce a
> whole family of curves with exactly the same symmetry properties.
>
> That was the key point I was trying to demonstrate: the ability to do
> powerful geometrical calculations in just a few lines of code.
>

Well, hardwired Values in Pattern-Creation-Routines are not all
that uncommon. And they allow some flexibility as well (if needed).

Adding a few more Optional Params to the freestanding Wing-Routine
I've posted earlier, allows for quite some variety as e.g.:

*using defaults*
RenderTriWingPattern "c:\temp\Example1.png"
http://vbRichClient.com/Downloads/Example1.png

*Zoom 1.5, Tilt 30°, default-CurveFactor*
RenderTriWingPattern "c:\temp\Example2.png", 1.5, 30
http://vbRichClient.com/Downloads/Example2.png

*Zoom 1.25, Tilt 45°, CurveFactor = 0.8*
RenderTriWingPattern "c:\temp\Example3.png", 1.25, 45, 0.8
http://vbRichClient.com/Downloads/Example3.png

*Zoom 0.75, Tilt 60°, CurveFactor = 1.6*
RenderTriWingPattern "c:\temp\Example4.png", 0.75, 60, 1.6
http://vbRichClient.com/Downloads/Example4.png

*Zoom 1.75, Tilt 75°, CurveFactor = 2.6*
RenderTriWingPattern "c:\temp\Example5.png", 1.75, 75, 2.6
http://vbRichClient.com/Downloads/Example5.png

Here's the slightly enhanced routine (and BTW Tilting is
quite easy, using the normal Cairo-Pattern-Matrix, after
the thing comes back from the PopGroup-call).

Sub RenderTriWingPattern(FileName$, Optional Zoom = 1, _
                                     Optional TiltDeg = 0, _
                                     Optional CurveFac = 1)
Dim R As Double, A As Double, P(0 To 17), i, j, n, Pat As cCairoPattern

   With Cairo.CreateSurface(400, 400).CreateContext

     .PushGroup , 0, 0, 48 * Zoom, 84 * Zoom
     .ScaleDrawings Zoom, Zoom

     For j = 1 To 3
       For i = 1 To 3
         A = 270 * Cairo.PI / 180
         For n = 0 To 8
           R = Choose(n Mod 3 + 1, -7 * CurveFac ^ 2, 33 * CurveFac, 28)
           A = A + Choose(n Mod 3 + 1, 5, 40, 75) * Cairo.PI / 180
           P(2 * n) = R * Cos(A): P(2 * n + 1) = R * Sin(A)
         Next n
         .MoveTo P(16), P(17)
         For n = 0 To UBound(P) Step 6
           .CurveTo P(n), P(n+1), P(n+2), P(n+3), P(n+4), P(n+5)
         Next n
         .Fill , Cairo.CreateSolidPatternLng(vbBlack)
         .TranslateDrawings 48, 0
       Next i
       .TranslateDrawings -48 * (i - 0.5), 42
     Next j

     Set Pat = .PopGroup(True, CAIRO_EXTEND_REPEAT)
     Set Pat.Matrix = Pat.Matrix.RotateCoordsDeg(TiltDeg)

     .Paint , Cairo.CreateSolidPatternLng(vbWhite)
     .Paint , Pat
     .Surface.WriteContentToPngFile FileName
   End With
End Sub

So, that's still quite short for a freestanding routine - and
still easy to port to the normal C-flat-API.
<shrug>

Olaf



More information about the cairo mailing list