Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: WPF
I draw a polygon line with 100 points, the startPoint is (10.000001, 10.000001), the offset = 0.000001, the next point is plus the offset (10.000002, 10.000002), then I zoom the line to 1e6 times or more by matrixTransform, but I saw the the line is not a beeline, is a sawtooth line.
Why does it not work? Do you have any solution for this issue?
I draw the line in a coordinate system (x axis & y axis, like x axis range is the point.x's range, 10.000001~10.0001), and the points are the real data. I want to just draw it by the real points and by the matrixTransform to show the real line I needed.
A strange problem is, I transform the points by matrixTransform, and draw the line is not missing the precision. there is a simple demo for it.
Here is my code:
<Window x:Class="TestMatrix.MainWindow"
        Title="MainWindow" Height="600" Width="700">
    <Grid x:Name="grid">
            <ColumnDefinition Width="auto"/>
            <ColumnDefinition Width="auto"/>
        <Canvas Background="Blue" x:Name="canvas" Width="300" Height="500" Margin="25">
            <Path Stroke="Red" x:Name="path">
                    <PathGeometry x:Name="geometry">
                            <PathFigure x:Name="pathFigure" >
                                    <PolyLineSegment x:Name="polyLineSegment" ></PolyLineSegment>
        <Canvas x:Name="noMatrixCanvas" Grid.Column="1" Width="300" Height="500" Background="LightBlue">
            <Path Stroke="Red">
                    <PathGeometry x:Name="geometryNo">
                            <PathFigure x:Name="pathFigureNo" >
                                    <PolyLineSegment x:Name="polyLineSegmentNo" ></PolyLineSegment>
        private List<Point> m_listPoints = new List<Point>();
        private int count = 100;
        private double c_offset = 1e-7;
        private MatrixTransform m_transform;
        public MainWindow()
        private void DrawWithMatrix()
            pathFigure.StartPoint = m_listPoints[0];
            PolyLineSegment poly = new PolyLineSegment(m_listPoints, true);
            polyLineSegment.Points = poly.Points;
            m_transform = new MatrixTransform();
            m_transform.Matrix = GetMatrix(canvas, count);
            geometry.Transform = m_transform;
        private void InitPoints(int count)
            double xValue = 4.989826;
            double yValue = 9.012345;
            for (int i = 0; i < count; i++)
                m_listPoints.Add(new Point(xValue + i * c_offset, yValue + i * c_offset));
        private Matrix GetMatrix(FrameworkElement fe, int count)
            double scaleX = fe.Width / (count * c_offset);
            double scaleY = fe.Height / (count * c_offset);
            Matrix matrix = new Matrix();
            matrix.Translate(-m_listPoints[0].X, -m_listPoints[count - 1].Y);
            matrix.Scale(scaleX, -scaleY);
            return matrix;
        private void DrawNoMatrixTransform()
            var realPoints = GetRealPoints();
            pathFigureNo.StartPoint = realPoints[0];
            PolyLineSegment poly = new PolyLineSegment(realPoints, true);
            polyLineSegmentNo.Points = poly.Points;
        private List<Point> GetRealPoints()
            List<Point> pixelPoints = new List<Point>();
            m_listPoints.ForEach(point =>
                //get realPoint by compute
                //double x = (point.X - m_listPoints[0].X) * noMatrixCanvas.Width / (count * c_offset);
                //double y = -(point.Y - m_listPoints[count - 1].Y) * noMatrixCanvas.Height / (count * c_offset);
                //pixelPoints.Add(new Point(x, y));

                //get realPoint by matrix
                Point realPoint = m_transform.Transform(point);
            return pixelPoints;
Posted 20-Nov-12 5:40am
Edited 21-Nov-12 0:33am
Sergey Alexandrovich Kryukov at 20-Nov-12 14:22pm
It is possibly that this is precision, right. The solution is usually possible, but needs a lot more detail. And the solution is very likely lies in the application architecture, not implementation detail. You need to explain your purpose, and the purpose of zoom and other related behaiors...
songwq at 21-Nov-12 0:19am
Thank you for your reply. And I add more info, please pay attention to it.
Earloc at 21-Nov-12 2:54am
your sample is missing definition of
field "m_transform" and method "GetMatrix"
songwq at 21-Nov-12 5:35am
I had updated the code. Thank you for your reminder. :)
songwq at 23-Nov-12 23:37pm
Is there any solution for this issue?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Zoltán Zörgő 320
1 CHill60 185
2 /\jmot 160
3 Peter Leow 160
4 Pikoh 125
0 Sergey Alexandrovich Kryukov 9,423
1 OriginalGriff 6,891
2 Peter Leow 4,637
3 Zoltán Zörgő 4,304
4 CHill60 2,932

Advertise | Privacy | Mobile
Web03 | 2.8.150129.1 | Last Updated 21 Nov 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100