wpf-绘图-动态绘制基础图形-圆弧与直线的组合

基础示例

参考:How to: Create a LineSegment in a PathGeometry

绘制直线(纯xaml)

<Path Stroke="Black" StrokeThickness="1">
  <Path.Data>
    <PathGeometry>
      <PathFigure StartPoint="10,50">
        <LineSegment Point="200,70" />
      </PathFigure>
    </PathGeometry>
  </Path.Data>
</Path>

绘制直线(纯c#)

PathFigure myPathFigure = new PathFigure();
myPathFigure.StartPoint = new Point(10, 50);

LineSegment myLineSegment = new LineSegment();
myLineSegment.Point = new Point(200, 70);

PathSegmentCollection myPathSegmentCollection = new PathSegmentCollection();
myPathSegmentCollection.Add(myLineSegment);

myPathFigure.Segments = myPathSegmentCollection;

PathFigureCollection myPathFigureCollection = new PathFigureCollection();
myPathFigureCollection.Add(myPathFigure);

PathGeometry myPathGeometry = new PathGeometry();
myPathGeometry.Figures = myPathFigureCollection;

Path myPath = new Path();
myPath.Stroke = Brushes.Black;
myPath.StrokeThickness = 1;
myPath.Data = myPathGeometry;

我的练习

差不多要画成下面的样子(我的项目里要求叠放多层,没啥,循环一下即可)。。。刻度就是画直线,没啥好说的,算好坐标就行了。
在这里插入图片描述
下面代码中的xi与yi参考下图。红色箭头是组合的方向。d是两条弧之间的距离。
在这里插入图片描述
具体坐标计算不写了,就是三角函数的事情。

bool isLargeArc = false;
if (Math.Abs(angle2-angle1) > 180) {
	isLargeArc = true;
}
LineSegment line1 = new LineSegment();
PathFigure pthFigure = new PathFigure();
// 起点
pthFigure.StartPoint = new Point(x0, y0);
LineSegment lineSeg = new LineSegment();
lineSeg.Point = new Point(x1, y1); // 第一条线的终点
PathSegmentCollection myPathSegmentCollection = new PathSegmentCollection();
// 第一条线画完了
myPathSegmentCollection.Add(lineSeg);

// 外层圆弧
// 坐标是弧的终点,Size(width,height),角度单位是°,isLargeArc表示是否超过180,顺/逆时针,isStroked
ArcSegment arcSegment1 = new ArcSegment(new Point(x2,y2), new Size(curRadius1, curRadius1), (angle2-angle1),
isLargeArc, SweepDirection.Clockwise, true);
myPathSegmentCollection.Add(arcSegment1);

// 第二条直线
LineSegment line2 = new LineSegment();
line2.Point = new Point(x3, y3);
myPathSegmentCollection.Add(line2);

// 内层圆弧
// 因为是反方向,所以角度是360-(angle2-angle1),并且是逆时针
ArcSegment arcSegment2 = new ArcSegment(new Point(x0, y0), new Size(curRadius2, curRadius2),(360-angle2+angle1),
isLargeArc, SweepDirection.Counterclockwise, true);
myPathSegmentCollection.Add(arcSegment2);

// 组装上去
pthFigure.Segments = myPathSegmentCollection;
PathFigureCollection pthFigureCollection = new PathFigureCollection();
pthFigureCollection.Add(pthFigure);
PathGeometry pthGeometry = new PathGeometry();
pthGeometry.Figures = pthFigureCollection;
Path path = new Path();
// 线条的颜色
path.Stroke = new SolidColorBrush(Colors.Black);
path.StrokeThickness = 2;
path.Data = pthGeometry;
// 填充色
path.Fill = new SolidColorBrush(Colors.Yellow);
// 前台我放了一个Canvas对象,叫circleBone
this.circleBone.Children.Add(path);

如何指定颜色(c#)

我自定义的Range类有个string类型的Color属性,ranges是List< Range >类型的变量。
那么Color属性可以取值"AntiqueWhite"这种名称,也可以取值 #FFD3CFC7 这种数值。

path.Fill = new SolidColorBrush((Color)ColorConverter.ConvertFromString(ranges[k].Color));
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值