Re: Drawing a multicolor spline
Andrea Canciani <[email protected]>
| Newsgroups | gmane.comp.lib.cairo |
|---|---|
| Message-ID | <CAN_5=BBAC204WciopnLad1u7gBE0m4ZZoVLdu0x28+H32f2PZw@mail.gmail.com> |
On Fri, Apr 27, 2012 at 9:45 AM, Andrea Canciani <[email protected]> wrote: > On Wed, Apr 25, 2012 at 8:35 PM, Davide Calaminici <[email protected]> wrote: >> Hello, >> >> i'm trying to learn cairo graphics doing some test here and there. >> >> I want to draw a spline or bezier, simulating a Windows screen saver like >> Mistify or Ribbons. >> Anyone have a simple sample code in c for this? Some sample code which might be somewhat similar to Mystify is attached. Andrea > > Some sample code on how to draw Bezier curves can be found here: > http://cairographics.org/samples/curve_to/ > > If you want the spline to be multicolor, you will need to draw it with > a source that is not solid black, but some kind of pattern. > > > A different approach might be that of painting a gradient mesh pattern > (possibly without even using a mask). > Sample code can be found here: > http://cairographics.org/manual/cairo-cairo-pattern-t.html#cairo-pattern-create-mesh > > Andrea > >> >> Thank you very much >> >> -- >> cairo mailing list >> [email protected] >> http://lists.cairographics.org/mailman/listinfo/cairo -- cairo mailing list [email protected] http://lists.cairographics.org/mailman/listinfo/cairo
mystify.c
(text/x-csrc, 2.4 KB)
#include <stdio.h>
#include <stdlib.h>
#include <cairo.h>
#define WIDTH 1024
#define HEIGHT 768
#define RING_SIZE 16
#define MAX_POINTS 19
static double x[MAX_POINTS], y[MAX_POINTS];
static double r, g, b;
static double dx[MAX_POINTS], dy[MAX_POINTS];
static double dr, dg, db;
static double randdouble (double a, double b)
{
return (random () / (1. + RAND_MAX)) * (b - a) + a;
}
static void nextvalues (void)
{
int i;
for (i = 0; i < MAX_POINTS; i++) {
x[i] += dx[i];
y[i] += dy[i];
}
r += dr;
g += dg;
b += db;
}
static void init_values (void)
{
int i;
srandomdev ();
for (i = 0; i < MAX_POINTS; i++) {
x[i] = randdouble (0, WIDTH);
y[i] = randdouble (0, HEIGHT);
dx[i] = randdouble (-15., 15.);
dy[i] = randdouble (-15., 15.);
}
r = randdouble (0., 1.);
g = randdouble (0., 1.);
b = randdouble (0., 1.);
dr = randdouble (-0.1, 0.1);
dg = randdouble (-0.1, 0.1);
db = randdouble (-0.1, 0.1);
}
static void my_line(cairo_surface_t *surface)
{
cairo_t *cr;
int i;
cr = cairo_create (surface);
cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
cairo_paint (cr);
nextvalues ();
for (i = 0; i < MAX_POINTS; i++)
cairo_line_to (cr, x[i], y[i]);
cairo_close_path (cr);
cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
cairo_set_source_rgb (cr, r, g, b);
cairo_stroke (cr);
cairo_destroy (cr);
}
int main (int argc, char **argv)
{
char filename[256];
cairo_surface_t *ring[RING_SIZE];
cairo_surface_t *output;
cairo_t *cr;
int i, j, num_outs;
if (argc > 2)
num_outs = atoi (argv[1]);
else
num_outs = 1;
init_values ();
output = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, WIDTH, HEIGHT);
for (i = 0; i < RING_SIZE; i++) {
ring[i] = cairo_surface_create_similar (output,
CAIRO_CONTENT_COLOR_ALPHA,
WIDTH, HEIGHT);
my_line (ring[i]);
}
for (j = 0; j < num_outs; j++) {
cr = cairo_create (output);
cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
cairo_set_source_surface (cr, ring[j % RING_SIZE], 0, 0);
cairo_paint (cr);
cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
for (i = 1; i < RING_SIZE; i++) {
cairo_set_source_surface (cr, ring[(i + j) % RING_SIZE], 0, 0);
cairo_paint (cr);
}
cairo_destroy (cr);
sprintf (filename, "output%d.png", j);
cairo_surface_write_to_png (output, filename);
my_line (ring[j % RING_SIZE]);
}
return 0;
}