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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
|
/*
* Rect Toy
*
* Copyright 2008 njh <>
* multitoy approach
* Copyright 2008 Marco <>
*
* This library is free software; you can redistribute it and/or
* modify it either under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation
* (the "LGPL") or, at your option, under the terms of the Mozilla
* Public License Version 1.1 (the "MPL"). If you do not alter this
* notice, a recipient may use your version of this file under either
* the MPL or the LGPL.
*
* You should have received a copy of the LGPL along with this library
* in the file COPYING-LGPL-2.1; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* You should have received a copy of the MPL along with this library
* in the file COPYING-MPL-1.1
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
* OF ANY KIND, either express or implied. See the LGPL or the MPL for
* the specific language governing rights and limitations.
*/
#include <2geom/line.h>
#include <toys/path-cairo.h>
#include <toys/toy-framework-2.h>
#include <2geom/transforms.h>
#include <2geom/angle.h>
#include <vector>
#include <string>
#include <optional>
using namespace Geom;
std::string angle_formatter(double angle)
{
return default_formatter(decimal_round(deg_from_rad(angle),2));
}
class LineToy : public Toy
{
enum menu_item_t
{
SHOW_MENU = 0,
TEST_CREATE,
TEST_PROJECTION,
TEST_ORTHO,
TEST_DISTANCE,
TEST_POSITION,
TEST_SEG_BISEC,
TEST_ANGLE_BISEC,
TEST_COLLINEAR,
TEST_INTERSECTIONS,
TOTAL_ITEMS // this one must be the last item
};
enum handle_label_t
{
};
enum toggle_label_t
{
};
enum slider_label_t
{
ANGLE_SLIDER = 0,
};
static const char* menu_items[TOTAL_ITEMS];
static const char keys[TOTAL_ITEMS];
void first_time(int /*argc*/, char** /*argv*/) override
{
draw_f = &LineToy::draw_menu;
}
void init_common()
{
set_common_control_geometry = true;
set_control_geometry = true;
sliders.clear();
toggles.clear();
handles.clear();
}
virtual void draw_common( cairo_t *cr, std::ostringstream *notify,
int width, int height, bool /*save*/ )
{
init_common_ctrl_geom(cr, width, height, notify);
}
void init_create()
{
init_common();
p1.pos = Point(400, 50);
p2.pos = Point(450, 450);
sliders.emplace_back(0, 2*M_PI, 0, 0, "angle");
sliders[ANGLE_SLIDER].formatter(&angle_formatter);
handles.push_back(&p1);
handles.push_back(&p2);
handles.push_back(&(sliders[ANGLE_SLIDER]));
}
void draw_create(cairo_t *cr, std::ostringstream *notify,
int width, int height, bool save, std::ostringstream */*timer_stream*/)
{
draw_common(cr, notify, width, height, save);
init_create_ctrl_geom(cr, notify, width, height);
Rect r1(p1.pos, p2.pos);
cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);
cairo_set_line_width(cr, 0.3);
cairo_rectangle(cr, r1);
Affine rot = Translate(-r1.midpoint())*Rotate(sliders[ANGLE_SLIDER].value())*Translate(r1.midpoint());
cairo_rectangle(cr, r1*rot);
cairo_move_to(cr, r1.corner(3)*rot);
for(int i = 0; i < 4; i++) {
cairo_line_to(cr, r1.corner(i)*rot);
}
cairo_stroke(cr);
draw_label(cr, p1, "P1");
draw_label(cr, p2, "P2");
}
void init_intersections()
{
init_common();
p1.pos = Point(400, 50);
p2.pos = Point(450, 450);
p3.pos = Point(100, 250);
p4.pos = Point(200, 450);
p5.pos = Point(50, 150);
p6.pos = Point(480, 60);
handles.push_back(&p1);
handles.push_back(&p2);
handles.push_back(&p3);
handles.push_back(&p4);
handles.push_back(&p5);
handles.push_back(&p6);
}
void draw_intersections(cairo_t *cr, std::ostringstream *notify,
int width, int height, bool save,
std::ostringstream */*timer_stream*/)
{
draw_common(cr, notify, width, height, save);
Rect r1(p1.pos, p2.pos);
Rect r2(p3.pos, p4.pos);
Line l1(p5.pos, p6.pos);
cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);
cairo_set_line_width(cr, 0.3);
cairo_rectangle(cr, r1);
cairo_stroke(cr);
cairo_set_source_rgba(cr, 0.0, 0.3, 0.0, 1.0);
cairo_rectangle(cr, r2);
cairo_stroke(cr);
OptRect r1xr2 = intersect(r1, r2);
if(r1xr2) {
cairo_set_source_rgba(cr, 1.0, 0.7, 0.7, 1.0);
cairo_rectangle(cr, *r1xr2);
cairo_fill(cr);
}
cairo_set_source_rgba(cr, 0.2, 0.2, 0.2, 1.0);
cairo_set_line_width(cr, 0.3);
draw_line(cr, l1);
cairo_stroke(cr);
std::optional<LineSegment> ls = rect_line_intersect(r1, LineSegment(p5.pos, p6.pos));
*notify << "intersects: " << ((ls)?"true":"false") << std::endl;
if(ls) {
draw_handle(cr, (*ls)[0]);
draw_handle(cr, (*ls)[1]);
cairo_save(cr);
cairo_set_line_width(cr, 2);
cairo_move_to(cr, (*ls)[0]);
cairo_line_to(cr, (*ls)[1]);
cairo_stroke(cr);
cairo_restore(cr);
}
}
void init_common_ctrl_geom(cairo_t* /*cr*/, int /*width*/, int /*height*/, std::ostringstream* /*notify*/)
{
if ( set_common_control_geometry )
{
set_common_control_geometry = false;
}
}
void init_create_ctrl_geom(cairo_t* /*cr*/, std::ostringstream* /*notify*/, int /*width*/, int height)
{
if ( set_control_geometry )
{
set_control_geometry = false;
sliders[ANGLE_SLIDER].geometry(Point(50, height - 50), 180);
}
}
void draw_segment(cairo_t* cr, Point const& p1, Point const& p2)
{
cairo_move_to(cr, p1);
cairo_line_to(cr, p2);
}
void draw_segment(cairo_t* cr, Point const& p1, double angle, double length)
{
Point p2;
p2[X] = length * std::cos(angle);
p2[Y] = length * std::sin(angle);
p2 += p1;
draw_segment(cr, p1, p2);
}
void draw_segment(cairo_t* cr, LineSegment const& ls)
{
draw_segment(cr, ls[0], ls[1]);
}
void draw_ray(cairo_t* cr, Ray const& r)
{
double angle = r.angle();
draw_segment(cr, r.origin(), angle, m_length);
}
void draw_line(cairo_t* cr, Line const& l)
{
double angle = l.angle();
draw_segment(cr, l.origin(), angle, m_length);
draw_segment(cr, l.origin(), angle, -m_length);
}
void draw_label(cairo_t* cr, PointHandle const& ph, const char* label)
{
draw_text(cr, ph.pos+op, label);
}
void draw_label(cairo_t* cr, Line const& l, const char* label)
{
draw_text(cr, projection(Point(m_width/2-30, m_height/2-30), l)+op, label);
}
void draw_label(cairo_t* cr, LineSegment const& ls, const char* label)
{
draw_text(cr, middle_point(ls[0], ls[1])+op, label);
}
void draw_label(cairo_t* cr, Ray const& r, const char* label)
{
Point prj = r.pointAt(r.nearestTime(Point(m_width/2-30, m_height/2-30)));
if (L2(r.origin() - prj) < 100)
{
prj = r.origin() + 100*r.vector();
}
draw_text(cr, prj+op, label);
}
void init_menu()
{
handles.clear();
sliders.clear();
toggles.clear();
}
void draw_menu( cairo_t * /*cr*/, std::ostringstream *notify,
int /*width*/, int /*height*/, bool /*save*/,
std::ostringstream */*timer_stream*/)
{
*notify << std::endl;
for (int i = SHOW_MENU; i < TOTAL_ITEMS; ++i)
{
*notify << " " << keys[i] << " - " << menu_items[i] << std::endl;
}
}
void key_hit(GdkEventKey *e) override
{
char choice = std::toupper(e->keyval);
switch ( choice )
{
case 'A':
init_menu();
draw_f = &LineToy::draw_menu;
break;
case 'B':
init_create();
draw_f = &LineToy::draw_create;
break;
case 'C':
init_intersections();
draw_f = &LineToy::draw_intersections;
break;
}
redraw();
}
void draw( cairo_t *cr, std::ostringstream *notify,
int width, int height, bool save, std::ostringstream *timer_stream) override
{
m_width = width;
m_height = height;
m_length = (m_width > m_height) ? m_width : m_height;
m_length *= 2;
(this->*draw_f)(cr, notify, width, height, save, timer_stream);
Toy::draw(cr, notify, width, height, save, timer_stream);
}
public:
LineToy()
{
op = Point(5,5);
}
private:
typedef void (LineToy::* draw_func_t) (cairo_t*, std::ostringstream*, int, int, bool, std::ostringstream*);
draw_func_t draw_f;
bool set_common_control_geometry;
bool set_control_geometry;
PointHandle p1, p2, p3, p4, p5, p6, O;
std::vector<Toggle> toggles;
std::vector<Slider> sliders;
Point op;
double m_width, m_height, m_length;
}; // end class LineToy
const char* LineToy::menu_items[] =
{
"show this menu",
"rect generation",
"intersection with a line"
};
const char LineToy::keys[] =
{
'A', 'B', 'C'
};
int main(int argc, char **argv)
{
init( argc, argv, new LineToy());
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=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
|