猿问

如何在图形上创建镜像形状

我有积分表


List<Point> pointList = new List<Point>();


pointList.Add(new Point(0,0));

pointList.Add(new Point(30,0));

pointList.Add(new Point(30,-100));

pointList.Add(new Point(0,-100));

然后画线


Pen pen = new Pen(Color.Red,2);


g.Drawline(pen,pointList[0],pointList[1]);


g.Drawline(pen,pointList[3],poin,tList[4]);

为此,我将在链接中获得左侧图像的结果

如果我需要创建镜像以获得链接中正确图像的结果

有什么方法可以反映我从 pointlist 绘制的图形吗?

它有像复制和翻转图形和复合的东西吗?

谢谢


临摹微笑
浏览 100回答 2
2回答

侃侃尔雅

有了GraphicsPath,您可以使用以下方法来镜像路径:GraphicsPath MirrorLeft(GraphicsPath path){&nbsp; &nbsp; var r = path.GetBounds();&nbsp; &nbsp; var p = (GraphicsPath)path.Clone();&nbsp; &nbsp; p.Transform(new Matrix(-1, 0, 0, 1, 2 * r.Left, 0));&nbsp; &nbsp; return p;}GraphicsPath MirrorRight(GraphicsPath path){&nbsp; &nbsp; var r = path.GetBounds();&nbsp; &nbsp; var p = (GraphicsPath)path.Clone();&nbsp; &nbsp; p.Transform(new Matrix(-1, 0, 0, 1, 2 * (r.Left + r.Width), 0));&nbsp; &nbsp; return p;}MirrorLeft,以路径左侧为轴镜像路径,以路径MirrorRight右侧为轴。下图中,红色弧线为原图,绿色为左镜,蓝色为镜右:这里是上面输出的代码:protected override void OnPaint(PaintEventArgs e){&nbsp; &nbsp; e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;&nbsp; &nbsp; using (var path1 = new GraphicsPath())&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; path1.AddArc(new Rectangle(100, 100, 200, 200), -90, 90);&nbsp; &nbsp; &nbsp; &nbsp; using (var pen1 = new Pen(Color.Red, 3))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.Graphics.DrawPath(pen1, path1);&nbsp; &nbsp; &nbsp; &nbsp; using (var path2 = MirrorLeft(path1))&nbsp; &nbsp; &nbsp; &nbsp; using (var pen2 = new Pen(Color.Green, 3))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.Graphics.DrawPath(pen2, path2);&nbsp; &nbsp; &nbsp; &nbsp; using (var path3 = MirrorRight(path1))&nbsp; &nbsp; &nbsp; &nbsp; using (var pen3 = new Pen(Color.Blue, 3))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.Graphics.DrawPath(pen3, path3);&nbsp; &nbsp; }&nbsp; &nbsp; base.OnPaint(e);}

潇潇雨雨

您可以简单地翻转Graphics对象:e.Graphics.DrawLines(Pens.Black, pointList.ToArray());e.Graphics.ScaleTransform(-1, 1);// you need to know at which x value the flipping axis should be!e.Graphics.TranslateTransform(..., 0);e.Graphics.DrawLines(Pens.Red, pointList.ToArray());请注意,您需要知道要翻转的位置(镜像轴)。对于您显示的效果,您需要向右移动图形左边缘(最小值)的两倍..:int xmin = pointList.Min(x => x.X);int xmax = pointList.Max(x => x.X);e.Graphics.TranslateTransform(xmin * 2, 0);另请注意,除非您相应地移动 Graphics 对象,否则Graphics只能显示正值。所以没有TranslateTransform你的数字将永远不会显示。(我已经为演示更改了它们。)另请注意,应始终绘制连接Graphics.DrawLines线,否则连接将因笔宽较大和/或半透明颜色而变得混乱。正如 Jimi 所指出的,如果您想继续绘图,您将需要e.Graphics.ResetTransform();在翻转之后执行 a,或者,如果您已经通过将画布转换为正域来准备整个绘图,则恢复它在翻转之前的状态。对于第一个存储状态:var&nbsp;state&nbsp;=&nbsp;e.Graphics.Save();然后恢复它:e.Graphics.Restore(state);请注意,您需要注意这两个命令需要一对一匹配!
随时随地看视频慕课网APP
我要回答