【问题标题】:How to draw lines in c#如何在c#中画线
【发布时间】:2011-06-25 01:34:41
【问题描述】:

我有一个学校项目,我必须有一个功能,可以从屏幕上的任何地方画一条线到屏幕上的任何其他地方。我知道其中包含一些功能可以为我完成。

这是我目前所拥有的:(vga.setpixel 设置 (uint)x 像素,在 (uint)y 像素,颜色 (uint) 颜色)

class drawaline
{
   public static void swap(ref int a, ref int b)
{
    int temp = a; // Copy the first position's element
    a = b; // Assign to the second element
    b = temp; // Assign to the first element
}
public static int abs(int value)
{
    if (value < 0)
        value = value * -1;
    return value;
}
public static int fpart(int x)
{
    return x;
}
public static int rfpart(int x)
{
    x = 1 - fpart(x);
    return x;
}
public static int ipart(int x)
{
    return x;
}
public static void line(int x1, int y1, int x2, int y2, uint color)
{
    int dx = x2 - x1;
    int dy = y2 - y1;
    if (abs(dx) < (dy))
    {
        swap(ref x1, ref y1);
        swap(ref x2, ref y2);
        swap(ref dx, ref dy);
    }
    if (x2 < x1)
    {
        swap(ref x1, ref x2);
        swap(ref y1, ref y2);
    }
    int gradient = dy / dx;
    // handle first endpoint
    int xend = x1;
    int yend = y1 + gradient * (xend - x1);
    int x1p = x1 + (int).5;
    int xgap = rfpart(x1p);
    int xpxl1 = xend; // this will be used in the main loop
    int ypxl1 = ipart(yend);
    VGAScreen.SetPixel320x200x8((uint)xpxl1, (uint)ypxl1, (uint)color);
    int intery = yend + gradient; // first y-intersection for the main loop
    // handle second endpoint
    xend = x2;
    yend = y2 + gradient * (xend - x2);
    xgap = fpart(x2 + (int)0.5);
    int xpxl2 = xend; // this will be used in the main loop
    int ypxl2 = ipart(yend);
    VGAScreen.SetPixel320x200x8((uint)xpxl2, (uint)ypxl2, (uint)color);
    VGAScreen.SetPixel320x200x8((uint)xpxl2, (uint)ypxl2 + 1, (uint)color);

    // main loop
    for (x = 0; x < xpxl1 + 1; x++)
    {
        VGAScreen.SetPixel320x200x8((uint)x, (uint)intery, (uint)color);
        VGAScreen.SetPixel320x200x8((uint)x, (uint)intery, (uint)color);
        intery = intery + gradient;
    }
}
}

【问题讨论】:

  • 您的交换功能不起作用。您必须将 a 和 b 声明为 out。
  • 我该怎么做? (我是菜鸟)
  • 把“out”放在他们前面;) public static void swap(out int a, out int b)
  • 然后您应该阅读有关如何在 C# 中传递对象的内容。了解值类型与引用类型。
  • 还有一个标准库Math.Abs() 方法您可以利用。 :)

标签: c# drawing plot line pixel


【解决方案1】:

很多错误:

  1. swap 需要将其参数作为ref
  2. 您可以使用Math.Abs 而不是自己编写(技术上不是错误,但风格不好)
  3. 您的fpart 函数很可能无法执行您希望它执行的操作。我认为它应该返回double 的小数部分,而不是传入的int。让它接受并返回double
  4. (int)0.5 没有意义。它变成了 0。
  5. 大多数int 变量应该是双精度的。因为您的代码假定它们可以包含非整数。
  6. int gradient = dy / dx 使用整数除法。您需要在除法之前将其中一个转换为双倍,否则您会丢失小数部分。

【讨论】:

  • 你是对的;但不要介意,他的关注点与代码审查不同。
  • @Waqas 我假设这是关于学习编写画线算法的代码,而不仅仅是画一条线。
  • +1 无论最终要回答的问题是什么,显然 OP 需要学习很多 101 项内容,然后才能到达任何地方。
【解决方案2】:

Check this it will help you

有一个 icon.cs 文件包含你想要的代码

【讨论】:

  • 使用了已经制作好的“DrawLine”功能,我需要自己制作
【解决方案3】:
void draw_line()
        {
            Pen mypen;
            mypen = new Pen(Color.Black , 1);
            Graphics g = this.CreateGraphics();
            g.DrawLine(mypen, 0, 20, 1000, 20); 
            // 0,20 are starting points and 1000,20 are destination.
            mypen.Dispose();
            g.Dispose();
        }

【讨论】:

  • 当你想画一条线并给出一个起点和终点时,你只需调用这个函数......
猜你喜欢
  • 1970-01-01
  • 2013-08-14
  • 1970-01-01
  • 1970-01-01
  • 2013-03-06
  • 2022-11-03
  • 2011-08-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多