avatar
L Break Into Program @breakintoprogram.co.uk

The Cohen-Sutherland clipping algorithm uses a function to quickly determine if a line can be drawn as-is, can be ignored, and if it needs to be clipped, the point of interest. This is it in C, compared to the rewrite in Z80. It should run quicker.

Code snippet in C: // Line clipping // uint8_t xclipRegion(Point16 * p) { uint8_t code = 0; if(p->y < 0) { // top code |= 1; } else if(p->y > 191) { // bottom code |= 2; } if(p->x > 255) { // right code |= 4; } else if(p->x < 0) { code |= 8; // left } return code; } Code snippet in Z80 assembler: ; extern uint8_t clipRegion(Point16 * p) __z88dk_callee ; PUBLIC _clipRegion, clipRegion _clipRegion: POP BC POP IY ; Pointer to the coordinates PUSH BC ; clipRegion: LD L, 0 ; The return value LD A,(IY+3) ; A: p.y (MSB) RLC A ; Don't use RLCA as that doesn't set the Z flag JR C, clipRegionT ; Off top of the screen JR NZ, clipRegionB ; Off bottom of screen LD A,(IY+2) ; A: p.y (LSB) CP 192 JR C, clipRegionH ; We're in the top 192 lines of the screen, so skip clipRegionB: SET 1,L ; Bottom JR clipRegionH clipRegionT: SET 0,L ; Top ; clipRegionH: LD A,(IY+1) ; A: p.x (MSB) RLC A ; Don't use RLCA as that doesn't set the Z flag RET Z ; We're on screen, so ignore JR C, clipRegionL ; We're off the left of the screen, so skip to that SET 2,L ; Right RET clipRegionL: SET 3,L ; Left RET
aug 27, 2025, 4:26 pm • 11 0

Replies

avatar
Matt Godbolt @matt.godbolt.org

I didn't know it had a posh name. We always called it "outcoding"

aug 27, 2025, 4:44 pm • 3 0 • view
avatar
L Break Into Program @breakintoprogram.co.uk

I may have picked up that terminology from this book.

aug 27, 2025, 5:08 pm • 3 0 • view
avatar
Matt Godbolt @matt.godbolt.org

That book lived on my desk...I guess I hadn't ever put two and two together. (It also started a lifelong fascination with Vermeer)

aug 27, 2025, 6:11 pm • 2 0 • view
avatar
L Break Into Program @breakintoprogram.co.uk

My code clearly has ideas above its station.

aug 27, 2025, 4:54 pm • 1 0 • view
avatar
Matt Godbolt @matt.godbolt.org

Oh lol not at all! Any amount of junk learned by word of mouth at Argonaut in 1996 is probably not canonical hahah

aug 27, 2025, 5:29 pm • 1 0 • view
avatar
L Break Into Program @breakintoprogram.co.uk

Hahaha! I think Argonaut have earned their 3D stripes, so they can pretty much call it what they want.

aug 27, 2025, 5:56 pm • 0 0 • view
avatar
Matt Godbolt @matt.godbolt.org

I guess? Lol. I learned from some super smart people anyway. Lucky me 😊

aug 27, 2025, 6:10 pm • 1 0 • view