forked from jugglingcats/Spirograph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpiroInstance.cs
1032 lines (863 loc) · 24.8 KB
/
SpiroInstance.cs
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
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.Xml.Serialization;
namespace Spirograph
{
/// Represents the various areas that a point can be on a spiro
public enum HitInfo
{
None, // not on the spiro at all
Bounds, // somewhere in the BoundingRect
FixedCircle, // on the fixed cirle
MovingCircle, // on the moving circle
Pen, // on or near the pen
Curve, // near the actual curve
Resize // near the resize handle
}
/// Represents the different modes used for hit testing
public enum HitTestMode
{
Default, // the default
Selected // when selected
}
public enum RedrawMode
{
None,
Draw,
Invalidate
}
public abstract class SpiroInstance
{
protected Point location = new Point(250, 250);
protected int R = 210; // radius of fixed circle
protected int r = 30; // radius of moving circle
protected int f = 40; // distance of pen from moving circle center
protected int counter = 0; // travel around the spiro (t)
private PointF[] points = null; // collection of points to plot
private int penWidth=2; // pen thickness
private int revolutions; // number of orbits of the inner circle
protected int resolution = 10; // number of points to plot per orbit
private bool selected = false; // are we selected
private static readonly int snapRange = 3; // tolerance for hit tests
private bool complete=false; // have we finished drawing (for animate/repeat)
private bool animate=true; // should we animate
private bool showTrace=true; // should we show trace (circles moving)
private bool repeat=true; // after animating once, should we start again
private Color colour=Color.BlueViolet; // pen colour
protected double baseAngle=0; // offset used to retain angle after adjustment
private bool randomise=false; // after each complete draw, generate a new random spiro
private bool showInfo=false; // should we show info even if not selected
protected double baseMovingAngle=0;
private double speed=35.0; // measure of how fast we draw (lower = faster)
public int MaxRandomDiameter=0;
private int finalPauseCount=0;
public event EventHandler CycleComplete;
public SpiroInstance()
{
// we don't actually calc the points at this stage
// (in case this spiro is never actually drawn)
Reset();
}
protected abstract double CurrentMovingAngle
{
get;
}
private void ResetCounter()
{
counter=0;
finalPauseCount=0;
}
public Point Location
{
get { return location; }
set { location = value; }
}
public void Clone(SpiroInstance from)
{
// copy all properties from the other spiro
this.location=from.location;
this.R=from.R;
this.r=from.r;
this.f=from.f;
this.counter=from.counter;
// we can copy the points since they are essentially immutable
// (if this spiro is changed in any way a new array is created)
this.points=from.points;
this.penWidth=from.penWidth;
this.revolutions=from.revolutions;
this.resolution=from.resolution;
this.selected=from.selected;
this.complete=from.complete;
this.animate=from.animate;
this.repeat=from.repeat;
this.randomise=from.randomise;
this.showInfo=from.showInfo;
this.showTrace=from.ShowTrace;
this.colour=from.colour;
this.baseAngle=from.baseAngle;
this.baseMovingAngle=from.baseMovingAngle;
this.speed=from.speed;
this.MaxRandomDiameter=from.MaxRandomDiameter;
// only need to reset if we're a different type (ie. different points)
if ( this.GetType() != from.GetType() )
Reset();
// ensure the circle sizes are valid
if ( R < MinFixedRadius )
{
R=MinFixedRadius;
Reset();
}
// it's not actually necessary to do this
if ( r > MaxMovingRadius )
{
r=MaxMovingRadius;
Reset();
}
}
public void Draw(Graphics g, bool printmode)
{
if (points == null)
// make sure we have some points
Init();
// don't anti-alias the highlight
g.SmoothingMode=SmoothingMode.None;
// only draw the highlight under certain conditions
if ( !printmode && showTrace && !selected && animate )
DrawHighlight(g, HitInfo.None);
// show the info box when selected or specifically requested
if ( (selected && !printmode) || showInfo )
{
Point infoLocation=new Point(FocusRect.X, FocusRect.Bottom);
DrawInfo(g, infoLocation, colour);
}
// shift into spiro coords
g.TranslateTransform(location.X, location.Y, MatrixOrder.Append);
// by default draw everything
PointF[] subshape=points;
if ( counter > 0 )
{
subshape = points;
if ( !printmode && animate && !complete )
{
// copy subset of points into new array
subshape=new PointF[counter + 1];
Array.Copy(points, subshape, counter + 1);
}
} else if ( animate && !complete )
// nothing to draw
subshape=null;
Pen p=new Pen(colour, penWidth);
p.LineJoin=LineJoin.Round;
if ( !printmode )
g.SmoothingMode=SmoothingMode.AntiAlias;
if ( !printmode && selected && animate )
// show trace when selected
g.DrawLines(Pens.LightGray, points);
// show main drawing
if ( subshape != null )
g.DrawLines(p, subshape);
// return to canvas coords
g.TranslateTransform(-location.X, -location.Y);
}
private void DrawFocusRect(Graphics g)
{
ControlPaint.DrawFocusRectangle(g, FocusRect);
g.FillRectangle(Brushes.Black, ResizeHandleRect);
}
private Rectangle ResizeHandleRect
{
get
{
Point pt=new Point(FocusRect.Right, FocusRect.Bottom);
Rectangle rc=new Rectangle(pt, new Size(0,0));
rc.Inflate(3,3);
return rc;
}
}
protected double AbsoluteAngle
{
get { return (Math.PI/resolution*counter); }
}
protected double CurrentAngle
{
get { return AbsoluteAngle+baseAngle; }
}
/// Return information about the area under the specified point
public HitInfo HitTest(Point pt, HitTestMode mode)
{
if ( points == null )
Init();
// shift to spiro coords
Point translatedPoint=pt;
translatedPoint.Offset(-location.X, -location.Y);
// calc distance from center
double distance = LineUtil.Distance(new Point(0, 0), translatedPoint);
if ( mode == HitTestMode.Default )
{
if (distance > MaxExtent + 5)
// we're outside the area covered by the spiro
return HitInfo.None;
// is it near the line of the fixed circle
if ( distance > R - 5 && distance < R + 5 )
// yes
return HitInfo.FixedCircle;
// when testing in this mode, we only want to know about a couple
// of key areas
if (PointNearSegment(translatedPoint))
// point is on or near the curve
return HitInfo.Curve;
// not interested, in this mode
return HitInfo.None;
}
if ( ResizeHandleRect.Contains(pt) )
return HitInfo.Resize;
if ( !FocusRect.Contains(pt) )
// point is outside focus rect
return HitInfo.None;
// determine if the point is on or near the pen
RectangleF rc = new RectangleF(points[counter], new Size(0, 0));
rc.Inflate(5, 5);
if (rc.Contains(translatedPoint))
return HitInfo.Pen;
// is it near the line of the moving circle
double distToMoving = LineUtil.Distance(CurrentMovingCircleCenter, translatedPoint);
if ( distToMoving < r + 5 && distToMoving > r - 5 )
// yes
return HitInfo.MovingCircle;
// is it near the line of the fixed circle
if ( distance > R - 5 && distance < R + 5 )
// yes
return HitInfo.FixedCircle;
// nowhere in particular but is within bounds
return HitInfo.Bounds;
}
/// This represents all the parts of the spiro that need
/// redrawing when the counter changes
private Region RedrawRegion
{
get
{
if ( !showTrace )
return new Region(Rectangle.Empty);
// add the moving circle
RectangleF rc=MovingCircleBounds;
rc.Inflate(2, 2);
Region rgn=new Region(rc);
// add the cross area
rc=CrossRect;
rc.Offset(location.X, location.Y);
rc.Inflate(1,1);
rgn.Union(rc);
// add the line between moving center and cross
Region rgnLine=LineRegion;
rgn.Union(rgnLine);
return rgn;
}
}
/// Called to animate the spiro
public Region Tick()
{
if (points == null)
Init();
if ( selected || !animate )
// nothing to do if not animated
return null;
// this is the entire area (default redraw)
Region rgnAll=new Region(BoundingRect);
Region rgnIncrement=RedrawRegion;
counter++;
if (counter >= points.Length - 1)
{
if ( finalPauseCount < 100 )
{
counter = points.Length - 1;
finalPauseCount++;
}
else
{
// need to restart from the beginning
ResetCounter();
// if not repeating then we keep drawn (counter becomes unused)
complete=!repeat;
if ( this.CycleComplete != null )
{
this.CycleComplete(this, new EventArgs());
}
if ( randomise )
// create a new shape
ResetRandom();
// need to invalidate everything
return rgnAll;
}
}
// add the region covered by the new line segment
GraphicsPath p=new GraphicsPath();
p.AddLine(points[counter], points[counter-1]);
p.Widen(new Pen(Color.White, penWidth+2));
RectangleF rc=p.GetBounds();
rc.Inflate(penWidth, penWidth);
rc.Offset(location.X, location.Y);
rgnIncrement.Union(rc);
// add the region after increment
if ( showTrace )
rgnIncrement.Union(RedrawRegion);
return rgnIncrement;
}
/// Create a new shape within the bounds of the current one
/// (not as easy as it looks)
public void ResetRandom()
{
Random rnd=new Random();
int n;
if ( MaxRandomDiameter == 0 )
{
n=MaxPenDistance;
}
else
{
n=rnd.Next(50, MaxRandomDiameter);
}
// this is just some jiggering to get some sensible values (not
// always perfect but gives reasonable results)
int min=Math.Min(20, n/2);
int max=n-20;
int R=OptimalFixedVal(this.r, min, rnd.Next(min, max), max);
max=MaxMovingRadiusWithinBounds(R)-5;
if ( max <= 10 )
max=10;
int r=OptimalMovingVal(R, 10, rnd.Next(10, max), max);
this.R=R;
this.r=r;
f=MaxPenDistanceWithinBounds(n);
Reset();
}
/// Returns the maximum distance the pen can get from the center of the fixed circle
protected abstract int MaxPenDistance
{
get;
}
/// Returns the maximum pen distance (from the moving circle center, ie. f), that
/// is possible while still having MaxPenDistance <= n
protected abstract int MaxPenDistanceWithinBounds(int n);
/// Returns the maximum moving circle radius possible while still staying within
/// the given bounds
protected abstract int MaxMovingRadiusWithinBounds(int R);
/// Called by clients, typically when mouse moves to set a new circle size
public void ResizeFixedCircle(Point pt)
{
float d = (int) LineUtil.Distance(location, pt);
if (d < MinFixedRadius)
d = MinFixedRadius;
// set to an optimal value
int newR = OptimalFixedVal(r, MinFixedRadius, (int) Math.Round(d), int.MaxValue);
if ( newR != R )
{
SaveMovingAngle();
R=newR;
Reset();
}
}
// this is used to resize the moving circle
private PointF CurrentFixedCirclePoint
{
get
{
// return the point where the moving circle and fixed circle touch
return new PointF((float) (R*Math.Cos(CurrentAngle)), (float) (R*Math.Sin(CurrentAngle)));
}
}
/// Called by clients, typically when mouse moves to set a new moving circle size
public void ResizeMovingCircle(Point pt)
{
// this is a little involved because we need to work out the circle that
// will pass through the point the circle touches the fixed circle and the
// given point - because the center of the moving circle moves when resized
// and it jumps about and looks bad if we just use that
pt.Offset(-location.X, -location.Y);
Point pt2 = Point.Round(CurrentFixedCirclePoint);
double XA = LineUtil.Distance(pt2, pt)/2;
int OA = pt2.X - pt.X;
int OB = pt2.Y - pt.Y;
// adjust for rotation of the moving circle
double alpha = Math.Atan2(OA, OB)+CurrentAngle;
if ( this is Epitrochoid )
// correction for flip side of circle
alpha+=Math.PI;
int d = (int) Math.Round(XA/Math.Sin(alpha));
if (d > MaxMovingRadius)
d = MaxMovingRadius;
if (d < 5)
d = 5;
// set to an optimal value
int newr = OptimalMovingVal(R, 5, d, MaxMovingRadius);
if ( newr != r )
{
SaveMovingAngle();
r=newr;
Reset();
}
}
public void Resize(SpiroInstance current, int delta)
{
// this is a bit tricky - we need to resize both circles but give the same
// (integer) ratio between them
// in order to do this, each circle must increase/decrease by a multiple
// of GCD(r,R)/size, ie. GCD(r,R)/R or GCD(r,R)/r
// so we use the fixed circle as a base
if ( delta == 0 )
return;
int d=current.R + delta;
int multiple_R=R/GCD(r,R);
int multiple_r=r/GCD(r,R);
int new_R=(int) Math.Round((double) d/multiple_R)*multiple_R;
int new_r=new_R * multiple_r / multiple_R;
int new_f=(int) Math.Round(1.0 * f * (float) new_R / R);
if ( new_R < 5 || new_r < 5 )
// too small
return;
if ( new_R == R && new_r == r )
return;
SaveMovingAngle();
R=new_R;
r=new_r;
f=new_f;
Reset();
}
/// Called by clients, typically when mouse moves to move the pen in relation to the
/// moving circle
public void MovePen(Point pt)
{
// shift the point to spiro coords
pt.Offset(-location.X, -location.Y);
// measure distance to moving circle center
float d = LineUtil.Distance(CurrentMovingCircleCenter, pt);
// set new distance
int newf = (int) Math.Round(d);
if ( newf != f )
{
SaveMovingAngle();
f=newf;
Reset();
}
}
[XmlIgnore]
public bool Selected
{
set { this.selected = value; }
get { return selected; }
}
[XmlIgnore]
public Rectangle FocusRect
{
get
{
Rectangle rc=TrueBoundingRect;
rc.Inflate(penWidth+2,penWidth+2);
return rc;
}
}
/// Calculate the true bounding rect for this spiro
/// (the BoundingRect property adds some additional space)
protected abstract Rectangle TrueBoundingRect { get; }
/// Create a copy of this spiro
public abstract SpiroInstance Clone();
/// Calculate an actual point on the curve based on "time"
/// (time is not an actual time but an abstract notion of travel around
/// the curve - can also be thought of as angle)
protected abstract PointF CalcPoint(double t);
/// Get the center of the moving circle (at current t)
protected abstract PointF CurrentMovingCircleCenter { get; }
/// Get the min size of the fixed circle
protected abstract int MinFixedRadius { get; }
/// Get the max size of the moving circle
protected abstract int MaxMovingRadius { get; }
private void SaveMovingAngle()
{
// saved when shape is changed to give consistent start point for new
// shape (after a reset)
baseMovingAngle=CurrentMovingAngle % (2*Math.PI);
}
/// Reset everything (after a change, eg. to the size of a circle)
protected void Reset()
{
// this sets the new start point for drawing
// (this means that the trace highlight will continue from
// same spot after the reset - it's just visually nice)
baseAngle=CurrentAngle % (2*Math.PI);
CalcOrbits();
// points are created in the Init method
points = null;
counter = 0;
// resolution is a measure of how many points are calculated
// (making it a factor of R/r is very crude - should be improved)
resolution = CalcResolution;
}
protected int CheckResolution(int n)
{
if ( n < 10 )
n=10;
// can set an absolute limit here but it's hard because
// some shapes can be quite small but very fast and they get jagged
// if ( n > 300 )
// n=300;
return n;
}
protected abstract int CalcResolution
{
get;
}
/// Calculate the Greatest Common Divisor of a and b
/// (this this is Euclid's algorithm)
private static int GCD(int a, int b)
{
int r1 = b - (b/a)*a;
if (r1 == 0)
return a;
int r2 = a - (a/r1)*r1;
if (r2 == 0)
return r1;
int r3 = -1;
while (r3 != 0)
{
r3 = r1 - (r1/r2)*r2;
r1 = r2;
if (r3 != 0)
r2 = r3;
}
return r2;
}
private static void DrawCircle(Graphics g, Color c, int width, int x, int y, int r)
{
Pen p = new Pen(c, width);
g.DrawEllipse(p, x - r, y - r, r*2, r*2);
}
/// This is the number of revolutions (or orbits) that the pen takes before
/// it returns to the start point
private static int CalcOrbits(int A, int a)
{
int gcd = GCD(A, a);
return a/gcd;
}
/// Draw the highlight (fixed and moving circles, plus pen)
public void DrawHighlight(Graphics g, HitInfo hi)
{
if (points == null)
Init();
g.TranslateTransform(location.X, location.Y);
// save the current smoothing mode so we can reset
SmoothingMode sm=g.SmoothingMode;
g.SmoothingMode=SmoothingMode.None;
if ( selected )
g.SmoothingMode=SmoothingMode.AntiAlias;
// set the default colours
Color colFixedCircle = Selected ? Color.LightGray : Color.Gray;
Color colMovingCircle = Color.DarkGray;
Color colPen = colMovingCircle;
// adjust colours based on hit test info
switch (hi)
{
case HitInfo.FixedCircle:
colFixedCircle = Color.Blue;
break;
case HitInfo.MovingCircle:
colMovingCircle = Color.Blue;
break;
case HitInfo.Pen:
colPen = Color.Blue;
break;
}
int penWidth = Selected ? 2 : 1;
Pen pen = new Pen(colPen, penWidth);
// draw the fixed circle
DrawCircle(g, colFixedCircle, penWidth, 0, 0, R);
// find position of moving circle
Point cmc = Point.Round(CurrentMovingCircleCenter);
// and draw it
DrawCircle(g, colMovingCircle, penWidth, cmc.X, cmc.Y, r);
// get location of pen
PointF cross = CurrentPenPoint;
// draw line from center of moving circle to pen
g.DrawLine(pen, cmc, cross);
// draw the cross at the pen position
RectangleF rc=CrossRect;
g.DrawLine(pen, rc.Left, rc.Top, rc.Right, rc.Bottom);
g.DrawLine(pen, rc.Left, rc.Bottom, rc.Right, rc.Top);
// shift back to canvas coords
g.TranslateTransform(-location.X, -location.Y);
if ( selected )
DrawFocusRect(g);
// reset smoothing mode
g.SmoothingMode=sm;
}
private RectangleF MovingCircleBounds
{
get
{
Point cmc=Point.Round(CurrentMovingCircleCenter);
cmc.Offset(location.X, location.Y);
RectangleF rc=new RectangleF(cmc, SizeF.Empty);
rc.Inflate(r+penWidth, r+penWidth);
return rc;
}
}
// This is used when invalidating - it gives a region covering the line
// between center of moving circle and the pen
private Region LineRegion
{
get
{
PointF cross = CurrentPenPoint;
PointF center= CurrentMovingCircleCenter;
GraphicsPath path=new GraphicsPath();
path.AddLine(center, cross);
path.Widen(new Pen(Color.White, 2));
Region rgn=new Region(path);
rgn.Translate(location.X, location.Y);
return rgn;
}
}
private PointF CurrentPenPoint
{
get { return new PointF(points[counter].X, points[counter].Y); }
}
private RectangleF CrossRect
{
get
{
RectangleF rc = new RectangleF(CurrentPenPoint, SizeF.Empty);
rc.Inflate(6, 6);
return rc;
}
}
private string Pad(object o)
{
return string.Format("{0}", o).PadLeft(4);
}
/// Display the details of the current spiro
public void DrawInfo(Graphics g, Point pt, Color col)
{
string msg = string.Format(
"Fixed Radius\t{0}\n"+
"Moving Radius\t{1}\n"+
"Pen Distance\t{2}\n"+
"Number of Orbits\t{3}\n"+
"GCD of Radii\t{4}\n"+
"Resolution\t{5}",
Pad(R), Pad(r), Pad(f), Pad(revolutions), Pad(GCD(R, r)), Pad(resolution));
Font fnt = new Font("Arial", 8);
StringFormat fmt=new StringFormat();
fmt.SetTabStops(0, new float[] {100});
g.DrawString(msg, fnt, new SolidBrush(col), pt, fmt);
}
/// Calculate an optimal value for new fixed circle size within a given range
private int OptimalFixedVal(int r, int min, int R, int max)
{
// we're only interested in sizes within snapRange of the requested new size
min = Math.Max(min, R - snapRange);
max = Math.Min(max, R + snapRange);
// calculate new revolutions
int t = SpiroInstance.CalcOrbits(R, r);
int o = R;
// we test progressively further either side of the requested size, because
// we want the nearest value with the lowest revolutions
int sign=1; // used to flip between above / below
for (int dx = 1; dx <= snapRange;)
{
int tv = R + sign*dx;
// reverse the sign
sign=1-sign;
if ( sign > 0 )
// increment every other loop
dx++;
if (tv < min || tv > max)
continue;
int revs = SpiroInstance.CalcOrbits(tv, r);
if (revs < t)
{
// test val gives smaller number of revs so use it
t = revs;
o = tv;
}
}
return o;
}
/// Calculate an optimal value for new moving circle size within a given range
private int OptimalMovingVal(int R, int min, int r, int max)
{
min = Math.Max(min, r - snapRange);
max = Math.Min(max, r + snapRange);
int t = SpiroInstance.CalcOrbits(R, r);
int o = r;
int sign=1; // used to flip between above / below
for (int dx = 1; dx <= snapRange;)
{
int tv = r + sign*dx;
// reverse the sign
sign=1-sign;
if ( sign > 0 )
// increment every other loop
dx++;
if (tv < min || tv > max)
continue;
int revs = SpiroInstance.CalcOrbits(R, tv);
if (revs < t)
{
// test val gives smaller number of revs so use it
t = revs;
o = tv;
}
}
return o;
}
protected int InternalCalcResolution(double dxy)
{
// find the hypotenuse (max dx and dy on the edges)
double val=speed*Math.Sqrt(2*Math.Pow(dxy, 2)) / MaxPenDistance;
return CheckResolution((int) Math.Round(val)) ;
}
/// Calculate how far a given point is from the spiro curve
private bool PointNearSegment(Point pt)
{
if (points == null)
Init();
// go around the shape and construct a segment from consecutive
// points and see how far the point is from the segment
PointF pt1 = new PointF(points[0].X, points[0].Y);
for (int n = 1; n < points.Length; n++)
{
PointF pt2 = new PointF(points[n].X, points[n].Y);
Segment s = new Segment(pt1, pt2);
if (LineUtil.PointToSegment(pt, s) < 5)
return true;
pt1 = pt2;
}
return false;
}
/// Rebuild the points of the spiro (after a significant change, eg. resize)
private void Init()
{
points = new PointF[revolutions*resolution*2+1];
float totalLen=0;
for (int t = 0; t < revolutions*resolution*2+1; t++)
{
double theta = (Math.PI/resolution*t);
PointF pt = CalcPoint(theta);
points[t] = pt;
if ( t > 0 )
{
float d=LineUtil.Distance(points[t-1], points[t]);
totalLen+=d;
}
}
}
/// Recalculate the revolutions needed to return to start point
private void CalcOrbits()
{
revolutions = CalcOrbits(R, r);
}
/// Calculate the max distance of any drawing from the center (includes the circles
/// shown in a trace so can be greater than MaxPenDistance)
protected int MaxExtent
{
get { return TrueBoundingRect.Width/2; }
}
/// Get or set whether to animate the shape
public bool Animate
{
get { return animate; }
set { animate=value; }
}
/// Get or set whether to repeat animation after the first time
public bool Repeat
{
get { return repeat; }
set
{
if ( repeat != value )
{
repeat=value;
if ( repeat )
complete=false;
ResetCounter();
}
}
}
/// Get or set whether to always show spiro info underneath
[XmlIgnore]
public bool ShowInfo
{
get { return showInfo; }
set { showInfo=value; }
}
/// Get or set the pen thickness
public int PenWidth
{
get { return penWidth; }
set { penWidth=value; }
}
/// Get or set the spiro colour
public Color Colour
{
set { colour=value; }
}
/// Get the bounding rect, including info box if shown
[XmlIgnore]
public Rectangle BoundingRect
{
get
{
Rectangle rc=TrueBoundingRect;
if ( selected || showInfo )
rc.Height+=80; // allow room for info at bottom
rc.Inflate(10,10);
return rc;
}
}
/// Get or set whether to show the trace (the moving circle) during animation
public bool ShowTrace
{
get { return showTrace; }
set { showTrace=value; }
}
/// Get or set whether to randomise the shape after each full drawn
public bool Randomise
{
set { randomise=value; }
get { return randomise;}
}
public int FixedRadius
{
get { return R; }
set
{