Page 1 of 1

Which is faster...

Posted: Tue Jun 14, 2005 7:13 pm
by Guest
I have a created a new "revolutionary" *cough* new poly filling routine(fill in a triangle). It splits up the line tracing into one constant increment and another 2 part increment. It's really weird but works just fine except something still bothers me about it. Instead of using alot of division, my program depends heavily on IF... THEN statements and lots o' variables (and i mean LOTS!). Im trying to figure out which is better, my routine or a more mathamaticly based one. Please comment!

-Chris

Posted: Wed Jun 15, 2005 2:40 am
by Guest
Benchmark it.

Or Post the Code, and I'll benchmark it (I've been looking for one of these since the summer, and no division will mean it won't keep crashing when I try and fill squares).

Off hand I dunno which'd be faster, unless I see the code, you know?

matt

Posted: Wed Jun 15, 2005 5:21 am
by MystikShadows
It depends on how many ifs and thens the mathematical formulas replace.

An IF check is faster than a Formula calculation but if the formula was to replace a good amount of IFs then it might even itself out or endup faster.

Posted: Wed Jun 15, 2005 3:05 pm
by Zamaster
Here is the code, it's still a little buggy but it will work just fine:

Code: Select all

DECLARE SUB SetVectorXY (stx!, sty!, enx!, eny!)
SCREEN 13
CLS
RANDOMIZE TIMER
DIM SHARED VX AS SINGLE, VY AS SINGLE, Flength AS SINGLE
DIM Points(2, 1)
DIM Edges(479, 1)

TIMET = INT(TIMER)

DO
Points(0, 0) = INT(RND * 319) + 1: Points(0, 1) = INT(RND * 199) + 1
Points(1, 0) = INT(RND * 319) + 1: Points(1, 1) = INT(RND * 199) + 1
Points(2, 0) = INT(RND * 319) + 1: Points(2, 1) = INT(RND * 199) + 1


hy = 0: my = 0: ly = 479
hp = 0: mp = 0: lp = 0
FOR i% = 1 TO 4
q% = i% - 1: IF q% = 3 THEN q% = 0
IF Points(q%, 1) > hy THEN hy = Points(q%, 1): hp = q%
IF Points(q%, 1) < ly THEN ly = Points(q%, 1): lp = q%
NEXT i%
IF hp <> 0 AND lp <> 0 THEN mp = 0
IF hp <> 1 AND lp <> 1 THEN mp = 1
IF hp <> 2 AND lp <> 2 THEN mp = 2
SetVectorXY Points(lp, 0), Points(lp, 1), Points(hp, 0), Points(hp, 1)
constx = VX: consty = VY
lgth1 = Flength
SetVectorXY Points(lp, 0), Points(lp, 1), Points(mp, 0), Points(mp, 1)
part1x = VX: part1y = VY
adl1 = Flength
SetVectorXY Points(mp, 0), Points(mp, 1), Points(hp, 0), Points(hp, 1)
part2x = VX: part2y = VY
lgth2 = Flength + adl1
conssl = 0: conssr = 0
IF constx + consty > part1x + part1y THEN conssl = 0: conssr = 1 ELSE conssl = 1: conssr = 0
Edges(Points(lp, 1), conssl) = Points(lp, 0)
nl1 = 1
nl2 = 0
x1m = 0
y1m = 0
x2m = 0
y2m = 0
oy1 = 0
oy2 = 0
ph = 0
FOR i% = 1 TO lgth2
IF i% < lgth1 THEN
oy1 = y1m
x1m = x1m + constx: y1m = y1m + consty
IF INT(oy1) <> INT(y1m) THEN nl1 = nl1 + 1
Edges(Points(lp, 1) + nl1 - 1, conssl) = Points(lp, 0) + x1m
END IF
oy2 = y2m
IF ph = 0 THEN x2m = x2m + part1x: y2m = y2m + part1y
IF ph = 1 THEN x2m = x2m + part2x: y2m = y2m + part2y
IF INT(Points(lp, 1) + y2m) = INT(Points(mp, 1)) THEN ph = 1
IF INT(oy2) <> INT(y2m) THEN nl2 = nl2 + 1
Edges(Points(lp, 1) + nl2 - 1, conssr) = Points(lp, 0) + x2m
NEXT i%
cols = INT(RND * 13)
FOR i% = ly TO hy - 2
LINE (Edges(i%, 0), i% + 1)-(Edges(i%, 1), i% + 1), INT(RND * 2) + cols
'FOR scn% = Edges(i%, 0) TO Edges(i%, 1)
'PSET (scn%, i%), INT(RND * 2) + cols
'NEXT scn%
NEXT i%



