MORE ABOUT MOIRE by Charles VASSALLO 33 route des Traouieros - 22730 TREGASTEL - France The April issue of Jumpdisk contained a sample of Moire picture generating program, accompanied with an editorial comment regretting that no key was given to change the pattern. Actually, there is no key; practically the whole program must be changed. On the other hand, the program is basically quite simple. The following lines are aimed at giving some understanding of the operation. This kind of pattern calls for a palette of graduated colors and it requires colors of neighbouring pixels to be connected with some law inducing a smooth but rapid enough variation of colors. Which kind of law ? You could imagine recurrence rules determining the color at point (x,y) from colors of preceding points on the same line, i.e. (x-1,y), (x-2,y)... but also on the preceding line (x-1,y-1), (x,y-1), (x+1,y-1) and so on..., somewhat like the rules of 'Life Game'. A simpler method is to connect color changes with the variations of some analytical function F(x,y). If we are working with 10 colors, the simplest way is to take the last digit in F before the decimal point and to consider it as the color number in your working palette. This job could be done with a Basic instruction R% = F MOD 10 but this fails if F goes out of the short integer range (-32768,+32767) and it is safer to write R% = INT(F) - 10*INT(F/10) In other terms, R% is the remainder in the division of F with 10.If you are working with N colors (N <> 10), replace 10 with N. We thus arrive to the general structure of our program: 1 - Definition of the palette 2 - Pattern inscription : FOR y=0 to 195 : FOR x=0 to 311 F = ... instructions for computing F(x,y) R%= INT(F) - N*INT(F/N) PSET (x,y),R%+offset NEXT x : NEXT y That's all ! ('offset' in PSET instruction allows us to place the working palette anywhere in the screen palette, from number 'offset' to 'offset'+N. If offset=2, colors 0 and 1 which are generally used for texts are thus preserved.) The next page presents a simulation of an elementary program and demonstrates the necessity either for the working palette to be 'cyclic' (without any color discontinuity if it is looped on itself) or for the color coding to be cyclic - for instance, for N=10 : R% : 0 1 2 3 4 5 6 7 8 9 color number : 0 1 2 3 4 5 4 3 2 1 (+offset) Don't be afraid ! Things will seem easier after the demonstration. (several demonstrations are proposed hereafter, simulating low-res opera- tion. You can stop any of them by clicking once) The cyclic coding thus allows us to simulate a cyclic palette of N colors, partly duplicated. If N is odd, there are two neighbouring colors in this equivalent cyclic palette which are identical. It is better to take even N. Don't confuse this 'equivalent cyclic palette' (N colors) with the actual working palette (1+N/2 colors). The general structure of the program becomes : 1 - Definition of the palette (not cyclic - N/2+1 graduated colors) 2 - Pattern inscription FOR y=0 to 195 : FOR x=0 TO 311 calculation of F = F(x,y) R% = INT(F) - N*INT(F/N) IF R% > N/2 THEN R% = N - R% (take even N) PSET (x,y),R%+offset NEXT x : NEXT y How can one change the pattern ? The most obvious way is to change the function F. The following page gives some illustrations. Any positive function F(x,y) can be convenient, provided that its calcula- tion is accurate enough for its unit digit to be exact. Personnally, I like to start from equations of known analytical curves like f(x,y)=0 in order to obtain patterns leaning against these curves, as the last example which was built on an elliptical structure. The equation of an ellipse tangent to the four sides of the window is f(x,y) = (2*x/w-1)^2 + (2*y/h-1)^2 -1 = 0 (w = width ; h = heigth) Replacing 1 with 0.8 or 0.6 gives a smaller ellipse. A suitable F is then given with 200*f^2 or 500*f^2 . The choice of the multiplicative constant is a matter of trials and errors. If it is too small, colors do not vary fast enough ; too large, the pattern turns random. Another choices are possible, for instance F = 500*SQR(ABS(f)^3) ... You can also try sine and cosine functions. I made some spiral pictures based on Archimede spiral equation : xc = x-w/2 : yc = y-h/2 t = a*SQR(xc^2 + yc^2) (a=constant connected with the number of turns) f = xc-t/a*COS(t) : F = constant * f^2 SPEED UP THE PROGRAM ! A major problem with Basic is its slowness. Some (limited) improvements can be made by avoiding repeated calculations. For instance, instead of FOR x=0 to 311 : FOR y=0 TO 195 F = x^2 + y^2 coding of color NEXT y : NEXT x write DIM y2&(195) : FOR y=0 TO 195 : y2&(y)=y^2 : NEXT y FOR x=0 TO 311 : x2&=x^2 FOR y=0 TO 195 F = x2& + y2&(y) coding of color NEXT y NEXT x This can be less easy for more complex functions. This also makes the program less readable, but can be quite useful. (our simulations are not pure Basic. An assembler routine computes the colors for a whole line; the Basic program then reads them and writes them on the screen: this only writing process is in itself already slow) COLOR CODING Only a very simple color coding was used in the above examples. It can be complexified in several ways. A first method is to use 2 working palettes, switching from one to the other one according to the sign of some function. For instance, in the above ellipse example (with F=200*f^2), f(x,y) is positive outside the ellipse and negative inside : this can be used to color the inside with red hues and the outside with yellow hues. The following example is based upon the same mechanism. COLOR CODING - 2 Other interesting possibilities consist in using a varying number of colors or a varying color offset, with a slow dependance on x and y. Of course, you must take care not to go out from the working palette.