private PolyBezierSegment CreateSegments(float[] ews, float[] nss) { PolyBezierSegment segments = new PolyBezierSegment(); if (ews != null && nss != null && ews.Length > 4 && nss.Length > 4) { int length = Math.Min(ews.Length, nss.Length); for (int i = 1; i < length; i++)//跳过第一个点,第一个点是起始点 { segments.Points.Add(new Point() { X = ews[i], Y = nss[i] }); } int reminder = segments.Points.Count % 3; if (reminder != 0) {//贝塞尔曲线必须被3整除(否则爆Win32致命异常),如果不够,根据业务需要,添加起始点来完善 //必然是1或者2,起码要添加一个: segments.Points.Add(new Point() { X = ews[0], Y = nss[0] }); if (reminder == 1) {//差两个,已经添加了一个,再添加一个 segments.Points.Add(new Point() { X = ews[0], Y = nss[0] }); } } } return segments; }
public object ConvertToNative(Geometry geometry) { winMedia.Geometry winGeometry = null; // Determine what type of geometry we're dealing with. if (geometry is LineGeometry) { LineGeometry xamGeometry = geometry as LineGeometry; winGeometry = new winMedia.LineGeometry { StartPoint = ConvertPoint(xamGeometry.StartPoint), EndPoint = ConvertPoint(xamGeometry.EndPoint) }; } else if (geometry is RectangleGeometry) { Rect rect = (geometry as RectangleGeometry).Rect; winGeometry = new winMedia.RectangleGeometry { Rect = new winFound.Rect(rect.X, rect.Y, rect.Width, rect.Height) }; } else if (geometry is EllipseGeometry) { EllipseGeometry xamGeometry = geometry as EllipseGeometry; winGeometry = new winMedia.EllipseGeometry { Center = ConvertPoint(xamGeometry.Center), RadiusX = xamGeometry.RadiusX, RadiusY = xamGeometry.RadiusY }; } else if (geometry is GeometryGroup) { GeometryGroup xamGeometry = geometry as GeometryGroup; winGeometry = new winMedia.GeometryGroup { FillRule = ConvertFillRule(xamGeometry.FillRule) }; foreach (Geometry xamChild in xamGeometry.Children) { winMedia.Geometry winChild = ConvertToNative(xamChild) as winMedia.Geometry; (winGeometry as winMedia.GeometryGroup).Children.Add(winChild); } } else if (geometry is PathGeometry) { PathGeometry xamPathGeometry = geometry as PathGeometry; winMedia.PathGeometry winPathGeometry = new winMedia.PathGeometry { FillRule = ConvertFillRule(xamPathGeometry.FillRule) }; foreach (PathFigure xamPathFigure in xamPathGeometry.Figures) { winMedia.PathFigure winPathFigure = new winMedia.PathFigure { StartPoint = ConvertPoint(xamPathFigure.StartPoint), IsFilled = xamPathFigure.IsFilled, IsClosed = xamPathFigure.IsClosed }; winPathGeometry.Figures.Add(winPathFigure); foreach (PathSegment xamPathSegment in xamPathFigure.Segments) { // LineSegment if (xamPathSegment is LineSegment) { LineSegment xamSegment = xamPathSegment as LineSegment; winMedia.LineSegment winSegment = new winMedia.LineSegment { Point = ConvertPoint(xamSegment.Point) }; winPathFigure.Segments.Add(winSegment); } // PolylineSegment if (xamPathSegment is PolyLineSegment) { PolyLineSegment xamSegment = xamPathSegment as PolyLineSegment; winMedia.PolyLineSegment winSegment = new winMedia.PolyLineSegment(); foreach (Point point in xamSegment.Points) { winSegment.Points.Add(ConvertPoint(point)); } winPathFigure.Segments.Add(winSegment); } // BezierSegment if (xamPathSegment is BezierSegment) { BezierSegment xamSegment = xamPathSegment as BezierSegment; winMedia.BezierSegment winSegment = new winMedia.BezierSegment { Point1 = ConvertPoint(xamSegment.Point1), Point2 = ConvertPoint(xamSegment.Point2), Point3 = ConvertPoint(xamSegment.Point3) }; winPathFigure.Segments.Add(winSegment); } // PolyBezierSegment else if (xamPathSegment is PolyBezierSegment) { PolyBezierSegment xamSegment = xamPathSegment as PolyBezierSegment; winMedia.PolyBezierSegment winSegment = new winMedia.PolyBezierSegment(); foreach (Point point in xamSegment.Points) { winSegment.Points.Add(ConvertPoint(point)); } winPathFigure.Segments.Add(winSegment); } // QuadraticBezierSegment if (xamPathSegment is QuadraticBezierSegment) { QuadraticBezierSegment xamSegment = xamPathSegment as QuadraticBezierSegment; winMedia.QuadraticBezierSegment winSegment = new winMedia.QuadraticBezierSegment { Point1 = ConvertPoint(xamSegment.Point1), Point2 = ConvertPoint(xamSegment.Point2), }; winPathFigure.Segments.Add(winSegment); } // PolyQuadraticBezierSegment else if (xamPathSegment is PolyQuadraticBezierSegment) { PolyQuadraticBezierSegment xamSegment = xamPathSegment as PolyQuadraticBezierSegment; winMedia.PolyQuadraticBezierSegment winSegment = new winMedia.PolyQuadraticBezierSegment(); foreach (Point point in xamSegment.Points) { winSegment.Points.Add(ConvertPoint(point)); } winPathFigure.Segments.Add(winSegment); } // ArcSegment else if (xamPathSegment is ArcSegment) { ArcSegment xamSegment = xamPathSegment as ArcSegment; winMedia.ArcSegment winSegment = new winMedia.ArcSegment(); winSegment.Size = new winFound.Size(xamSegment.Size.Width, xamSegment.Size.Height); winSegment.RotationAngle = xamSegment.RotationAngle; winSegment.IsLargeArc = xamSegment.IsLargeArc; winSegment.SweepDirection = xamSegment.SweepDirection == SweepDirection.Clockwise ? winMedia.SweepDirection.Clockwise : winMedia.SweepDirection.Counterclockwise; winSegment.Point = ConvertPoint(xamSegment.Point); winPathFigure.Segments.Add(winSegment); } } } winGeometry = winPathGeometry; } // Set transform. if (geometry.Transform != null) { winGeometry.Transform = (winMedia.Transform)geometry.Transform.GetNativeObject(); } return(winGeometry); }
public static WMedia.Geometry ToWindows(this Geometry geometry) { WMedia.Geometry wGeometry = null; if (geometry is LineGeometry) { LineGeometry lineGeometry = geometry as LineGeometry; wGeometry = new WMedia.LineGeometry { StartPoint = lineGeometry.StartPoint.ToWindows(), EndPoint = lineGeometry.EndPoint.ToWindows() }; } else if (geometry is RectangleGeometry) { var rect = (geometry as RectangleGeometry).Rect; wGeometry = new WMedia.RectangleGeometry { Rect = new WFoundation.Rect(rect.X, rect.Y, rect.Width, rect.Height) }; } else if (geometry is EllipseGeometry) { EllipseGeometry ellipseGeometry = geometry as EllipseGeometry; wGeometry = new WMedia.EllipseGeometry { Center = ellipseGeometry.Center.ToWindows(), RadiusX = ellipseGeometry.RadiusX, RadiusY = ellipseGeometry.RadiusY }; } else if (geometry is GeometryGroup) { GeometryGroup geometryGroup = geometry as GeometryGroup; wGeometry = new WMedia.GeometryGroup { FillRule = ConvertFillRule(geometryGroup.FillRule) }; foreach (Geometry children in geometryGroup.Children) { WMedia.Geometry winChild = children.ToWindows(); (wGeometry as WMedia.GeometryGroup).Children.Add(winChild); } } else if (geometry is PathGeometry) { PathGeometry pathGeometry = geometry as PathGeometry; WMedia.PathGeometry wPathGeometry = new WMedia.PathGeometry { FillRule = ConvertFillRule(pathGeometry.FillRule) }; foreach (PathFigure xamPathFigure in pathGeometry.Figures) { WMedia.PathFigure wPathFigure = new WMedia.PathFigure { StartPoint = xamPathFigure.StartPoint.ToWindows(), IsFilled = xamPathFigure.IsFilled, IsClosed = xamPathFigure.IsClosed }; wPathGeometry.Figures.Add(wPathFigure); foreach (PathSegment pathSegment in xamPathFigure.Segments) { // LineSegment if (pathSegment is LineSegment) { LineSegment lineSegment = pathSegment as LineSegment; WMedia.LineSegment winSegment = new WMedia.LineSegment { Point = lineSegment.Point.ToWindows() }; wPathFigure.Segments.Add(winSegment); } // PolylineSegment if (pathSegment is PolyLineSegment) { PolyLineSegment polyLineSegment = pathSegment as PolyLineSegment; WMedia.PolyLineSegment wSegment = new WMedia.PolyLineSegment(); foreach (var point in polyLineSegment.Points) { wSegment.Points.Add(point.ToWindows()); } wPathFigure.Segments.Add(wSegment); } // BezierSegment if (pathSegment is BezierSegment) { BezierSegment bezierSegment = pathSegment as BezierSegment; WMedia.BezierSegment wSegment = new WMedia.BezierSegment { Point1 = bezierSegment.Point1.ToWindows(), Point2 = bezierSegment.Point2.ToWindows(), Point3 = bezierSegment.Point3.ToWindows() }; wPathFigure.Segments.Add(wSegment); } // PolyBezierSegment else if (pathSegment is PolyBezierSegment) { PolyBezierSegment polyBezierSegment = pathSegment as PolyBezierSegment; WMedia.PolyBezierSegment wSegment = new WMedia.PolyBezierSegment(); foreach (var point in polyBezierSegment.Points) { wSegment.Points.Add(point.ToWindows()); } wPathFigure.Segments.Add(wSegment); } // QuadraticBezierSegment if (pathSegment is QuadraticBezierSegment) { QuadraticBezierSegment quadraticBezierSegment = pathSegment as QuadraticBezierSegment; WMedia.QuadraticBezierSegment wSegment = new WMedia.QuadraticBezierSegment { Point1 = quadraticBezierSegment.Point1.ToWindows(), Point2 = quadraticBezierSegment.Point2.ToWindows() }; wPathFigure.Segments.Add(wSegment); } // PolyQuadraticBezierSegment else if (pathSegment is PolyQuadraticBezierSegment) { PolyQuadraticBezierSegment polyQuadraticBezierSegment = pathSegment as PolyQuadraticBezierSegment; WMedia.PolyQuadraticBezierSegment wSegment = new WMedia.PolyQuadraticBezierSegment(); foreach (var point in polyQuadraticBezierSegment.Points) { wSegment.Points.Add(point.ToWindows()); } wPathFigure.Segments.Add(wSegment); } // ArcSegment else if (pathSegment is ArcSegment) { ArcSegment arcSegment = pathSegment as ArcSegment; WMedia.ArcSegment wSegment = new WMedia.ArcSegment { Size = new WFoundation.Size(arcSegment.Size.Width, arcSegment.Size.Height), RotationAngle = arcSegment.RotationAngle, IsLargeArc = arcSegment.IsLargeArc, SweepDirection = arcSegment.SweepDirection == SweepDirection.Clockwise ? WMedia.SweepDirection.Clockwise : WMedia.SweepDirection.Counterclockwise, Point = arcSegment.Point.ToWindows() }; wPathFigure.Segments.Add(wSegment); } } } wGeometry = wPathGeometry; } return(wGeometry); }
private void CalculateFit() { double normalizedGain1 = Gain1 * 100 / 15; double normalizedGain2 = Gain2 * 100 / 15; double normalizedGain3 = Gain3 * 100 / 15; double normalizedGain4 = Gain4 * 100 / 15; double normalizedFrequency1 = Frequency1 * 600 / 100; double normalizedFrequency2 = Frequency2 * 600 / 100; double normalizedFrequency3 = Frequency3 * 600 / 100; double normalizedFrequency4 = Frequency4 * 600 / 100; double div1 = (1 / (Quality1 * Quality1)) * 1000; double div2 = (1 / (Quality2 * Quality2)) * 1000; double div3 = (1 / (Quality3 * Quality3)) * 1000; double div4 = (1 / (Quality4 * Quality4)) * 1000; pf.Segments.Clear(); PolyBezierSegment seg = new PolyBezierSegment(); for (int x = 0; x < 600; x = x + 2) { double dx1 = x - normalizedFrequency1; double dx2 = x - normalizedFrequency2; double dx3 = x - normalizedFrequency3; double dx4 = x - normalizedFrequency4; double y1 = normalizedGain1 * Math.Exp(-(dx1 * dx1) / div1); double y2 = normalizedGain2 * Math.Exp(-(dx2 * dx2) / div2); double y3 = normalizedGain3 * Math.Exp(-(dx3 * dx3) / div3); double y4 = normalizedGain4 * Math.Exp(-(dx4 * dx4) / div4); double dy = y1 + y2 + y3 + y4; if (Double.IsNaN(dy)) dy = 0; seg.Points.Add(new Point(x, 100 - dy)); } pf.Segments.Add(seg); }