Omnimaga

Calculator Community => TI Calculators => Axe => Topic started by: Hayleia on November 16, 2012, 12:29:07 pm

Title: Clipped line routine ?
Post by: Hayleia on November 16, 2012, 12:29:07 pm
Hello everyone, as the title says, I need a clipped line routine (I mean a routine that still draws the line even if it is partly off-screen) that I can call with LINE(X1,Y1,X2,Y2) or that would use X1,Y1,X2 and Y2 as variables (there roles in the routine being obvious).

I saw this one (http://ourl.ca/4129/251821) by Darl181 but it is not a routine that I can call with LINE(X1,Y1,X2,Y2), it is a full program that includes line clipping, but only supporting one point and the other one always at the center :(

Then I saw this (http://ourl.ca/4129/250304) and tried to write it myself but failed miserably.
Here is the source in the spoiler if you see where the error is (note that it fails even though it is not optimized, I coded it noob-style to be sure that it would work but still failed D:)
Spoiler For Spoiler:
Lbl LINE
Y1-(Y1-Y2**X1//(X1-X2))->B
If X1<<0
 0->X1
 B->Y1
ElseIf X1>95
 Y1-Y2**95//(X1-X2)+B->Y1
 95->X1
End
If Y1<<0
 ~B**(X1-X2)//(Y1-Y2)->X1
 0->Y1
ElseIf Y1>63
 63->Y1
 63-B**(X1-X2)//(Y1-Y2)->X1
End
If X2<<0
 0->X2
 B->Y2
ElseIf X2>95
 Y1-Y2**95//(X1-X2)+B->Y2
 95->X2
End
If Y2<<0
 ~B**(X1-X2)//(Y1-Y2)->X2
 0->Y2
ElseIf Y2>63
 63->Y2
 63-B**(X1-X2)//(Y1-Y2)->X2
End
Line(X1,Y1,X2,Y2)
Return

So, anyone has a solution ?
Title: Re: Clipped line routine ?
Post by: TheMachine02 on November 16, 2012, 12:33:38 pm
Why are you using the fixed ** multiplication ? I tought that it was only for 8.8 fixed number  ??? .
Title: Re: Clipped line routine ?
Post by: Hayleia on November 16, 2012, 12:35:11 pm
Why are you using the fixed ** multiplication ? I tought that it was only for 8.8 fixed number  ??? .
???
Isn't it for multiplication between two signed integers ?
"The signed multiplication is performed using the high byte as the integer part and the low byte as the decimal part returning a number in the same format."
Title: Re: Clipped line routine ?
Post by: calc84maniac on November 16, 2012, 12:36:38 pm
It is indeed signed, but not treated as integers. Normal multiplication, on the other hand, works for any type of integer (signed or unsigned).
Title: Re: Clipped line routine ?
Post by: TheMachine02 on November 16, 2012, 12:39:26 pm
yes it's true. I think there is a tuto on this.
Title: Re: Clipped line routine ?
Post by: Hayleia on November 16, 2012, 12:41:03 pm
It is indeed signed, but not treated as integers. Normal multiplication, on the other hand, works for any type of integer (signed or unsigned).
Ah. Yeah, I see it now in "the low byte as the decimal part" o.o°

But now that I changed all the ** and // into * and / in my program, it is worse :P
Title: Re: Clipped line routine ?
Post by: TheMachine02 on November 16, 2012, 12:42:55 pm
 // still the signed division (with two integer), isn't it ?
Title: Re: Clipped line routine ?
Post by: calc84maniac on November 16, 2012, 12:43:55 pm
// still the signed division (with two integer), isn't it ?

Yes, that's correct, you still need signed division.
Title: Re: Clipped line routine ?
Post by: TheMachine02 on November 16, 2012, 12:46:19 pm
see this one : http://ourl.ca/16928
(only for y>64)
Title: Re: Clipped line routine ?
Post by: Hayleia on November 16, 2012, 12:47:36 pm
// still the signed division (with two integer), isn't it ?

Yes, that's correct, you still need signed division.
So I need to put * and // ?
That is not very intuitive -.-°

see this one : http://ourl.ca/16928
(only for y>64)
Thanks, I'll try it :)
But that would be great to have one that supports clipping in all 4 directions.
Title: Re: Clipped line routine ?
Post by: Hayleia on November 17, 2012, 10:06:40 am
But that would be great to have one that supports clipping in all 4 directions.
Done :D
Code in spoiler.
Spoiler For Spoiler:
Lbl LINE
If r1<<0
 r4-(r2-r4*r3//(r1-r3))->r2
 0->r1
End
If r1>94
 r2-r4*94//(r1-r3)+r2-(r2-r4*r1//(r1-r3))->r2
 94->r1
End
If r2<<0
 r1-(r1-r3*r2//(r2-r4))->r1
 0->r2
End
If r2>62
 62-(r2-(r2-r4*r1//(r1-r3)))*(r1-r3)//(r2-r4)->r1
 62->r2
End

If r3<<0
 r4-(r2-r4*r3//(r1-r3))->r4
 0->r3
End
If r3>94
 r2-r4*94//(r1-r3)+r2-(r2-r4*r1//(r1-r3))->r4
 94->r3
End
If r4<<0
 r1-(r1-r3*r2//(r2-r4))->r3
 0->r4
End
If r4>62
 62-(r2-(r2-r4*r1//(r1-r3)))*(r1-r3)//(r2-r4)->r3
 62->r4
End
Line(r1,r2,r3,r4)
Return
However, after my recent fail, I decided to code this one like a complete noob, so there are lines that are the same and a lot of basic optimizations to do (and also less basic optimizations but I don't think I'll see them anyway :P). Feel free to optimize it :)

(Also, lol, I just double posted and quoted myself :P)
Title: Re: Clipped line routine ?
Post by: aeTIos on November 17, 2012, 10:07:53 am
What is the cause of the line being cut off at the right side of the screen?
Title: Re: Clipped line routine ?
Post by: Hayleia on November 17, 2012, 10:09:54 am
What is the cause of the line being cut off at the right side of the screen?
What do you mean ? Do you mean that it gets clipped before reaching the edge of the screen ?
Well that is because I put a wrong number :P
Same for what happens at the bottom :P
I put 62 and 94 instead of 63 and 95, but that is not really difficult to change :)
Title: Re: Clipped line routine ?
Post by: aeTIos on November 17, 2012, 10:14:05 am
ah lol.
Title: Re: Clipped line routine ?
Post by: squidgetx on November 17, 2012, 11:42:39 am
Nice Hayleia. Clipping is annoying lol -_-

How does that perform speed-wise? I think one easy optimization would be to make
Code: [Select]
If r1<<0
 r4-(r2-r4*r3//(r1-r3))->r2
 0->r1
End
If r1>94
 r2-r4*94//(r1-r3)+r2-(r2-r4*r1//(r1-r3))->r2
 94->r1
End
into
Code: [Select]
If r1<<0
 -1->r1
 r4-(r2-r4*r3//(r1-r3))->r2
ElseIf r1>94
 r2-r4*94//(r1-r3)+r2-(r2-r4*r1//(r1-r3))->r2
 94->r1
End
and same for all the other pieces, since a variable can't be less than 0 and greater than 94 at the same time..there'll be a small speed gain there (and a tiny bit saved by manipulating the placement of 0->r1.

Preevaluating dX and dY would also be helpful

I'll look at it some more later, but maybe Runer112 will show up lol
Title: Re: Clipped line routine ?
Post by: Hayleia on November 17, 2012, 12:08:47 pm
I agree with the ElseIf part (I had put it in my first routine but didn't put it in this one yet because I first wanted it to work before optimizing) but not with the -1->r1 because in the line that changes r2 needs r1 to be unchanged, so I have to change r1 only after I changed r2.
Title: Re: Clipped line routine ?
Post by: squidgetx on November 17, 2012, 12:58:12 pm
Oops,  you're right about that. Anyway here are some algebraic optimizations. I subbed in X for r1-r3 and Y for r2-r4 for readability but i think you'll have to put that into the code each time (or you could make a lambda out of it to save on size, but i don't know how that would affect the speed.
Code: [Select]
Lbl LINE

If r1<<0
 -Y*r3//X+r4->r2
 0->r1
ElseIf r1>94
 94-r1*Y//X+r2->r2
 94->r1
End

If r2<<0
 -X*r2//Y+r1->r1
 0->r2
ElseIf r2>62
 62-r2*X//Y+r1->r1
 62->r2
End

If r3<<0
 -Y*r3//X+r4->r4
 0->r3
ElseIf r3>94
 94-r1*Y//X+r2->r4
 94->r3
End

If r4<<0
 -X*r2//Y+r1->r3
 0->r4
ElseIf r4>62
 62-r2*X//Y+r1->r3
 62->r4
End
Line(r1,r2,r3,r4)
Return