Graphics utilities: H/V lines, grids and tiles

Contributions to this software library are always welcome. Please ensure that you post program listings rather than .raw files. They give a reasonable idea of what your program does without having to load them into a DM42 and you can also include comments in your code. Check out the following link for a decoder/encoder: https://technical.swissmicros.com/decoders/dm42/

You can then copy/paste the listing and post it in "code" tags.
Post Reply
whuyse
Posts: 197
Joined: Thu Dec 21, 2017 1:23 pm

Graphics utilities: H/V lines, grids and tiles

Post by whuyse »

0. Abstract

My ultimate goal is to draw a chess board. To achieve this in a modular
fashion, we'll need tools to draw lines horizontally and vertically, use these
to draw a grid, and then be able to fill half of the grid cells with a shading
pattern. So, without further ado:

1. HLINE

Code: Select all

	X	Y	Z	T
In:	n	x0	y0
	n	(x0,y0)
	n	[(x0,y0) (x1,y1) .. ]
Out:	endpoint = start+n-1 n>0
                 = start     n=0
                 = start+n+1 n<0
HLINE draws a horizontal line of n points.
If n is positive, it draws to the right, else to the left of the starting point.
It will accept either two reals, a complex number, or a matrix of complex
numbers and return the endpoint in the same format as it was entered.

The routine is mode-independent, does not use subroutines, and only 3 stack
levels in case the coordinates are complex numbers or a complex matrix.
Works on all flavours of the 42, even a real one.

HLIND draws a double-width line (one pixel more downward)

2. VLINE

Code: Select all

	X	Y	Z	T
In:	n	x0	y0
	n	(x0,y0)
	n	[(x0,y0) (x1,y1) .. ]
Out:	endpoint = start+n-1 n>0
                 = start     n=0
                 = start+n+1 n<0
VLINE draws a vertical line of n points.
If n is positive, it draws downward, else upward.
It will accept either two reals, a complex number, or a matrix of complex
numbers and returns the endpoint in the same format as it was entered.
The routine depends on RECT mode and uses only the stack.

VLIND draws a double-width line (one pixel more to the right)


3. GRID

Code: Select all

	X	Y	Z	T
In:	[R]	[C]
[R] is the real matrix of x-coefficients to draw the vertical lines
[C] is the real matrix of y-coefficients to draw the horizontal lines

We may draw a rectangular grid defined by a row and column matrix of coefficients.
The dimensions of the matrix do not really matter, only the number of elements.
To draw a square grid, simply enter the same matrix twice with ENTER.

The usage is best illustrated with an example (that will work on a real 42S as well):

Code: Select all

  Y: [ 1 6 11 16 ]
  X: [ 1 27 53 79 105 131 ]
  XEQ "GRID"
A Sudoku grid (on the DM42, GrMod=2):

Code: Select all

  X: [ 4 5 17 29 41 42 54 66 78 79 91 103 115 116 ]
  ENTER
  XEQ "GRID"
Sudoku.bmp
Sudoku.bmp (12.31 KiB) Viewed 6244 times
4. FILL (tiling)

Code: Select all

	X	Y	Z	T	Alpha
In:	(x1,y1)	(x0,y0)			pattern
FILL will fill the box defined by (x0,y0) and (x1,y1) with the pattern as specified in
the Alpha register.
It will determine the height of the pattern, and the size of the box, and execute FILLh:

Code: Select all

	X	Y	Z	T	Alpha
 			[[][]]
In:	h	(dx,dy)	(x0,y0)		pattern
FILLh accepts the upper left corner of the box (x0,y0) in Z, its dimensions (dx,dy) in Y
and the height of the pattern h in x - and the pattern itself in Alpha.
Contrary to FILL, FILLh will accept a Nx1 matrix of coefficients in Z, all with the same
dimensions (dx,dy), and fill all the boxes.

Again, FILL and FILLh are entirely stack-based. RECT mode is needed, and assumed.

FILLh uses two auxiliary routines M0N and Mx+:

M0N
n XEQ "M0N" will create the 1xn matrix [[ 0 1 .. n-1 ]]

Mx+
Problem: combine an nx1 and 1xm matrix to produce an nxm matrix,
but instead of multiplying each combination of the elements, add them instead.
This occurs frequently when trying to create a grid of coordinates, so assume
the entries are all integers < 1000.

eg. to combine

Code: Select all

  [[ 1 ] and [[ 4 5 ]]
   [ 2 ]
   [ 3 ]]
into

