Пример #1
0
 private PointF[] concatenar(PointF[] trozo1, PointF[] trozo2)
 {
     PointF[] vectorSuma = new PointF[trozo1.Length + trozo2.Length];
     trozo1.CopyTo(vectorSuma, 0);
     trozo2.CopyTo(vectorSuma, trozo1.Length);
     return vectorSuma;
 }
Пример #2
0
        /// <summary>
        /// Vytvoří konvexní obal z daných vertexů
        /// </summary>
        /// <param name="Vertices">Vertexy</param>
        /// <returns>Pole vertexů tvořící konvexní obal</returns>
        public static PointF[] GetConvexHull(PointF[] Vertices)
        {
            int n = Vertices.Length, k = 0;
            PointF[] Hull = new PointF[2 * n],Sorted = new PointF[Vertices.Length];
            Vertices.CopyTo(Sorted, 0);

            Array.Sort<PointF>(Sorted, new Comparison<PointF>(LexicalPointComparison));

            for (int i = 0; i < n; i++)
            {
                while (k >= 2 && zc((PointF)Hull[k - 2],(PointF)Hull[k - 1], Sorted[i]) <= 0) k--;
                Hull[k++] = Sorted[i];
            }

            for (int i = n - 2, t = k + 1; i >= 0; i--)
            {
                while (k >= t && zc((PointF)Hull[k - 2], (PointF)Hull[k - 1], Sorted[i]) <= 0) k--;
                Hull[k++] = Sorted[i];
            }

            PointF[] Ret = new PointF[k-1];
            for (int i = 0; i < k - 1; i++)
            {
                Ret[i].X = Hull[i].X;
                Ret[i].Y = Hull[i].Y;
            }
            return Ret;
        }
Пример #3
0
        /// <summary>
        /// Výchozí konstruktor
        /// </summary>
        public GeometryDescriptor(PointF[] Default)
        {
            Centroid = new PointF(0, 0);
            Height = Width = Depth = FrontalArea = 0;

            DefaultVertices = new PointF[Default.Length];
            Default.CopyTo(DefaultVertices, 0);
        }
Пример #4
0
        /// <summary>
        /// Vytvoří fyzický model tělesa jako objekt z daných vertexů
        /// </summary>       
        /// <param name="Vertices">Vertexy tělesa</param>
        /// <param name="InitPosition">Počáteční poloha tělesa</param>
        /// <param name="COG">Těžiště tělesa</param>
        public Geometry(PointF[] Vertices,PointF InitPosition, PointF? COG)
        {
            if (Vertices == null || Vertices.Length < 3) throw new ArgumentException();
            surf = vol = angle = 0;
            scale = 1.0f;

            desc = AnalyzeVertexGroup(Vertices);
            geom = new PointF[Vertices.Length];
            Vertices.CopyTo(geom, 0);

            if (COG.HasValue)
                center = (Vector)COG;
            else center = (Vector)desc.Centroid;

            Nail = (Vector)InitPosition;
            Position = (Vector)InitPosition;
        }
Пример #5
0
 /// <summary>
 /// Определить отрисовываемые точки
 /// </summary>
 /// <param name="pts"></param>
 public void InsertPoints(PointF[] pts)
 {
     points = new PointF[pts.Length];
     pts.CopyTo(points, 0);
 }
Пример #6
0
        public static PointF[] FillPoints(PointF[] sourcePoints, float stepDistance)
        {
            if (sourcePoints.Length < 2)
            {
                PointF[] retArray = new PointF[sourcePoints.Length];
                sourcePoints.CopyTo(retArray, 0);
                return retArray;
            }

            List<PointF> result = new List<PointF>(sourcePoints.Length);
            PointF lastPoint = sourcePoints[0];
            int i = 1;
            result.Add(lastPoint);

            while (i < sourcePoints.Length)
            {
                float dist = Distance(lastPoint, sourcePoints[i]);
                // point is within the proper distance
                if (dist <= stepDistance)
                {
                    result.Add(sourcePoints[i]);
                    lastPoint = sourcePoints[i];
                    i++;
                }
                else
                {
                    // slope between lastPoint and sourcePoints[i]
                    float vX = (sourcePoints[i].X - lastPoint.X) / dist;
                    float vY = (sourcePoints[i].Y - lastPoint.Y) / dist;
                    PointF newPt = new PointF(lastPoint.X + vX * stepDistance, lastPoint.Y + vY * stepDistance);
                    result.Add(newPt);
                    lastPoint = newPt;
                }

            }
            return result.ToArray();
        }
