summaryrefslogtreecommitdiffstats
path: root/src/toys/portion-test.cpp
blob: bc64db37e8aad320e8baf992e721f92a015f5bc7 (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
#include <2geom/d2.h>
#include <2geom/sbasis.h>
#include <2geom/sbasis-2d.h>
#include <2geom/bezier-to-sbasis.h>
#include <2geom/sbasis-geometric.h>

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

#include <time.h>
using std::vector;
using namespace Geom;
using namespace std;

// TODO: 
// use path2
// replace Ray stuff with path2 line segments.

//-----------------------------------------------

class PortionTester: public Toy {
    PointSetHandle curve_handle;
    PointHandle sample_point1, sample_point2;
    std::vector<Toggle> toggles;
    void mouse_pressed(GdkEventButton* e) override {
        toggle_events(toggles, e);
        Toy::mouse_pressed(e);
    }
    void draw(cairo_t *cr, std::ostringstream *notify, int width, int height, bool save, std::ostringstream *timer_stream) override {
    
        draw_toggles(cr, toggles);
        D2<SBasis> B = curve_handle.asBezier();
        
        cairo_set_line_width (cr, 1);
        cairo_set_source_rgba (cr, 0., 0.5, 0., 1);
        cairo_d2_sb(cr, B);
        cairo_stroke(cr);

	sample_point1.pos[1]=400;
	sample_point1.pos[0]=std::max(150.,sample_point1.pos[0]);
	sample_point1.pos[0]=std::min(450.,sample_point1.pos[0]);
	sample_point2.pos[1]=400;
	sample_point2.pos[0]=std::max(150.,sample_point2.pos[0]);
	sample_point2.pos[0]=std::min(450.,sample_point2.pos[0]);
	cairo_move_to(cr, Geom::Point(150,400));
	cairo_line_to(cr, Geom::Point(450,400));
	cairo_set_source_rgba (cr, 0., 0., 0.5, 0.8);
	cairo_stroke(cr);

	double t0=std::max(0.,std::min(1.,(sample_point1.pos[0]-150)/300.));
	double t1=std::max(0.,std::min(1.,(sample_point2.pos[0]-150)/300.));
        
        Path P;
        P.append(B);
        
        if (toggles[0].on) {
            if (toggles[1].on)
                cairo_curve(cr, P.portion(t0,t1)[0]);
            else
                cairo_path(cr, P.portion(t0,t1));
        } else
            cairo_d2_sb(cr, portion(B,t0,t1));
        
        
	cairo_set_source_rgba (cr, 0.5, 0.2, 0., 0.8);
	cairo_stroke(cr);
        Toy::draw(cr, notify, width, height, save,timer_stream);
    }        
  
public:
    PortionTester(){
        toggles.emplace_back("Path", true);
        toggles[0].bounds = Rect(Point(10,100), Point(100, 130));
        toggles.emplace_back("Curve", true);
        toggles[1].bounds = Rect(Point(10,130), Point(100, 160));
        if(handles.empty()) {
            handles.push_back(&curve_handle);
            handles.push_back(&sample_point1);
            handles.push_back(&sample_point2);
            for(unsigned i = 0; i < 4; i++)
                curve_handle.push_back(150+uniform()*300,150+uniform()*300);
            sample_point1.pos = Geom::Point(250,300);
            sample_point2.pos = Geom::Point(350,300);
        }
    }
};

int main(int argc, char **argv) {
    std::cout << "testing unit_normal(multidim_sbasis) based offset." << std::endl;
    init(argc, argv, new PortionTester);
    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:expandtab:shiftwidth = 4:tabstop = 8:softtabstop = 4:encoding = utf-8:textwidth = 99 :