Code: Select all

  [[ (1,4) (1,5) ]
   [ (2,4) (2,5) ]
   [ (3,4) (3,5) ]]
do

Code: Select all

  [[ 1 ][ 2 ][ 3 ]]	( 3x1 matrix )
  [[ 4 5 ]]		( 1x2 matrix )
   FP			( multiply by i )
   LASTX
   COMPLEX		( [[ (0,4) (0,5) ]] )
   XEQ "Mx+" 

5. CHESS

Only the board, I'm afraid. So, bringing it all together, this routine draws a chess board in GrMod=2
(the shading is actually done in GrMod=3). It leaves GrMod=3 upon return.
Chess.bmp
Chess.bmp (12.31 KiB) Viewed 6244 times
All routines are combined into a single file for easy download to the DM42, in attachment as a .raw file, or plain source here:

Code: Select all

00 { 127-Byte Prgm }
01▶LBL "HLIND"
02 "∫"
03 GTO 00
04▶LBL "HLINE"
05 "×"
06▶LBL 00
07 SIGN
08 X<0?
09 STO- ST Y
10 X<> ST L
11 44
12 X<Y?
13 GTO 00
14 +/-
15 X≤Y?
16 GTO 01
17▶LBL 00
18 ASTO ST L
19 ARCL ST L
20 ASTO ST L
21 ARCL ST L
22 ASTO ST L
23 ARCL ST L
24 ASTO ST L
25 ARCL ST L
26 ARCL ST L
27 ARCL ST L
28 ARCL ST L
29 ARCL ST L
30 ARCL ST L
31 X<0?
32 GTO 03
33▶LBL 02
34 -
35 R↓
36 AGRAPH
37 R^
38 LASTX
39 STO+ ST Z
40 X<Y?
41 GTO 02
42 GTO 01
43▶LBL 03
44 STO+ ST Z
45 -
46 R↓
47 AGRAPH
48 R^
49 LASTX
50 X>Y?
51 GTO 03
52▶LBL 01
53 R↓
54 X=0?
55 GTO 00
56 X<0?
57 STO+ ST Y
58 SIGN
59 LASTX
60 NEWMAT
61 ATOX
62 STO+ ST Y
63 R↓
64 CLA
65 XTOA
66 R↓
67 AGRAPH
68 LASTX
69 ENTER
70 X>0?
71 SIGN
72 -
73▶LBL 00
74 +
75 END

00 { 159-Byte Prgm }
01▶LBL "VLIND"
02 AON
03▶LBL "VLINE"
04 255
05 CLA
06 XTOA
07 FS? 48
08 XTOA
09 R↓
10 STO ST T
11 R↓
12 AOFF
13 REAL?
14 GTO 00
15 COMPLEX
16 X<>Y
17 AON
18▶LBL 00
19 X<>Y
20 R^
21 X<0?
22 GTO 01
23 8
24 X≥Y?
25 GTO 00
26▶LBL 02
27 R↓
28 R↓
29 COMPLEX
30 AGRAPH
31 COMPLEX
32 R↓
33 R↓
34 STO+ ST Z
35 STO- ST Y
36 X<Y?
37 GTO 02
38 GTO 00
39▶LBL 01
40 SIGN
41 STO- ST Y
42 X<> ST L
43 -8
44 X≤Y?
45 GTO 00
46▶LBL 03
47 STO+ ST Z
48 R↓
49 R↓
50 COMPLEX
51 AGRAPH
52 COMPLEX
53 R↓
54 R↓
55 STO- ST Y
56 X>Y?
57 GTO 03
58▶LBL 00
59 R↓
60 X=0?
61 GTO 00
62 2
63 1/X
64 X<Y?
65 1/X
66 X<>Y
67 Y^X
68 DSE ST X
69 ALENG
70 X<>Y
71 CLA
72 XTOA
73 DSE ST Y
74 XTOA
75 R↓
76 R↓
77 LASTX
78 X<0?
79 STO+ ST Y
80 R↓
81 COMPLEX
82 AGRAPH
83 COMPLEX
84 R^
85 ENTER
86 SIGN
87 -
88 X>0?
89 STO+ ST Y
90▶LBL 00
91 R↓
92 X<>Y
93 FC? 48
94 RTN
95 X<>Y
96 COMPLEX
97 AOFF
98 END

