如何在c#中画线

发布于 2024-10-17 00:33:50 字数 2019 浏览 3 评论 0原文

我有一个学校项目,我必须有一个函数可以从屏幕上的任何位置到屏幕上的任何其他位置绘制一条线。我知道其中有一些功能可以为我做到这一点。

这就是我到目前为止所拥有的:( 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;
    }
}
}

i have a school project where i have to have a function that can draw a line from anywhere on the screen to anywhere else on the screen. i know that there are some functions included that do it for me.

this is what i have so far: (the vga.setpixel thing sets the (uint)x pixel, at the (uint)y pixal, the color (uint) color)

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;
    }
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

独夜无伴 2024-10-24 00:33:50

许多错误:

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

Many mistakes:

  1. swap needs to take its parameters as ref
  2. There is Math.Abs which you can use instead of writing your own(Technically not a mistake but bad style)
  3. Your fpart function most likely doesn't do what you want it to do. I assume it should return the fractional part of a double and not the int passed in. Make it take and return a double.
  4. (int)0.5 doesn't make sense. It becomes 0.
  5. Most of your int variables should be double instead. Since your code assumes that they can contain non integral numbers.
  6. int gradient = dy / dx uses integer division. You need to cast one of them to double before dividing or you lose the fractional part.
最好是你 2024-10-24 00:33:50

检查一下它会对您有所帮助

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

Check this it will help you

there is an icon.cs file which contains the code you want

染火枫林 2024-10-24 00:33:50
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();
        }
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();
        }
明明#如月 2024-10-24 00:33:50
Graphics g = this.CreateGraphics ();
Pen p = new Pen (Color.Black , 8);

//draw line method 1
//in this method we are declaring two different points 
//The First one is x and y coordinates 

PointF p1 = new PointF(0.0F, 0.0F);

//The second one is starting and ending points
PointF p2 = new PointF(200.0F, 0.0F);
        
//this is used for drawling line
e.Graphics.DrawLine(p, p1, p2);

//The second Method is simplest one 

e.Graphics.DrawLine (p,100,150,40,40);
Graphics g = this.CreateGraphics ();
Pen p = new Pen (Color.Black , 8);

//draw line method 1
//in this method we are declaring two different points 
//The First one is x and y coordinates 

PointF p1 = new PointF(0.0F, 0.0F);

//The second one is starting and ending points
PointF p2 = new PointF(200.0F, 0.0F);
        
//this is used for drawling line
e.Graphics.DrawLine(p, p1, p2);

//The second Method is simplest one 

e.Graphics.DrawLine (p,100,150,40,40);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文