summaryrefslogtreecommitdiff
path: root/test/in-fill-trapezoid.c
blob: 84b323a050165b3f3818c859de9ef5f04e71b9d9 (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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
 * Copyright © 2008 Chris Wilson
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 * Author: Chris Wilson <chris@chris-wilson.co.uk>
 */

#include "cairo-test.h"

static cairo_test_status_t
preamble (cairo_test_context_t *ctx)
{
    cairo_test_status_t ret = CAIRO_TEST_SUCCESS;
    cairo_surface_t *surface;
    cairo_t *cr;

    surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 0, 0);
    cr = cairo_create (surface);
    cairo_surface_destroy (surface);

    cairo_set_fill_rule (cr, CAIRO_FILL_RULE_EVEN_ODD);

    /* simple rectangle */
    cairo_new_path (cr);
    cairo_rectangle (cr, -10, -10, 20, 20);
    if (! cairo_in_fill (cr, 0, 0)) {
	cairo_test_log (ctx, "Error: Failed to find point inside rectangle\n");
	ret = CAIRO_TEST_FAILURE;
    }

    /* rectangular boundary tests */
    if (! cairo_in_fill (cr, -10, -10)) {
	cairo_test_log (ctx, "Error: Failed to find top-left vertex inside rectangle\n");
	ret = CAIRO_TEST_FAILURE;
    }
    if (! cairo_in_fill (cr, -10, 10)) {
	cairo_test_log (ctx, "Error: Failed to find bottom-left vertex inside rectangle\n");
	ret = CAIRO_TEST_FAILURE;
    }
    if (! cairo_in_fill (cr, 10, -10)) {
	cairo_test_log (ctx, "Error: Failed to find top-right vertex inside rectangle\n");
	ret = CAIRO_TEST_FAILURE;
    }
    if (! cairo_in_fill (cr, 10, 10)) {
	cairo_test_log (ctx, "Error: Failed to find bottom-right vertex inside rectangle\n");
	ret = CAIRO_TEST_FAILURE;
    }
    if (! cairo_in_fill (cr, -10, 0)) {
	cairo_test_log (ctx, "Error: Failed to find left edge inside rectangle\n");
	ret = CAIRO_TEST_FAILURE;
    }
    if (! cairo_in_fill (cr, 0, -10)) {
	cairo_test_log (ctx, "Error: Failed to find top edge inside rectangle\n");
	ret = CAIRO_TEST_FAILURE;
    }
    if (! cairo_in_fill (cr, 10, 0)) {
	cairo_test_log (ctx, "Error: Failed to find right edge inside rectangle\n");
	ret = CAIRO_TEST_FAILURE;
    }
    if (! cairo_in_fill (cr, 0, 10)) {
	cairo_test_log (ctx, "Error: Failed to find bottom edge inside rectangle\n");
	ret = CAIRO_TEST_FAILURE;
    }

    /* simple circle */
    cairo_new_path (cr);
    cairo_arc (cr, 0, 0, 10, 0, 2 * M_PI);
    if (! cairo_in_fill (cr, 0, 0)) {
	cairo_test_log (ctx, "Error: Failed to find point inside circle [even-odd]\n");
	ret = CAIRO_TEST_FAILURE;
    }

    /* holey rectangle */
    cairo_new_path (cr);
    cairo_rectangle (cr, -10, -10, 20, 20);
    cairo_rectangle (cr, -5, -5, 10, 10);
    if (cairo_in_fill (cr, 0, 0)) {
	cairo_test_log (ctx, "Error: Found an unexpected point inside rectangular eo-hole\n");
	ret = CAIRO_TEST_FAILURE;
    }

    /* holey circle */
    cairo_new_path (cr);
    cairo_arc (cr, 0, 0, 10, 0, 2 * M_PI);
    cairo_arc (cr, 0, 0, 5, 0, 2 * M_PI);
    if (cairo_in_fill (cr, 0, 0)) {
	cairo_test_log (ctx, "Error: Found an unexpected point inside circular eo-hole\n");
	ret = CAIRO_TEST_FAILURE;
    }


    cairo_set_fill_rule (cr, CAIRO_FILL_RULE_WINDING);

    /* simple rectangle */
    cairo_new_path (cr);
    cairo_rectangle (cr, -10, -10, 20, 20);
    if (! cairo_in_fill (cr, 0, 0)) {
	cairo_test_log (ctx, "Error: Failed to find point inside rectangle\n");
	ret = CAIRO_TEST_FAILURE;
    }

    /* simple circle */
    cairo_new_path (cr);
    cairo_arc (cr, 0, 0, 10, 0, 2 * M_PI);
    if (! cairo_in_fill (cr, 0, 0)) {
	cairo_test_log (ctx, "Error: Failed to find point inside circle [nonzero]\n");
	ret = CAIRO_TEST_FAILURE;
    }

    /* overlapping circle/rectangle */
    cairo_new_path (cr);
    cairo_rectangle (cr, -10, -10, 20, 20);
    cairo_new_sub_path (cr);
    cairo_arc (cr, 0, 0, 10, 0, 2 * M_PI);
    if (! cairo_in_fill (cr, 0, 0)) {
	cairo_test_log (ctx, "Error: Failed to find point inside circle+rectangle\n");
	ret = CAIRO_TEST_FAILURE;
    }

    /* holey rectangle */
    cairo_new_path (cr);
    cairo_rectangle (cr, -10, -10, 20, 20);
    cairo_rectangle (cr, 5, -5, -10, 10);
    if (cairo_in_fill (cr, 0, 0)) {
	cairo_test_log (ctx, "Error: Found an unexpected point inside rectangular non-zero-hole\n");
	ret = CAIRO_TEST_FAILURE;
    }

    /* holey circle */
    cairo_new_path (cr);
    cairo_arc (cr, 0, 0, 10, 0, 2 * M_PI);
    cairo_arc_negative (cr, 0, 0, 5, 0, -2 * M_PI);
    if (cairo_in_fill (cr, 0, 0)) {
	cairo_test_log (ctx, "Error: Found an unexpected point inside circular non-zero-hole\n");
	ret = CAIRO_TEST_FAILURE;
    }

    /* not a holey circle */
    cairo_new_path (cr);
    cairo_arc (cr, 0, 0, 10, 0, 2 * M_PI);
    cairo_arc (cr, 0, 0, 5, 0, 2 * M_PI);
    if (! cairo_in_fill (cr, 0, 0)) {
	cairo_test_log (ctx, "Error: Failed to find point inside two circles\n");
	ret = CAIRO_TEST_FAILURE;
    }

    /* check off-centre */
    cairo_new_path (cr);
    cairo_arc (cr, 7.5, 0, 10, 0, 2 * M_PI);
    cairo_arc_negative (cr, 7.5, 0, 5, 0, -2 * M_PI);
    if (cairo_in_fill (cr, 7.5, 0)) {
	cairo_test_log (ctx, "Error: Found an unexpected point inside off-centre-x circular non-zero-hole\n");
	ret = CAIRO_TEST_FAILURE;
    }
    cairo_new_path (cr);
    cairo_arc (cr, 0, 7.5, 10, 0, 2 * M_PI);
    cairo_arc_negative (cr, 0, 7.5, 5, 0, -2 * M_PI);
    if (cairo_in_fill (cr, 0, 7.5)) {
	cairo_test_log (ctx, "Error: Found an unexpected point inside off-centre-y circular non-zero-hole\n");
	ret = CAIRO_TEST_FAILURE;
    }
    cairo_new_path (cr);
    cairo_arc (cr, 15, 0, 10, 0, 2 * M_PI);
    if (! cairo_in_fill (cr, 15, 0)) {
	cairo_test_log (ctx, "Error: Failed to find point inside off-centre-x circle\n");
	ret = CAIRO_TEST_FAILURE;
    }
    cairo_new_path (cr);
    cairo_arc (cr, 0, 15, 10, 0, 2 * M_PI);
    if (! cairo_in_fill (cr, 0, 15)) {
	cairo_test_log (ctx, "Error: Failed to find point inside off-centre-y circle\n");
	ret = CAIRO_TEST_FAILURE;
    }

    /* simple rectangle */
    cairo_new_path (cr);
    cairo_rectangle (cr, 10, 0, 5, 5);
    if (cairo_in_fill (cr, 0, 0)) {
	cairo_test_log (ctx, "Error: Found an unexpected point outside rectangle\n");
	ret = CAIRO_TEST_FAILURE;
    }
    if (cairo_in_fill (cr, 20, 20)) {
	cairo_test_log (ctx, "Error: Found an unexpected point outside rectangle\n");
	ret = CAIRO_TEST_FAILURE;
    }
    if (! cairo_in_fill (cr, 12.5, 2.5)) {
	cairo_test_log (ctx, "Error: Failed to find point inside rectangle\n");
	ret = CAIRO_TEST_FAILURE;
    }

    /* off-centre triangle */
    cairo_new_path (cr);
    cairo_move_to (cr, 10, 0);
    cairo_line_to (cr, 15, 5);
    cairo_line_to (cr, 5, 5);
    cairo_close_path (cr);
    if (cairo_in_fill (cr, 0, 0) ||
	cairo_in_fill (cr, 5, 0) ||
	cairo_in_fill (cr, 15, 0) ||
	cairo_in_fill (cr, 20, 0) ||
	cairo_in_fill (cr, 0, 10) ||
	cairo_in_fill (cr, 10, 10) ||
	cairo_in_fill (cr, 20, 10) ||
	cairo_in_fill (cr, 7, 2.5) ||
	cairo_in_fill (cr, 13, 2.5))
    {
	cairo_test_log (ctx,
			"Error: Found an unexpected point outside triangle\n"
			"\t(0, 0) -> %s\n"
			"\t(5, 0) -> %s\n"
			"\t(15, 0) -> %s\n"
			"\t(20, 0) -> %s\n"
			"\t(0, 10) -> %s\n"
			"\t(10, 10) -> %s\n"
			"\t(20, 10) -> %s\n"
			"\t(7, 2.5) -> %s\n"
			"\t(13, 2.5) -> %s\n",
			cairo_in_fill (cr, 0, 0) ? "inside" : "outside",
			cairo_in_fill (cr, 5, 0) ? "inside" : "outside",
			cairo_in_fill (cr, 15, 0) ? "inside" : "outside",
			cairo_in_fill (cr, 20, 0) ? "inside" : "outside",
			cairo_in_fill (cr, 0, 10) ? "inside" : "outside",
			cairo_in_fill (cr, 10, 10) ? "inside" : "outside",
			cairo_in_fill (cr, 20, 10) ? "inside" : "outside",
			cairo_in_fill (cr, 7, 2.5) ? "inside" : "outside",
			cairo_in_fill (cr, 13, 2.5) ? "inside" : "outside");
	ret = CAIRO_TEST_FAILURE;
    }
    if (! cairo_in_fill (cr, 7.5, 2.5) ||
	! cairo_in_fill (cr, 12.5, 2.5) ||
	! cairo_in_fill (cr, 10, 5))
    {
	cairo_test_log (ctx,
			"Error: Failed to find point on triangle edge\n"
			"\t(7.5, 2.5) -> %s\n"
			"\t(12.5, 2.5) -> %s\n"
			"\t(10, 5) -> %s\n",
			cairo_in_fill (cr, 7.5, 2.5) ? "inside" : "outside",
			cairo_in_fill (cr, 12.5, 2.5) ? "inside" : "outside",
			cairo_in_fill (cr, 10, 5) ? "inside" : "outside");
	ret = CAIRO_TEST_FAILURE;
    }
    if (! cairo_in_fill (cr, 8, 2.5) ||
	! cairo_in_fill (cr, 12, 2.5))
    {
	cairo_test_log (ctx, "Error: Failed to find point inside triangle\n");
	ret = CAIRO_TEST_FAILURE;
    }

    cairo_destroy (cr);

    return ret;
}

CAIRO_TEST (in_fill_trapezoid,
	    "Test cairo_in_fill",
	    "in, trap", /* keywords */
	    NULL, /* requirements */
	    0, 0,
	    preamble, NULL)