00 { 62-Byte Prgm }
01▶LBL "GRID"
02 CLLCD
03 XEQ 14
04 COMPLEX
05 X<>Y
06 XEQ "HLINE"
07 COMPLEX
08 X<>Y
09 R↓
10 XEQ 14
11 X<>Y
12 COMPLEX
13 X<>Y
14 XEQ "VLINE"
15 RTN
16▶LBL 14
17 EDIT
18 SIGN
19 LASTX
20 ←
21 STO+ ST Y
22 EXITALL
23 X<>Y
24 LASTX
25 STO- ST Y
26 R^
27 STO+ ST Y
28 STO- ST Y
29 END

00 { 25-Byte Prgm }
01▶LBL "M0N"
02 SIGN
03 LASTX
04 NEWMAT
05 EDIT
06 DSE ST L
07▶LBL 02
08 ←
09 LASTX
10 DSE ST L
11 GTO 02
12 EXITALL
13 END

00 { 23-Byte Prgm }
01▶LBL "M×+"
02 9
03 1/X
04 1/X
05 FP
06 1/X
07 STO+ ST Y
08 STO+ ST Z
09 ÷
10 STO× ST Y
11 X<> ST L
12 -
13 END

00 { 95-Byte Prgm }
01▶LBL "FILL"
02 1
03 ENTER
04 COMPLEX
05 +
06 RCL- ST Y
07 ALENG
08 ABS
09 CLX
10▶LBL 02
11 ATOX
12 XTOA
13 X>Y?
14 X<>Y
15 R↓
16 DSE ST L
17 GTO 02
18 LOG
19 2
20 LOG
21 ÷
22 IP
23 1
24 +
25▶LBL "FILLh"
26 X<>Y
27 COMPLEX
28 R↓
29 X<>Y
30 STO÷ ST T
31 COMPLEX
32 R^
33 XEQ "M0N"
34 X<>Y
35 COMPLEX
36 X<>Y
37 R↓
38 ×
39 FP
40 LASTX
41 COMPLEX
42 XEQ "M×+"
43 X<>Y
44▶LBL 04
45 X<>Y
46 AGRAPH
47 X<>Y
48 ALENG
49 STO+ ST Z
50 -
51 X>0?
52 GTO 04
53 END

00 { 172-Byte Prgm }
01▶LBL "CHESS"
02 2
03 XEQ 14
04 CLLCD
05 9
06 XEQ "M0N"
07 13
08 ×
09 5
10 +
11 TRANS
12 EDIT
13 ENTER
14 INSR
15 SIGN
16 -
17 ←
18 GROW
19 ENTER
20 →
21 SIGN
22 +
23 EXITALL
24 ENTER
25 XEQ "GRID"
26 3
27 XEQ 14
28 CLA
29 85
30 XTOA
31 STO+ ST X
32 XTOA
33 ASTO ST X
34 ARCL ST X
35 ARCL ST X
36 ASTO ST X
37 ARCL ST X
38 ARCL ST X
39 ARCL ST X
40 4
41 XEQ "M0N"
42 52
43 ×
44 RCL ST X
45 11
46 +
47 TRANS
48 X<>Y
49 37
50 +
51 FP
52 LASTX
53 COMPLEX
54 XEQ "M×+"
55 STO "."
56 16
57 1
58 DIM "."
59 RCL "."
60 XEQ 13
61 RCL "."
62 CLV "."
63 COMPLEX
64 X<>Y
65 COMPLEX
66▶LBL 13
67 24
68 ENTER
69 COMPLEX
70 8
71 XEQ "FILLh"
72 RTN
73▶LBL 14
74 STO "GrMod"
75 END
Comments etc. welcome!
Cheers, Werner
Attachments
grid.raw
(684 Bytes) Downloaded 270 times
41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE, DM15L
svarre69
Posts: 15
Joined: Tue Oct 01, 2019 2:09 pm

Re: Graphics utilities: H/V lines, grids and tiles

Post by svarre69 »

Hello Werner

Trying to encode the program throws this error: "Invalid number format in line 01"

I found out, that it is the character, that is between the line number and the instruction that has to be deleted.

Everything is ok now. :)

With kind regards
Peter
whuyse
Posts: 197
Joined: Thu Dec 21, 2017 1:23 pm

Re: Graphics utilities: H/V lines, grids and tiles

Post by whuyse »

It’s a printout from the Dm42 itself, not compatible with the program import. To import, use the .raw file.
Cheers, Werner
41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE, DM15L
Vitasam

Re: Graphics utilities: H/V lines, grids and tiles

Post by Vitasam »

Great job!

It will be nice to have a kind of "DM42-extension" for HP42s language - graphics primitives.
Post Reply