Пример #7
0
      public void TestRotationMatrix2D()
      {
         double angle = 32;
         Size size = new Size(960, 480);
         PointF center = new PointF(size.Width * 0.5f, size.Height * 0.5f);
         using (RotationMatrix2D rotationMatrix = new RotationMatrix2D(center, -angle, 1))
         {
            PointF[] corners = new PointF[] {
                  new PointF(0, 0),
                  new PointF(size.Width - 1, 0),
                  new PointF(size.Width - 1, size.Height - 1),
                  new PointF(0, size.Height - 1)};
            PointF[] oldCorners = new PointF[corners.Length];
            corners.CopyTo(oldCorners, 0);

            rotationMatrix.RotatePoints(corners);

            Mat transformation = CvInvoke.EstimateRigidTransform(oldCorners, corners, true);

            Matrix<double> delta = new Matrix<double>(transformation.Size);
            CvInvoke.AbsDiff(rotationMatrix, transformation, delta);
            double min = 0, max = 0;
            Point minLoc = new Point(), maxLoc = new Point();
            CvInvoke.MinMaxLoc(delta, ref min, ref max, ref minLoc, ref maxLoc, null);

            double min2, max2;
            int[] minLoc2 = new int[2], maxLoc2 = new int[2];
            CvInvoke.MinMaxIdx(delta, out min2, out max2, minLoc2, maxLoc2, null);
            EmguAssert.IsTrue(min == min2);
            EmguAssert.IsTrue(max == max2);
            EmguAssert.IsTrue(minLoc.X == minLoc2[1]);
            EmguAssert.IsTrue(minLoc.Y == minLoc2[0]);
            EmguAssert.IsTrue(maxLoc.X == maxLoc2[1]);
            EmguAssert.IsTrue(maxLoc.Y == maxLoc2[0]);

            EmguAssert.IsTrue(max < 1.0e-4, String.Format("Error {0} is too large. Expected to be less than 1.0e-4", max));
         }
      }
Пример #8
0
        //Code adapted and improved from: http://blog.csharphelper.com/2010/01/04/find-a-polygons-centroid-in-c.aspx
        // refer to wikipedia for math formulas centroid of polygon http://en.wikipedia.org/wiki/Centroid
        private PointF FindCentroid(PointF[] Hull)
        {
            // Add the first point at the end of the array.
            int num_points = Hull.Length;
            PointF[] pts = new PointF[num_points + 1];
            Hull.CopyTo(pts, 0);
            pts[num_points] = Hull[0];

            // Find the centroid.
            float X = 0;
            float Y = 0;
            float second_factor;
            for (int i = 0; i < num_points; i++)
            {
                second_factor = pts[i].X * pts[i + 1].Y - pts[i + 1].X * pts[i].Y;
                X += (pts[i].X + pts[i + 1].X) * second_factor;
                Y += (pts[i].Y + pts[i + 1].Y) * second_factor;
            }
            // Divide by 6 times the polygon's area.
            float polygon_area = Math.Abs(SignedPolygonArea(Hull));
            X /= (6 * polygon_area);
            Y /= (6 * polygon_area);

            // If the values are negative, the polygon is
            // oriented counterclockwise so reverse the signs.
            if (X < 0)
            {
                X = -X;
                Y = -Y;
            }
            return new PointF(X, Y);
        }
