Very strange bug in Line [SOLVED]
Posted: Sat Mar 12, 2016 4:35 am
I found the following strange behavior by mistake.
If you run the following code you get a result like the attached (the lines to not meet at the center). I fully realize that there is an error where I convert degrees to radians BUT that should not cause this behavior. The value of pCenter never changes and the sin and cos of ANY number is still in the range -1 to 1 so the resulting lines are definitely wrong.
Even if you fix the radians conversion so that the value if correct as follows.
The resulting lines are STILL wrong but not as badly (which makes no sense at all to me)
If you run the following code you get a result like the attached (the lines to not meet at the center). I fully realize that there is an error where I convert degrees to radians BUT that should not cause this behavior. The value of pCenter never changes and the sin and cos of ANY number is still in the range -1 to 1 so the resulting lines are definitely wrong.
Code: Select all
Point pCenter = new Point(150, 150);
Color pColor = Colors.Red;
double pLineLength = 130;
for (int i = 10; i < 360; i += 10)
{
double radians = i * 180.0 / Math.PI;
double sin = Math.Sin(radians);
double cos = Math.Cos(radians);
Point pPoint = new Point(((cos * pLineLength) + pCenter.X),
((sin * pLineLength) + pCenter.Y));
Windows.UI.Xaml.Shapes.Line line = new Windows.UI.Xaml.Shapes.Line();
line.Stroke = new Windows.UI.Xaml.Media.SolidColorBrush(pColor);
line.X1 = pCenter.X;
line.Y1 = pCenter.Y;
line.X2 = pPoint.X;
line.Y2 = pPoint.Y;
CompassCanvas.Children.Add(line);
}
Even if you fix the radians conversion so that the value if correct as follows.
Code: Select all
Point pCenter = new Point(150, 150);
Color pColor = Colors.Red;
double pLineLength = 130;
for (int i = 10; i < 360; i += 10)
{
double radians = i * Math.PI / 180.0;
double sin = Math.Sin(radians);
double cos = Math.Cos(radians);
Point pPoint = new Point(((cos * pLineLength) + pCenter.X),
((sin * pLineLength) + pCenter.Y));
Windows.UI.Xaml.Shapes.Line line = new Windows.UI.Xaml.Shapes.Line();
line.Stroke = new Windows.UI.Xaml.Media.SolidColorBrush(pColor);
line.X1 = pCenter.X;
line.Y1 = pCenter.Y;
line.X2 = pPoint.X;
line.Y2 = pPoint.Y;
CompassCanvas.Children.Add(line);
}
The resulting lines are STILL wrong but not as badly (which makes no sense at all to me)