亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在圖形上創建鏡像形狀

如何在圖形上創建鏡像形狀

C#
臨摹微笑 2022-07-10 16:37:45
我有積分表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 繪制的圖形嗎?它有像復制和翻轉圖形和復合的東西嗎?謝謝
查看完整描述

2 回答

?
侃侃爾雅

TA貢獻1801條經驗 獲得超16個贊

有了GraphicsPath,您可以使用以下方法來鏡像路徑:


GraphicsPath MirrorLeft(GraphicsPath path)

{

    var r = path.GetBounds();

    var p = (GraphicsPath)path.Clone();

    p.Transform(new Matrix(-1, 0, 0, 1, 2 * r.Left, 0));

    return p;

}

GraphicsPath MirrorRight(GraphicsPath path)

{

    var r = path.GetBounds();

    var p = (GraphicsPath)path.Clone();

    p.Transform(new Matrix(-1, 0, 0, 1, 2 * (r.Left + r.Width), 0));

    return p;

}

MirrorLeft,以路徑左側為軸鏡像路徑,以路徑MirrorRight右側為軸。


下圖中,紅色弧線為原圖,綠色為左鏡,藍色為鏡右:

http://img1.sycdn.imooc.com//62ca9002000177c903290220.jpg

這里是上面輸出的代碼:


protected override void OnPaint(PaintEventArgs e)

{

    e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

    using (var path1 = new GraphicsPath())

    {

        path1.AddArc(new Rectangle(100, 100, 200, 200), -90, 90);

        using (var pen1 = new Pen(Color.Red, 3))

            e.Graphics.DrawPath(pen1, path1);


        using (var path2 = MirrorLeft(path1))

        using (var pen2 = new Pen(Color.Green, 3))

            e.Graphics.DrawPath(pen2, path2);

        using (var path3 = MirrorRight(path1))

        using (var pen3 = new Pen(Color.Blue, 3))

            e.Graphics.DrawPath(pen3, path3);

    }

    base.OnPaint(e);

}


查看完整回答
反對 回復 2022-07-10
?
瀟瀟雨雨

TA貢獻1833條經驗 獲得超4個贊

您可以簡單地翻轉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);

http://img1.sycdn.imooc.com//62ca90150001fa1902620237.jpg

另請注意,除非您相應地移動 Graphics 對象,否則Graphics只能顯示正值。所以沒有TranslateTransform你的數字將永遠不會顯示。(我已經為演示更改了它們。)

另請注意,應始終繪制連接Graphics.DrawLines線,否則連接將因筆寬較大和/或半透明顏色而變得混亂。

正如 Jimi 所指出的,如果您想繼續繪圖,您將需要e.Graphics.ResetTransform();在翻轉之后執行 a,或者,如果您已經通過將畫布轉換為正域來準備整個繪圖,則恢復它在翻轉之前的狀態。對于第一個存儲狀態:

var state = e.Graphics.Save();

然后恢復它:

e.Graphics.Restore(state);

請注意,您需要注意這兩個命令需要一對一匹配!


查看完整回答
反對 回復 2022-07-10
  • 2 回答
  • 0 關注
  • 130 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號