Пример #9
0
        public void OnMouseDown(MouseEventArgs e)
        {
            bool flag2;
            bool flag3;
            PointF tf1;

            if (e.Button == MouseButtons.Left)
            {//wlwl
                this.startpoint = this.mouseAreaControl.PicturePanel.PointToView(new PointF(e.X,e.Y));
                if(this.mouseAreaControl.CurrentOperation==ToolOperation.XPolyLine)
                {
                    if(points.Length>0)
                    {
                        this.startpoint = this.mouseAreaControl.PicturePanel.PointToView(new PointF((float) e.X, points[0].Y));
                    }
                    else
                    {
                        this.startpoint = this.mouseAreaControl.PicturePanel.PointToView(new PointF((float) e.X, (float) e.Y));
                    }
                }
                if(this.mouseAreaControl.CurrentOperation==ToolOperation.YPolyLine)
                {
                    if(points.Length>0)
                    {
                        this.startpoint = this.mouseAreaControl.PicturePanel.PointToView(new PointF(points[0].X, (float) e.Y));
                    }
                    else
                    {
                        this.startpoint = this.mouseAreaControl.PicturePanel.PointToView(new PointF((float) e.X, (float) e.Y));
                    }
                }
                if(this.mouseAreaControl.CurrentOperation==ToolOperation.PolyLine ||
                    this.mouseAreaControl.CurrentOperation==ToolOperation.Polygon ||
                    this.mouseAreaControl.CurrentOperation==ToolOperation.InterEnclosure ||
                    this.mouseAreaControl.CurrentOperation==ToolOperation.Enclosure ||
                    this.mouseAreaControl.CurrentOperation==ToolOperation.LeadLine ||
                    this.mouseAreaControl.CurrentOperation==ToolOperation.AreaPolygon)
                {
                    this.startpoint = this.mouseAreaControl.PicturePanel.PointToView(new PointF((float) e.X, (float) e.Y));
                }
                if (this.mouseAreaControl.CurrentOperation == ToolOperation.Enclosure || this.mouseAreaControl.CurrentOperation == ToolOperation.InterEnclosure)
                {
                    this.AreaPoints.Add(new PointF((float) e.X, (float) e.Y));
                }
                SizeF ef1 = this.mouseAreaControl.PicturePanel.GridSize;
                float single1 = ef1.Height;
                float single2 = ef1.Width;
                if (this.mouseAreaControl.PicturePanel.SnapToGrid)
                {
                    int num1 = (int) ((this.startpoint.X + (single2 / 2f)) / single2);
                    int num2 = (int) ((this.startpoint.Y + (single1 / 2f)) / single1);
                    this.startpoint = (PointF) new Point((int) (num1 * single2), (int) (num2 * single1));
                }
                SvgDocument document1 = this.mouseAreaControl.SVGDocument;
                bool flag1 = document1.AcceptChanges;
                document1.NumberOfUndoOperations = 200;
                this.mousedown = true;
                switch (this.operate)
                {
                    case PolyOperate.Draw:
                    {
                        document1.AcceptChanges = false;
                        flag2 = false;
                        if (this.graph != null)
                        {
                            if (!(this.graph is Polygon) && !(this.graph is Polyline))
                            {
                                flag2 = true;
                            }
                            break;
                        }
                        flag2 = true;
                        break;
                    }
                    case PolyOperate.MovePath:
                    {
                        goto Label_05F5;
                    }
                    case PolyOperate.MovePoint:
                    {
                        PointF tf3;
                        this.nextPoint = tf3 = PointF.Empty;
                        this.prePoint = tf3;
                        if ((this.moveindex < 0) || (this.moveindex >= this.points.Length))
                        {
                            goto Label_05F5;
                        }
                        flag3 = this.graph is Polygon;
                        if ((this.moveindex - 1) < 0)
                        {
                            if ((this.points.Length >= 3) && flag3)
                            {
                                this.prePoint = this.points[this.points.Length - 1];
                            }
                            goto Label_042C;
                        }
                        this.prePoint = this.points[this.moveindex - 1];
                        goto Label_042C;
                    }
                    case PolyOperate.Del:
                    {
                        if ((this.moveindex >= 0) && (this.moveindex < this.points.Length))
                        {
                            ArrayList list1 = new ArrayList(this.points);
                            list1.RemoveAt(this.moveindex);
                            this.points = new PointF[list1.Count];
                            list1.CopyTo(this.points);
                            Matrix matrix1 = this.graph.GraphTransform.Matrix.Clone();
                            matrix1.Invert();
                            if (this.points.Length > 0)
                            {
                                matrix1.TransformPoints(this.points);
                            }
                        }
                        goto Label_05F5;
                    }
                    case PolyOperate.Break://��·�Ͽ�
                    {
                        if ((this.moveindex > 0) && (this.moveindex < this.points.Length - 1))
                        {
                            ArrayList list1 = new ArrayList(this.points);
                            PointF[] points2 = new PointF[this.points.Length - moveindex];

                            this.points = new PointF[moveindex + 1];
                            list1.CopyTo(0, this.points, 0, this.moveindex + 1);
                            list1.CopyTo(moveindex, points2, 0, list1.Count - moveindex);

                            Matrix matrix1 = this.graph.GraphTransform.Matrix.Clone();
                            matrix1.Invert();
                            if (points2.Length > 0)
                            {
                                matrix1.TransformPoints(points2);
                                SvgElement copyEelement = (this.graph as XmlNode).CloneNode(true) as SvgElement;
                                IGraph graph1 = this.graph;

                                copyEelement.SetAttribute("info-name", ((SvgElement)graph1).GetAttribute("info-name") + "-2");
                                ((SvgElement)graph).SetAttribute("info-name", ((SvgElement)graph1).GetAttribute("info-name") + "-1");
                                copyEelement = this.mouseAreaControl.PicturePanel.AddElement(copyEelement);
                                this.mouseAreaControl.SVGDocument.CurrentElement = graph1 as SvgElement;
                                copyEelement.RemoveAttribute("points");
                                UpdateGraph(copyEelement, points2);
                                this.mouseAreaControl.PicturePanel.InvalidateElement(copyEelement);
                                BreakElementEventArgs copy = new BreakElementEventArgs(copyEelement);
                                if (OnPolyLineBreak != null &&  copyEelement!= null)
                                {

                                    OnPolyLineBreak(this.mouseAreaControl.SVGDocument.CurrentElement,copy);
                                }
                            }
                            if (this.points.Length > 0)
                            {
                                matrix1.TransformPoints(this.points);
                            }
                        }
                        else
                        {
                            return;
                        }
                        goto Label_05F5;
                    }
                    case PolyOperate.Add:
                    {
                        if ((this.insertindex < 0) || (this.insertindex >= this.points.Length))
                        {
                            goto Label_05F5;
                        }
                        this.points = new PointF[0];
                        if (!(this.graph is Polygon))
                        {
                            if (this.graph is Polyline)
                            {
                                this.points = ((Polyline) this.graph).Points;
                            }
                            goto Label_058D;
                        }
                        this.points = ((Polygon) this.graph).Points;
                        goto Label_058D;
                    }
                    default:
                    {
                        goto Label_05F5;
                    }
                }
                if (flag2)
                {
                    IGraph graph1 = this.mouseAreaControl.PicturePanel.PreGraph;
                    if (graph1 == null)
                    {
                        return;
                    }
                    this.graph = (Graph) ((SvgElement) graph1).Clone();
                    this.mouseAreaControl.SVGDocument.AcceptChanges = false;
                    if (this.graph != null)
                    {
                        ((SvgElement) this.graph).RemoveAttribute("points");
                    }
                    if (((SvgElement) this.graph) is IGraphPath)
                    {
                        if ((((SvgElement) graph1).GetAttribute("style") != string.Empty) && (((SvgElement) graph1).GetAttribute("style") != null))
                        {
                            this.mouseAreaControl.SVGDocument.AcceptChanges = false;
                            AttributeFunc.SetAttributeValue((SvgElement) this.graph, "style", ((SvgElement) this.graph).GetAttribute("style"));
                        }
                        ISvgBrush brush1 = ((IGraphPath) graph1).GraphBrush;
                        if (brush1 is SvgElement)
                        {
                            ISvgBrush brush2 = (ISvgBrush) ((SvgElement) brush1).Clone();
                            ((IGraphPath) this.graph).GraphBrush = brush2;
                            ((SvgElement) brush2).pretime = -1;
                        }
                        else
                        {
                            ((IGraphPath) this.graph).GraphBrush = brush1;
                        }
                        brush1 = ((IGraphPath) graph1).GraphStroke.Brush;
                        if (brush1 is SvgElement)
                        {
                            ISvgBrush brush3 = (ISvgBrush) ((SvgElement) brush1).Clone();
                            ((IGraphPath) this.graph).GraphStroke = new Stroke(brush3);
                            ((SvgElement) brush3).pretime = -1;
                        }
                        else
                        {
                            ((IGraphPath) this.graph).GraphStroke.Brush = brush1;
                        }
                    }
                }
                PointF[] tfArray1 = new PointF[0];
                if (this.graph is Polygon)
                {
                    tfArray1 = ((Polygon) this.graph).Points;
                }
                else if (this.graph is Polyline)
                {
                    tfArray1 = ((Polyline) this.graph).Points;
                }
                this.points = new PointF[1];
                int insertIndex= 0;
                if (tfArray1 != null)
                {
                    ;
                    this.points = new PointF[tfArray1.Length + 1];

                    if(addBegin)
                    {
                        tfArray1.CopyTo(this.points, 1);
                    }
                    else
                    {
                        tfArray1.CopyTo(this.points, 0);
                        insertIndex=tfArray1.Length;
                    }
                }
                if(addBegin || addEnd)
                {
                    Matrix matrix1 = this.graph.GraphTransform.Matrix.Clone();
                    matrix1.Invert();
                    PointF[] points2 =new PointF[1]{new PointF(e.X,e.Y)};
                    matrix1.TransformPoints(points2);
                    this.startpoint = points2[0];
                }

                this.points[insertIndex] = this.startpoint;
                goto Label_05F5;
            Label_042C:
                if ((this.moveindex + 1) < this.points.Length)
                {
                    this.nextPoint = this.points[this.moveindex + 1];
                    goto Label_05F5;
                }
                if ((this.points.Length >= 3) && flag3)
                {
                    this.nextPoint = this.points[0];
                }
                goto Label_05F5;
            Label_058D:
                Matrix matrix2 = this.graph.GraphTransform.Matrix.Clone();
                matrix2.Invert();
                //tf1 = this.mouseAreaControl.PicturePanel.PointToView(new PointF((float)e.X, (float)e.Y));//new PointF((float) e.X, (float) e.Y)
                PointF[] tfTemp = new PointF[1] { new PointF(e.X, e.Y) };
                matrix2.TransformPoints(tfTemp);
                ArrayList list2 = new ArrayList(this.points);
                list2.Insert(this.insertindex, tfTemp[0]);
                this.points = new PointF[list2.Count];
                list2.CopyTo(this.points);

            Label_05F5:
                //2006-10-23 ����Χ����ʼ��ɫ
            //				if(this.mouseAreaControl.CurrentOperation == ToolOperation.Enclosure || this.mouseAreaControl.CurrentOperation == ToolOperation.InterEnclosure)
            //				{
            //					((XmlElement) this.graph).SetAttribute("style","fill:#C0C0FF;fill-opacity:0.3;stroke:#000000;stroke-opacity:1;");
            //				}

                if (((this.operate == PolyOperate.Del) || (this.operate == PolyOperate.Draw)) || (this.operate == PolyOperate.Add) ||(this.operate==PolyOperate.Break))
                {

                    StringBuilder text1 = new StringBuilder();
                    int num3 = 0;
                    PointF[] tfArray2 = this.points;
                    for (int num4 = 0; num4 < tfArray2.Length; num4++)
                    {
                        PointF tf2 = tfArray2[num4];
                        text1.Append( tf2.X.ToString() + " " + tf2.Y.ToString());
                        if (num3 < (this.points.Length - 1))
                        {
                            text1.Append( ",");
                        }
                        num3++;
                    }
                    this.mouseAreaControl.PicturePanel.InvalidateElement((SvgElement) this.graph);
                    if (((SvgElement) this.graph).ParentNode == null)
                    {
                        this.UpdateGraph(text1.ToString());
                        document1.AcceptChanges = true;
                        IGraph graph2 = this.graph;
                        this.mouseAreaControl.PicturePanel.AddElement(this.graph);
                        this.graph = graph2;
                    }
                    else
                    {
                        document1.AcceptChanges = true;
                        this.UpdateGraph(text1.ToString());
                    }
                    document1.AcceptChanges = flag1;
                    this.mouseAreaControl.Invalidate();
                    if (this.graph != null)
                    {
                        ((SvgElement) this.graph).pretime = -1;
                        this.mouseAreaControl.PicturePanel.InvalidateElement((SvgElement) this.graph);
                    }

                }
                document1.NotifyUndo();
                this.reversePath.Reset();
            }
            else if(e.Button == MouseButtons.Right)//����1
            {
                if (this.mouseAreaControl.CurrentOperation == ToolOperation.Enclosure || this.mouseAreaControl.CurrentOperation == ToolOperation.InterEnclosure)
                {
                    //this.AreaPoints.Add(new PointF((float) e.X, (float) e.Y));

            //					this.mouseAreaControl.GoBottom((SvgElement)this.graph);
            //					((SvgElement)this.graph).Clone();
                    PointF[] tfArray1 = new PointF[this.AreaPoints.Count];
                    this.AreaPoints.CopyTo(tfArray1, 0);
                    this.AreaPoints.Clear();

                    Matrix matrix1 = new Matrix();
                    if(tfArray1.Length<3)
                    {

                        this.mouseAreaControl.CurrentOperation=ToolOperation.Select;
                        return;
                    }
                    this.selectAreaPath = new GraphicsPath();
                    this.selectAreaPath.AddLines(tfArray1);
                    this.selectAreaPath.CloseFigure();

                    Region region1 = new Region(this.selectAreaPath);

                    RectangleF r1=selectAreaPath.GetBounds();
                    /* 2005���� ��ǰ������Ҫ�ֶ���ӽ�����*/
                    XmlNode node1=((SvgElement)this.graph).Clone();
                    SvgElement svgele=(SvgElement)node1;

                    this.mouseAreaControl.SVGDocument.ClearSelects();

                    using(Graphics g =Graphics.FromHwnd(IntPtr.Zero))
                          {
                        foreach(ILayer layer1 in mouseAreaControl.SVGDocument.Layers)
                        {
                            if(!layer1.Visible )continue;

                            SvgElementCollection.ISvgElementEnumerator	enumerator1 = layer1.GraphList.GetEnumerator();
                            while (enumerator1.MoveNext())
                            {
                                IGraph graph1 = (IGraph) enumerator1.Current;
                                if(!graph1.Visible || !graph1.DrawVisible || this.graph==graph1)continue;
                                GraphicsPath path1 = (GraphicsPath) graph1.GPath.Clone();
                                path1.Transform(graph1.GraphTransform.Matrix);
                                Region ef1 =null;
                                if(graph1 is Use)
                                {
                                    ef1 = new Region(PathFunc.GetBounds(path1));
                                }
                                else if (graph1 is Line)
                                {
                                    ef1 = new Region(PathFunc.GetBounds(path1));

                                }else
                                {
                                    ef1 =new Region(path1);
                                }

                                // ����Χ��ѡ��ʽΪ��ȫ��������ѡ��
                                if(this.mouseAreaControl.CurrentOperation==ToolOperation.Enclosure)
                                {
                                    RectangleF rt1 = ef1.GetBounds(g);
                                    ef1.Intersect(region1);
                                    if(ef1.GetBounds(g)==rt1)
                                    {
                                        this.mouseAreaControl.SVGDocument.AddSelectElement(graph1);
                                    }
                                    continue;
                                }

                                // ����Χ��ѡ��ʽΪ��߼�ѡ��
                                if(this.mouseAreaControl.CurrentOperation==ToolOperation.InterEnclosure)
                                {
                                    ef1.Intersect(region1);

                                    if(!ef1.GetBounds(g).IsEmpty)
                                        //if ((region1.IsVisible(ef1/*new System.Drawing.Rectangle((int) ef1.X, (int) ef1.Y, (int) ef1.Width, (int) ef1.Height)*/) && !graph1.IsLock) && (graph1.DrawVisible /*&& (AnimFunc.GetKeyIndex((SvgElement) graph1, this.mouseAreaControl.SVGDocument.ControlTime, true) >= 0)*/))
                                    {
                                        this.mouseAreaControl.SVGDocument.AddSelectElement(graph1);
                                    }
                                }
                            }

                        }
                        this.mouseAreaControl.SVGDocument.AddSelectElement(graph);
                    }

                    GraphicsPath path2 = new GraphicsPath();
                    path2.AddLines(tfArray1);
                    RectangleF ef2 = path2.GetBounds();
                    /* 2005 ������ʹ��,2003��������ɾ����������ظ�.ԭ����.*/
                    if(this.mouseAreaControl.CurrentOperation==ToolOperation.Enclosure)
                    {
                        this.mouseAreaControl.SVGDocument.AddSelectElement(svgele);
                    }
                    this.mouseAreaControl.Invalidate(new System.Drawing.Rectangle(((int) ef2.X) - 10, ((int) ef2.Y) - 10, ((int) ef2.Width) + 20, ((int) ef2.Height) + 20));
                    return;
                }

            } //����Ҽ�̧��
        }
