如何在c#中画线
我有一个学校项目,我必须有一个函数可以从屏幕上的任何位置到屏幕上的任何其他位置绘制一条线。我知道其中有一些功能可以为我做到这一点。
这就是我到目前为止所拥有的:( 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
许多错误:
swap
需要将其参数作为ref
Math.Abs
您可以使用它而不是编写自己的(技术上不是一个错误但风格不好)fpart
函数很可能不会执行您希望它执行的操作。我认为它应该返回 double 的小数部分,而不是传入的 int 。让它接受并返回 double 。(int)0.5
没有意义。它变成 0。int
变量应该是 double。因为您的代码假设它们可以包含非整数。int Gradient = dy / dx
使用整数除法。在除法之前,您需要将其中一个转换为双倍,否则您会丢失小数部分。Many mistakes:
swap
needs to take its parameters asref
Math.Abs
which you can use instead of writing your own(Technically not a mistake but bad style)fpart
function most likely doesn't do what you want it to do. I assume it should return the fractional part of adouble
and not theint
passed in. Make it take and return adouble
.(int)0.5
doesn't make sense. It becomes 0.int
variables should be double instead. Since your code assumes that they can contain non integral numbers.int gradient = dy / dx
uses integer division. You need to cast one of them to double before dividing or you lose the fractional part.检查一下它会对您有所帮助
有一个包含代码的 icon.cs 文件你想要
Check this it will help you
there is an icon.cs file which contains the code you want