summaryrefslogtreecommitdiffstatsci
path: root/tests/curve.c
blob: d1bcfd1faf62a2d25294036af76fdc1326ac5cfd (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
#include "test.h"

//"M80 170   C100 170 160 170 180 170 lZ"
void test3() {
    VkvgContext ctx = vkvg_create(surf);
    vkvg_clear(ctx);
    vkvg_set_line_width(ctx, 20);
    vkvg_set_source_rgb(ctx, 1, 0, 0);
    vkvg_move_to(ctx, 80, 170);
    vkvg_curve_to(ctx, 100, 170, 160, 171, 180, 170);
    // vkvg_rel_line_to (ctx, -30,100);
    vkvg_close_path(ctx);
    vkvg_stroke(ctx);
    vkvg_destroy(ctx);
}
void test() {
    VkvgContext ctx = vkvg_create(surf);

    vkvg_set_line_width(ctx, 20);

    vkvg_scale(ctx, 2, 2);
    vkvg_set_line_join(ctx, VKVG_LINE_JOIN_BEVEL);

    // vkvg_arc (ctx, 200, 500, 100, 0, M_PI);

    vkvg_set_source_rgb(ctx, 0.5f, 0, 0);

    /*vkvg_move_to(ctx,100,100);
    vkvg_line_to(ctx,300,100);
    vkvg_line_to(ctx,500,300);
    vkvg_line_to(ctx,300,500);
    //vkvg_arc (ctx, 200, 500, 100, 0, M_PI);
    vkvg_line_to(ctx,300,700);
    vkvg_line_to(ctx,100,500);*/

    /*vkvg_arc(ctx, 300, 300, 100, 0, M_PI);
    vkvg_line_to(ctx,100,200);
    vkvg_line_to(ctx,200,100);
    vkvg_arc(ctx, 250, 100, 50, M_PI, M_PI * 1.5f);
    vkvg_line_to(ctx,350,50);
    vkvg_arc(ctx, 350, 100, 50, M_PI*1.5f, M_PI * 2.0f);

    vkvg_stroke(ctx);
    vkvg_translate(ctx,400,30);

    */
    // vkvg_set_fill_rule(ctx,VKVG_FILL_RULE_EVEN_ODD);
    vkvg_translate(ctx, 200, 30);
    vkvg_arc(ctx, 200, 200, 20, 0, M_PIF * 2);
    // vkvg_stroke(ctx);

    vkvg_set_source_rgba(ctx, 0.5f, 0.0f, 1.0f, 0.5f);
    vkvg_move_to(ctx, 100, 100);
    vkvg_line_to(ctx, 200, 100);
    // vkvg_move_to(ctx,200,100);
    vkvg_curve_to(ctx, 250, 100, 300, 150, 300, 200);
    vkvg_line_to(ctx, 300, 300);
    vkvg_curve_to(ctx, 300, 350, 250, 400, 200, 400);
    vkvg_line_to(ctx, 100, 400);
    vkvg_curve_to(ctx, 50, 400, 10, 350, 10, 300);
    vkvg_line_to(ctx, 10, 200);
    vkvg_curve_to(ctx, 10, 150, 50, 100, 100, 100);
    // vkvg_close_path(ctx);
    vkvg_fill_preserve(ctx);
    vkvg_set_source_rgba(ctx, 0.1f, 0.3f, 0.7f, 0.5f);
    vkvg_stroke(ctx);

    vkvg_destroy(ctx);
}

void curved_rect() {
    VkvgContext ctx = vkvg_create(surf);

    float x = 50, y = 50, width = 150, height = 140, radius = 30;

    vkvg_scale(ctx, 2, 2);
    // vkvg_rotate(ctx,0.5f);

    vkvg_set_line_width(ctx, 15);
    vkvg_set_source_rgba(ctx, 0, 0.5f, 0.4f, 1);

    if ((radius > height / 2) || (radius > width / 2))
        radius = MIN(height / 2, width / 2);

    vkvg_move_to(ctx, x, y + radius);
    vkvg_arc(ctx, x + radius, y + radius, radius, M_PIF, (float)-M_PI_2);
    vkvg_line_to(ctx, x + width - radius, y);
    vkvg_arc(ctx, x + width - radius, y + radius, radius, (float)-M_PI_2, 0);
    vkvg_line_to(ctx, x + width, y + height - radius);
    vkvg_arc(ctx, x + width - radius, y + height - radius, radius, 0, (float)M_PI_2);
    vkvg_line_to(ctx, x + radius, y + height);
    vkvg_arc(ctx, x + radius, y + height - radius, radius, (float)M_PI_2, M_PIF);
    vkvg_line_to(ctx, x, y + radius);
    vkvg_close_path(ctx);
    vkvg_fill_preserve(ctx);
    vkvg_set_source_rgba(ctx, 0.5f, 0, 0, 0.5f);
    vkvg_stroke(ctx);

    vkvg_destroy(ctx);
}

void test2() {
    VkvgContext ctx = vkvg_create(surf);

    vkvg_move_to(ctx, 100, 400);
    vkvg_curve_to(ctx, 100, 100, 600, 700, 600, 400);
    vkvg_curve_to(ctx, 1000, 100, 100, 800, 1000, 800);
    vkvg_curve_to(ctx, 1000, 500, 700, 500, 700, 100);
    vkvg_close_path(ctx);

    // vkvg_set_source_rgba   (ctx, 0.5,0.0,1.0,0.5);
    // vkvg_fill_preserve(ctx);

    vkvg_set_source_rgba(ctx, 1, 0, 0, 1);
    vkvg_set_line_width(ctx, 40);
    vkvg_stroke(ctx);

    vkvg_destroy(ctx);
}

static bool fillAndStroke = true;

void random_curves_stroke() {
    float w = (float)test_width;
    float h = (float)test_height;

    VkvgContext ctx = _initCtx();

    for (uint32_t i = 0; i < test_size; i++) {
        randomize_color(ctx);
        float x1 = rndf() * w;
        float y1 = rndf() * h;

        vkvg_move_to(ctx, x1, y1);
        draw_random_curve(ctx);

        vkvg_stroke(ctx);
    }
    vkvg_destroy(ctx);
}

void _long_curv() {
    float w = (float)test_width;
    float h = (float)test_height;

    VkvgContext ctx = _initCtx();

    randomize_color(ctx);
    float x1 = rndf() * w;
    float y1 = rndf() * h;
    vkvg_move_to(ctx, x1, y1);

    for (uint32_t i = 0; i < test_size; i++) {
        draw_random_curve(ctx);
    }

    if (fillAndStroke) {
        vkvg_fill_preserve(ctx);
        randomize_color(ctx);
        vkvg_stroke(ctx);
    } else
        vkvg_fill(ctx);

    vkvg_destroy(ctx);
}

void long_curv_fill_nz() {
    fillAndStroke = false;
    fill_rule     = VKVG_FILL_RULE_NON_ZERO;
    _long_curv();
}
void long_curv_fill_eo() {
    fillAndStroke = false;
    fill_rule     = VKVG_FILL_RULE_EVEN_ODD;
    _long_curv();
}
void long_curv_fill_stroke_nz() {
    fillAndStroke = true;
    fill_rule     = VKVG_FILL_RULE_NON_ZERO;
    _long_curv();
}
void long_curv_fill_stroke_eo() {
    fillAndStroke = true;
    fill_rule     = VKVG_FILL_RULE_EVEN_ODD;
    _long_curv();
}
int main(int argc, char *argv[]) {
    no_test_size = true;
    PERFORM_TEST(test3, argc, argv);
    // return 0;
    PERFORM_TEST(test, argc, argv);
    PERFORM_TEST(test2, argc, argv);
    PERFORM_TEST(curved_rect, argc, argv);
    // PERFORM_TEST(long_curv_fill_nz, argc, argv);
    no_test_size = false;
    PERFORM_TEST(long_curv_fill_eo, argc, argv);
    // PERFORM_TEST(long_curv_fill_stroke_nz, argc, argv);
    PERFORM_TEST(long_curv_fill_stroke_eo, argc, argv);
    PERFORM_TEST(random_curves_stroke, argc, argv);
    return 0;
}