| 1 |
module snippets.curve_rectangle; |
|---|
| 2 |
|
|---|
| 3 |
private |
|---|
| 4 |
{ |
|---|
| 5 |
import std.math; |
|---|
| 6 |
import cairooo.all; |
|---|
| 7 |
import snippets.common; |
|---|
| 8 |
} |
|---|
| 9 |
|
|---|
| 10 |
void snippet_curve_rectangle(Context cr, int width, int height) |
|---|
| 11 |
{ |
|---|
| 12 |
/* a custom shape, that could be wrapped in a function */ |
|---|
| 13 |
double x0 = 0.1, /*< parameters like cairo_rectangle */ |
|---|
| 14 |
y0 = 0.1, |
|---|
| 15 |
rect_width = 0.8, |
|---|
| 16 |
rect_height = 0.8, |
|---|
| 17 |
radius = 0.4; /*< and an approximate curvature radius */ |
|---|
| 18 |
|
|---|
| 19 |
double x1,y1; |
|---|
| 20 |
|
|---|
| 21 |
x1=x0+rect_width; |
|---|
| 22 |
y1=y0+rect_height; |
|---|
| 23 |
if (!rect_width || !rect_height) |
|---|
| 24 |
return; |
|---|
| 25 |
if (rect_width/2<radius) { |
|---|
| 26 |
if (rect_height/2<radius) { |
|---|
| 27 |
cr.moveTo (x0, (y0 + y1)/2); |
|---|
| 28 |
cr.curveTo (x0 ,y0, x0, y0, (x0 + x1)/2, y0); |
|---|
| 29 |
cr.curveTo (x1, y0, x1, y0, x1, (y0 + y1)/2); |
|---|
| 30 |
cr.curveTo (x1, y1, x1, y1, (x1 + x0)/2, y1); |
|---|
| 31 |
cr.curveTo (x0, y1, x0, y1, x0, (y0 + y1)/2); |
|---|
| 32 |
} else { |
|---|
| 33 |
cr.moveTo (x0, y0 + radius); |
|---|
| 34 |
cr.curveTo (x0 ,y0, x0, y0, (x0 + x1)/2, y0); |
|---|
| 35 |
cr.curveTo (x1, y0, x1, y0, x1, y0 + radius); |
|---|
| 36 |
cr.lineTo (x1 , y1 - radius); |
|---|
| 37 |
cr.curveTo (x1, y1, x1, y1, (x1 + x0)/2, y1); |
|---|
| 38 |
cr.curveTo (x0, y1, x0, y1, x0, y1- radius); |
|---|
| 39 |
} |
|---|
| 40 |
} else { |
|---|
| 41 |
if (rect_height/2<radius) { |
|---|
| 42 |
cr.moveTo (x0, (y0 + y1)/2); |
|---|
| 43 |
cr.curveTo (x0 , y0, x0 , y0, x0 + radius, y0); |
|---|
| 44 |
cr.lineTo (x1 - radius, y0); |
|---|
| 45 |
cr.curveTo (x1, y0, x1, y0, x1, (y0 + y1)/2); |
|---|
| 46 |
cr.curveTo (x1, y1, x1, y1, x1 - radius, y1); |
|---|
| 47 |
cr.lineTo (x0 + radius, y1); |
|---|
| 48 |
cr.curveTo (x0, y1, x0, y1, x0, (y0 + y1)/2); |
|---|
| 49 |
} else { |
|---|
| 50 |
cr.moveTo (x0, y0 + radius); |
|---|
| 51 |
cr.curveTo (x0 , y0, x0 , y0, x0 + radius, y0); |
|---|
| 52 |
cr.lineTo (x1 - radius, y0); |
|---|
| 53 |
cr.curveTo (x1, y0, x1, y0, x1, y0 + radius); |
|---|
| 54 |
cr.lineTo (x1 , y1 - radius); |
|---|
| 55 |
cr.curveTo (x1, y1, x1, y1, x1 - radius, y1); |
|---|
| 56 |
cr.lineTo (x0 + radius, y1); |
|---|
| 57 |
cr.curveTo (x0, y1, x0, y1, x0, y1- radius); |
|---|
| 58 |
} |
|---|
| 59 |
} |
|---|
| 60 |
cr.closePath(); |
|---|
| 61 |
|
|---|
| 62 |
cr.setSourceRGB(0.5, 0.5, 1); |
|---|
| 63 |
cr.fillPreserve(); |
|---|
| 64 |
cr.setSourceRGBA(0.5, 0, 0, 0.5); |
|---|
| 65 |
cr.stroke(); |
|---|
| 66 |
} |
|---|
| 67 |
|
|---|
| 68 |
static this() |
|---|
| 69 |
{ |
|---|
| 70 |
snippets_hash["curve_rectangle"] = &snippet_curve_rectangle; |
|---|
| 71 |
} |
|---|