f% = f% + 1
IF INT(TIMER) > TIMET THEN
TIMET = INT(TIMER)
fps% = f%
f% = 0
LOCATE 1, 1: PRINT "PPS:"; fps%
END IF

LOOP


SUB SetVectorXY (stx, sty, enx, eny)
VX = 0: VY = 0
Flength = 0
IF stx > enx THEN
xl = stx - enx
ELSE
xl = enx - stx
END IF
IF sty > eny THEN
yl = sty - eny
ELSE
yl = eny - sty
END IF
IF xl > yl THEN Flength = xl: Slength = yl: VX = 1
IF xl < yl THEN Flength = yl: Slength = xl: VY = 1
IF xl = yl THEN VX = 1: VY = 1: Flength = xl: GOTO 1
IF stx = sty AND enx = eny THEN VX = 1: VY = 1: GOTO 1
IF stx < enx AND sty = eny THEN VX = 1: VY = 0: EXIT SUB
IF stx > enx AND sty = eny THEN VX = -1: VY = 0: EXIT SUB
IF sty < eny AND stx = enx THEN VX = 0: VY = 1: EXIT SUB
IF sty > eny AND stx = enx THEN VX = 0: VY = -1: EXIT SUB
Scount = Slength / Flength
IF VX = 1 THEN VY = Scount ELSE : VX = Scount
1 IF enx < stx THEN VX = VX * -1
2 IF eny < sty THEN VY = VY * -1
END SUB
I also wanted to find out how fast IF... THEN statements were compared to divisions and here is what I got-

Out of 200 averaged tries of 10,000 command sequence calls:
(IF... THEN on the left, Division on the right)
Result 1#- 0.5401953 , 0.5182031
Result 2#- 0.54024 , 0.5217316
Result 3#- 0.5506699 , 0.5460852
Result 4#- 0.5336909 , 0.5466366
Result 5#- 0.5300513 , 0.5342176


So unfortunetly it appears as if divisions are faster, yet
when compared to a standard mathamticaly based routine,
(the one I compared mine with was by Rich Geldreich) There were
so many multiplications, divisions, If... Thens and other functions that
mine proved to be way faster(My routine takes 3 divisions per polygon
whereas the other routine I looked at had 9)! So yeah, dont use my routine yet though cause it's not yet perfect plus it's MINE! Eventually
Im gonna put it in ASM so that I can actually have a
USER FREIDNLY/EASY 3D library to use. As far as I can tell nobody else has done this yet.


-Chris

Posted: Wed Jun 15, 2005 3:59 pm
by Guest
If you take the calculation of the triangle color

Code: Select all

INT(RND * 2) + cols
you will get more speed and triangles of a single color, with a better effect.

Posted: Wed Jun 15, 2005 4:27 pm
by Antoni
Can't edit my previous post, as I was unlogged...

I meant if you take that calculation out of the drawing loop.

Posted: Wed Jun 15, 2005 8:47 pm
by Zamaster
Oh cmon'! Who cares? It's going to wind up as a texture mapping routine anyway. It's one lousy little randomize statement! By the way, what do you think of it? Original perhaps?