summaryrefslogtreecommitdiff
path: root/doc/tutorial/src/singular.c
blob: 958b2b0472db929646c722160d8384bc8740dbc4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/**
 * Uses Singular values of transformation matrix to find the length of the
 * major and minor axes of the scaled pen.
 *
 * Put this file in cairo/doc/tutorial/src and type "make"
 */

#define WIDTH 300
#define HEIGHT 300

#include "cairo-tutorial.h"

#include <math.h>

/*
 * Finds the singular values of the non-translation part of matrix.
 *
 * Let M be the cairo transformation matrix in question:
 *
 *       ⌈xx xy⌉
 *   M = |yx yy|
 *       ⌊x0 y0⌋
 *
 * The non-translation part is:
 *
 *   A = ⌈xx xy⌉
 *       ⌊yx yy⌋
 *
 * The non-zero singular values of A are the square roots of the non-zero
 * eigenvalues of A⁺ A, where A⁺ is A-transpose.
 *
 *   A⁺ A = ⌈xx yx⌉⌈xx xy⌉ = ⌈xx²+yx²     xx*xy+yx*yy⌉
 *          ⌊xy yy⌋⌊yx yy⌋   ⌊xx*xy+yx*yy     xy²+yy²⌋
 *
 * Name those:
 *
 *   B = A⁺ A = ⌈a k⌉
 *              ⌊k b⌋
 *
 * The eigenvalues of B satisfy:
 *
 *   λ² - (a+b).λ + a.b - k² = 0
 *
 * The eigenvalues are:
 *                __________________
 *       (a+b) ± √(a+b)² - 4(a.b-k²)
 *   λ = ---------------------------
 *                   2
 * that simplifies to:
 *                  _______________
 *   λ = (a+b)/2 ± √((a-b)/2)² + k²
 *
 * And the Singular values are the root of λs.
 *
 */
static void
get_singular_values (const cairo_matrix_t *matrix,
		     double *major,
		     double *minor)
{
    double xx = matrix->xx, xy = matrix->xy;
    double yx = matrix->yx, yy = matrix->yy;

    double a = xx*xx+yx*yx;
    double b = xy*xy+yy*yy;
    double k = xx*xy+yx*yy;

    double f = (a+b) * .5;
    double g = (a-b) * .5;
    double delta = sqrt (g*g + k*k);

    if (major)
	*major = sqrt (f + delta);
    if (minor)
	*minor = sqrt (f - delta);
}

/*
 * Finds the length of the major and minor axes of the pen for a cairo_t,
 * identified by the current transformation matrix and line width.
 *
 * Returned values are in device units.
 */
static void
get_pen_axes (cairo_t *cr,
	      double *major,
	      double *minor)
{
    double width;
    cairo_matrix_t matrix;

    width = cairo_get_line_width (cr);
    cairo_get_matrix (cr, &matrix);

    get_singular_values (&matrix, major, minor);

    if (major)
	*major *= width;
    if (minor)
	*minor *= width;
}

static void
draw (cairo_t *cr, int width, int height)
{
    double major_width, minor_width;

    /* clear background */
    cairo_set_source_rgb (cr, 1.0, 1.0, 1.0); /* white */
    cairo_paint (cr);

#define W width
#define H height
#define B ((width+height)/16)

    /* the spline we want to stroke */
    cairo_move_to  (cr, W-B, B);
    cairo_curve_to (cr, -W,   B,
		        2*W, H-B,
			B,   H-B);

    /* the effect is show better with round caps */
    cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);

    /* set the skewed pen */
    cairo_rotate (cr, +.7);
    cairo_scale  (cr, .5, 2.);
    cairo_rotate (cr, -.7);
    cairo_set_line_width (cr, B);

    get_pen_axes (cr, &major_width, &minor_width);

    /* stroke with "major" pen in translucent red */
    cairo_save (cr);
    cairo_identity_matrix (cr);
    cairo_set_line_width (cr, major_width);
    cairo_set_source_rgba (cr, 1.0, 0.0, 0.0, .9);
    cairo_stroke_preserve (cr);
    cairo_restore (cr);

    /* stroke with skewed pen in translucent black */
    cairo_set_source_rgba (cr, 0.0, 0.0, 0.0, .9);
    cairo_stroke_preserve (cr);

    /* stroke with "minor" pen in translucent yellow */
    cairo_save (cr);
    cairo_identity_matrix (cr);
    cairo_set_line_width (cr, minor_width);
    cairo_set_source_rgba (cr, 1.0, 1.0, 0.0, .9);
    cairo_stroke_preserve (cr);
    cairo_restore (cr);

    /* stroke with hairline in black */
    cairo_save (cr);
    cairo_identity_matrix (cr);
    cairo_set_line_width (cr, 1);
    cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
    cairo_stroke_preserve (cr);
    cairo_restore (cr);

    cairo_new_path (cr);
}