【发布时间】: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