Skip to content

Commit 16bbf42

Browse files
committed
Improve handling of big coordinates (avoid overflow exception, improve scales drawing).
1 parent a815b73 commit 16bbf42

File tree

1 file changed

+44
-28
lines changed

1 file changed

+44
-28
lines changed

Visual_Studio_2017/GraphicalDebugging/Viewport.cs

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,11 @@ public static bool DrawAxes(Graphics graphics, Geometry.Box box, Geometry.Unit u
667667
return false;
668668

669669
LocalCS cs = new LocalCS(box, graphics, fill);
670-
//Geometry.Box viewBox = cs.ViewBox();
670+
671+
// NOTE: the coordinates limit of MS GDI+ is 1073741951 so below
672+
// avoid passing such coordinates. But instead of checking this
673+
// value or similar one check whether or not an axis is range
674+
// of an image.
671675

672676
// Axes
673677
float h = graphics.VisibleClipBounds.Height;
@@ -676,17 +680,14 @@ public static bool DrawAxes(Graphics graphics, Geometry.Box box, Geometry.Unit u
676680
if (unit == Geometry.Unit.None)
677681
{
678682
// Y axis
679-
//if (Geometry.IntersectsX(viewBox, 0.0))
680-
{
681-
float x0 = cs.ConvertX(0.0);
683+
float x0 = cs.ConvertX(0.0);
684+
if (0 <= x0 && x0 <= w)
682685
graphics.DrawLine(prime_pen, x0, 0, x0, h);
683-
}
686+
684687
// X axis
685-
//if (Geometry.IntersectsY(viewBox, 0.0))
686-
{
687-
float y0 = cs.ConvertY(0.0);
688+
float y0 = cs.ConvertY(0.0);
689+
if (0 <= y0 && y0 <= h)
688690
graphics.DrawLine(prime_pen, 0, y0, w, y0);
689-
}
690691
}
691692
else
692693
{
@@ -781,15 +782,6 @@ public static bool DrawScales(Graphics graphics, Geometry.Box box, Colors colors
781782
// Find closest power of 10 lesser than the width and height
782783
double pd_x = AbsOuterPow10(mima_x / wStrNumX);
783784
double pd_y = AbsOuterPow10(mima_y / wStrNumH);
784-
// Find starting x and y values being the first lesser whole
785-
// values of the same magnitude, per axis
786-
double x = ScaleStart(mi_x, pd_x);
787-
double y = ScaleStart(mi_y, pd_y);
788-
// Make sure the scale starts outside the view
789-
if (x > mi_x)
790-
x -= pd_x;
791-
if (y > mi_y)
792-
y -= pd_y;
793785
// Create the string output pattern, e.g. 0.00 for previously calculated step
794786
string xStrFormat = StringFormat(pd_x);
795787
string yStrFormat = StringFormat(pd_y);
@@ -801,10 +793,13 @@ public static bool DrawScales(Graphics graphics, Geometry.Box box, Colors colors
801793
int smallScaleY = SmallScaleSegments(wd_y, 10);
802794
float wd_y_step = wd_y / smallScaleY;
803795
float wd_y_limit = wd_y - wd_y_step / 2;
796+
// Find axes intervals
797+
IntervalI xInterval = ScaleStepsInterval(mi_x, ma_x, pd_x);
798+
IntervalI yInterval = ScaleStepsInterval(mi_y, ma_y, pd_y);
804799
// Draw horizontal scale
805-
double limit_x = ma_x + pd_x * 1.001;
806-
for (; x < limit_x; x += pd_x)
800+
for (int i = xInterval.Min; i <= xInterval.Max; ++i)
807801
{
802+
double x = i * pd_x;
808803
float wx = cs.ConvertX(x);
809804
// scale
810805
graphics.DrawLine(penAabb, wx, wHeight, wx, wHeight - 5);
@@ -819,9 +814,9 @@ public static bool DrawScales(Graphics graphics, Geometry.Box box, Colors colors
819814
graphics.DrawLine(penAabb, wsx, wHeight, wsx, wHeight - 3);
820815
}
821816
// Draw vertical scale
822-
double limit_y = ma_y + pd_y * 1.001;
823-
for (; y < limit_y; y += pd_y)
817+
for (int j = yInterval.Min; j <= yInterval.Max; ++j)
824818
{
819+
double y = j * pd_y;
825820
float wy = cs.ConvertY(y);
826821
// scale
827822
graphics.DrawLine(penAabb, wWidth, wy, wWidth - 5, wy);
@@ -913,9 +908,13 @@ private static string StringFormat(double p)
913908
{
914909
double n = Math.Floor(Math.Log10(p));
915910
string result = "0";
916-
if (n < 0.0)
911+
if (n >= 16 || n <= -16)
917912
{
918-
int ni = (int)Math.Max(n, -16.0);
913+
result = "G16";
914+
}
915+
else if (n < 0.0)
916+
{
917+
int ni = (int)n;
919918
result += '.';
920919
for (int i = -1; i >= ni; --i)
921920
result += '0';
@@ -933,11 +932,28 @@ private static double AbsInnerPow10(double x)
933932
return Math.Pow(10, Math.Floor(Math.Log10(Math.Abs(x))));
934933
}
935934

936-
private static double ScaleStart(double val, double step)
935+
private struct IntervalI
936+
{
937+
public IntervalI(int min, int max)
938+
{
939+
Min = min;
940+
Max = max;
941+
}
942+
public int Min;
943+
public int Max;
944+
}
945+
946+
private static IntervalI ScaleStepsInterval(double mi, double ma, double step)
937947
{
938-
double r = val / step;
939-
double i = val >= 0 ? Math.Ceiling(r) : Math.Floor(r);
940-
return i * step;
948+
double r = mi / step;
949+
int min = (int)(mi >= 0 ? Math.Ceiling(r) : Math.Floor(r));
950+
if (min * step > mi)
951+
--min;
952+
r = ma / step;
953+
int max = (int)(ma >= 0 ? Math.Floor(r) : Math.Ceiling(r));
954+
if (max * step < ma)
955+
++max;
956+
return new IntervalI(min, max);
941957
}
942958

943959
private static int SmallScaleSegments(float wStep, float wMinSize)

0 commit comments

Comments
 (0)