Click to See Complete Forum and Search --> : [RESOLVED] drawing a triangle (general algorithm)
mebhas
Oct 14th, 2009, 08:28 PM
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.
jmcilhinney
Oct 14th, 2009, 08:40 PM
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.
mebhas
Oct 14th, 2009, 08:48 PM
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.
jmcilhinney
Oct 14th, 2009, 09:16 PM
sorry, i could not find the right place for this.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.
jemidiah
Oct 15th, 2009, 01:28 AM
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.
mebhas
Oct 15th, 2009, 02:14 AM
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.
jemidiah
Oct 15th, 2009, 04:37 AM
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.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.