summaryrefslogtreecommitdiffstats
path: root/src/toys/circle-line-intersect.cpp
blob: 3946ca1e8e0004b8b4aa0adac3057eb7f0a4cb33 (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
#include <2geom/circle.h>
#include <toys/toy-framework-2.h>

using namespace Geom;

class CircleIntersect : public Toy {
    PointSetHandle psh[2];
    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 r = Geom::distance(psh[0].pts[0], psh[0].pts[1]);

        Circle circ(psh[0].pts[0], r);
        Line line(psh[1].pts[0], psh[1].pts[1]);

        std::vector<ShapeIntersection> result = circ.intersect(line);

        cairo_set_line_width(cr, 1.0);

        // draw the shapes
        cairo_set_source_rgb(cr, 0, 0, 0);
        cairo_arc(cr, circ.center(X), circ.center(Y), circ.radius(), 0, 2*M_PI);
        draw_line(cr, line, all);
        cairo_stroke(cr);

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

        // show message
        if (!result.empty()) {
            for (auto & i : result) {
                *notify << i.point() << ", ";
            }
        } else {
            *notify << "No intersection";
        }

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

    public:
    CircleIntersect(){
        psh[0].push_back(200,200); psh[0].push_back(250,200);
        psh[1].push_back(150,150); psh[1].push_back(250,150);
        handles.push_back(&psh[0]);
        handles.push_back(&psh[1]);
    }
};

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 :