0

这是我的代码,我想在不使用 c# 中的 drawLine 方法的情况下实现 DDA 算法。我尝试使用 PutPixel 方法,但它不起作用。我的窗户里什么都没有。有没有办法在c#中不使用drawLine方法画线?

    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        grafik = e.Graphics;

        DDACiz(ilk.X, ilk.Y, ikinci.X, ikinci.Y, grafik, Color.DarkRed);


    }

    void PutPixel(Graphics g, int x, int y, Color c) // sadece bir pixel icin
    {
        System.Drawing.Bitmap bm = new System.Drawing.Bitmap(10, 10);
        bm.SetPixel(0, 0, Color.DarkRed);
        g.DrawImageUnscaled(bm, x, y);
    }

    void DDACiz(int x1, int y1, int x2, int y2,Graphics grafik, Color renk)
    {

        int PikselSayisi;

        int dx, dy;
        float x, xFark;
        float y, yFark;

        dx = x2 - x1;
        dy = y2 - y1;

        PikselSayisi = Math.Abs(dx) > Math.Abs(dy) ? Math.Abs(dx) : Math.Abs(dy);

        xFark = (float)dx / (float)PikselSayisi;
        yFark = (float)dy / (float)PikselSayisi;

        x = (float)x1;
        y = (float)y1;

        while (PikselSayisi!=0)
        {
           PutPixel(grafik,(int)Math.Floor(x + 0.5F),(int) Math.Floor(y + 0.5f),renk);
            x += xFark;
            y += yFark;
            PikselSayisi--;
        }
    }
}
}
4

1 回答 1

1

没有方法,所以用你需要使用Graphics.DrawPoint的对象绘制单个像素GraphicsGraphics.FillRectangle

改变

void PutPixel(Graphics g, int x, int y, Color c) // sadece bir pixel icin
{
    System.Drawing.Bitmap bm = new System.Drawing.Bitmap(10, 10);
    bm.SetPixel(0, 0, Color.DarkRed);
    g.DrawImageUnscaled(bm, x, y);
}

void PutPixel(Graphics g, int x, int y, Color c) // sadece bir pixel icin
{
    g.FillRectangle(Brushes.DarkRed, x, y, 1, 1);
}

或者如果你想使用Colorc:

void PutPixel(Graphics g, int x, int y, Color c) // sadece bir pixel icin
{
    using (SolidBrush brush = new SolidBrush(c) )
       g.FillRectangle(brush , x, y, 1, 1);
}

您也可以使用绘制位图的方法,但您需要将其设置为1x1像素宽或确保它是透明的并使用 CompositingMode.SourceOver.

写一个画线方法是一个有趣的练习;比看起来要困难得多,对于除了 之外的其他通道来说真的很难PenWidths1.0更不用说对于完全不透明以外的 alpha 通道了..

于 2015-04-02T21:59:26.430 回答