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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
|
/** @file
* @brief Demonstration of elliptical arc functions
*//*
* Authors:
* Marco Cecchetti <mrcekets at gmail.com>
* Krzysztof Kosiński <tweenk.pl@gmail.com>
* Copyright 2008-2015 Authors
*
* 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/elliptical-arc.h>
#include <toys/path-cairo.h>
#include <toys/toy-framework-2.h>
#include <2geom/cairo-path-sink.h>
#include <vector>
#include <string>
using namespace Geom;
std::string angle_formatter(double angle)
{
return default_formatter(decimal_round(deg_from_rad(angle),2));
}
class EllipticalArcToy: public Toy
{
enum menu_item_t
{
SHOW_MENU = 0,
TEST_BASIC,
TEST_COMPARISON,
TEST_PORTION,
TEST_REVERSE,
TEST_NEAREST_POINTS,
TEST_DERIVATIVE,
TEST_ROOTS,
TEST_BOUNDS,
TEST_FITTING,
TEST_TRANSFORM,
TOTAL_ITEMS // this one must be the last item
};
enum handle_label_t
{
START_POINT = 0,
END_POINT,
POINT
};
enum toggle_label_t
{
LARGE_ARC_FLAG = 0,
SWEEP_FLAG,
X_Y_TOGGLE
};
enum slider_label_t
{
RX_SLIDER = 0,
RY_SLIDER,
ROT_ANGLE_SLIDER,
T_SLIDER,
FROM_SLIDER = T_SLIDER,
TO_SLIDER,
TM0_SLIDER = T_SLIDER,
TM1_SLIDER,
TM2_SLIDER,
TM3_SLIDER
};
static const char* menu_items[TOTAL_ITEMS];
static const char keys[TOTAL_ITEMS];
void first_time(int /*argc*/, char** /*argv*/) override
{
draw_f = &EllipticalArcToy::draw_menu;
}
void init_common()
{
set_common_control_geometry = true;
set_control_geometry = true;
double start_angle = (10.0/6.0) * M_PI;
double sweep_angle = (4.0/6.0) * M_PI;
double end_angle = start_angle + sweep_angle;
double rot_angle = (0.0/6.0) * M_PI;
double rx = 200;
double ry = 150;
double cx = 300;
double cy = 300;
Point start_point( cx + rx * std::cos(start_angle),
cy + ry * std::sin(start_angle) );
Point end_point( cx + rx * std::cos(end_angle),
cy + ry * std::sin(end_angle) );
bool large_arc = false;
bool sweep = true;
initial_point.pos = start_point;
final_point.pos = end_point;
try
{
ea.set (initial_point.pos,
rx, ry, rot_angle,
large_arc, sweep,
final_point.pos);
}
catch (RangeError const &e)
{
no_solution = true;
std::cerr << e.what() << std::endl;
}
sliders.clear();
sliders.reserve(50);
sliders.emplace_back(0, 500, 0, ea.ray(X), "ray X");
sliders.emplace_back(0, 500, 0, ea.ray(Y), "ray Y");
sliders.emplace_back(0, 2*M_PI, 0, ea.rotationAngle(), "rot angle");
sliders[ROT_ANGLE_SLIDER].formatter(&angle_formatter);
toggles.clear();
toggles.reserve(50);
toggles.emplace_back("Large Arc Flag", ea.largeArc());
toggles.emplace_back("Sweep Flag", ea.sweep());
handles.clear();
handles.push_back(&initial_point);
handles.push_back(&final_point);
handles.push_back(&(sliders[RX_SLIDER]));
handles.push_back(&(sliders[RY_SLIDER]));
handles.push_back(&(sliders[ROT_ANGLE_SLIDER]));
handles.push_back(&(toggles[LARGE_ARC_FLAG]));
handles.push_back(&(toggles[SWEEP_FLAG]));
}
virtual void draw_common( cairo_t *cr, std::ostringstream *notify,
int width, int height, bool /*save*/,
std::ostringstream *timer_stream=0)
{
if(timer_stream == 0)
timer_stream = notify;
init_common_ctrl_geom(cr, width, height, notify);
no_solution = false;
try
{
ea.set( initial_point.pos,
sliders[0].value(),
sliders[1].value(),
sliders[2].value(),
toggles[0].on,
toggles[1].on,
final_point.pos );
}
catch (RangeError const &e)
{
no_solution = true;
std::cerr << e.what() << std::endl;
return;
}
degenerate = ea.isDegenerate();
point_overlap = false;
if ( are_near(ea.initialPoint(), ea.finalPoint()) )
{
point_overlap = true;
}
// calculate the center of the two possible ellipse supporting the arc
std::pair<Point,Point> centers
= calculate_ellipse_centers( ea.initialPoint(), ea.finalPoint(),
ea.ray(X), ea.ray(Y), ea.rotationAngle(),
ea.largeArc(), ea.sweep() );
// draw axes passing through the center of the ellipse supporting the arc
cairo_set_source_rgba(cr, 0.0, 1.0, 0.0, 1.0);
cairo_set_line_width(cr, 0.5);
draw_axes(cr);
// draw the 2 ellipse with rays rx, ry passing through
// the 2 given point and with the x-axis inclined of rot_angle
if ( !(are_near(ea.ray(X), 0) || are_near(ea.ray(Y), 0)) )
{
cairo_elliptiarc( cr,
centers.first[X], centers.first[Y],
ea.ray(X), ea.ray(Y),
0, 2*M_PI,
ea.rotationAngle() );
cairo_stroke(cr);
cairo_elliptiarc( cr,
centers.second[X], centers.second[Y],
ea.ray(X), ea.ray(Y),
0, 2*M_PI,
ea.rotationAngle() );
cairo_stroke(cr);
}
// convert the elliptical arc to a sbasis path and draw it
D2<SBasis> easb = ea.toSBasis();
cairo_set_line_width(cr, 0.5);
cairo_set_source_rgba(cr, 0.0, 0.0, 1.0, 1.0);
cairo_d2_sb(cr, easb);
cairo_stroke(cr);
// draw initial and final point labels
draw_text(cr, ea.initialPoint() + Point(5, -15), "initial");
draw_text(cr, ea.finalPoint() + Point(5, 0), "final");
cairo_stroke(cr);
// TODO re-enable this
//*notify << ea;
}
void draw_comparison(cairo_t *cr, std::ostringstream *notify,
int width, int height, bool save, std::ostringstream */*timer_stream*/)
{
draw_common(cr, notify, width, height, save);
if ( no_solution || point_overlap ) return;
// draw the arc with cairo in order to make a visual comparison
cairo_set_line_width(cr, 1);
cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 1.0);
if (ea.isDegenerate())
{
cairo_move_to(cr, ea.initialPoint());
cairo_line_to(cr, ea.finalPoint());
}
else
{
if ( ea.sweep() )
{
cairo_elliptiarc( cr,
ea.center(X), ea.center(Y),
ea.ray(X), ea.ray(Y),
ea.initialAngle(), ea.finalAngle(),
ea.rotationAngle() );
}
else
{
cairo_elliptiarc( cr,
ea.center(X), ea.center(Y),
ea.ray(X), ea.ray(Y),
ea.finalAngle(), ea.initialAngle(),
ea.rotationAngle() );
}
}
cairo_stroke(cr);
}
void init_portion()
{
init_common();
from_t = 0;
to_t = 1;
sliders.emplace_back(0, 1, 0, from_t, "from");
sliders.emplace_back(0, 1, 0, to_t, "to");
handles.push_back(&(sliders[FROM_SLIDER]));
handles.push_back(&(sliders[TO_SLIDER]));
}
void draw_portion(cairo_t *cr, std::ostringstream *notify,
int width, int height, bool save, std::ostringstream */*timer_stream*/)
{
draw_common(cr, notify, width, height, save);
init_portion_ctrl_geom(cr, notify, width, height);
if ( no_solution || point_overlap ) return;
from_t = sliders[FROM_SLIDER].value();
to_t = sliders[TO_SLIDER].value();
EllipticalArc* eapp
= static_cast<EllipticalArc*>(ea.portion(from_t, to_t));
EllipticalArc& eap = *eapp;
cairo_set_line_width(cr, 0.8);
cairo_set_source_rgba(cr, 0.0, 1.0, 1.0, 1.0);
cairo_move_to(cr, eap.center(X), eap.center(Y));
cairo_line_to(cr, eap.initialPoint()[X], eap.initialPoint()[Y]);
cairo_move_to(cr, eap.center(X), eap.center(Y));
cairo_line_to(cr, eap.finalPoint()[X], eap.finalPoint()[Y]);
cairo_stroke(cr);
D2<SBasis> sub_arc = eap.toSBasis();
cairo_d2_sb(cr, sub_arc);
cairo_stroke(cr);
delete eapp;
}
void init_reverse()
{
init_common();
time = 0;
sliders.emplace_back(0, 1, 0, time, "t");
handles.push_back(&(sliders[T_SLIDER]));
}
void draw_reverse(cairo_t *cr, std::ostringstream *notify,
int width, int height, bool save, std::ostringstream */*timer_stream*/)
{
draw_common(cr, notify, width, height, save);
init_reverse_ctrl_geom(cr, notify, width, height);
if ( no_solution || point_overlap ) return;
time = sliders[T_SLIDER].value();
EllipticalArc* eapp = static_cast<EllipticalArc*>(ea.reverse());
EllipticalArc& eap = *eapp;
cairo_set_line_width(cr, 0.8);
cairo_set_source_rgba(cr, 0.2, 0.2, 0.2, 1.0);
cairo_move_to(cr, eap.center(X), eap.center(Y));
cairo_line_to(cr, eap.valueAt(time,X), eap.valueAt(time,Y));
draw_circ(cr, eap.pointAt(time));
cairo_stroke(cr);
cairo_set_source_rgba(cr, 0.0, 1.0, 1.0, 1.0);
D2<SBasis> sub_arc = eap.toSBasis();
cairo_d2_sb(cr, sub_arc);
cairo_stroke(cr);
delete eapp;
}
void init_np()
{
init_common();
nph.pos = Point(10,10);
handles.push_back(&nph);
}
void draw_np(cairo_t *cr, std::ostringstream *notify,
int width, int height, bool save, std::ostringstream */*timer_stream*/)
{
draw_common(cr, notify, width, height, save);
if ( no_solution || point_overlap ) return;
std::vector<double> times = ea.allNearestTimes( nph.pos );
for (double time : times)
{
cairo_move_to(cr,nph.pos);
cairo_line_to( cr, ea.pointAt(time) );
}
cairo_stroke(cr);
}
void init_derivative()
{
init_common();
time = 0;
sliders.emplace_back(0, 1, 0, time, "t");
handles.push_back(&(sliders[T_SLIDER]));
}
void draw_derivative(cairo_t *cr, std::ostringstream *notify,
int width, int height, bool save, std::ostringstream */*timer_stream*/)
{
draw_common(cr, notify, width, height, save);
init_reverse_ctrl_geom(cr, notify, width, height);
if ( no_solution || point_overlap ) return;
time = sliders[T_SLIDER].value();
Curve* der = ea.derivative();
Point p = ea.pointAt(time);
Point v = der->pointAt(time) + p;
delete der;
// std::vector<Point> points = ea.pointAndDerivatives(time, 8);
// Point p = points[0];
// Point v = points[1] + p;
cairo_move_to(cr, p);
cairo_line_to(cr, v);
cairo_stroke(cr);
}
void init_roots()
{
init_common();
ph.pos = Point(10,10);
toggles.emplace_back("X/Y roots", true );
handles.push_back(&ph);
handles.push_back(&(toggles[X_Y_TOGGLE]));
}
void draw_roots(cairo_t *cr, std::ostringstream *notify,
int width, int height, bool save, std::ostringstream */*timer_stream*/)
{
draw_common(cr, notify, width, height, save);
init_roots_ctrl_geom(cr, notify, width, height);
if ( no_solution || point_overlap ) return;
Dim2 DIM = toggles[X_Y_TOGGLE].on ? X : Y;
Point p1[2] = { Point(ph.pos[X], -1000),
Point(-1000, ph.pos[Y]) };
Point p2[2] = { Point(ph.pos[X], 1000),
Point(1000, ph.pos[Y]) };
cairo_set_line_width(cr, 0.5);
cairo_set_source_rgba(cr, 0.3, 0.3, 0.3, 1.0);
cairo_move_to(cr, p1[DIM]);
cairo_line_to(cr, p2[DIM]);
std::vector<double> times;
try
{
times = ea.roots(ph.pos[DIM], DIM);
*notify << "winding: " << ea.winding(ph.pos);
}
catch(Geom::Exception e)
{
std::cerr << e.what() << std::endl;
}
for (double time : times)
{
draw_handle(cr, ea.pointAt(time));
}
cairo_stroke(cr);
}
void init_bounds()
{
init_common();
}
void draw_bounds(cairo_t *cr, std::ostringstream *notify,
int width, int height, bool save, std::ostringstream */*timer_stream*/)
{
draw_common(cr, notify, width, height, save);
if ( no_solution || point_overlap ) return;
// const char* msg[] = { "xmax", "xmin", "ymax", "ymin" };
Rect bb = ea.boundsFast();
// for ( unsigned int i = 0; i < limits.size(); ++i )
// {
// std::cerr << "angle[" << i << "] = " << deg_from_rad(limits[i]) << std::endl;
// Point extreme = ea.pointAtAngle(limits[i]);
// draw_handle(cr, extreme );
// draw_text(cr, extreme, msg[i]);
// }
cairo_rectangle( cr, bb.left(), bb.top(), bb.width(), bb.height() );
cairo_stroke(cr);
}
void init_fitting()
{
init_common();
}
void draw_fitting(cairo_t * cr, std::ostringstream *notify,
int width, int height, bool save, std::ostringstream */*timer_stream*/)
{
draw_common(cr, notify, width, height, save);
if ( no_solution || point_overlap ) return;
D2<SBasis> easb = ea.toSBasis();
try
{
EllipticalArc earc;
if (!arc_from_sbasis(earc, easb, 0.1, 5)) return;
D2<SBasis> arc = earc.toSBasis();
arc[0] += Linear(50, 50);
cairo_d2_sb(cr, arc);
cairo_stroke(cr);
}
catch (RangeError const &e)
{
std::cerr << "conversion failure" << std::endl;
std::cerr << e.what() << std::endl;
return;
}
}
void init_transform()
{
init_common();
double max = 4;
double min = -max;
sliders.emplace_back(min, max, 0, 1, "TM0");
sliders.emplace_back(min, max, 0, 0, "TM1");
sliders.emplace_back(min, max, 0, 0, "TM2");
sliders.emplace_back(min, max, 0, 1, "TM3");
handles.push_back(&(sliders[TM0_SLIDER]));
handles.push_back(&(sliders[TM1_SLIDER]));
handles.push_back(&(sliders[TM2_SLIDER]));
handles.push_back(&(sliders[TM3_SLIDER]));
}
void draw_transform(cairo_t *cr, std::ostringstream *notify,
int width, int height, bool save, std::ostringstream */*timer_stream*/)
{
draw_common(cr, notify, width, height, save);
init_transform_ctrl_geom(cr, notify, width, height);
if ( no_solution || point_overlap ) return;
Affine TM(sliders[TM0_SLIDER].value(), sliders[TM1_SLIDER].value(),
sliders[TM2_SLIDER].value(), sliders[TM3_SLIDER].value(),
ea.center(X), ea.center(Y));
Affine tm( 1, 0,
0, 1,
-ea.center(X), -ea.center(Y) );
EllipticalArc* tea = static_cast<EllipticalArc*>(ea.transformed(tm));
EllipticalArc* eat = NULL;
eat = static_cast<EllipticalArc*>(tea->transformed(TM));
delete tea;
if (eat == NULL)
{
std::cerr << "elliptiarc transformation failed" << std::endl;
return;
}
CairoPathSink ps(cr);
//D2<SBasis> sb = eat->toSBasis();
cairo_set_line_width(cr, 0.8);
cairo_set_source_rgba(cr, 0.8, 0.1, 0.1, 1.0);
//cairo_d2_sb(cr, sb);
ps.feed(*eat);
cairo_stroke(cr);
delete eat;
}
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;
sliders[RX_SLIDER].geometry(Point(50, height-120), 250);
sliders[RY_SLIDER].geometry(Point(50, height-85), 250);
sliders[ROT_ANGLE_SLIDER].geometry(Point(50, height-50), 180);
toggles[LARGE_ARC_FLAG].bounds = Rect(Point(400, height-120), Point(540, height-95));
toggles[SWEEP_FLAG].bounds = Rect(Point(400, height-70), Point(520, height-45));
}
}
void init_portion_ctrl_geom(cairo_t* /*cr*/, std::ostringstream* /*notify*/, int /*width*/, int height)
{
if ( set_control_geometry )
{
set_control_geometry = false;
Point from_sp = Point(600, height - 120);
Point to_sp = from_sp + Point(0,45);
double from_to_len = 100;
sliders[FROM_SLIDER].geometry(from_sp, from_to_len);
sliders[TO_SLIDER].geometry(to_sp, from_to_len);
}
}
void init_reverse_ctrl_geom(cairo_t* /*cr*/, std::ostringstream* /*notify*/, int /*width*/, int height)
{
if ( set_control_geometry )
{
set_control_geometry = false;
Point t_sp = Point(600, height - 120);
double t_len = 200;
sliders[T_SLIDER].geometry(t_sp, t_len);
}
}
void init_roots_ctrl_geom(cairo_t* /*cr*/, std::ostringstream* /*notify*/, int /*width*/, int height)
{
if ( set_control_geometry )
{
set_control_geometry = false;
Point T(600, height - 120);
toggles[X_Y_TOGGLE].bounds = Rect( T, T + Point(100,25) );
}
}
void init_transform_ctrl_geom(cairo_t* /*cr*/, std::ostringstream* /*notify*/, int /*width*/, int height)
{
if ( set_control_geometry )
{
set_control_geometry = false;
Point sp = Point(600, height - 140);
Point op = Point(0, 30);
double len = 200;
sliders[TM0_SLIDER].geometry(sp, len);
sliders[TM1_SLIDER].geometry(sp += op, len);
sliders[TM2_SLIDER].geometry(sp += op, len);
sliders[TM3_SLIDER].geometry(sp += op, len);
}
}
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 = &EllipticalArcToy::draw_menu;
break;
case 'B':
init_common();
draw_f = &EllipticalArcToy::draw_common;
break;
case 'C':
init_common();
draw_f = &EllipticalArcToy::draw_comparison;
break;
case 'D':
draw_f = &EllipticalArcToy::draw_menu;
init_portion();
draw_f = &EllipticalArcToy::draw_portion;
break;
case 'E':
init_reverse();
draw_f = &EllipticalArcToy::draw_reverse;
break;
case 'F':
init_np();
draw_f = &EllipticalArcToy::draw_np;
break;
case 'G':
init_derivative();
draw_f = &EllipticalArcToy::draw_derivative;
break;
case 'H':
init_roots();
draw_f = &EllipticalArcToy::draw_roots;
break;
case 'I':
init_bounds();
draw_f = &EllipticalArcToy::draw_bounds;
break;
case 'J':
init_fitting();
draw_f = &EllipticalArcToy::draw_fitting;
break;
case 'K':
init_transform();
draw_f = &EllipticalArcToy::draw_transform;
break;
}
redraw();
}
void draw_axes(cairo_t* cr) const
{
Point D(std::cos(ea.rotationAngle()), std::sin(ea.rotationAngle()));
Point Dx = (ea.ray(X) + 20) * D;
Point Dy = (ea.ray(Y) + 20) * D.cw();
Point C(ea.center(X),ea.center(Y));
Point LP = C - Dx;
Point RP = C + Dx;
Point UP = C - Dy;
Point DP = C + Dy;
cairo_move_to(cr, LP[X], LP[Y]);
cairo_line_to(cr, RP[X], RP[Y]);
cairo_move_to(cr, UP[X], UP[Y]);
cairo_line_to(cr, DP[X], DP[Y]);
cairo_move_to(cr, 0, 0);
cairo_stroke(cr);
}
void cairo_elliptiarc( cairo_t *cr,
double _cx, double _cy,
double _rx, double _ry,
double _sa, double _ea,
double _ra = 0
) const
{
double cos_rot_angle = std::cos(_ra);
double sin_rot_angle = std::sin(_ra);
cairo_matrix_t transform_matrix;
cairo_matrix_init( &transform_matrix,
_rx * cos_rot_angle, _rx * sin_rot_angle,
-_ry * sin_rot_angle, _ry * cos_rot_angle,
_cx, _cy
);
cairo_save(cr);
cairo_transform(cr, &transform_matrix);
cairo_arc(cr, 0, 0, 1, _sa, _ea);
cairo_restore(cr);
}
std::pair<Point,Point>
calculate_ellipse_centers( Point _initial_point, Point _final_point,
double m_rx, double m_ry,
double m_rot_angle,
bool m_large_arc, bool m_sweep
)
{
std::pair<Point,Point> result;
if ( _initial_point == _final_point )
{
result.first = result.second = _initial_point;
return result;
}
m_rx = std::fabs(m_rx);
m_ry = std::fabs(m_ry);
Point d = _initial_point - _final_point;
if ( are_near(m_rx, 0) || are_near(m_ry, 0) )
{
result.first = result.second
= middle_point(_initial_point, _final_point);
return result;
}
double sin_rot_angle = std::sin(m_rot_angle);
double cos_rot_angle = std::cos(m_rot_angle);
Affine m( cos_rot_angle, -sin_rot_angle,
sin_rot_angle, cos_rot_angle,
0, 0 );
Point p = (d / 2) * m;
double rx2 = m_rx * m_rx;
double ry2 = m_ry * m_ry;
double rxpy = m_rx * p[Y];
double rypx = m_ry * p[X];
double rx2py2 = rxpy * rxpy;
double ry2px2 = rypx * rypx;
double num = rx2 * ry2;
double den = rx2py2 + ry2px2;
assert(den != 0);
double rad = num / den;
Point c(0,0);
if (rad > 1)
{
rad -= 1;
rad = std::sqrt(rad);
if (m_large_arc == m_sweep) rad = -rad;
c = rad * Point(rxpy / m_ry, -rypx / m_rx);
m[1] = -m[1];
m[2] = -m[2];
c = c * m;
}
d = middle_point(_initial_point, _final_point);
result.first = c + d;
result.second = -c + d;
return result;
}
void draw( cairo_t *cr, std::ostringstream *notify,
int width, int height, bool save, std::ostringstream *timer_stream) override
{
(this->*draw_f)(cr, notify, width, height, save, timer_stream);
Toy::draw(cr, notify, width, height, save,timer_stream);
}
public:
EllipticalArcToy() {}
private:
typedef void (EllipticalArcToy::* 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;
bool no_solution, point_overlap;
bool degenerate;
PointHandle initial_point, final_point;
PointHandle nph, ph;
std::vector<Toggle> toggles;
std::vector<Slider> sliders;
EllipticalArc ea;
double from_t;
double to_t;
double time;
};
const char* EllipticalArcToy::menu_items[] =
{
"show this menu",
"basic",
"comparison",
"portion, pointAt",
"reverse, valueAt",
"nearest points",
"derivative",
"roots",
"bounding box",
"fitting",
"transformation"
};
const char EllipticalArcToy::keys[] =
{
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K'
};
int main(int argc, char **argv)
{
init( argc, argv, new EllipticalArcToy(), 850, 780 );
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 :
|