1

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;
    }
}
}
dude
  • 11
  • 3
  • your swap function won't work. you have to declare a and b as out. – BlackBear Feb 10 '11 at 18:26
  • how do i do that? (im a noob) – dude Feb 10 '11 at 18:27
  • just put "out" before them ;) public static void swap(out int a, out int b) – BlackBear Feb 10 '11 at 18:28
  • You should then go read up on how objects are passed around in C#. Read about value types versus reference types. – Ed S. Feb 10 '11 at 18:32
  • There's also a standard library [Math.Abs()](http://msdn.microsoft.com/en-us/library/system.math.abs.aspx) method you can take advantage of. :) – Dan J Feb 10 '11 at 18:32
  • it says a and b are now unassigned :/ – dude Feb 10 '11 at 18:34
  • Also, your `for` loop executes while x > `xpxl1` and increments `x`. Sure that shouldn't be `x < xpxl1`? Because this loop won't execute unless xpxl1 is negative, and then it will run infinitely... – Dan J Feb 10 '11 at 18:35
  • @dude Edit your question to show us the new swap() method, and we can probably help. :) – Dan J Feb 10 '11 at 18:37
  • @CodeInChaos: C is waay better without this mess on ref or out. They're pointers anyway, and (in my opinion) they could be both ref and out. :) – BlackBear Feb 10 '11 at 18:39
  • i edited the post, this is my new code – dude Feb 10 '11 at 18:39
  • @BlackBreat `ref` and `out` ensure that your "pointer" doesn't survive longer than the local variable it points to. And if you want to use pointers you can use unsafe code. But that's not very idiomatic. So you typically do it only if it gives a significant performance gain. – CodesInChaos Feb 10 '11 at 18:41
  • i keep seeing something about making my int's into doubles, can i convert doubles into uints? cause the drawpixal function at the end wont accept (double) x, (double) y, and (uint) color :/ – dude Feb 10 '11 at 18:42
  • @CodeInChaos: pointer are unsafe only if used careless – BlackBear Feb 10 '11 at 18:44
  • you should round them to `int` directly before passing them to `DrawPixel` and not any earlier. – CodesInChaos Feb 10 '11 at 18:44
  • @BlackBear Or when used maliciously. One nice thing about C# code is that its easily sandboxed. That's much harder with c... – CodesInChaos Feb 10 '11 at 18:45
  • @CodeInChaos: I agree with that. But I've learned C first, so I feel a bit limited with C# – BlackBear Feb 10 '11 at 18:48
  • i tried this int x2a = (int)x; int interya = (int)intery; but it wont work – dude Feb 10 '11 at 18:53
  • @dude the most important thing for you is to learn basic programming an C#. – CodesInChaos Feb 10 '11 at 19:10

4 Answers4

1

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.
CodesInChaos
  • 106,488
  • 23
  • 218
  • 262
  • you are right; but don't mind, his concerns are different instead of code revie. – Waqas Raja Feb 10 '11 at 18:45
  • @Waqas I assume this about learning to code a line drawing algorithm, not about just drawing a line. – CodesInChaos Feb 10 '11 at 18:47
  • 1
    +1 Whatever the ultimate question to be answered, there's clearly a lot of 101 stuff the OP needs to learn before he'll get anywhere. – Dan J Feb 10 '11 at 18:55
0
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();
        }
0

Check this it will help you

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

Nighil
  • 4,099
  • 7
  • 30
  • 56
0
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);
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/34435153) – Wahlstrommm May 24 '23 at 13:46