Post Reply 
(42S) challenge: repeat a character N times
01-15-2018, 12:38 PM
Post: #1
(42S) challenge: repeat a character N times
Suppose the alpha register contains a single character, and you want it duplicated N times, 0<=N<=44. This will come in handy when drawing lines on the DM-42 ;-)

A simple way would be

Code:
>LBL "$N"
 ASTO ST L
 CLA
 X=0?
 GTO 00
>LBL 02
 ARCL ST L
 DSE ST X
 GTO 02
>LBL 00
 Rv
 END

But that loops 44 times for the longest string. Can you do better & still preserve Y and Z?

So, In:
A: char
Z: z
Y: y
X: N

Out:
A: N times char
Y: z
X: y

My current attempt's worst case is about 2.5 times faster than the above, and still reasonably short at 49 bytes.

Cheers, Werner
Find all posts by this user
Quote this message in a reply
01-15-2018, 02:48 PM (This post was last modified: 01-15-2018 02:53 PM by Didier Lachieze.)
Post: #2
RE: (42S) challenge: repeat a character N times
Here is a 22-byte solution. I don't know how much faster it is.

Code:
00 { 22-Byte Prgm }
01▸LBL "$N"
02 X=0?
03 CLA
04 X=0?
05 GTO 00
06 1
07 NEWMAT
08 ATOX
09 +
10 XTOA
11▸LBL 00
12 R↓
13 END
Find all posts by this user
Quote this message in a reply
01-15-2018, 03:13 PM
Post: #3
RE: (42S) challenge: repeat a character N times
(01-15-2018 02:48 PM)Didier Lachieze Wrote:  Here is a 22-byte solution.

Nice, but it has one small bug: when N > 0, it puts one character too many in A, because it appends N characters to the one that's already there.
Find all posts by this user
Quote this message in a reply
01-15-2018, 03:18 PM
Post: #4
RE: (42S) challenge: repeat a character N times
(01-15-2018 03:13 PM)Thomas Okken Wrote:  
(01-15-2018 02:48 PM)Didier Lachieze Wrote:  Here is a 22-byte solution.

Nice, but it has one small bug: when N > 0, it puts one character too many in A, because it appends N characters to the one that's already there.

It works fine because ATOX removes one character from the Alpha register, so we end up we the right number of characters in A.
Find all posts by this user
Quote this message in a reply
01-15-2018, 03:20 PM
Post: #5
RE: (42S) challenge: repeat a character N times
(01-15-2018 03:18 PM)Didier Lachieze Wrote:  
(01-15-2018 03:13 PM)Thomas Okken Wrote:  Nice, but it has one small bug: when N > 0, it puts one character too many in A, because it appends N characters to the one that's already there.

It works fine because ATOX removes one character from the Alpha register, so we end up we the right number of characters in A.

You're right, my apologies! Bug report withdrawn.
Find all posts by this user
Quote this message in a reply
01-15-2018, 03:46 PM
Post: #6
RE: (42S) challenge: repeat a character N times
I KNEW I had to ask someone else. Thanks Didier for a very elegant solution!
Werner
Find all posts by this user
Quote this message in a reply
01-17-2018, 12:19 PM
Post: #7
RE: (42S) challenge: repeat a character N times
Nice solution indeed! Now I don't see how this could help building a line function on the dm42?
Find all posts by this user
Quote this message in a reply
01-17-2018, 12:56 PM
Post: #8
RE: (42S) challenge: repeat a character N times
Try and write a program that will draw a horizontal line of n pixels towards the right from the current position (x0,y0), and offer the option of drawing a single-width line or a double-width line. (I'm drawing sudoku grids ;-)
(I put the arrow at the beginning to make it show up on the menu, so that I can see the difference between drawing left, right, up and down..)

>LBL "\->DRAW"

In:
A: char ("x" for single-width, "integral sign" for double-width etc.)
Z: y0
Y: x0
X: N

Out: draws 'char' from (x0,y0) to (x0+n-1,y0)
Y: y0
X: x0+n

Ideally it should also accept (x0,y0) in Y, and return (x0+n,y0) in X. That is not hard at all in this case.

The 'best' way to draw multiple pixels at a time is (I think - but I may be wrong, as I was in the use of XTOA above) using AGRAPH with as many characters as possible in the Alpha register, up to 44. So, if you want to draw a line of 40 pixels, you need to duplicate the character you want to draw 40 times.

Cheers, Werner
Find all posts by this user
Quote this message in a reply
01-17-2018, 01:32 PM
Post: #9
RE: (42S) challenge: repeat a character N times
(01-17-2018 12:56 PM)Werner Wrote:  The 'best' way to draw multiple pixels at a time is (I think - but I may be wrong, as I was in the use of XTOA above) using AGRAPH with as many characters as possible in the Alpha register, up to 44.

The only other way to draw multiple pixels at a time is by passing a complex matrix to PIXEL, but it is not obvious how to populate that matrix efficiently.

Of course this should be addressed by SwissMicros. With that 400x240 pixel screen just screaming for graphics, the DM42 needs some decent drawing primitives. LINE would be a good start. Smile
Find all posts by this user
Quote this message in a reply
01-17-2018, 01:54 PM
Post: #10
RE: (42S) challenge: repeat a character N times
(01-17-2018 12:56 PM)Werner Wrote:  The 'best' way to draw multiple pixels at a time is (I think - but I may be wrong, as I was in the use of XTOA above) using AGRAPH with as many characters as possible in the Alpha register, up to 44. So, if you want to draw a line of 40 pixels, you need to duplicate the character you want to draw 40 times.

Cheers, Werner

I agree for some applications. AGRAPH is best if you are drawing any shape bigger than a 1x1 pixel over and over again including line segments. I think the longest horizontal line you can draw is 44 pixels long with AGRAPH because each character in the alpha register is an encoded column.

PIXEL's secondary use is to draw straight lines. In this case, you don't want a horizontal line to the edges of the display , but PIXEL's line drawing is quite efficient.

Now, I found that fiddling with XTOA and the question of looping might be over-complicating things if the objects to be drawn are known ahead of time and there are only a couple of discreet options. It might be simpler to recall the pre-stored alpha string you need based on what needs to be drawn. But if you need a continuum of possible shapes/lengths, looping XTOA makes sense.

And, I'll echo Thomas' point. Drawing one pixel at a time becomes very intensive at 400x240.
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)