Qbasicnews.com
February 26, 2020, 01:47:29 AM
 Pages: [1] 2 3 4
 Author Topic: Triangle routine gone haywire  (Read 10521 times)
Ninkazu
Been there, done that

Posts: 1169

 « on: May 26, 2003, 12:29:33 PM »

Well, not haywire, but at some points it will just draw two dots instead of the entire triangle. I was hoping you guys would be able to figure it out.
Code:
DEFINT A-Z
'\$DYNAMIC
DECLARE SUB Triangle2 (x1%, y1%, x2%, y2%, x3%, y3%, c%)

DIM SHARED buffer(32001), lutsegy(199) AS LONG

CONST PI180 = 3.141592654# / 180

buffer(0) = 2560: buffer(1) = 200
FOR n& = 0 TO 199
lutsegy(n&) = n& * 320 + 4
NEXT

SCREEN 13
DEF SEG = VARSEG(buffer(0))

x1 = 11: y1 = 1
x2 = 1: y2 = 21
x3 = 21: y3 = 21
xc = 149
yc = 89
t# = TIMER
DO
Triangle2 x1, y1, x2, y2, x3, y3, 4
ang = ang + 3: IF ang > 359 THEN ang = ang - 360
x1 = (COS(ang * PI180) * 30) + 189
y1 = (SIN(ang * PI180) * 30) + 149
x2 = (COS((ang + 120) * PI180) * 30) + 189
y2 = (SIN((ang + 120) * PI180) * 30) + 149
x3 = (COS((ang + 240) * PI180) * 30) + 189
y3 = (SIN((ang + 240) * PI180) * 30) + 149
PUT (0, 0), buffer, PSET
REDIM buffer(32001)
buffer(0) = 2560: buffer(1) = 200
fr = fr + 1
FOR g = 0 TO 500: WAIT &H3DA, 8: NEXT
LOOP UNTIL LEN(INKEY\$)

PRINT fr / (TIMER - t#)

SUB Triangle2 (x1%, y1%, x2%, y2%, x3%, y3%, c%)
'This one uses some tricks with integer math to avoid
'any floating point math

IF y1% > y2% THEN SWAP y1%, y2%: SWAP x1%, x2%
IF y1% > y3% THEN SWAP y1%, y3%: SWAP x1%, x3%
IF y2% > y3% THEN SWAP y2%, y3%: SWAP x2%, x3%

DeltaY1% = y2% - y1%
DeltaY2% = y3% - y2%
DeltaY3% = y3% - y1%

IF DeltaY1% THEN d1& = (x2% - x1%) * &H10000 \ DeltaY1%
IF DeltaY2% THEN d2& = (x3% - x2%) * &H10000 \ DeltaY2%
IF DeltaY3% THEN d3& = (x3% - x1%) * &H10000 \ DeltaY3%

lx& = x1% * &H10000
rx& = lx&

FOR y% = y1% TO y2% - 1 'Draw from high point to mid point
lx% = lx& \ &H10000
rx% = rx& \ &H10000
FOR l = lx% TO rx%
NEXT
lx& = lx& + d1&
rx& = rx& + d3&
NEXT

lx& = x2% * &H10000
FOR y% = y2% TO y3%     'Draw from mid point to low point
lx% = lx& \ &H10000
rx% = rx& \ &H10000
FOR l = lx% TO rx%
NEXT
lx& = lx& + d2&
rx& = rx& + d3&
NEXT

END SUB
 Logged

am an asshole. Get used to it.
Ninkazu
Been there, done that

Posts: 1169

 « Reply #1 on: May 26, 2003, 06:59:29 PM »

Can no one figure this out??
 Logged

am an asshole. Get used to it.
seph
Na_th_an

Posts: 1915

 « Reply #2 on: May 26, 2003, 07:26:51 PM »

Too complicated for my simple, unintellegent mind.
 Logged

earn.
Ninkazu
Been there, done that

Posts: 1169

 « Reply #3 on: May 26, 2003, 07:38:33 PM »

Dang it! It works when I use the LINE command, but not with my buffering stuff! THIS IS SO CONFUSING!!
 Logged

am an asshole. Get used to it.
Agamemnus
x/ \z

Posts: 3491

 « Reply #4 on: May 26, 2003, 07:41:37 PM »

maybe your buffering stuff is somehow different than your line command. try to compare them....
 Logged

Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
Ninkazu
Been there, done that

Posts: 1169

 « Reply #5 on: May 26, 2003, 07:52:08 PM »

What do you mean "compare them"?
 Logged

am an asshole. Get used to it.
relsoft
*/-\*

Posts: 3927

 « Reply #6 on: May 27, 2003, 02:49:14 AM »

I really haven't looked at the code in detail but FOR NEXT loops Increment (unles you -Step) so you might try to look at it. :*)
 Logged

y smiley is 24 bit.

Genso's Junkyard:
http://rel.betterwebber.com/
Ninkazu
Been there, done that

Posts: 1169

 « Reply #7 on: May 27, 2003, 08:26:16 AM »

I don't really understand what you mean, rel. :\
 Logged

am an asshole. Get used to it.
Antoni Gual
Na_th_an

Posts: 1434

 « Reply #8 on: May 27, 2003, 04:45:03 PM »

Rel probably means you should put
Code:
IF rx%>lx% THEN SWAP rx%,rx%

before your inner loops, because this is what the FOR expects
 Logged

Antoni
Ninkazu
Been there, done that

Posts: 1169

 « Reply #9 on: May 27, 2003, 05:05:05 PM »

Thanks, that worked perfectly, but is there any way at all to reduce the amount of IF statements? It's really hurting the speed...
 Logged

am an asshole. Get used to it.
Antoni Gual
Na_th_an

Posts: 1434

 « Reply #10 on: May 27, 2003, 05:19:20 PM »

What hurts the speed is just this line:
Code:
FOR g = 0 TO 500: WAIT &H3DA, 8: NEXT

 Logged

Antoni
Agamemnus
x/ \z

Posts: 3491

 « Reply #11 on: May 27, 2003, 05:27:10 PM »

Instead of swapping, you can create separate code for each y1 y2 y3 possibility. (there are only 6) That would remove three IFs. (the  deltaxy if code)
 Logged

Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
Antoni Gual
Na_th_an

Posts: 1434

 « Reply #12 on: May 27, 2003, 05:43:51 PM »

Before removing IFs you should get rid of products, divides and long variables. For this you could use  Bresenham to calculate the ends of horizontal lines (it's slow in the IDE but very fast if compiled).
 Logged

Antoni
Ninkazu
Been there, done that

Posts: 1169

 « Reply #13 on: May 27, 2003, 07:30:57 PM »

Would you mind giving me an example of how to do that?
 Logged

am an asshole. Get used to it.
Ninkazu
Been there, done that

Posts: 1169

 « Reply #14 on: May 27, 2003, 09:30:18 PM »

Oh, I think I may have a solution for my IF problem, but I'm not sure how to do it. I could use STEP, and somehow reduce rx - lx to something like 1 or -1 so it takes the correct direction.. As I said, I'm not sure how, so could you tell me if there is an easy, fast way of doing that?
 Logged

am an asshole. Get used to it.
 Pages: [1] 2 3 4