summaryrefslogtreecommitdiffstats
path: root/src/toys/ellipse-bezier-intersect-toy.cpp
blob: 5468d9797ddb6e8d23158a2f66b1c0efaa731704 (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
#include <2geom/cairo-path-sink.h>
#include <2geom/ellipse.h>
#include <2geom/line.h>
#include <2geom/polynomial.h>
#include <toys/toy-framework-2.h>

using namespace Geom;

class CircleIntersect : public Toy {
    PointSetHandle eh, bh;

    void draw(cairo_t *cr, std::ostringstream *notify, int width, int height, bool save, std::ostringstream *timer_stream) override {

        Rect all(Point(0,0), Point(width, height));

        double rx = Geom::distance(eh.pts[0], eh.pts[1]);
        double ry = Geom::distance(eh.pts[0], eh.pts[2]);
        double rot = Geom::atan2(eh.pts[1] - eh.pts[0]);

        Ellipse e(eh.pts[0], Point(rx, ry), rot);
        D2<Bezier> b(bh.pts);

        cairo_set_line_width(cr, 1.0);
        Geom::CairoPathSink cps(cr);

        // draw Bezier control polygon
        cairo_set_source_rgba(cr, 0, 0, 1, 0.3);
        cps.moveTo(bh.pts[0]);
        for (unsigned i = 1; i < bh.pts.size(); ++i) {
            cps.lineTo(bh.pts[i]);
        }
        cairo_stroke(cr);

        // draw Bezier curve and ellipse
        cairo_set_source_rgb(cr, 0, 0, 0);
        cps.feed(BezierCurve(b), true);
        cps.feed(e);
        cairo_stroke(cr);

        std::vector<ShapeIntersection> result = e.intersect(b);

        cairo_set_source_rgb(cr, 1, 0, 0);
        for (auto & i : result) {
            draw_handle(cr, i.point());
        }
        cairo_stroke(cr);

        Toy::draw(cr, notify, width, height, save,timer_stream);
    }

    public:
    CircleIntersect(){
        eh.push_back(300,300); eh.push_back(450,150); eh.push_back(250, 350);
        bh.push_back(100,100); bh.push_back(500,100); bh.push_back(100,500); bh.push_back(500,500);
        handles.push_back(&eh);
        handles.push_back(&bh);
    }
};

int main(int argc, char **argv) {
    init(argc, argv, new CircleIntersect());
    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 :