summaryrefslogtreecommitdiffstats
path: root/src/3rdparty/2geom/src/toys/sweep.cpp
blob: 4f26b8161208a10f07af93b2f0f4bc8522de04d3 (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
#include <2geom/sweep-bounds.h>

#include <toys/path-cairo.h>
#include <toys/toy-framework-2.h>

using namespace Geom;

class Sweep: public Toy {
public:
    PointSetHandle hand;
    unsigned count_a, count_b;
    void draw(cairo_t *cr, std::ostringstream *notify, int width, int height, bool save, std::ostringstream *timer_stream) override {
        std::vector<Rect> rects_a, rects_b;
        cairo_set_source_rgb(cr, 0,0,0);

        for(unsigned i = 0; i < count_a; i++)
            rects_a.emplace_back(hand.pts[i*2], hand.pts[i*2+1]);

        for(unsigned i = 0; i < count_b; i++)
            rects_b.emplace_back(hand.pts[i*2 + count_a*2], hand.pts[i*2+1 + count_a*2]);
        
        {
            std::vector<std::vector<unsigned> > res = sweep_bounds(rects_a);
            cairo_set_line_width(cr,0.5);
            cairo_save(cr);
            cairo_set_source_rgb(cr, 1, 0, 0);
            for(unsigned i = 0; i < res.size(); i++) {
                for(unsigned j = 0; j < res[i].size(); j++) {
                    draw_line_seg(cr, rects_a[i].midpoint(), rects_a[res[i][j]].midpoint());
                    cairo_stroke(cr);
                }
            }
            cairo_restore(cr);
        }{
            std::vector<std::vector<unsigned> > res = sweep_bounds(rects_a, rects_b);
            cairo_set_line_width(cr,0.5);
            cairo_save(cr);
            cairo_set_source_rgb(cr, 0.5, 0, 0.5);
            for(unsigned i = 0; i < res.size(); i++) {
                for(unsigned j = 0; j < res[i].size(); j++) {
                    draw_line_seg(cr, rects_a[i].midpoint(), rects_b[res[i][j]].midpoint());
                    cairo_stroke(cr);
                }
            }
            cairo_restore(cr);
        }
        cairo_set_line_width(cr,3);
        cairo_set_source_rgba(cr,1,0,0,1);
        for(unsigned i = 0; i < count_a; i++)
            cairo_rectangle(cr, rects_a[i].left(), rects_a[i].top(), rects_a[i].width(), rects_a[i].height());
        cairo_stroke(cr);
        
        cairo_set_source_rgba(cr,0,0,1,1);
        for(unsigned i = 0; i < count_b; i++)
            cairo_rectangle(cr, rects_b[i].left(), rects_b[i].top(), rects_b[i].width(), rects_b[i].height());
        cairo_stroke(cr);
        
        Toy::draw(cr, notify, width, height, save,timer_stream);
    }
    bool should_draw_numbers() override { return false; }
    public:
    Sweep () {
        count_a = 20;
        count_b = 10;
        for(unsigned i = 0; i < (count_a + count_b); i++) {
            Point dim(uniform() * 90 + 10, uniform() * 90 + 10),
                  pos(uniform() * 500 + 50, uniform() * 500 + 50);
            hand.pts.push_back(pos - dim/2);
            hand.pts.push_back(pos + dim/2);
        }
        handles.push_back(&hand);
    }
};

int main(int argc, char **argv) {
    init(argc, argv, new Sweep());
    return 0;
}

/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=4:softtabstop=4:fileencoding=utf-8:textwidth=99 :