Пример #10
0
        private static void AddBezierSpline(PointF[] points, GraphicsPath path)
        {
            PointF[] tempPoints = new PointF[points.Length];

            float width = (float)Math.Sqrt(
                              Math.Pow(points[0].X - points[points.Length-1].X , 2) +
                              Math.Pow(points[0].Y - points[points.Length-1].Y , 2));
            float step = 1.0f/width;
            float t = 0;

            PointF current = points[0];
            PointF old = points[0];

            while(t <= 1.0f) {
                points.CopyTo(tempPoints, 0);
                for(int j = points.Length - 1; j>0 ; j--) {
                    for(int i = 0; i<j; i++) {
                        tempPoints[i].X = (1.0f-t)*tempPoints[i].X + t*tempPoints[i+1].X;
                        tempPoints[i].Y = (1.0f-t)*tempPoints[i].Y + t*tempPoints[i+1].Y;
                    }
                }
                current = tempPoints[0];

                path.AddLine(old, current);
                old = current;
                t+=step;
            }

            path.AddLine(current, points[points.Length -1]);
        }
Пример #11
0
        private static PointF SplinePoint(PointF[] points, double t)
        {
            PointF[] p = new PointF[points.Length];
            points.CopyTo(p, 0);

            while (p.Length > 1)
            {
                p = SplineFunctionOo(p, t);
            }
            return p[0];
        }
