home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / compsrcs / misc / volume06 / liss < prev    next >
Encoding:
Internet Message Format  |  1991-08-27  |  6.4 KB

  1. From decwrl!labrea!rutgers!mailrus!csd4.milw.wisc.edu!leah!itsgw!steinmetz!uunet!allbery Sun Jan 29 20:31:06 PST 1989
  2. Article 804 of comp.sources.misc:
  3. Path: granite!decwrl!labrea!rutgers!mailrus!csd4.milw.wisc.edu!leah!itsgw!steinmetz!uunet!allbery
  4. From: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc)
  5. Newsgroups: comp.sources.misc
  6. Subject: v06i014: liss - draw Lissajous figures on a Sun
  7. Message-ID: <47746@uunet.UU.NET>
  8. Date: 29 Jan 89 20:24:15 GMT
  9. Sender: allbery@uunet.UU.NET
  10. Reply-To: jeff@ddsw1.UUCP (Jeff Haferman)
  11. Distribution: na
  12. Organization: ddsw1.MCS.COM, Mundelein, IL
  13. Lines: 207
  14. Approved: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc)
  15.  
  16. Posting-number: Volume 6, Issue 14
  17. Submitted-by: jeff@ddsw1.MCS.COM (Jeff Haferman)
  18. Archive-name: liss
  19.  
  20. [I had to "shar" this.  ++bsa]
  21.  
  22. The following program is something I adapted from an APL program
  23. used as an example by the Professor of a Graphics course I took
  24. last year.  I put it together to begin experimenting with Sunview
  25. programming, so this is a first for me.  There are a couple of 
  26. sloppy things, but as far as I can tell, it should work on all
  27. Suns, though I've only tested it on a 3/50.  Also, it should be
  28. easy to port to any machine (I know a Macintosh port would be 
  29. simple.)
  30.  
  31. Anyway, this is a starting point for me to get more into Sun
  32. programming.  Enjoy.
  33.  
  34. ----------cut here---------cut here-----------cut here-----------cut here----
  35. #! /bin/sh
  36. # This file was wrapped with "dummyshar".  "sh" this file to extract.
  37. # Contents:  liss.c
  38. echo extracting 'liss.c'
  39. if test -f 'liss.c' -a -z "$1"; then echo Not overwriting 'liss.c'; else
  40. sed 's/^X//' << \EOF > 'liss.c'
  41. X/*********************************************************************/
  42. X/*                              liss                                 */
  43. X/*      Purpose : Visual entertainment on a Sun.  Displays a         */
  44. X/*                "Lissajous" figure, which is the connection        */
  45. X/*                of points { cos(2*PI*x*i/n), sin(2*PI*y*i/n) }     */
  46. X/*                for i=0,1,2,...,n and suitably chosen positive     */
  47. X/*                integers x,y, and n.  Must use SunView.            */
  48. X/*                                                                   */
  49. X/*      Org :  Jeff Haferman 12/27/88                                */
  50. X/*             jeff@ddsw1.mcs.com  or ...!oddjob!ddsw1.mcs.com!jeff  */
  51. X/*                                                                   */
  52. X/*      Compile:                                                     */
  53. X/*            cc liss.c -o liss -lsuntool -lsunwindow -lpixrect -lm  */
  54. X/*                                                                   */
  55. X/*      Sample:                                                      */
  56. X/*            liss 100 200 359                                       */
  57. X/*                                                                   */
  58. X/*********************************************************************/
  59. X
  60. X#include <suntool/sunview.h>
  61. X#include <suntool/canvas.h>
  62. X#include <stdio.h>
  63. X#include <math.h>
  64. X
  65. X#define X 1
  66. X#define Y 2
  67. X#define TRUE 1
  68. X#define FALSE 0
  69. X#define MAT(a) ((float *) a)
  70. X
  71. Xchar *progname;
  72. X
  73. Xmain (argc, argv)
  74. Xchar **argv;
  75. Xint argc;
  76. X{
  77. X    Frame frame;
  78. X    Canvas canvas;
  79. X    Pixwin *pw;
  80. X    int x,y,n,i;
  81. X    int plotx, ploty, plotx_last, ploty_last;
  82. X    double dblx, dbly;
  83. X    int first_pass=TRUE;
  84. X    int width, height;
  85. X    static float r2sun[3][3];
  86. X    float *plot, *transform();
  87. X
  88. X    progname=argv[0];
  89. X    if ( argc != 4 )
  90. X        usage();
  91. X
  92. X    x= char2num(argv[1]);
  93. X    y= char2num(argv[2]);
  94. X    n= char2num(argv[3]);
  95. X
  96. X    frame = window_create(NULL, FRAME, 0);
  97. X    canvas = window_create(frame, CANVAS,
  98. X        CANVAS_FIXED_IMAGE, FALSE,
  99. X        0);
  100. X    pw= canvas_pixwin(canvas);
  101. X
  102. X    width = ((int) window_get(canvas, CANVAS_WIDTH))-1;
  103. X    height = ((int) window_get(canvas, CANVAS_HEIGHT))-1;
  104. X    get_transform(width,height,r2sun);
  105. X
  106. X    for (i=0; i<=n; i++) {
  107. X        plotx_last = plotx; 
  108. X        ploty_last = ploty; 
  109. X        dblx = cos ((double)(2*M_PI*x*((float)i/(float)n)));
  110. X          dbly = sin ((double)(2*M_PI*y*((float)i/(float)n)));
  111. X          plot = transform((float)dblx,(float)dbly,r2sun);
  112. X          plotx = (int) plot[X-1];
  113. X          ploty = (int) plot[Y-1];
  114. X        if (!first_pass) 
  115. X            pw_vector(pw,plotx_last,ploty_last,plotx,ploty,PIX_SRC,1);
  116. X        else 
  117. X            first_pass=FALSE;
  118. X    }
  119. X    
  120. X    window_main_loop(frame);
  121. X    exit(0);
  122. X}
  123. X
  124. X
  125. Xget_transform(w,h,tranmat)
  126. X    int w,h;
  127. X    float *tranmat;
  128. X{
  129. Xint i,j;
  130. X    float w0,w1,h0,h1;    
  131. X    static float a[3][3]= { {-1,0,1},
  132. X                             {-1,1,0},
  133. X                             {1,0,0}};
  134. X    static float b[3][3]= { {0,0,1},
  135. X                             {0,0,1},
  136. X                             {0,0,1}};
  137. X    w0=(float)w;
  138. X    w1=w0/2;
  139. X    h0=(float)h;
  140. X    h1=h0/2;
  141. X    
  142. X    b[0][0]=w1;
  143. X    b[0][1]=h1;
  144. X    b[1][0]=w1;
  145. X    b[2][0]=w0;
  146. X    b[2][1]=h1;
  147. X
  148. X    m_mult(a,b,tranmat,3,3,3);
  149. X}
  150. X
  151. X
  152. Xfloat
  153. X*transform(x,y,tranmat)
  154. X    float x,y,*tranmat;
  155. X{
  156. Xint j;
  157. X    static float a[3]={0,0,1};
  158. X    float b[3];
  159. X
  160. X    a[0]=x;
  161. X    a[1]=y;
  162. X    m_mult(a,tranmat,b,1,3,3);
  163. X
  164. X    return(b);
  165. X}
  166. X
  167. X
  168. X/*------------------------------------------------------------*/
  169. X/*                                                            */
  170. X/*   m_mult(A,B,C,m,n,p)                                      */
  171. X/*          A is  an (m x n) matrix of floats                 */
  172. X/*          B is  an (n x p) matrix of floats                 */
  173. X/*          C is  an (m x p) matrix of floats                 */
  174. X/*                                                            */
  175. X/*    A and B must be set before the call.  C is then set     */
  176. X/*    here as the product of A and B.  No checking is done    */
  177. X/*    here on the dimensions of A and B.                      */
  178. X/*                                                            */
  179. X/*------------------------------------------------------------*/
  180. X
  181. Xm_mult(A,B,C,m,n,p)
  182. X    float **A, **B, **C;
  183. X    int    m, n, p;
  184. X
  185. X/* for a good explantion on passing arbitrary sized matrices
  186. X   to functions, see pp. 119-121 in C++                       */
  187. X
  188. X{
  189. X    int i, j, k;
  190. X
  191. X    for(i=0; i<=m-1; i++)
  192. X        for(j=0; j<=p-1; j++) {
  193. X            MAT(C)[i*p+j]=0;
  194. X            for(k=0; k<=n-1; k++)    
  195. X                MAT(C)[i*p+j]+= ( MAT(A)[i*n+k] * MAT(B)[k*p+j] );
  196. X        }
  197. X}
  198. X
  199. Xchar2num(c)
  200. X    char *c;
  201. X{
  202. X    int i;
  203. X
  204. X    for (i = 0; *c >= '0' && *c <= '9'; c++)
  205. X        i *= 10, i += *c - '0';
  206. X    if (*c) 
  207. X        usage();
  208. X    return (i);
  209. X}
  210. X
  211. X
  212. Xusage()
  213. X{
  214. X    fprintf(stderr, "usage: %s \<x\> \<y\> \<n\>\n", progname);
  215. X    fprintf(stderr, "       where x, y, and n are positive integers.");
  216. X    exit(1);
  217. X}
  218. EOF
  219. chars=`wc -c < 'liss.c'`
  220. if test $chars !=     4785; then echo 'liss.c' is $chars characters, should be     4785 characters!; fi
  221. fi
  222. exit 0
  223.  
  224.  
  225.