WPF绘图问题

时间:2014-10-18 13:04:05

标签: c# wpf

我想在WPF中绘制折线。但它只是因为点之间的差异很小而显示出一点。我也应用了scaleTransform和TranslateTransform,但问题仍然存在。

代码是

double minX = 25.52524938052284;
double minY = 44.267051317656474;
double maxX = 25.525239580522843;
double maxY = 44.26684671765647;
System.Windows.Point point1 = new System.Windows.Point(minX, minY);
System.Windows.Point point2 = new System.Windows.Point(maxX, maxY);

Polyline myPolyline = new Polyline();
myPolyline.Stroke = System.Windows.Media.Brushes.SaddleBrown;
myPolyline.StrokeThickness = 1;

polylinePointCollection.Add(point1);
polylinePointCollection.Add(point2);

ScaleTransform st = new ScaleTransform(100000,100000,25,25);
myPolyline.RenderTransform = st;
TranslateTransform tt = new TranslateTransform(100, 100);
myPolyline.RenderTransform = tt;
canvas.Children.Add(myPolyline);  

这是示例代码。我有一个15到20之间的点列表,所以坐标之间的这个小的基本差异必须显示在屏幕上,但它只显示一个点。

1 个答案:

答案 0 :(得分:1)

您正在处理一些非常小的差异,因此,转换坐标非常敏感。使用您的代码并进行一些小的调整,请考虑以下事项:

(略过翻译并调整数量非常少)

double minX = 25.52524938052284;
double minY = 44.267051317656474;
double maxX = 25.525239580522843;
double maxY = 44.26684671765647;

double diffX = maxX - minX;         // -0.0000097999999972842033
double diffY = maxY - minY;         // -0.00020460000000355194

// setting values to positive small differences
minX = 25.525d;
minY = 44.266d;
maxX = 25.526d;
maxY = 44.267d;

System.Windows.Point point1 = new System.Windows.Point(minX, minY);
System.Windows.Point point2 = new System.Windows.Point(maxX, maxY);

Polyline myPolyline = new Polyline();
myPolyline.Stroke = System.Windows.Media.Brushes.SaddleBrown;
myPolyline.StrokeThickness = 0.0003d;  // <-- necessary due to scaling

myPolyline.Points.Add(point1);
myPolyline.Points.Add(point2);

System.Windows.Point center = new System.Windows.Point();
center.X = minX + ((maxX - minX) / 2.0d) - 0.001d;
center.Y = minY + ((maxY - minY) / 2.0d) - 0.001d;

double scale = 100000.0d;
//ScaleTransform st = new ScaleTransform(100000, 100000, 25, 25);
ScaleTransform st = new ScaleTransform(scale, scale, center.X, center.Y);
myPolyline.RenderTransform = st;

//TranslateTransform tt = new TranslateTransform(100, 100);
//myPolyline.RenderTransform = tt;
canvas.Children.Add(myPolyline);

产生这个结果:

screenshot

规模和翻译的容差非常敏感。您可能会发现使用某个系数放大数字会更容易。注意,将行程厚度调整到如此小的值。稍微增加它实际上会使线看起来像是朝向不同的方向。

希望这有帮助!