Results 1 to 7 of 7

Thread: [RESOLVED] drawing a triangle (general algorithm)

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    May 2003
    Location
    Sydney
    Posts
    1,123

    Resolved [RESOLVED] drawing a triangle (general algorithm)

    this is a general algorithm question. sorry, i could not find the right place for this. so here it goes.

    I have a line defined by 2 points a(x1,y1) and b(x2,y2). I want to draw a triangle as shown in the image.

    i know it can be done as i have done it before (6 years ago, vb6). now i can't remember how i did it and i don't have the code with me anymore to use as a reference.

    please help and thanks in advance.
    Attached Images Attached Images  

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: drawing a triangle (general algorithm)

    If you're talking about what calculations to perform then this is a maths question and this site has a Maths forum. If you're talking about the actual drawing then that's done using GDI+ in the Paint event handler of the control you're drawing on, which is quite different to VB6. Once you've got the coordinates you simply call Graphics.DrawLine a few times.

    There are lots of GDI+ examples around. You can find one by following the CodeBank link in my signature and finding my Simple Drawing thread.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    May 2003
    Location
    Sydney
    Posts
    1,123

    Re: drawing a triangle (general algorithm)

    hmmm, sorry, didn't know there was a math forum. sorry.

    can a moderator please move this to the maths forum? thanks

    I am talking about the algorithms of finding the coordinates of the vertices of the triangle.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: drawing a triangle (general algorithm)

    Quote Originally Posted by mebhas View Post
    sorry, i could not find the right place for this.
    Quote Originally Posted by mebhas View Post
    hmmm, sorry, didn't know there was a math forum.
    You say that you couldn't find the right place to post but you also say that you didn't know there was a maths forum, so you must not actually have looked for the right place to post. The maths forum is listed with all the other forums on the home page so if you had looked you would have found it.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: drawing a triangle (general algorithm)

    Let v = the normalized vector starting at a, pointing to b. I'll assume a is not equal to b. Then we have v = b-a/|b-a| = (x2-x1, y2-y1) / Sqrt((x2-x1)^2+(y2-y1)^2). I assume you know the height h and width w of the triangle. The upper vertex of the triangle is then a + h v, using vector math.

    The lower left vertex is slightly more complicated. Let w be the vector perpendicular to v. A property of perpendicular vectors in the plane is that their coordinates are just flipped, with one negated, with a scaling factor between them. Take v=(v1, v2), using the above formulae for v1 and v2. Then u = the vector perpendicular to v = (-v2, v1). Since v is a unit vector, so is u. The lower left vertex of the triangle is then simply a + u * w/2, and the lower right vertex is a - u * w/2.

    You can do all of this in terms of angles and trig functions, but this is much neater, and doesn't suffer from singularities that you have to special case out.


    A note on vector math: do everything component-by-component. c*(d,e) = (c*d, c*e); (f,g)+(h,i) = (f+h,g+i). Using these properties and the formulae above you should be able to code it up without much trouble.
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    May 2003
    Location
    Sydney
    Posts
    1,123

    Re: drawing a triangle (general algorithm)

    i'll have to get back to you on this, jemidiah. i am not good with vector math and that is not the way i remember doing it before (basically means i have never used vector maths in my programming before).

    i'll have a look at what you have given me and try to do some reading in vector maths and see what i can understand form it.

    thanks a lot.

  7. #7
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: drawing a triangle (general algorithm)

    You can think of the vectors as points. I've defined the operations I used. Theoretically, you might be able to code that up without any real knowledge of vector math--just some knowledge of points and mental pattern matching. I mentioned the other method (the one you probably used) that uses geometry and angles and trig--it's uglier, though, so I didn't use it.

    To be explicit, draw your triangle by using the following:

    Let LL = lower left vertex = a + u * w/2
    Let LR = lower right vertex = a - u * w/2
    Let U = upper vertex = a + h * v
    Let a = (x1, y1), b = (x2, y2)
    Let v = (x2-x1, y2-y1) / Sqrt((x2-x1)^2+(y2-y1)^2) = (v1, v2)
    Let u = (-v2, v1)
    Let h = height of your triangle, w = width of your triangle

    Draw line from LL to LR.
    Draw line from LR to U.
    Draw line from U to LL.


    Intuitively, I've just "mathified" the following:
    1. Start at a. Move in the direction from a to b the distance h. This becomes a + h*v; v is a vector pointing from a to b. Dividing by the Sqrt(...) is done to make the amount of movement work out.
    2. Start at a. Move along the base of the triangle a distance w/2. I used magic to say that this direction is u as I've given it in terms of v1 and v2. This becomes a + u * w/2 to get to the lower left vertex. The lower right is similar.
    Last edited by jemidiah; Oct 15th, 2009 at 05:12 AM.
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width