Author Topic: Clipped line routine ?  (Read 6059 times)

0 Members and 2 Guests are viewing this topic.

Offline Hayleia

  • Programming Absol
  • Coder Of Tomorrow
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3367
  • Rating: +393/-7
    • View Profile
Clipped line routine ?
« 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 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 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 ?
« Last Edit: November 16, 2012, 12:31:01 pm by Hayleia »
I own: 83+ ; 84+SE ; 76.fr ; CX CAS ; Prizm ; 84+CSE
Sorry if I answer with something that seems unrelated, English is not my primary language and I might not have understood well. Sorry if I make English mistakes too.

click here to know where you got your last +1s

Offline TheMachine02

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 452
  • Rating: +105/-0
  • me = EF99+F41A
    • View Profile
Re: Clipped line routine ?
« Reply #1 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  ??? .
AXE/asm programmer - unleash the power of z80 //C++//C

epic 3D things http://www.ntu.edu.sg/home/ehchua/programming/opengl/CG_BasicsTheory.html

Offline Hayleia

  • Programming Absol
  • Coder Of Tomorrow
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3367
  • Rating: +393/-7
    • View Profile
Re: Clipped line routine ?
« Reply #2 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."
I own: 83+ ; 84+SE ; 76.fr ; CX CAS ; Prizm ; 84+CSE
Sorry if I answer with something that seems unrelated, English is not my primary language and I might not have understood well. Sorry if I make English mistakes too.

click here to know where you got your last +1s

Offline calc84maniac

  • eZ80 Guru
  • Coder Of Tomorrow
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2912
  • Rating: +471/-17
    • View Profile
    • TI-Boy CE
Re: Clipped line routine ?
« Reply #3 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).
"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman

Offline TheMachine02

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 452
  • Rating: +105/-0
  • me = EF99+F41A
    • View Profile
Re: Clipped line routine ?
« Reply #4 on: November 16, 2012, 12:39:26 pm »
yes it's true. I think there is a tuto on this.
AXE/asm programmer - unleash the power of z80 //C++//C

epic 3D things http://www.ntu.edu.sg/home/ehchua/programming/opengl/CG_BasicsTheory.html

Offline Hayleia

  • Programming Absol
  • Coder Of Tomorrow
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3367
  • Rating: +393/-7
    • View Profile
Re: Clipped line routine ?
« Reply #5 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
I own: 83+ ; 84+SE ; 76.fr ; CX CAS ; Prizm ; 84+CSE
Sorry if I answer with something that seems unrelated, English is not my primary language and I might not have understood well. Sorry if I make English mistakes too.

click here to know where you got your last +1s

Offline TheMachine02

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 452
  • Rating: +105/-0
  • me = EF99+F41A
    • View Profile
Re: Clipped line routine ?
« Reply #6 on: November 16, 2012, 12:42:55 pm »
 // still the signed division (with two integer), isn't it ?
AXE/asm programmer - unleash the power of z80 //C++//C

epic 3D things http://www.ntu.edu.sg/home/ehchua/programming/opengl/CG_BasicsTheory.html

Offline calc84maniac

  • eZ80 Guru
  • Coder Of Tomorrow
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2912
  • Rating: +471/-17
    • View Profile
    • TI-Boy CE
Re: Clipped line routine ?
« Reply #7 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.
"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman

Offline TheMachine02

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 452
  • Rating: +105/-0
  • me = EF99+F41A
    • View Profile
Re: Clipped line routine ?
« Reply #8 on: November 16, 2012, 12:46:19 pm »
see this one : http://ourl.ca/16928
(only for y>64)
AXE/asm programmer - unleash the power of z80 //C++//C

epic 3D things http://www.ntu.edu.sg/home/ehchua/programming/opengl/CG_BasicsTheory.html

Offline Hayleia

  • Programming Absol
  • Coder Of Tomorrow
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3367
  • Rating: +393/-7
    • View Profile
Re: Clipped line routine ?
« Reply #9 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.
I own: 83+ ; 84+SE ; 76.fr ; CX CAS ; Prizm ; 84+CSE
Sorry if I answer with something that seems unrelated, English is not my primary language and I might not have understood well. Sorry if I make English mistakes too.

click here to know where you got your last +1s

Offline Hayleia

  • Programming Absol
  • Coder Of Tomorrow
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3367
  • Rating: +393/-7
    • View Profile
Re: Clipped line routine ?
« Reply #10 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)
« Last Edit: November 17, 2012, 11:54:38 am by Hayleia »
I own: 83+ ; 84+SE ; 76.fr ; CX CAS ; Prizm ; 84+CSE
Sorry if I answer with something that seems unrelated, English is not my primary language and I might not have understood well. Sorry if I make English mistakes too.

click here to know where you got your last +1s

Offline aeTIos

  • Nonbinary computing specialist
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3915
  • Rating: +184/-32
    • View Profile
    • wank.party
Re: Clipped line routine ?
« Reply #11 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?
I'm not a nerd but I pretend:

Offline Hayleia

  • Programming Absol
  • Coder Of Tomorrow
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3367
  • Rating: +393/-7
    • View Profile
Re: Clipped line routine ?
« Reply #12 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 :)
I own: 83+ ; 84+SE ; 76.fr ; CX CAS ; Prizm ; 84+CSE
Sorry if I answer with something that seems unrelated, English is not my primary language and I might not have understood well. Sorry if I make English mistakes too.

click here to know where you got your last +1s

Offline aeTIos

  • Nonbinary computing specialist
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3915
  • Rating: +184/-32
    • View Profile
    • wank.party
Re: Clipped line routine ?
« Reply #13 on: November 17, 2012, 10:14:05 am »
ah lol.
I'm not a nerd but I pretend:

Offline squidgetx

  • Food.
  • CoT Emeritus
  • LV10 31337 u53r (Next: 2000)
  • *
  • Posts: 1881
  • Rating: +503/-17
  • rawr.
    • View Profile
Re: Clipped line routine ?
« Reply #14 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
« Last Edit: November 17, 2012, 11:44:16 am by squidgetx »