Пример #12
0
        public static void CalcSpline(PointF[] points, int filecount, out PointF[] output, float minVal, float maxVal)
        {
            int Pcount = points.Length;
            output = new PointF[filecount];

            //to find out where the curve calculation should start: (in case there are equal Y values wich should build a straight line)
            int start = 0;
            for (int i = 1; i < Pcount; i++)
            {
                if (points[i].Y != points[i - 1].Y)
                {
                    start = i - 1;
                    break;
                }
            }

            if (Pcount == 2)
            {
                float stepY = (points[points.Length - 1].Y - points[0].Y) / (float)(filecount - 1);
                float stepX = (points[points.Length - 1].X - points[0].X) / (float)(filecount - 1);
                for (int i = 0; i < filecount; i++)
                {
                    output[i].X = points[0].X + (stepX * i);
                    output[i].Y = points[0].Y + (stepY * i);
                    if (output[i].Y < minVal) { output[i].Y = minVal; }
                    else if (output[i].Y > maxVal) { output[i].Y = maxVal; }
                }
            }
            else if (Pcount > 2)
            {
                float[] yCoords = new float[Pcount];        // Newton form coefficients
                float[] xCoords = new float[Pcount];        // x-coordinates of nodes
                float y;
                float x;

                int precision = (filecount - 1) / (Pcount - 1);
                int npp = (Pcount * precision);                     // number of points used for drawing

                for (int i = 0; i < Pcount; i++)
                {
                    xCoords[i] = points[i].X;
                    yCoords[i] = points[i].Y;
                }

                float[] a = new float[Pcount];
                float x1;
                float x2;
                float[] h = new float[Pcount];
                for (int i = 1; i < Pcount; i++)
                {
                    h[i] = xCoords[i] - xCoords[i - 1];
                }

                float[] sub = new float[Pcount - 1];
                float[] diag = new float[Pcount - 1];
                float[] sup = new float[Pcount - 1];

                for (int i = 1; i < Pcount - 1; i++)
                {
                    diag[i] = (h[i] + h[i + 1]) / 3;
                    sup[i] = h[i + 1] / 6;
                    sub[i] = h[i] / 6;
                    a[i] = (yCoords[i + 1] - yCoords[i]) / h[i + 1] - (yCoords[i] - yCoords[i - 1]) / h[i];
                }
                solveTridiag(sub, diag, sup, ref a, Pcount - 2);

                int count = 1;

                output[0].X = points[0].X;
                output[0].Y = points[0].Y;

                for (int i = 1; i < Pcount; i++)
                {
                    for (int j = 1; j <= precision; j++)
                    {
                        x1 = (h[i] * j) / precision;
                        x2 = h[i] - x1;
                        y = ((-a[i - 1] / 6 * (x2 + h[i]) * x1 + yCoords[i - 1]) * x2 +
                            (-a[i] / 6 * (x1 + h[i]) * x2 + yCoords[i]) * x1) / h[i];
                        x = xCoords[i - 1] + x1;

                        output[count].X = x;
                        output[count].Y = y;
                        if (output[count].Y < minVal) { output[count].Y = minVal; }
                        else if (output[count].Y > maxVal) { output[count].Y = maxVal; }

                        if (start > 0 && i <= start)
                        {
                            output[count].X = points[i].X;
                            output[count].Y = points[i].Y;
                            if (output[count].Y < minVal) { output[count].Y = minVal; }
                            else if (output[count].Y > maxVal) { output[count].Y = maxVal; }
                        }

                        count++;
                    }
                }

                //interpolate if there isn´t the same amount of points and filecount:
                if (npp - filecount - precision != 0)
                {
                    int part = filecount / (Math.Abs(npp - filecount - precision) + 1);
                    PointF[] tmpOut = new PointF[output.Length];
                    output.CopyTo(tmpOut, 0);
                    int nr = 0;

                    for (int i = 0; i < filecount; i++)
                    {
                        if (i == part * (nr + 1) && nr < Math.Abs(npp - filecount - precision))
                        {
                            output[i].X = tmpOut[i - nr].X + ((tmpOut[i - nr].X - tmpOut[i - nr + 1].X) / 2);
                            output[i].Y = tmpOut[i - nr].Y + ((tmpOut[i - nr].Y - tmpOut[i - nr + 1].Y) / 2);
                            nr++;
                        }
                        else if (nr < Math.Abs(npp - filecount - precision))
                        {
                            output[i] = tmpOut[i - nr];
                        }
                        else
                        {
                            output[i] = tmpOut[i - nr + 1];
                        }
                    }
                }
            }
            else
            {
                for (int i = 0; i < filecount; i++)
                {
                    output[i].Y = points[0].Y;
                    output[i].X = points[0].X;
                }
            }
        }