private void DrawEllipse(StreamGeometryContext context) { // Do not use because it is in DrawLine(...) // context.BeginFigure(...); double controlPointRatio = (Math.Sqrt(2) - 1) * 4 / 3; var x0 = X2 - Radius; var x1 = X2 - Radius * controlPointRatio; var x2 = X2; var x3 = X2 + Radius * controlPointRatio; var x4 = X2 + Radius; var y0 = Y2 - Radius; var y1 = Y2 - Radius * controlPointRatio; var y2 = Y2; var y3 = Y2 + Radius * controlPointRatio; var y4 = Y2 + Radius; context.BeginFigure(new Point(x2, y0), false, true); context.BezierTo(new Point(x3, y0), new Point(x4, y1), new Point(x4, y2), true, true); context.BezierTo(new Point(x4, y3), new Point(x3, y4), new Point(x2, y4), true, true); context.BezierTo(new Point(x1, y4), new Point(x0, y3), new Point(x0, y2), true, true); context.BezierTo(new Point(x0, y1), new Point(x1, y0), new Point(x2, y0), true, true); }
/// <summary> /// Draw an arc /// </summary> /// <param name="context"></param> private void InternalDrawNodeGeometry(StreamGeometryContext context) { if (R == 0 || Spokes == null || Spokes.Count < 2) { return; } context.BeginFigure(Center, false, false); context.LineTo(spokep(0), true, false); for (int i = 1; i < Spokes.Count; i++) { if (Math.Abs(Spokes[i] - Spokes[i - 1]) > 2 * Math.PI - .001) { // Display a circle double ControlPointRatio = (Math.Sqrt(2) - 1) * 4 / 3; var x0 = Center.X - R; var x1 = Center.X - R * ControlPointRatio; var x2 = Center.X; var x3 = Center.X + R * ControlPointRatio; var x4 = Center.X + R; var y0 = Center.Y - R; var y1 = Center.Y - R * ControlPointRatio; var y2 = Center.Y; var y3 = Center.Y + R * ControlPointRatio; var y4 = Center.Y + R; context.BeginFigure(new Point(x2, y0), true, true); context.BezierTo(new Point(x3, y0), new Point(x4, y1), new Point(x4, y2), true, true); context.BezierTo(new Point(x4, y3), new Point(x3, y4), new Point(x2, y4), true, true); context.BezierTo(new Point(x1, y4), new Point(x0, y3), new Point(x0, y2), true, true); context.BezierTo(new Point(x0, y1), new Point(x1, y0), new Point(x2, y0), true, true); } else { bool largearc = Math.Abs(Spokes[i] - Spokes[i - 1]) > Math.PI; SweepDirection sd = SweepDirection.Counterclockwise; if (Spokes[i] < Spokes[i - 1]) { sd = SweepDirection.Clockwise; } context.ArcTo(spokep(i), new Size(R, R), 0, largearc, sd, true, false); } context.BeginFigure(Center, false, false); context.LineTo(spokep(i), true, false); } }
private void button1_Click(object sender, System.Windows.RoutedEventArgs e) { Path path = new Path(); Canvas.SetLeft(path, 00); //设置和canvas1的相对位置 Canvas.SetTop(path, 00); path.Stroke = Brushes.Red; path.StrokeThickness = 2; StreamGeometry sg = new StreamGeometry(); sg.FillRule = FillRule.EvenOdd; //取消交叉部分 //打开1个属于sg的StreamGeometryContext(sg.Open()) using (StreamGeometryContext sgc = sg.Open()){ //设置图形的起点,true可利用图形剪裁,true构成封闭图形 sgc.BeginFigure(new Point(0, 200), true, true); //绘制直线,终点,true描边,true平滑连接 sgc.LineTo(new Point(100, 150), true, true); Point p = new Point(200, 150); //椭圆弧终点 Size s = new Size(100, 50); //圆弧宽、高 //绘制圆弧参数:终点,大小,沿X轴旋转角度,大于180度弧线,旋转方向,描边,平滑连接 sgc.ArcTo(p, s, 0, true, SweepDirection.Clockwise, true, true); Point p1 = new Point(200, 150); //第1个控制点 Point p2 = new Point(300, 10); //第2个控制点 Point p3 = new Point(400, 230); //终点 sgc.BezierTo(p1, p2, p3, true, true); //绘制贝塞尔曲线 } path.Data = sg; this.canvas1.Children.Add(path); //canvas1是path的容器 this.image.Clip = sg; //剪裁图片 }
/// <summary> /// 使用原始序列和指定的连接方式绘制几何图形 /// </summary> /// <param name="points">样本序列</param> /// <param name="isFilled">是否填充</param> /// <param name="isClosed">是否闭合</param> /// <returns>连接得到的几何图形</returns> private Geometry DrawGeometry(List <Point> points, bool isFilled, bool isClosed, LineType type) { if (points != null) { StreamGeometry geometry = new StreamGeometry(); using (StreamGeometryContext ctx = geometry.Open()) //打开一个画板 { ctx.BeginFigure(points[0], isFilled /* is filled */, isClosed /* is closed */); //设定画图的起始点 //画出一系列线段并且以折线的形式顺次连接 if (type == LineType.Line) { for (int i = 1; i < points.Count && points[i].X <= 480; i++) { ctx.LineTo(points[i], true, false); } } //使用bezier曲线进行连接 else if (type == LineType.Bezier) { Bezier bezier = new Bezier(points, 0.7); for (int i = 1; i < points.Count; i++) { ctx.BezierTo(bezier.controlPointCollection[2 * i - 2], bezier.controlPointCollection[2 * i - 1], points[i], true, false); } } } return(geometry);//返回得到的点列 } else { throw new Exception("请先输入参数"); } }
private void OnMeasurePropertyChanged(DependencyPropertyChangedEventArgs args) { if (_StreamGeometry == null) { _StreamGeometry = new StreamGeometry(); } if (Points == null) { return; } using (StreamGeometryContext sgc = _StreamGeometry.Open()) { // Get Bezier Spline Control Points. PointCollection pnts = cardinalSpline(Points, .5, Closed); sgc.BeginFigure(pnts[0], true, false); for (int i = 1; i < pnts.Count; i += 3) { sgc.BezierTo(pnts[i], pnts[i + 1], pnts[i + 2], true, false); } } InvalidateMeasure(); OnRenderPropertyChanged(args); }
internal static void DrawArc(this StreamGeometryContext ctx, Point center, double radiusX, double radiusY, ArcPos pos) { var x0 = center.X - radiusX; var x1 = center.X - radiusX * ControlPointRatio; var x2 = center.X; var x3 = center.X + radiusX * ControlPointRatio; var x4 = center.X + radiusX; var y0 = center.Y - radiusY; var y1 = center.Y - radiusY * ControlPointRatio; var y2 = center.Y; var y3 = center.Y + radiusY * ControlPointRatio; var y4 = center.Y + radiusY; switch (pos) { case ArcPos.RightTop: ctx.BeginFigure(new Point(x2, y0), false, false); ctx.BezierTo(new Point(x3, y0), new Point(x4, y1), new Point(x4, y2), true, true); break; case ArcPos.LeftTop: ctx.BeginFigure(new Point(x2, y0), false, false); ctx.BezierTo(new Point(x1, y0), new Point(x0, y1), new Point(x0, y2), true, true); break; case ArcPos.RightBottom: ctx.BeginFigure(new Point(x2, y4), false, false); ctx.BezierTo(new Point(x3, y4), new Point(x4, y3), new Point(x4, y2), true, true); break; case ArcPos.LeftBottom: ctx.BeginFigure(new Point(x2, y4), false, false); ctx.BezierTo(new Point(x1, y4), new Point(x0, y3), new Point(x0, y2), true, true); break; default: throw new ArgumentOutOfRangeException(nameof(pos), pos, null); } }
internal static void DrawEllipse(this StreamGeometryContext ctx, Point center, double radiusX, double radiusY, bool isFilled, bool isStroked) { var x0 = center.X - radiusX; var x1 = center.X - radiusX * ControlPointRatio; var x2 = center.X; var x3 = center.X + radiusX * ControlPointRatio; var x4 = center.X + radiusX; var y0 = center.Y - radiusY; var y1 = center.Y - radiusY * ControlPointRatio; var y2 = center.Y; var y3 = center.Y + radiusY * ControlPointRatio; var y4 = center.Y + radiusY; ctx.BeginFigure(new Point(x2, y0), isFilled, true); ctx.BezierTo(new Point(x3, y0), new Point(x4, y1), new Point(x4, y2), isStroked, true); ctx.BezierTo(new Point(x4, y3), new Point(x3, y4), new Point(x2, y4), isStroked, true); ctx.BezierTo(new Point(x1, y4), new Point(x0, y3), new Point(x0, y2), isStroked, true); ctx.BezierTo(new Point(x0, y1), new Point(x1, y0), new Point(x2, y0), isStroked, true); }
public override void Draw(DrawingContext ctx, Matrix matrix) { if (!this.IsValid) { return; } PathFigureEditor pathFigureEditor = new PathFigureEditor(this.PathGeometry.Figures[this.FigureIndex]); StreamGeometry streamGeometry = new StreamGeometry(); StreamGeometryContext streamGeometryContext = streamGeometry.Open(); switch (this.PathPointKind) { case PathPointKind.Start: throw new InvalidOperationException(ExceptionStringTable.PathSegmentAdornerIsolatedPointSegment); case PathPointKind.Arc: ArcSegment arcSegment = (ArcSegment)pathFigureEditor.PathFigure.Segments[this.SegmentIndex]; streamGeometryContext.BeginFigure(this.GetPoint(pathFigureEditor, -1), false, false); streamGeometryContext.ArcTo(arcSegment.Point, arcSegment.Size, arcSegment.RotationAngle, arcSegment.IsLargeArc, arcSegment.SweepDirection, true, false); break; case PathPointKind.Line: streamGeometryContext.BeginFigure(this.GetPoint(pathFigureEditor, -1), false, false); streamGeometryContext.LineTo(this.GetPoint(pathFigureEditor, 0), true, false); break; case PathPointKind.Quadratic: streamGeometryContext.BeginFigure(this.GetPoint(pathFigureEditor, -2), false, false); streamGeometryContext.QuadraticBezierTo(this.GetPoint(pathFigureEditor, -1), this.GetPoint(pathFigureEditor, 0), true, false); break; case PathPointKind.Cubic: streamGeometryContext.BeginFigure(this.GetPoint(pathFigureEditor, -3), false, false); streamGeometryContext.BezierTo(this.GetPoint(pathFigureEditor, -2), this.GetPoint(pathFigureEditor, -1), this.GetPoint(pathFigureEditor, 0), true, false); break; case PathPointKind.BezierHandle: throw new InvalidOperationException(ExceptionStringTable.PathSegmentAdornerLastPointIsBezier); default: throw new NotImplementedException(ExceptionStringTable.PathSegmentAdornerUnknownPathPoint); } streamGeometryContext.Close(); MatrixTransform matrixTransform = new MatrixTransform(matrix); matrixTransform.Freeze(); streamGeometry.Transform = (Transform)matrixTransform; streamGeometry.Freeze(); Pen pen = this.IsActive ? this.ThickPathSegmentPen : this.ThinPathSegmentPen; ctx.DrawGeometry((Brush)null, pen, (System.Windows.Media.Geometry)streamGeometry); ctx.DrawGeometry((Brush)null, PathSegmentAdorner.HitTestPen, (System.Windows.Media.Geometry)streamGeometry); }
/// <summary> /// Draws the And-Control /// </summary> /// <param name="context"></param> protected void InternalDrawAndGeometry(StreamGeometryContext context) { Point pt1 = new Point(Width, 0); Point pt2 = new Point(Width / 2, 0); Point pt3 = new Point(-Width / 2, Height / 2); Point pt4 = new Point(Width / 2, Height); Point pt5 = new Point(Width, Height); context.BeginFigure(pt1, true, true); context.LineTo(pt2, true, false); context.BezierTo(pt2, pt3, pt4, true, true); context.LineTo(pt5, true, false); }
public void DrawCurve(double x0, double y0, double x1, double y1, double x2, double y2, double x3, double y3, Brush strokeBrush, double lineWidth) { StreamGeometry geometry = new StreamGeometry(); using (StreamGeometryContext ctx = geometry.Open()) { ctx.BeginFigure(new Point(x0, y0), false, false); ctx.BezierTo(new Point(x1, y1), new Point(x2, y2), new Point(x3, y3), true, false); } dc.DrawGeometry(Brushes.Transparent, new Pen(strokeBrush, lineWidth), geometry); }
public void DrawAndFillBeziers(Pen pen, Brush brush, TPointF[] points, TClippingStyle clippingStyle) { if (points == null || points.Length <= 1) { return; } if (!CheckPoints(points)) { return; } StreamGeometry Line = new StreamGeometry(); using (StreamGeometryContext sc = Line.Open()) { sc.BeginFigure(new Point(points[0].X, points[0].Y), brush != null, false); for (int i = 1; i < points.Length - 2; i += 3) { sc.BezierTo(new Point(points[i].X, points[i].Y), new Point(points[i + 1].X, points[i + 1].Y), new Point(points[i + 2].X, points[i + 2].Y), true, true); } } if (Line.CanFreeze) { Line.Freeze(); } if (brush != null) { switch (clippingStyle) { case TClippingStyle.Exclude: IntersectClip(Reverse(Line)); break; case TClippingStyle.Include: IntersectClip(Line); break; default: //Will be handled below. break; } } if (clippingStyle == TClippingStyle.None) { FCanvas.DrawGeometry(brush, pen, Line); } }
public void DrawPath(IEnumerable <PathOp> ops, Pen pen, Brush brush) { StreamGeometry streamGeometry = new StreamGeometry(); using (StreamGeometryContext geometryContext = streamGeometry.Open()) { foreach (var op in ops) { var mt = op as MoveTo; if (mt != null) { geometryContext.BeginFigure(mt.Point.GetPoint(), true, false); continue; } var lt = op as LineTo; if (lt != null) { geometryContext.LineTo(lt.Point.GetPoint(), true, false); continue; } var at = op as ArcTo; if (at != null) { /* * Point c1, c2; * at.GetCircles(pp, out c1, out c2); * var circleCenter = at.LargeArc ^ !at.SweepClockwise ? c2 : c1; * var rotationAngle = (float)Math.Atan2(at.Point.Y - circleCenter.Y, at.Point.X - circleCenter.X); * geometryContext.ArcTo(at.Point.GetPoint(), at.Radius.GetSize(), rotationAngle, * at.LargeArc, at.SweepClockwise ? SweepDirection.Clockwise : SweepDirection.Counterclockwise, * true, false * );*/ throw new NotImplementedException(); } var ct = op as CurveTo; if (ct != null) { geometryContext.BezierTo(ct.Control1.GetPoint(), ct.Control2.GetPoint(), ct.Point.GetPoint(), true, false); } throw new NotSupportedException(); } } dc.DrawGeometry(brush?.GetBrush(), pen?.GetPen(), streamGeometry); }
public static void DrawFigure(this StreamGeometryContext ctx, PathFigure figure) { ctx.BeginFigure(figure.StartPoint, figure.IsFilled, figure.IsClosed); foreach (var segment in figure.Segments) { if (segment is LineSegment lineSegment) { ctx.LineTo(lineSegment.Point, lineSegment.IsStroked, lineSegment.IsSmoothJoin); continue; } if (segment is BezierSegment bezierSegment) { ctx.BezierTo(bezierSegment.Point1, bezierSegment.Point2, bezierSegment.Point3, bezierSegment.IsStroked, bezierSegment.IsSmoothJoin); continue; } if (segment is QuadraticBezierSegment quadraticSegment) { ctx.QuadraticBezierTo(quadraticSegment.Point1, quadraticSegment.Point2, quadraticSegment.IsStroked, quadraticSegment.IsSmoothJoin); continue; } if (segment is PolyLineSegment polyLineSegment) { ctx.PolyLineTo(polyLineSegment.Points, polyLineSegment.IsStroked, polyLineSegment.IsSmoothJoin); continue; } if (segment is PolyBezierSegment polyBezierSegment) { ctx.PolyBezierTo(polyBezierSegment.Points, polyBezierSegment.IsStroked, polyBezierSegment.IsSmoothJoin); continue; } if (segment is PolyQuadraticBezierSegment polyQuadraticSegment) { ctx.PolyQuadraticBezierTo(polyQuadraticSegment.Points, polyQuadraticSegment.IsStroked, polyQuadraticSegment.IsSmoothJoin); continue; } if (segment is ArcSegment arcSegment) { ctx.ArcTo(arcSegment.Point, arcSegment.Size, arcSegment.RotationAngle, arcSegment.IsLargeArc, arcSegment.SweepDirection, arcSegment.IsStroked, arcSegment.IsSmoothJoin); continue; } } }
public static void DrawFigure(StreamGeometryContext ctx, PathFigure figure) { ctx.BeginFigure(figure.StartPoint, figure.IsFilled, figure.IsClosed); foreach (var segment in figure.Segments) { var lineSegment = segment as WpfLineSegment; if (lineSegment != null) { ctx.LineTo(lineSegment.Point, lineSegment.IsStroked, lineSegment.IsSmoothJoin); continue; } var bezierSegment = segment as BezierSegment; if (bezierSegment != null) { ctx.BezierTo(bezierSegment.Point1, bezierSegment.Point2, bezierSegment.Point3, bezierSegment.IsStroked, bezierSegment.IsSmoothJoin); continue; } var quadraticSegment = segment as QuadraticBezierSegment; if (quadraticSegment != null) { ctx.QuadraticBezierTo(quadraticSegment.Point1, quadraticSegment.Point2, quadraticSegment.IsStroked, quadraticSegment.IsSmoothJoin); continue; } var polyLineSegment = segment as PolyLineSegment; if (polyLineSegment != null) { ctx.PolyLineTo(polyLineSegment.Points, polyLineSegment.IsStroked, polyLineSegment.IsSmoothJoin); continue; } var polyBezierSegment = segment as PolyBezierSegment; if (polyBezierSegment != null) { ctx.PolyBezierTo(polyBezierSegment.Points, polyBezierSegment.IsStroked, polyBezierSegment.IsSmoothJoin); continue; } var polyQuadraticSegment = segment as PolyQuadraticBezierSegment; if (polyQuadraticSegment != null) { ctx.PolyQuadraticBezierTo(polyQuadraticSegment.Points, polyQuadraticSegment.IsStroked, polyQuadraticSegment.IsSmoothJoin); continue; } var arcSegment = segment as ArcSegment; if (arcSegment != null) { ctx.ArcTo(arcSegment.Point, arcSegment.Size, arcSegment.RotationAngle, arcSegment.IsLargeArc, arcSegment.SweepDirection, arcSegment.IsStroked, arcSegment.IsSmoothJoin); continue; } } }
/// <summary> /// Generates a WPF geometry for a single graphic path geometry /// </summary> public static Geometry GenerateGeometry(GraphicPathGeometry graphicPathGeometry) { if (graphicPathGeometry == null) { return(null); } StreamGeometry geometry = new StreamGeometry(); geometry.FillRule = ConvertFillRule(graphicPathGeometry.FillRule); StreamGeometryContext ctx = geometry.Open(); foreach (var segment in graphicPathGeometry.Segments) { switch (segment) { case GraphicMoveSegment graphicMove: { ctx.BeginFigure(graphicMove.StartPoint, true, graphicMove.IsClosed); break; } case GraphicLineSegment graphicLineTo: { ctx.LineTo(graphicLineTo.To, true, true); break; } case GraphicCubicBezierSegment graphicCubicBezier: { ctx.BezierTo(graphicCubicBezier.ControlPoint1, graphicCubicBezier.ControlPoint2, graphicCubicBezier.EndPoint, true, true); break; } case GraphicQuadraticBezierSegment graphicQuadraticBezier: { ctx.QuadraticBezierTo(graphicQuadraticBezier.ControlPoint, graphicQuadraticBezier.EndPoint, true, true); break; } } } ctx.Close(); geometry.Freeze(); return(geometry); }
internal void FillContextForICurve(StreamGeometryContext context, ICurve iCurve) { if (iCurve == null) { return; } context.BeginFigure(CommonX.WpfPoint(iCurve.Start), false, false); var c = iCurve as Curve; if (c != null) { FillContexForCurve(context, c); } else { var cubicBezierSeg = iCurve as CubicBezierSegment; if (cubicBezierSeg != null) { context.BezierTo(CommonX.WpfPoint(cubicBezierSeg.B(1)), CommonX.WpfPoint(cubicBezierSeg.B(2)), CommonX.WpfPoint(cubicBezierSeg.B(3)), true, false); } else { var ls = iCurve as LineSegment; if (ls != null) { context.LineTo(CommonX.WpfPoint(ls.End), true, false); } else { var rr = iCurve as RoundedRect; if (rr != null) { FillContexForCurve(context, rr.Curve); } else { var poly = iCurve as Polyline; FillContexForPolyline(context, poly); } } } } }
private void InternalDrawArrowGeometry(StreamGeometryContext context) { _lstCtrPoints.Clear(); List <Point> lstPoints = new List <Point>(); List <Point> lstCtrPoints = new List <Point>(); foreach (Point pt in BezierPoints) { lstPoints.Add(new Point(pt.X, pt.Y)); } if (_bDrawing == true && (BezierTmpPoint.X > 0 || BezierTmpPoint.Y > 0)) { lstPoints.Add(new Point(BezierTmpPoint.X, BezierTmpPoint.Y)); for (int i = 0; i < lstPoints.Count; i++) { lstCtrPoints.AddRange(Control1(lstPoints, i)); if (i < lstPoints.Count - 1) { _lstCtrPoints.AddRange(Control1(lstPoints, i)); } } } else if (_bDrawing == false) { for (int i = 0; i < lstPoints.Count; i++) { lstCtrPoints.AddRange(Control1(lstPoints, i)); _lstCtrPoints.AddRange(Control1(lstPoints, i)); } } if (lstPoints != null && lstPoints.Count > 1) { Point ptStart = lstPoints[0]; Point ptEnd = lstPoints[lstPoints.Count - 1]; //先绘制开始线帽 SetLineCap(lstPoints[lstPoints.Count - 2], ptEnd, ptStart, lstPoints[1]); context.BeginFigure(ptStart, true, false); //再绘制曲线 for (int i = 1; i < lstPoints.Count; i++) { context.BezierTo(lstCtrPoints[i * 2 - 1], lstCtrPoints[i * 2], lstPoints[i], true, true); } } }
public void DrawBezier(Point[] pts, DrawingContext dc, Brush brush) { foreach (Point pt in pts)//绘制点 { dc.DrawEllipse(brush, null, pt, 5, 5); } StreamGeometry sg = new StreamGeometry(); StreamGeometryContext sgc = sg.Open(); sgc.BeginFigure(pts[0], true, false); sgc.BezierTo(pts[1], pts[2], pts[3], true, true); sgc.BeginFigure(pts[0], true, false); sgc.PolyLineTo(pts, true, true); sgc.Close(); Pen pen = new Pen(brush, 1); dc.DrawGeometry(null, pen, sg); }
private static void DeserializeBezierTo(BinaryReader br, byte firstByte, StreamGeometryContext sc) { Point point1; Point point2 = new Point(); Point point3 = new Point(); bool isStroked; bool isSmoothJoin; DeserializePointAndTwoBools(br, firstByte, out point1, out isStroked, out isSmoothJoin); point2.X = XamlSerializationHelper.ReadDouble(br); point2.Y = XamlSerializationHelper.ReadDouble(br); point3.X = XamlSerializationHelper.ReadDouble(br); point3.Y = XamlSerializationHelper.ReadDouble(br); sc.BezierTo(point1, point2, point3, isStroked, isSmoothJoin); }
static void FillContexForCurve(StreamGeometryContext context, Curve c) { foreach (ICurve seg in c.Segments) { var bezSeg = seg as CubicBezierSegment; if (bezSeg != null) { context.BezierTo(CommonX.WpfPoint(bezSeg.B(1)), CommonX.WpfPoint(bezSeg.B(2)), CommonX.WpfPoint(bezSeg.B(3)), true, false); } else { var ls = seg as LineSegment; if (ls != null) { context.LineTo(CommonX.WpfPoint(ls.End), true, false); } else { var ellipse = seg as Ellipse; if (ellipse != null) { // context.LineTo(Common.WpfPoint(ellipse.End),true,false); double sweepAngle = EllipseSweepAngle(ellipse); bool largeArc = Math.Abs(sweepAngle) >= Math.PI; Rectangle box = ellipse.FullBox(); context.ArcTo(CommonX.WpfPoint(ellipse.End), new Size(box.Width / 2, box.Height / 2), sweepAngle, largeArc, sweepAngle < 0 ? SweepDirection.Counterclockwise : SweepDirection.Clockwise, true, true); } else { throw new NotImplementedException(); } } } } }
/// <summary> /// Returns the geometry scaled to the provided size. /// </summary> /// <param name="width">The width.</param> /// <param name="height">The height.</param> public Geometry Scaled(double width, double height) { StreamGeometry Result = new StreamGeometry(); GeometryScale ScaledWidth = (WidthScale != null) ? new GeometryScale(WidthScale, width) : null; GeometryScale ScaledHeight = (HeightScale != null) ? new GeometryScale(HeightScale, height) : null; using (StreamGeometryContext sgc = Result.Open()) { sgc.BeginFigure(ScaledPoint(Figure.StartPoint, ScaledWidth, ScaledHeight), Figure.IsFilled, Figure.IsClosed); foreach (PathSegment Segment in Figure.Segments) { LineSegment AsLineSegment; PolyLineSegment AsPolyLineSegment; BezierSegment AsBezierSegment; PolyBezierSegment AsPolyBezierSegment; if ((AsLineSegment = Segment as LineSegment) != null) { sgc.LineTo(ScaledPoint(AsLineSegment.Point, ScaledWidth, ScaledHeight), AsLineSegment.IsStroked, AsLineSegment.IsSmoothJoin); } else if ((AsPolyLineSegment = Segment as PolyLineSegment) != null) { sgc.PolyLineTo(ScaledPointList(AsPolyLineSegment.Points, ScaledWidth, ScaledHeight), AsPolyLineSegment.IsStroked, AsPolyLineSegment.IsSmoothJoin); } else if ((AsBezierSegment = Segment as BezierSegment) != null) { sgc.BezierTo(ScaledPoint(AsBezierSegment.Point1, ScaledWidth, ScaledHeight), ScaledPoint(AsBezierSegment.Point2, ScaledWidth, ScaledHeight), ScaledPoint(AsBezierSegment.Point3, ScaledWidth, ScaledHeight), AsBezierSegment.IsStroked, AsBezierSegment.IsSmoothJoin); } else if ((AsPolyBezierSegment = Segment as PolyBezierSegment) != null) { sgc.PolyBezierTo(ScaledPointList(AsPolyBezierSegment.Points, ScaledWidth, ScaledHeight), AsPolyBezierSegment.IsStroked, AsPolyBezierSegment.IsSmoothJoin); } } } return(Result); }
private void clearAndDrawCurves(IEnumerable <CubicBezier> curves, bool colorize) { using (DrawingContext dctx = _drawing.Open()) { clearDrawing(dctx); int i = 0; foreach (CubicBezier curve in curves) { // each curve segment is a seperate StreamGeometry since they use different pens Pen pen = colorize ? _partPens[i++ % _partPens.Length] : _partPens[0]; StreamGeometry geo = new StreamGeometry(); using (StreamGeometryContext gctx = geo.Open()) { gctx.BeginFigure(toWpfPoint(curve.p0), false, false); gctx.BezierTo(toWpfPoint(curve.p1), toWpfPoint(curve.p2), toWpfPoint(curve.p3), true, false); } geo.Freeze(); dctx.DrawGeometry(null, pen, geo); } } }
private void button1_Click(object sender, System.Windows.RoutedEventArgs e) { StreamGeometry sg = new StreamGeometry(); sg.FillRule = FillRule.EvenOdd; using (StreamGeometryContext sgc = sg.Open()){ sgc.BeginFigure(new Point(0, 200), true, true); sgc.LineTo(new Point(100, 150), true, true); Point p = new Point(200, 150); //椭圆弧终点 Size s = new Size(100, 50); //圆弧宽、高 sgc.ArcTo(p, s, 0, true, SweepDirection.Clockwise, true, true); Point p1 = new Point(200, 150); //第1个控制点 Point p2 = new Point(300, 10); //第2个控制点 Point p3 = new Point(400, 230); //终点 sgc.BezierTo(p1, p2, p3, true, true); //绘制贝塞尔曲线 } GeometryDrawing gd = new GeometryDrawing(); //定义绘制几何形状对象 gd.Geometry = sg; //绘制的几何图形 LinearGradientBrush lgb = new LinearGradientBrush(); //定义线性渐变 //默认的线性渐变是沿对角线方向进行的, //StartPoint是被填充区域的左上角 (0,0),EndPoint是被填充区域的右下角 (1,1), //EndPoint(1,0)水平方向,EndPoint(0,1)垂直方向。 lgb.StartPoint = new Point(0, 0); lgb.EndPoint = new Point(1, 0); lgb.GradientStops.Add(new GradientStop(Colors.Red, 0.1)); //插入渐变停止点 lgb.GradientStops.Add(new GradientStop(Colors.LightSkyBlue, 0.25)); lgb.GradientStops.Add(new GradientStop(Colors.Blue, 0.5)); lgb.GradientStops.Add(new GradientStop(Colors.LightGreen, 0.75)); gd.Brush = lgb; //绘制图形填充色 gd.Pen = new Pen(Brushes.Red, 2); //描边色 DrawingImage di = new DrawingImage(gd); //绘制图像用于显示 Image myimage = new Image(); //定义图像对象 myimage.Source = di; //赋予图像对象 Canvas.SetLeft(myimage, 60); //位置坐标 Canvas.SetTop(myimage, 30); this.canvas1.Children.Add(myimage); //图像装入容器显示 }
/// <span class="code-SummaryComment"><summary></span> /// Draws an Arrow /// <span class="code-SummaryComment"></summary></span> private void InternalDrawArrowGeometry(StreamGeometryContext context) { vm = DataContext as ConnectionViewModel; if (vm == null) { return; } if (vm.Type == EdgeLineType.Line) { context.BeginFigure(FromPoint, false, false); context.LineTo(ToPoint, true, false); } else if (vm.Type == EdgeLineType.Bezier) { var bezierPoints = vm.GetBezierPoints(); context.BeginFigure(bezierPoints[0], false, false); context.BezierTo(bezierPoints[1], bezierPoints[2], bezierPoints[3], true, true); } }
public StreamGeometryBezierToExample() { // Create a StreamGeometry to use to specify myPath. StreamGeometry geometry = new StreamGeometry(); geometry.FillRule = FillRule.EvenOdd; // Open a StreamGeometryContext that can be used to describe this StreamGeometry // object's contents. using (StreamGeometryContext ctx = geometry.Open()) { // Set the begin point of the shape. ctx.BeginFigure(new Point(10, 100), true /* is filled */, false /* is closed */); // Create a Bezier curve using the 3 specifed points where the first two points // are control points and the last point is the destination point for the curve. ctx.BezierTo(new Point(100, 0), new Point(200, 200), new Point(300, 100), true /* is stroked */, false /* is smooth join */); } // Create a path to draw a geometry with. Path myPath = new Path(); myPath.Stroke = Brushes.Black; myPath.StrokeThickness = 1; // Freeze the geometry (make it unmodifiable) // for additional performance benefits. geometry.Freeze(); // specify the shape (Bezier Curve) of the path using the StreamGeometry. myPath.Data = geometry; // Add path shape to the UI. StackPanel mainPanel = new StackPanel(); mainPanel.Children.Add(myPath); this.Content = mainPanel; }
/// <summary> /// 绘图函数绘出图像如果过于折现化说明样本点密度过低,请改用bezier函数连接以改善图形 /// </summary> /// <param name="values">样本序列</param> /// <param name="isFilled"></param> /// <param name="isClosed">是否闭合</param> /// <returns></returns> private Geometry DrawGeometry(List <Point> values, bool isFilled, bool isClosed, Type type) { try { StreamGeometry geometry = new StreamGeometry(); using (StreamGeometryContext ctx = geometry.Open()) { ctx.BeginFigure(values[0], isFilled /* is filled */, isClosed /* is closed */); //画出一系列线段并且连接成曲线 if (type == Type.Line) { for (int i = 1; i < values.Count && values[i].X <= 480; i++) { ctx.LineTo(values[i], true, false); } } //使用bezier曲线进行连接 else if (type == Type.Bezier) { Bezier bezier = new Bezier(values, 0.7); for (int i = 1; i < values.Count; i++) { ctx.BezierTo(bezier.controlPointCollection[2 * i - 2], bezier.controlPointCollection[2 * i - 1], values[i], true, false); } } } return(geometry); } catch (Exception) { MessageBox.Show("请先输入频率"); return(null); } }
public static void Write(this StreamGeometryContext ctx, PathSegment pathSegment) { switch (pathSegment) { case ArcSegment arc: ctx.ArcTo(arc.Point, arc.Size, arc.RotationAngle, arc.IsLargeArc, arc.SweepDirection, false, false); break; case BezierSegment bezier: ctx.BezierTo(bezier.Point1, bezier.Point2, bezier.Point3, false, false); break; case PolyBezierSegment polyBezier: ctx.PolyBezierTo(polyBezier.Points, false, false); break; case PolyLineSegment polyLine: ctx.PolyLineTo(polyLine.Points, false, false); break; case QuadraticBezierSegment quadraticBezier: ctx.QuadraticBezierTo(quadraticBezier.Point1, quadraticBezier.Point2, false, false); break; case PolyQuadraticBezierSegment polyQuadraticBezier: ctx.PolyQuadraticBezierTo(polyQuadraticBezier.Points, false, false); break; case LineSegment line: ctx.LineTo(line.Point, false, false); break; default: break; } }
/// <summary> /// Parse a PathFigureCollection string /// </summary> internal void ParseToGeometryContext( StreamGeometryContext context, string pathString, int startIndex) { // [BreakingChange] Dev10 Bug #453199 // We really should throw an ArgumentNullException here for context and pathString. // From original code // This is only used in call to Double.Parse _formatProvider = System.Globalization.CultureInfo.InvariantCulture; _context = context; _pathString = pathString; _pathLength = pathString.Length; _curIndex = startIndex; _secondLastPoint = new Point(0, 0); _lastPoint = new Point(0, 0); _lastStart = new Point(0, 0); _figureStarted = false; bool first = true; char last_cmd = ' '; while (ReadToken()) // Empty path is allowed in XAML { char cmd = _token; if (first) { if ((cmd != 'M') && (cmd != 'm')) // Path starts with M|m { ThrowBadToken(); } first = false; } switch (cmd) { case 'm': case 'M': // XAML allows multiple points after M/m _lastPoint = ReadPoint(cmd, !AllowComma); context.BeginFigure(_lastPoint, IsFilled, !IsClosed); _figureStarted = true; _lastStart = _lastPoint; last_cmd = 'M'; while (IsNumber(AllowComma)) { _lastPoint = ReadPoint(cmd, !AllowComma); context.LineTo(_lastPoint, IsStroked, !IsSmoothJoin); last_cmd = 'L'; } break; case 'l': case 'L': case 'h': case 'H': case 'v': case 'V': EnsureFigure(); do { switch (cmd) { case 'l': _lastPoint = ReadPoint(cmd, !AllowComma); break; case 'L': _lastPoint = ReadPoint(cmd, !AllowComma); break; case 'h': _lastPoint.X += ReadNumber(!AllowComma); break; case 'H': _lastPoint.X = ReadNumber(!AllowComma); break; case 'v': _lastPoint.Y += ReadNumber(!AllowComma); break; case 'V': _lastPoint.Y = ReadNumber(!AllowComma); break; } context.LineTo(_lastPoint, IsStroked, !IsSmoothJoin); }while (IsNumber(AllowComma)); last_cmd = 'L'; break; case 'c': case 'C': // cubic Bezier case 's': case 'S': // smooth cublic Bezier EnsureFigure(); do { Point p; if ((cmd == 's') || (cmd == 'S')) { if (last_cmd == 'C') { p = Reflect(); } else { p = _lastPoint; } _secondLastPoint = ReadPoint(cmd, !AllowComma); } else { p = ReadPoint(cmd, !AllowComma); _secondLastPoint = ReadPoint(cmd, AllowComma); } _lastPoint = ReadPoint(cmd, AllowComma); context.BezierTo(p, _secondLastPoint, _lastPoint, IsStroked, !IsSmoothJoin); last_cmd = 'C'; }while (IsNumber(AllowComma)); break; case 'q': case 'Q': // quadratic Bezier case 't': case 'T': // smooth quadratic Bezier EnsureFigure(); do { if ((cmd == 't') || (cmd == 'T')) { if (last_cmd == 'Q') { _secondLastPoint = Reflect(); } else { _secondLastPoint = _lastPoint; } _lastPoint = ReadPoint(cmd, !AllowComma); } else { _secondLastPoint = ReadPoint(cmd, !AllowComma); _lastPoint = ReadPoint(cmd, AllowComma); } context.QuadraticBezierTo(_secondLastPoint, _lastPoint, IsStroked, !IsSmoothJoin); last_cmd = 'Q'; }while (IsNumber(AllowComma)); break; case 'a': case 'A': EnsureFigure(); do { // A 3,4 5, 0, 0, 6,7 double w = ReadNumber(!AllowComma); double h = ReadNumber(AllowComma); double rotation = ReadNumber(AllowComma); bool large = ReadBool(); bool sweep = ReadBool(); _lastPoint = ReadPoint(cmd, AllowComma); context.ArcTo( _lastPoint, new Size(w, h), rotation, large, #if PBTCOMPILER sweep, #else sweep ? SweepDirection.Clockwise : SweepDirection.Counterclockwise, #endif IsStroked, !IsSmoothJoin ); }while (IsNumber(AllowComma)); last_cmd = 'A'; break; case 'z': case 'Z': EnsureFigure(); context.SetClosedState(IsClosed); _figureStarted = false; last_cmd = 'Z'; _lastPoint = _lastStart; // Set reference point to be first point of current figure break; default: ThrowBadToken(); break; } } }
static private void CreateEdgePathFromCurve(StreamGeometryContext context, ICurve iCurve) { context.BeginFigure(iCurve.Start.ToWpf(), false, false); var c = iCurve as Curve; if (c != null) { FillContexForCurve(context, c); return; } var cubicBezierSeg = iCurve as CubicBezierSegment; if (cubicBezierSeg != null) { context.BezierTo(cubicBezierSeg.B(1).ToWpf(), cubicBezierSeg.B(2).ToWpf(), cubicBezierSeg.B(3).ToWpf(), true, false); return; } var ls = iCurve as GeometryLineSegment; if (ls != null) { context.LineTo(ls.End.ToWpf(), true, false); return; } var rr = iCurve as RoundedRect; if (rr != null) { FillContexForCurve(context, rr.Curve); return; } var poly = iCurve as GeometryPolyline; if (poly != null) { FillContexForPolyline(context, poly); return; } var ellipse = iCurve as GeometryEllipse; if (ellipse != null) { // context.LineTo(Common.WpfPoint(ellipse.End),true,false); double sweepAngle = EllipseSweepAngle(ellipse); bool largeArc = Math.Abs(sweepAngle) >= Math.PI; GeometryRectangle box = ellipse.FullBox(); context.ArcTo(ellipse.End.ToWpf(), new Size(box.Width / 2, box.Height / 2), sweepAngle, largeArc, sweepAngle < 0 ? SweepDirection.Counterclockwise : SweepDirection.Clockwise, true, true); return; } else { throw new NotImplementedException($"{iCurve.GetType()}"); } }
void ellipse(double xm, double ym, double r, StreamGeometryContext context) { double ctrl_ell_x1 = Radius * kappa; double ctrl_ell_y1 = Radius; double ctrl_ell_x2 = Radius; double ctrl_ell_y2 = Radius * kappa; double ell_x = Radius * Math.Sin(2 * Math.PI * 0.25); double ell_y = Radius * Math.Cos(2 * Math.PI * 0.25); var ctrlPoint1 = new Point(xm + ctrl_ell_x1, ym - ctrl_ell_y1); var ctrlPoint2 = new Point(xm + ctrl_ell_x2, ym - ctrl_ell_y2); var point = new Point(xm + ell_x, ym - ell_y); context.BezierTo(ctrlPoint1, ctrlPoint2, point, true, true); //-------------------------------------------------------------------------- ctrl_ell_x2 = Radius * kappa; ctrl_ell_y2 = -Radius; ctrl_ell_x1 = Radius; ctrl_ell_y1 = -Radius * kappa; ell_x = Radius * Math.Sin(2 * Math.PI * 0.50); ell_y = Radius * Math.Cos(2 * Math.PI * 0.50); ctrlPoint1 = new Point(xm + ctrl_ell_x1, ym - ctrl_ell_y1); ctrlPoint2 = new Point(xm + ctrl_ell_x2, ym - ctrl_ell_y2); point = new Point(xm + ell_x, ym - ell_y); context.BezierTo(ctrlPoint1, ctrlPoint2, point, true, true); //-------------------------------------------------------------------------- ctrl_ell_x1 = -Radius * kappa; ctrl_ell_y1 = -Radius; ctrl_ell_x2 = -Radius; ctrl_ell_y2 = -Radius * kappa; ell_x = Radius * Math.Sin(2 * Math.PI * 0.75); ell_y = Radius * Math.Cos(2 * Math.PI * 0.75); ctrlPoint1 = new Point(xm + ctrl_ell_x1, ym - ctrl_ell_y1); ctrlPoint2 = new Point(xm + ctrl_ell_x2, ym - ctrl_ell_y2); point = new Point(xm + ell_x, ym - ell_y); context.BezierTo(ctrlPoint1, ctrlPoint2, point, true, true); //-------------------------------------------------------------------------- ctrl_ell_x2 = -Radius * kappa; ctrl_ell_y2 = Radius; ctrl_ell_x1 = -Radius; ctrl_ell_y1 = Radius * kappa; ell_x = Radius * Math.Sin(2 * Math.PI * 1); ell_y = Radius * Math.Cos(2 * Math.PI * 1); ctrlPoint1 = new Point(xm + ctrl_ell_x1, ym - ctrl_ell_y1); ctrlPoint2 = new Point(xm + ctrl_ell_x2, ym - ctrl_ell_y2); point = new Point(xm + ell_x, ym - ell_y); context.BezierTo(ctrlPoint1, ctrlPoint2, point, true, true); //-------------------------------------------------------------------------- }