Omnimaga

Calculator Community => TI Calculators => ASM => Topic started by: Matrefeytontias on March 07, 2013, 04:57:44 pm

Title: [z80] Needs help debugging a triangle filling routine
Post by: Matrefeytontias on March 07, 2013, 04:57:44 pm
Hey guys,

I'm trying to build a triangle filler out of the algorithm ben_g sent me, and I'm encountering some problems... In most cases, the triangle is not drawn at the location I wanted it to be.

So here's the pseudocode of the algorithm :
Code: [Select]
inputs pt1,pt2,pt3,color       // color is unused for now

// we sorts the points with the smallest Y first
if (pt1.y > pt2.y) swap(pt1,pt2)     // swaps pt1.x with pt2.x and pt1.y with pt2.y

If (pt2.y > pt3.y) swap(pt2,pt3)

If(pt1.y > pt3.y) swap(pt1,pt3)

dx1 = (x2-x1)/(y2-y1)
dx2 = (x3-x1)/(y3-y1)
slx2 = slx1 = x1
sly = y1

drawLoop:
While (sly < y2)
{
  HorizontalLine(sly, slx1, slx2)
  slx1 = slx1 + dx1
  slx2 = slx2 + dx2
  sly++
}
If (y2 != y3)
{
  dx1 = (x3-x2)/(y3-y2)
  y2 = y3
  goto drawLoop
}

And here's the asm code I have so far (http://pastebin.com/QFrLsQw5) (no screenie, I'm on a phone :/ ). So if you guys see anything wrong...

Thanks by advance :)
Title: Re: [z80] Needs help debugging a triangle filling routine
Post by: chickendude on March 08, 2013, 01:56:40 am
What are sub_88div and sub_HLline?
Title: Re: [z80] Needs help debugging a triangle filling routine
Post by: Matrefeytontias on March 08, 2013, 01:58:29 am
Oh yeah I forgot to mention that, the routine is incorporated inside an axiom, so these are calls to Axe functions. sub_88Div is a 8.8 fixed signed division (in Axe HL /* DE) and sub_HLine draws an horizontal line (HLine(Y,X1,X2)).

And btw, I took a screenie. The inverted dots are the points supposed to be the corners of the triangles :

(http://mattias.refeyton.fr/espace-ti/bugTriangles.gif)

EDIT : updated the source at the same address (in the first post).
Title: Re: [z80] Needs help debugging a triangle filling routine
Post by: chickendude on March 08, 2013, 03:25:45 am
A couple problems, your variables dX1/2 don't handle negative values. You sort according to Y values, which doesn't mean that the Xs will also be smallest to largest. Also, i don't know how Axe's horizontal line routine would handle it, but if say x1 > x2, it might encounter problems expecting to draw from left to right and not the other way around.

You said you found a better algorithm, but looking at if statements and for loops gives me a headache, so i'm not even going to try understanding it. If you make an assembly version of it i'd be more than happy to look through it, though :D
Title: Re: Re: [z80] Needs help debugging a triangle filling routine
Post by: Streetwalrus on March 08, 2013, 12:28:32 pm
I think he's trying to code this algorithm in asm.
I'm not an asm expert so I can't help you though. :-\