|
-
Oct 14th, 2009, 08:28 PM
#1
Thread Starter
Frenzied Member
-
Oct 14th, 2009, 08:40 PM
#2
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.
-
Oct 14th, 2009, 08:48 PM
#3
Thread Starter
Frenzied Member
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.
-
Oct 14th, 2009, 09:16 PM
#4
Re: drawing a triangle (general algorithm)
 Originally Posted by mebhas
sorry, i could not find the right place for this.
 Originally Posted by mebhas
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.
-
Oct 15th, 2009, 01:28 AM
#5
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.
-
Oct 15th, 2009, 02:14 AM
#6
Thread Starter
Frenzied Member
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.
-
Oct 15th, 2009, 04:37 AM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|