C Programming: Drawing a Triangle

Discussion in 'OT Technology' started by Dysfnctnl85, Oct 23, 2007.

  1. Dysfnctnl85

    Dysfnctnl85 IT/Apple/Rotary/(D)SLR Crew

    Joined:
    Dec 20, 2004
    Messages:
    3,064
    Likes Received:
    0
    Location:
    Fayetteville, Georgia
    Apparently I'm an idiot because, for the life of me, I cannot figure out how to draw a triangle!

    Can someone help me out here?
     
  2. mobbarley

    mobbarley Active Member

    Joined:
    Mar 4, 2005
    Messages:
    9,256
    Likes Received:
    2
    Location:
    Sydney
    pull out the printf and the char *
     
  3. mobbarley

    mobbarley Active Member

    Joined:
    Mar 4, 2005
    Messages:
    9,256
    Likes Received:
    2
    Location:
    Sydney
    :mamoru:
     
    Last edited: Oct 23, 2007
  4. Dysfnctnl85

    Dysfnctnl85 IT/Apple/Rotary/(D)SLR Crew

    Joined:
    Dec 20, 2004
    Messages:
    3,064
    Likes Received:
    0
    Location:
    Fayetteville, Georgia
    Well let's say I want to do it given the center of the triangle.

    I'm making a cursor that is going to be used ala Etch-A-Sketch. So no printf's.
     
  5. GOGZILLA

    GOGZILLA Double-Uranium Member

    Joined:
    Jan 16, 2003
    Messages:
    10,760
    Likes Received:
    3
    Location:
    Plantation, FL
    bust out logo writer, very easy on there
     
  6. CodeX

    CodeX Guest

    What? you mean draw with text? Why the fuck would you do that? Look into the Windows GDI if you are not making a game, if you are making a game I would suggest DirectDraw 7, or, do like I do and use D3D 9 using textured quads for sprite based graphics.
     
  7. Dysfnctnl85

    Dysfnctnl85 IT/Apple/Rotary/(D)SLR Crew

    Joined:
    Dec 20, 2004
    Messages:
    3,064
    Likes Received:
    0
    Location:
    Fayetteville, Georgia
    It's not in text. This is actually for a Gameboy Advance. I need to do this from scratch, with no libraries, so that's why I'm scratching my head. It's really easy to draw a square haha.
     
  8. ez4me2c3d

    ez4me2c3d Cold Member

    Joined:
    Sep 19, 2003
    Messages:
    1,836
    Likes Received:
    0
    Location:
    Minneapolis, MN
    how do you draw a square? or a line for that matter?
    the basics of geomtry will allow you to draw a triangle if you can draq a line.
     
  9. Dysfnctnl85

    Dysfnctnl85 IT/Apple/Rotary/(D)SLR Crew

    Joined:
    Dec 20, 2004
    Messages:
    3,064
    Likes Received:
    0
    Location:
    Fayetteville, Georgia
    It's very simple to draw a line given a start and endpoint. But how do you construct a triangle with lines? That is, FILL IT IN, not just draw the edges.
     
  10. GOGZILLA

    GOGZILLA Double-Uranium Member

    Joined:
    Jan 16, 2003
    Messages:
    10,760
    Likes Received:
    3
    Location:
    Plantation, FL
    post what you have for a filled in square
     
  11. Dysfnctnl85

    Dysfnctnl85 IT/Apple/Rotary/(D)SLR Crew

    Joined:
    Dec 20, 2004
    Messages:
    3,064
    Likes Received:
    0
    Location:
    Fayetteville, Georgia
    /* Method for drawing a rectangle */
    void fillRect(u16 row, u16 col,
    u16 height, u16 width,
    u16 color)
    {
    int r, c;
    for (r=row; r<row+width; r++) {
    for (c=col; c < col+height; c++) {
    videoBuffer[OFFSET(r,c)] = color;
    }
    }
    }//end fillRect
     
  12. GOGZILLA

    GOGZILLA Double-Uranium Member

    Joined:
    Jan 16, 2003
    Messages:
    10,760
    Likes Received:
    3
    Location:
    Plantation, FL
    what type of triangle are you expecting to be drawing? if you just want to do equilateral triangles or a constant type of triangle then the calculations can be simpler. do you have a prototype that you want to use for the function?

    like if it was an equilateral triangle all you would need is something like

    void fillTriangle(u16 x, u16 y, u16 length)

    x,y for the center of the triangle on the screen and length would be the length of the sides
     
  13. CodeX

    CodeX Guest

    Scan it from top to bottom, filling it with horizontal lines.

    Say you have the triangle (-10,-10),(10,-10),(0,10) and you use each of those coordinates for the endpoint of a line... you are likely using the bressenham line drawing algorithm, so you start at the top most y value and left most x value, draw a horizontal line to the right most x value, you then move down one line, scan back left to find the left most value on that line, then draw a horizontal line to the right most value, repeat that process until you reach the bottom right most pixel that falls within the triangle
     
  14. CodeX

    CodeX Guest

  15. Darketernal

    Darketernal Watch: Aria The Origination =)

    Joined:
    Oct 8, 2002
    Messages:
    10,498
    Likes Received:
    3
    Location:
    Care,for all is like a bonsai tree
  16. CodeX

    CodeX Guest

    You can always use a toolkit to do anything for you but it is nice knowing how it is actually accomplished behind the scenes
     

Share This Page