Yaay triangle questions! :D

Blitz3D Forums/Blitz3D Programming/Yaay triangle questions! :D

Adam Novagen(Posted 2009) [#1]
Hey all,

So, I've been wondering, what would be the best way to quickly draw a filled triangle, using only 2D commands??

Also, what would be the best way to determine if a triangle drawn between three points (X1,Y1, etc) is going clockwise or counter-clockwise.


Ross C(Posted 2009) [#2]
Quickly, that's a good one. I have no idea... However, i don't get your second question. A triangle don't go any direction, it just is there. Can you explain further? Do you mean the order of the 3 points from first to last?


Adam Novagen(Posted 2009) [#3]
Yeah, exactly. In other words, you have three points or vertices, let's say 0,1,2. If you mark the positions of 0,1,2 in order, they'll either go clockwise or counter-clockwise. Like this:

CLOCKWISE

     0
    / '-,
   /     '-,
  2_________'1

COUNTER-CLOCKWISE

0-----2
|    /
|   /
|  /
| /
|/
1



Ross C(Posted 2009) [#4]
I would add the angles between each point up. If they comes to a positive answer, it's a anti clockwise triangle. If it's negative, it's a clockwise triangle. (It might be the other way around, i can't remember what way the 2d rotational system works in blitz :)


Warpy(Posted 2009) [#5]
Take the vector cross product of the line from point 0 -> point 1 and the line from point 0 to point 2.

It'll be either negative or positive, depending on if the points are given clockwise or anti-clockwise.


puki(Posted 2009) [#6]
what would be the best way to determine if a triangle drawn between three points (X1,Y1, etc) is going clockwise or counter-clockwise.

Can you not just do that prior to drawing the triangle?


Adam Novagen(Posted 2009) [#7]
Okay, I'll see what I can come up with... Thanks guys! (Is it just me, or do I say this a lot?? O_o)

Can you not just do that prior to drawing the triangle?

Not if the triangle's points are moved unpredictably in realtime.


Ross C(Posted 2009) [#8]
Add up the angles ;) that'll work. I would agree with Warner, but i don't really have much of a grasp of dot products :(


Warner(Posted 2009) [#9]
You must mean Warpy :)


Ross C(Posted 2009) [#10]
Heh, erm... yeah :) Oops!


puki(Posted 2009) [#11]
I'm not totally following this:
Not if the triangle's points are moved unpredictably in realtime.


Exactly what are you doing? What is 'unpredictably'? Considering this is all 2D.


Ross C(Posted 2009) [#12]
I think someones trying to create a 3d engine?


Adam Novagen(Posted 2009) [#13]
I think someones trying to create a 3d engine?


Boingo. Nailed it.

Actually, not a 3D engine as such - that would be totally redundant - it's just an experiment in math, specifically trig, and graphics stuff. Which is why I'm trying, thus far unsuccessfully, to do this with as little help as possible. :D


puki(Posted 2009) [#14]
This place is full of secrets these days.

I'm starting to get paranoid, that you are all getting paranoid that I may just steal everything.

Hell, some of you don't even plug your webcams in anymore. I'm starting to get a complex.


Adam Novagen(Posted 2009) [#15]
Hell, some of you don't even plug your webcams in anymore.

Aaand you would know that HOW??? No wonder we're paranoid. O_o


BlitzSupport(Posted 2009) [#16]
Use skidracer's code: Polygon


Adam Novagen(Posted 2009) [#17]
Hmm, that's certainly given me some thoughts... Makes sense really, do horizontal scanlines by dividing the polygons into right triangles... Hm.


Adam Novagen(Posted 2009) [#18]
I DID IT!!! WEEHEE!!! :D

Well, not the filled thing - I haven't tackled that yet - but I finally figured out the clockwise thing.

Ross, I tried your suggestion of adding the angles together. Trouble was, they always came out to 180, which I actually should've realized; it's basic geometry. Then I thought maybe you'd meant adding the slope angles of the sides together, using ATan2(), but that didn't work out either. BUT, after three days of bustin' my butt, I figured it out.

I suddenly realized that, assuming the global slope (using ATan2()) of the first side (between points 2 and 0) was 0 degrees, the second side (points 0 to 1) would have to have a global slope of -179 to 180 degrees, ignoring decimal fractions. If the second side's slope was positive, then the triangle would be going clockwise, and vice versa!

Of course, the first side wouldn't always have a slope of 0, so I had to make the second side's slope relative to the first, but that was easy; just subtract the first slope from the second, and BINGO!

That probably lost most of you; I'm afraid that while I can sum up most things concisely and expertly, I have enormous trouble explaining my own thoughts. Here's the function I came up with, tried and true; maybe you'll understand it better from that.

Function TriClockwise2D3D_SUB(V1X#,V1Y#,V2X#,V2Y#,V3X#,V3Y#)


Local AngleDiff# = ATan2(V2Y - V1Y,V2X - V1X) - ATan2(V1Y - V3Y,V1X - V3X)


;Keep the relative second angle within the proper -179 to 180 range
If AngleDiff < -179 Then AngleDiff = AngleDiff + 360 ElseIf AngleDiff > 180 Then AngleDiff = AngleDiff - 360


If AngleDiff > 0 Return True Else Return False


End Function



Stevie G(Posted 2009) [#19]

Take the vector cross product of the line from point 0 -> point 1 and the line from point 0 to point 2.

It'll be either negative or positive, depending on if the points are given clockwise or anti-clockwise.



You had the answer right there, abeit cross product is really a 3d specific thing.

The code below does the same thing without the need for the Atan2 and other conditional overheads. Probably makes little difference speedwise unless you're doing loads of calcs :

Function CrossProduct( x0# , y0# , x1#, y1# , x2#, y2# )

	Local ax#, ay#, bx#, by#

	ax# = x0 - x1
	ay# = y0 - y1
	bx# = x2 - x1
	by# = y2 - y1
	
	Return ( ( ax * by - ay * bx ) < 0 )
	
End Function 



Adam Novagen(Posted 2009) [#20]
You had the answer right there, abeit cross product is really a 3d specific thing.


Oh... Wow... So that's what he meant. I had no idea. XD


Warpy(Posted 2009) [#21]
Yeah, I'm normally a lot more helpful with these kinds of things, but I've been helping the entire engineering department with this stuff over the exam period and I'm tired of it!


Adam Novagen(Posted 2009) [#22]
Yeah, I'm normally a lot more helpful with these kinds of things, but I've been helping the entire engineering department with this stuff over the exam period and I'm tired of it!


Oh, I can understand that; I didn't mean your explanation was bad, I just meant that it was beyond me.


AJ00200(Posted 2009) [#23]
I'm writing a 3D engine for a kids computer program, Microworlds EX.
But wurking with triandles there would be to slow, so the creator has to create a system of turned images.