1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* crystal --- polygons moving according to plane group rules */
5 static const char sccsid[] = "@(#)crystal.c 4.12 98/09/10 xlockmore";
9 * Copyright (c) 1997 by Jouk Jansen <joukj@crys.chem.uva.nl>
11 * Permission to use, copy, modify, and distribute this software and its
12 * documentation for any purpose and without fee is hereby granted,
13 * provided that the above copyright notice appear in all copies and that
14 * both that copyright notice and this permission notice appear in
15 * supporting documentation.
17 * This file is provided AS IS with no warranties of any kind. The author
18 * shall have no liability with respect to the infringement of copyrights,
19 * trade secrets or any patents by this file or any part thereof. In no
20 * event will the author be liable for any lost revenue or profits or
21 * other special, indirect and consequential damages.
23 * The author should like to be notified if changes have been made to the
24 * routine. Response will only be guaranteed when a VMS version of the
25 * program is available.
27 * A moving polygon-mode. The polygons obey 2D-planegroup symmetry.
29 * The groupings of the cells fall in 3 categories:
30 * oblique groups 1 and 2 where the angle gamma ranges from 60 to 120 degrees
31 * square groups 3 through 11 where the angle gamma is 90 degrees
32 * hexagonal groups 12 through 17 where the angle gamma is 120 degrees
35 * 03-Dec-98: Random inversion of y-axis included to simulate hexagonal groups
36 * with an angle of 60 degrees.
37 * 10-Sep-98: new colour scheme
38 * 24-Feb-98: added option centre which turns on/off forcing the centre of
39 * the screen to be used
40 * added option maxsize which forces the dimensions to be chasen
41 * in such ua way that the largest possible part of the screen is
43 * When only one unit cell is drawn, it is chosen at random
44 * 18-Feb-98: added support for negative numbers with -nx and -ny meaning
45 * "random" choice with given maximum
46 * added +/-grid option. If -cell is specified this option
47 * determines if one or all unit cells are drawn.
48 * -batchcount is now a parameter for all the objects on the screen
49 * instead of the number of "unique" objects
50 * The maximum size of the objects now scales with the part
52 * fixed "size" problem. Now very small non-vissable objects
54 * 13-Feb-98: randomized the unit cell size
55 * runtime options -/+cell (turn on/off unit cell drawing)
56 * -nx num (number of translational symmetries in x-direction
57 * -ny num (idem y-direction but ignored for square and
58 * hexagonal space groups
59 * i.e. try xlock -mode crystal -nx 3 -ny 2
60 * Fullrandom overrules the -/+cell option.
61 * 05-Feb-98: Revision + bug repairs
63 * use part of the screen for unit cell
64 * in hexagonal and square groups a&b axis forced to be equal
65 * cell angle for oblique groups randomly chosen between 60 and 120
66 * bugs solved: planegroups with cell angles <> 90.0 now work properly
67 * 19-Sep-97: Added remaining hexagonal groups
72 # define DEFAULTS "*delay: 60000 \n" \
77 "*fpsSolid: true \n" \
79 # define crystal_handle_event 0
80 # include "xlockmore.h" /* in xscreensaver distribution */
81 #else /* STANDALONE */
82 # include "xlock.h" /* in xlockmore distribution */
84 #endif /* STANDALONE */
86 #define DEF_CELL "True" /* Draw unit cell */
87 #define DEF_GRID "False" /* Draw unit all cell if DEF_CELL is True */
88 #define DEF_NX "-3" /* number of unit cells in x-direction */
89 #define DEF_NX1 1 /* number of unit cells in x-direction */
90 #define DEF_NY "-3" /* number of unit cells in y-direction */
91 #define DEF_NY1 1 /* number of unit cells in y-direction */
92 #define DEF_CENTRE "False"
93 #define DEF_MAXSIZE "False"
94 #define DEF_CYCLE "True"
97 #define NRAND(n) ( (n) ? (int) (LRAND() % (n)) : 0)
99 #define min(a,b) ((a) <= (b) ? (a) : (b))
103 static Bool unit_cell, grid_cell, centre, maxsize, cycle_p;
105 static XrmOptionDescRec opts[] =
107 {"-nx", "crystal.nx", XrmoptionSepArg, 0},
108 {"-ny", "crystal.ny", XrmoptionSepArg, 0},
109 {"-centre", ".crystal.centre", XrmoptionNoArg, "on"},
110 {"+centre", ".crystal.centre", XrmoptionNoArg, "off"},
111 {"-maxsize", ".crystal.maxsize", XrmoptionNoArg, "on"},
112 {"+maxsize", ".crystal.maxsize", XrmoptionNoArg, "off"},
113 {"-cell", ".crystal.cell", XrmoptionNoArg, "on"},
114 {"+cell", ".crystal.cell", XrmoptionNoArg, "off"},
115 {"-grid", ".crystal.grid", XrmoptionNoArg, "on"},
116 {"+grid", ".crystal.grid", XrmoptionNoArg, "off"},
117 {"-shift", ".crystal.shift", XrmoptionNoArg, "on"},
118 {"+shift", ".crystal.shift", XrmoptionNoArg, "off"}
121 static argtype vars[] =
123 {&nx, "nx", "nx", DEF_NX, t_Int},
124 {&ny, "ny", "ny", DEF_NY, t_Int},
125 {¢re, "centre", "Centre", DEF_CENTRE, t_Bool},
126 {&maxsize, "maxsize", "Maxsize", DEF_MAXSIZE, t_Bool},
127 {&unit_cell, "cell", "Cell", DEF_CELL, t_Bool},
128 {&grid_cell, "grid", "Grid", DEF_GRID, t_Bool},
129 {&cycle_p, "shift", "Shift", DEF_CYCLE, t_Bool}
131 static OptionStruct desc[] =
133 {"-nx num", "Number of unit cells in x-direction"},
134 {"-ny num", "Number of unit cells in y-direction"},
135 {"-/+centre", "turn on/off centering on screen"},
136 {"-/+maxsize", "turn on/off use of maximum part of screen"},
137 {"-/+cell", "turn on/off drawing of unit cell"},
138 {"-/+grid", "turn on/off drawing of grid of unit cells (if -cell is on)"},
139 {"-/+shift", "turn on/off colour cycling"}
142 ENTRYPOINT ModeSpecOpt crystal_opts =
143 {sizeof opts / sizeof opts[0], opts, sizeof vars / sizeof vars[0], vars, desc};
146 ModStruct crystal_description =
147 {"crystal", "init_crystal", "draw_crystal", "release_crystal",
148 "refresh_crystal", "init_crystal", NULL, &crystal_opts,
149 60000, -40, 200, -15, 64, 1.0, "",
150 "Shows polygons in 2D plane groups", 0, NULL};
154 #define DEF_NUM_ATOM 10
156 #define DEF_SIZ_ATOM 10
158 #define PI_RAD (M_PI / 180.0)
160 static Bool centro[17] =
181 static Bool primitive[17] =
202 static short numops[34] =
223 static short operation[114] =
247 unsigned long colour;
248 int x0, y0, velocity[2];
249 float angle, velocity_a;
250 int num_point, at_type, size_at;
256 int win_width, win_height, num_atom;
257 int planegroup, a, b, offset_w, offset_h, nx, ny;
261 Bool unit_cell, grid_cell;
265 Bool cycle_p, mono_p, no_colors;
266 unsigned long blackpixel, whitepixel, fg, bg;
267 int direction, invert;
270 static crystalstruct *crystals = NULL;
273 trans_coor(XPoint * xyp, XPoint * new_xyp, int num_points,
278 for (i = 0; i <= num_points; i++) {
279 new_xyp[i].x = xyp[i].x +
280 (int) (xyp[i].y * sin((gamma - 90.0) * PI_RAD));
281 new_xyp[i].y = (int) (xyp[i].y / cos((gamma - 90.0) * PI_RAD));
286 trans_coor_back(XPoint * xyp, XPoint * new_xyp,
287 int num_points, float gamma, int offset_w, int offset_h ,
288 int winheight , int invert )
292 for (i = 0; i <= num_points; i++) {
293 new_xyp[i].y = (int) (xyp[i].y * cos((gamma - 90) * PI_RAD)) +
295 new_xyp[i].x = xyp[i].x - (int) (xyp[i].y * sin((gamma - 90.0)
296 * PI_RAD)) + offset_w;
297 if ( invert ) new_xyp[i].y = winheight - new_xyp[i].y;
302 crystal_setupatom(crystalatom * atom0, float gamma)
307 y0 = (int) (atom0->y0 * cos((gamma - 90) * PI_RAD));
308 x0 = atom0->x0 - (int) (atom0->y0 * sin((gamma - 90.0) * PI_RAD));
309 switch (atom0->at_type) {
310 case 0: /* rectangles */
311 xy[0].x = x0 + (int) (2 * atom0->size_at *
313 (int) (atom0->size_at * sin(atom0->angle));
314 xy[0].y = y0 + (int) (atom0->size_at *
316 (int) (2 * atom0->size_at * sin(atom0->angle));
317 xy[1].x = x0 + (int) (2 * atom0->size_at *
319 (int) (atom0->size_at * sin(atom0->angle));
320 xy[1].y = y0 - (int) (atom0->size_at *
322 (int) (2 * atom0->size_at * sin(atom0->angle));
323 xy[2].x = x0 - (int) (2 * atom0->size_at *
325 (int) (atom0->size_at * sin(atom0->angle));
326 xy[2].y = y0 - (int) (atom0->size_at *
328 (int) (2 * atom0->size_at * sin(atom0->angle));
329 xy[3].x = x0 - (int) (2 * atom0->size_at *
331 (int) (atom0->size_at * sin(atom0->angle));
332 xy[3].y = y0 + (int) (atom0->size_at *
334 (int) (2 * atom0->size_at *
338 trans_coor(xy, atom0->xy, 4, gamma);
340 case 1: /* squares */
341 xy[0].x = x0 + (int) (1.5 * atom0->size_at *
343 (int) (1.5 * atom0->size_at *
345 xy[0].y = y0 + (int) (1.5 * atom0->size_at *
347 (int) (1.5 * atom0->size_at *
349 xy[1].x = x0 + (int) (1.5 * atom0->size_at *
351 (int) (1.5 * atom0->size_at *
353 xy[1].y = y0 - (int) (1.5 * atom0->size_at *
355 (int) (1.5 * atom0->size_at *
357 xy[2].x = x0 - (int) (1.5 * atom0->size_at *
359 (int) (1.5 * atom0->size_at *
361 xy[2].y = y0 - (int) (1.5 * atom0->size_at *
363 (int) (1.5 * atom0->size_at *
365 xy[3].x = x0 - (int) (1.5 * atom0->size_at *
367 (int) (1.5 * atom0->size_at *
369 xy[3].y = y0 + (int) (1.5 * atom0->size_at *
371 (int) (1.5 * atom0->size_at *
375 trans_coor(xy, atom0->xy, 4, gamma);
377 case 2: /* triangles */
378 xy[0].x = x0 + (int) (1.5 * atom0->size_at *
380 xy[0].y = y0 + (int) (1.5 * atom0->size_at *
382 xy[1].x = x0 + (int) (1.5 * atom0->size_at *
384 (int) (1.5 * atom0->size_at *
386 xy[1].y = y0 - (int) (1.5 * atom0->size_at *
388 (int) (1.5 * atom0->size_at *
390 xy[2].x = x0 - (int) (1.5 * atom0->size_at *
392 (int) (1.5 * atom0->size_at *
394 xy[2].y = y0 - (int) (1.5 * atom0->size_at *
396 (int) (1.5 * atom0->size_at *
400 trans_coor(xy, atom0->xy, 3, gamma);
406 crystal_drawatom(ModeInfo * mi, crystalatom * atom0)
408 crystalstruct *cryst;
409 Display *display = MI_DISPLAY(mi);
410 Window window = MI_WINDOW(mi);
413 cryst = &crystals[MI_SCREEN(mi)];
414 for (j = numops[2 * cryst->planegroup + 1];
415 j < numops[2 * cryst->planegroup]; j++) {
416 XPoint xy[5], new_xy[5];
420 xtrans = operation[j * 6] * atom0->x0 + operation[j * 6 + 1] *
421 atom0->y0 + (int) (operation[j * 6 + 4] * cryst->a /
423 ytrans = operation[j * 6 + 2] * atom0->x0 + operation[j * 6 +
424 3] * atom0->y0 + (int) (operation[j * 6 + 5] *
427 if (xtrans < -cryst->a)
428 xtrans = 2 * cryst->a;
431 } else if (xtrans >= cryst->a)
437 else if (ytrans >= cryst->b)
441 for (k = 0; k < atom0->num_point; k++) {
442 xy[k].x = operation[j * 6] * atom0->xy[k].x +
443 operation[j * 6 + 1] *
444 atom0->xy[k].y + (int) (operation[j * 6 + 4] *
447 xy[k].y = operation[j * 6 + 2] * atom0->xy[k].x +
448 operation[j * 6 + 3] *
449 atom0->xy[k].y + (int) (operation[j * 6 + 5] *
453 xy[atom0->num_point].x = xy[0].x;
454 xy[atom0->num_point].y = xy[0].y;
455 for (l = 0; l < cryst->nx; l++) {
456 for (m = 0; m < cryst->ny; m++) {
458 for (k = 0; k <= atom0->num_point; k++) {
459 xy_1[k].x = xy[k].x + l * cryst->a;
460 xy_1[k].y = xy[k].y + m * cryst->b;
462 trans_coor_back(xy_1, new_xy, atom0->num_point,
463 cryst->gamma, cryst->offset_w,
467 XFillPolygon(display, window, cryst->gc, new_xy,
468 atom0->num_point, Convex, CoordModeOrigin);
471 if (centro[cryst->planegroup] == True) {
472 for (k = 0; k <= atom0->num_point; k++) {
473 xy[k].x = cryst->a - xy[k].x;
474 xy[k].y = cryst->b - xy[k].y;
476 for (l = 0; l < cryst->nx; l++) {
477 for (m = 0; m < cryst->ny; m++) {
479 for (k = 0; k <= atom0->num_point; k++) {
480 xy_1[k].x = xy[k].x + l * cryst->a;
481 xy_1[k].y = xy[k].y + m * cryst->b;
483 trans_coor_back(xy_1, new_xy, atom0->num_point,
489 XFillPolygon(display, window, cryst->gc,
491 atom0->num_point, Convex,
496 if (primitive[cryst->planegroup] == False) {
497 if (xy[atom0->num_point].x >= (int) (cryst->a / 2.0))
498 xtrans = (int) (-cryst->a / 2.0);
500 xtrans = (int) (cryst->a / 2.0);
501 if (xy[atom0->num_point].y >= (int) (cryst->b / 2.0))
502 ytrans = (int) (-cryst->b / 2.0);
504 ytrans = (int) (cryst->b / 2.0);
505 for (k = 0; k <= atom0->num_point; k++) {
506 xy[k].x = xy[k].x + xtrans;
507 xy[k].y = xy[k].y + ytrans;
509 for (l = 0; l < cryst->nx; l++) {
510 for (m = 0; m < cryst->ny; m++) {
512 for (k = 0; k <= atom0->num_point; k++) {
513 xy_1[k].x = xy[k].x + l * cryst->a;
514 xy_1[k].y = xy[k].y + m * cryst->b;
516 trans_coor_back(xy_1, new_xy, atom0->num_point,
522 XFillPolygon(display, window, cryst->gc,
524 atom0->num_point, Convex,
528 if (centro[cryst->planegroup] == True) {
531 for (k = 0; k <= atom0->num_point; k++) {
532 xy1[k].x = cryst->a - xy[k].x;
533 xy1[k].y = cryst->b - xy[k].y;
535 for (l = 0; l < cryst->nx; l++) {
536 for (m = 0; m < cryst->ny; m++) {
538 for (k = 0; k <= atom0->num_point; k++) {
539 xy_1[k].x = xy1[k].x + l * cryst->a;
540 xy_1[k].y = xy1[k].y + m * cryst->b;
542 trans_coor_back(xy_1, new_xy, atom0->num_point,
548 XFillPolygon(display, window,
550 new_xy, atom0->num_point,
551 Convex, CoordModeOrigin);
559 ENTRYPOINT void init_crystal(ModeInfo * mi);
560 ENTRYPOINT void release_crystal(ModeInfo * mi);
564 draw_crystal(ModeInfo * mi)
566 Display *display = MI_DISPLAY(mi);
567 crystalstruct *cryst = &crystals[MI_SCREEN(mi)];
570 #ifdef HAVE_COCOA /* Don't second-guess Quartz's double-buffering */
571 XClearWindow(MI_DISPLAY(mi), MI_WINDOW(mi));
574 if (cryst->no_colors) {
579 cryst->painted = True;
580 MI_IS_DRAWN(mi) = True;
581 XSetFunction(display, cryst->gc, GXxor);
584 if (cryst->cycle_p) {
585 rotate_colors(display, cryst->cmap, cryst->colors, cryst->ncolors,
587 if (!(LRAND() % 1000))
588 cryst->direction = -cryst->direction;
590 for (i = 0; i < cryst->num_atom; i++) {
593 atom0 = &cryst->atom[i];
595 if (MI_IS_INSTALL(mi) && MI_NPIXELS(mi) > 2) {
596 XSetForeground(display, cryst->gc, cryst->colors[atom0->colour].pixel);
598 XSetForeground(display, cryst->gc, atom0->colour);
600 crystal_drawatom(mi, atom0);
601 atom0->velocity[0] += NRAND(3) - 1;
602 atom0->velocity[0] = MAX(-20, MIN(20, atom0->velocity[0]));
603 atom0->velocity[1] += NRAND(3) - 1;
604 atom0->velocity[1] = MAX(-20, MIN(20, atom0->velocity[1]));
605 atom0->x0 += atom0->velocity[0];
606 /*if (cryst->gamma == 90.0) { */
608 atom0->x0 += cryst->a;
609 else if (atom0->x0 >= cryst->a)
610 atom0->x0 -= cryst->a;
611 atom0->y0 += atom0->velocity[1];
613 atom0->y0 += cryst->b;
614 else if (atom0->y0 >= cryst->b)
615 atom0->y0 -= cryst->b;
617 atom0->velocity_a += ((float) NRAND(1001) - 500.0) / 2000.0;
618 atom0->angle += atom0->velocity_a;
619 crystal_setupatom(atom0, cryst->gamma);
620 crystal_drawatom(mi, atom0);
622 XSetFunction(display, cryst->gc, GXcopy);
626 refresh_crystal(ModeInfo * mi)
628 Display *display = MI_DISPLAY(mi);
629 Window window = MI_WINDOW(mi);
630 crystalstruct *cryst = &crystals[MI_SCREEN(mi)];
636 XSetFunction(display, cryst->gc, GXxor);
638 if (cryst->unit_cell) {
639 int y_coor1 , y_coor2;
641 if (MI_NPIXELS(mi) > 2)
642 XSetForeground(display, cryst->gc, MI_PIXEL(mi, NRAND(MI_NPIXELS(mi))));
644 XSetForeground(display, cryst->gc, MI_WHITE_PIXEL(mi));
645 if (cryst->grid_cell) {
649 y_coor1 = y_coor2 = cryst->win_height - cryst->offset_h;
651 y_coor1 = y_coor2 = cryst->offset_h;
652 XDrawLine(display, window, cryst->gc, cryst->offset_w,
653 y_coor1, cryst->offset_w + cryst->nx * cryst->a,
657 y_coor1 = cryst->win_height - cryst->offset_h;
658 y_coor2 = cryst->win_height - (int) (cryst->ny *
660 cos((cryst->gamma - 90) * PI_RAD)) -
665 y_coor1 = cryst->offset_h;
666 y_coor2 = (int) (cryst->ny * cryst->b *
667 cos((cryst->gamma - 90) * PI_RAD)) +
670 XDrawLine(display, window, cryst->gc, cryst->offset_w,
671 y_coor1, (int) (cryst->offset_w - cryst->ny * cryst->b *
672 sin((cryst->gamma - 90) * PI_RAD)),
675 for (iny = 1; iny <= cryst->ny; iny++) {
678 y_coor1 = cryst->win_height -
679 (int) (iny * cryst->b * cos((cryst->gamma - 90) *
680 PI_RAD)) - cryst->offset_h;
681 y_coor2 = cryst->win_height -
682 (int) (iny * cryst->b * cos((cryst->gamma - 90) *
688 y_coor1 = (int) (iny * cryst->b * cos((cryst->gamma - 90) *
689 PI_RAD)) + cryst->offset_h;
690 y_coor2 = (int) (iny * cryst->b * cos((cryst->gamma - 90) * PI_RAD)) +
693 XDrawLine(display, window, cryst->gc,
694 (int) (cryst->offset_w +
695 inx * cryst->a - (int) (iny * cryst->b *
696 sin((cryst->gamma - 90) * PI_RAD))),
698 (int) (cryst->offset_w - iny * cryst->b *
699 sin((cryst->gamma - 90) * PI_RAD)),
703 for (inx = 1; inx <= cryst->nx; inx++) {
706 y_coor1 =cryst->win_height -
707 (int) (iny * cryst->b *
708 cos((cryst->gamma - 90) *
709 PI_RAD)) - cryst->offset_h;
710 y_coor2 =cryst->win_height - cryst->offset_h;
714 y_coor1 =(int) (iny * cryst->b *
715 cos((cryst->gamma - 90) *
716 PI_RAD)) + cryst->offset_h;
717 y_coor2 =cryst->offset_h;
719 XDrawLine(display, window, cryst->gc,
720 (int) (cryst->offset_w +
721 inx * cryst->a - (int) (iny * cryst->b *
722 sin((cryst->gamma - 90) * PI_RAD))),
724 cryst->offset_w + inx * cryst->a,
730 inx = NRAND(cryst->nx);
731 iny = NRAND(cryst->ny);
734 y_coor1 =cryst->win_height -
735 (int) (iny * cryst->b *
736 cos((cryst->gamma - 90) *
739 y_coor2 =cryst->win_height -
740 (int) ( ( iny + 1 ) * cryst->b *
741 cos((cryst->gamma - 90) *
747 y_coor1 =(int) (iny * cryst->b *
748 cos((cryst->gamma - 90) *
751 y_coor2 =(int) (( iny + 1 ) * cryst->b *
752 cos((cryst->gamma - 90) *
756 XDrawLine(display, window, cryst->gc,
757 cryst->offset_w + inx * cryst->a - (int) (iny * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
759 cryst->offset_w + (inx + 1) * cryst->a - (int) (iny * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
761 XDrawLine(display, window, cryst->gc,
762 cryst->offset_w + inx * cryst->a - (int) (iny * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
764 cryst->offset_w + inx * cryst->a - (int) ((iny + 1) * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
766 XDrawLine(display, window, cryst->gc,
767 cryst->offset_w + (inx + 1) * cryst->a - (int) (iny * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
769 cryst->offset_w + (inx + 1) * cryst->a - (int) ((iny + 1) * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
771 XDrawLine(display, window, cryst->gc,
772 cryst->offset_w + inx * cryst->a - (int) ((iny + 1) * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
774 cryst->offset_w + (inx + 1) * cryst->a - (int) ((iny + 1) * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
778 for (i = 0; i < cryst->num_atom; i++) {
781 atom0 = &cryst->atom[i];
782 if (MI_IS_INSTALL(mi) && MI_NPIXELS(mi) > 2) {
783 XSetForeground(display, cryst->gc, cryst->colors[atom0->colour].pixel);
785 XSetForeground(display, cryst->gc, atom0->colour);
787 crystal_drawatom(mi, atom0);
789 XSetFunction(display, cryst->gc, GXcopy);
793 release_crystal(ModeInfo * mi)
795 Display *display = MI_DISPLAY(mi);
797 if (crystals != NULL) {
800 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
801 crystalstruct *cryst = &crystals[screen];
803 if (MI_IS_INSTALL(mi) && MI_NPIXELS(mi) > 2) {
804 MI_WHITE_PIXEL(mi) = cryst->whitepixel;
805 MI_BLACK_PIXEL(mi) = cryst->blackpixel;
807 MI_FG_PIXEL(mi) = cryst->fg;
808 MI_BG_PIXEL(mi) = cryst->bg;
810 if (cryst->colors && cryst->ncolors && !cryst->no_colors)
811 free_colors(display, cryst->cmap, cryst->colors, cryst->ncolors);
813 (void) free((void *) cryst->colors);
814 #if 0 /* #### wrong! -jwz */
815 XFreeColormap(display, cryst->cmap);
818 if (cryst->gc != NULL)
819 XFreeGC(display, cryst->gc);
820 if (cryst->atom != NULL)
821 (void) free((void *) cryst->atom);
823 (void) free((void *) crystals);
829 init_crystal(ModeInfo * mi)
831 Display *display = MI_DISPLAY(mi);
832 Window window = MI_WINDOW(mi);
833 crystalstruct *cryst;
834 int i, max_atoms, size_atom, neqv;
840 if (crystals == NULL) {
841 if ((crystals = (crystalstruct *) calloc(MI_NUM_SCREENS(mi),
842 sizeof (crystalstruct))) == NULL)
845 cryst = &crystals[MI_SCREEN(mi)];
848 if (MI_IS_INSTALL(mi) && MI_NPIXELS(mi) > 2) {
852 extern char *background;
853 extern char *foreground;
855 cryst->fg = MI_FG_PIXEL(mi);
856 cryst->bg = MI_BG_PIXEL(mi);
858 cryst->blackpixel = MI_BLACK_PIXEL(mi);
859 cryst->whitepixel = MI_WHITE_PIXEL(mi);
860 #if 0 /* #### wrong! -jwz */
861 cryst->cmap = XCreateColormap(display, window,
862 MI_VISUAL(mi), AllocNone);
863 XSetWindowColormap(display, window, cryst->cmap);
865 cryst->cmap = mi->xgwa.colormap;
867 (void) XParseColor(display, cryst->cmap, "black", &color);
868 (void) XAllocColor(display, cryst->cmap, &color);
869 MI_BLACK_PIXEL(mi) = color.pixel;
870 (void) XParseColor(display, cryst->cmap, "white", &color);
871 (void) XAllocColor(display, cryst->cmap, &color);
872 MI_WHITE_PIXEL(mi) = color.pixel;
874 (void) XParseColor(display, cryst->cmap, background, &color);
875 (void) XAllocColor(display, cryst->cmap, &color);
876 MI_BG_PIXEL(mi) = color.pixel;
877 (void) XParseColor(display, cryst->cmap, foreground, &color);
878 (void) XAllocColor(display, cryst->cmap, &color);
879 MI_FG_PIXEL(mi) = color.pixel;
884 if ((cryst->gc = XCreateGC(display, MI_WINDOW(mi),
885 (unsigned long) 0, (XGCValues *) NULL)) == None)
890 cryst->painted = False;
891 XSetFunction(display, cryst->gc, GXxor);
894 /*Set up crystal data */
895 cryst->direction = (LRAND() & 1) ? 1 : -1;
896 if (MI_IS_FULLRANDOM(mi)) {
898 cryst->unit_cell = True;
900 cryst->unit_cell = False;
902 cryst->unit_cell = unit_cell;
903 if (cryst->unit_cell) {
904 if (MI_IS_FULLRANDOM(mi)) {
906 cryst->grid_cell = True;
908 cryst->grid_cell = False;
910 cryst->grid_cell = grid_cell;
912 cryst->win_width = MI_WIDTH(mi);
913 cryst->win_height = MI_HEIGHT(mi);
914 cell_min = min(cryst->win_width / 2 + 1, MIN_CELL);
915 cell_min = min(cell_min, cryst->win_height / 2 + 1);
916 cryst->planegroup = NRAND(17);
917 cryst->invert = NRAND(2);
918 if (MI_IS_VERBOSE(mi))
919 (void) fprintf(stdout, "Selected plane group no %d\n",
920 cryst->planegroup + 1);
921 if (cryst->planegroup > 11)
922 cryst->gamma = 120.0;
923 else if (cryst->planegroup < 2)
924 cryst->gamma = 60.0 + NRAND(60);
927 neqv = numops[2 * cryst->planegroup] - numops[2 * cryst->planegroup + 1];
928 if (centro[cryst->planegroup] == True)
930 if (primitive[cryst->planegroup] == False)
937 cryst->nx = NRAND(-nx) + 1;
940 if (cryst->planegroup > 8)
941 cryst->ny = cryst->nx;
945 cryst->ny = NRAND(-ny) + 1;
948 neqv = neqv * cryst->nx * cryst->ny;
950 cryst->num_atom = MI_COUNT(mi);
951 max_atoms = MI_COUNT(mi);
952 if (cryst->num_atom == 0) {
953 cryst->num_atom = DEF_NUM_ATOM;
954 max_atoms = DEF_NUM_ATOM;
955 } else if (cryst->num_atom < 0) {
956 max_atoms = -cryst->num_atom;
957 cryst->num_atom = NRAND(-cryst->num_atom) + 1;
960 cryst->num_atom = cryst->num_atom / neqv + 1;
962 if (cryst->atom == NULL)
963 cryst->atom = (crystalatom *) calloc(max_atoms, sizeof (
967 if (cryst->planegroup < 13) {
971 if (cryst->planegroup < 10) {
972 cryst->b = cryst->win_height;
973 cryst->a = cryst->win_width;
975 cryst->b = min(cryst->win_height, cryst->win_width);
979 cryst->gamma = 120.0;
980 cryst->a = (int) (cryst->win_width * 2.0 / 3.0);
982 cryst->offset_h = (int) (cryst->b * 0.25 *
983 cos((cryst->gamma - 90) * PI_RAD));
984 cryst->offset_w = (int) (cryst->b * 0.5);
988 cryst->offset_w = -1;
989 while (max_repeat-- &&
990 (cryst->offset_w < 4 || (int) (cryst->offset_w - cryst->b *
991 sin((cryst->gamma - 90) * PI_RAD)) < 4)
993 cryst->b = NRAND((int) (cryst->win_height / (cos((cryst->gamma - 90) *
994 PI_RAD))) - cell_min) + cell_min;
995 if (cryst->planegroup > 8)
998 cryst->a = NRAND(cryst->win_width - cell_min) + cell_min;
999 cryst->offset_w = (int) ((cryst->win_width - (cryst->a - cryst->b *
1000 sin((cryst->gamma - 90) *
1003 cryst->offset_h = (int) ((cryst->win_height - cryst->b * cos((
1004 cryst->gamma - 90) * PI_RAD)) / 2.0);
1006 if (cryst->offset_h > 0)
1007 cryst->offset_h = NRAND(2 * cryst->offset_h);
1008 cryst->offset_w = (int) (cryst->win_width - cryst->a -
1010 fabs(sin((cryst->gamma - 90) * PI_RAD)));
1011 if (cryst->gamma > 90.0) {
1012 if (cryst->offset_w > 0)
1013 cryst->offset_w = NRAND(cryst->offset_w) +
1014 (int) (cryst->b * sin((cryst->gamma - 90) * PI_RAD));
1016 cryst->offset_w = (int) (cryst->b * sin((cryst->gamma - 90) *
1018 } else if (cryst->offset_w > 0)
1019 cryst->offset_w = NRAND(cryst->offset_w);
1021 cryst->offset_w = 0;
1025 size_atom = min((int) ((float) (cryst->a) / 40.) + 1,
1026 (int) ((float) (cryst->b) / 40.) + 1);
1027 if (MI_SIZE(mi) < size_atom) {
1028 if (MI_SIZE(mi) < -size_atom)
1029 size_atom = -size_atom;
1031 size_atom = MI_SIZE(mi);
1033 cryst->a = cryst->a / cryst->nx;
1034 cryst->b = cryst->b / cryst->ny;
1035 if (cryst->unit_cell) {
1036 int y_coor1 , y_coor2;
1038 if (MI_NPIXELS(mi) > 2)
1039 XSetForeground(display, cryst->gc, MI_PIXEL(mi, NRAND(MI_NPIXELS(mi))));
1041 XSetForeground(display, cryst->gc, MI_WHITE_PIXEL(mi));
1042 if (cryst->grid_cell) {
1045 if ( cryst->invert )
1046 y_coor1 = y_coor2 = cryst->win_height - cryst->offset_h;
1048 y_coor1 = y_coor2 = cryst->offset_h;
1049 XDrawLine(display, window, cryst->gc, cryst->offset_w,
1050 y_coor1, cryst->offset_w + cryst->nx * cryst->a,
1052 if ( cryst->invert )
1054 y_coor1 = cryst->win_height - cryst->offset_h;
1055 y_coor2 = cryst->win_height - (int) (cryst->ny *
1057 cos((cryst->gamma - 90) * PI_RAD)) -
1062 y_coor1 = cryst->offset_h;
1063 y_coor2 = (int) (cryst->ny * cryst->b *
1064 cos((cryst->gamma - 90) * PI_RAD)) +
1067 XDrawLine(display, window, cryst->gc, cryst->offset_w,
1068 y_coor1, (int) (cryst->offset_w - cryst->ny * cryst->b *
1069 sin((cryst->gamma - 90) * PI_RAD)),
1072 for (iny = 1; iny <= cryst->ny; iny++) {
1073 if ( cryst->invert )
1075 y_coor1 = cryst->win_height -
1076 (int) (iny * cryst->b * cos((cryst->gamma - 90) *
1077 PI_RAD)) - cryst->offset_h;
1078 y_coor2 = cryst->win_height -
1079 (int) (iny * cryst->b * cos((cryst->gamma - 90) *
1085 y_coor1 = (int) (iny * cryst->b * cos((cryst->gamma - 90) *
1086 PI_RAD)) + cryst->offset_h;
1087 y_coor2 = (int) (iny * cryst->b * cos((cryst->gamma - 90) * PI_RAD)) +
1090 XDrawLine(display, window, cryst->gc,
1091 (int) (cryst->offset_w +
1092 inx * cryst->a - (int) (iny * cryst->b *
1093 sin((cryst->gamma - 90) * PI_RAD))),
1095 (int) (cryst->offset_w - iny * cryst->b *
1096 sin((cryst->gamma - 90) * PI_RAD)),
1100 for (inx = 1; inx <= cryst->nx; inx++) {
1101 if ( cryst->invert )
1103 y_coor1 =cryst->win_height -
1104 (int) (iny * cryst->b *
1105 cos((cryst->gamma - 90) *
1106 PI_RAD)) - cryst->offset_h;
1107 y_coor2 =cryst->win_height - cryst->offset_h;
1111 y_coor1 =(int) (iny * cryst->b *
1112 cos((cryst->gamma - 90) *
1113 PI_RAD)) + cryst->offset_h;
1114 y_coor2 =cryst->offset_h;
1116 XDrawLine(display, window, cryst->gc,
1117 (int) (cryst->offset_w +
1118 inx * cryst->a - (int) (iny * cryst->b *
1119 sin((cryst->gamma - 90) * PI_RAD))),
1121 cryst->offset_w + inx * cryst->a,
1127 inx = NRAND(cryst->nx);
1128 iny = NRAND(cryst->ny);
1129 if ( cryst->invert )
1131 y_coor1 =cryst->win_height -
1132 (int) (iny * cryst->b *
1133 cos((cryst->gamma - 90) *
1136 y_coor2 =cryst->win_height -
1137 (int) ( ( iny + 1 ) * cryst->b *
1138 cos((cryst->gamma - 90) *
1144 y_coor1 =(int) (iny * cryst->b *
1145 cos((cryst->gamma - 90) *
1148 y_coor2 =(int) (( iny + 1 ) * cryst->b *
1149 cos((cryst->gamma - 90) *
1153 XDrawLine(display, window, cryst->gc,
1154 cryst->offset_w + inx * cryst->a - (int) (iny * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
1156 cryst->offset_w + (inx + 1) * cryst->a - (int) (iny * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
1158 XDrawLine(display, window, cryst->gc,
1159 cryst->offset_w + inx * cryst->a - (int) (iny * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
1161 cryst->offset_w + inx * cryst->a - (int) ((iny + 1) * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
1163 XDrawLine(display, window, cryst->gc,
1164 cryst->offset_w + (inx + 1) * cryst->a - (int) (iny * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
1166 cryst->offset_w + (inx + 1) * cryst->a - (int) ((iny + 1) * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
1168 XDrawLine(display, window, cryst->gc,
1169 cryst->offset_w + inx * cryst->a - (int) ((iny + 1) * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
1171 cryst->offset_w + (inx + 1) * cryst->a - (int) ((iny + 1) * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
1175 if (MI_IS_INSTALL(mi) && MI_NPIXELS(mi) > 2) {
1176 /* Set up colour map */
1177 if (cryst->colors && cryst->ncolors && !cryst->no_colors)
1178 free_colors(display, cryst->cmap, cryst->colors, cryst->ncolors);
1180 (void) free((void *) cryst->colors);
1182 cryst->ncolors = MI_NCOLORS(mi);
1183 if (cryst->ncolors < 2)
1185 if (cryst->ncolors <= 2)
1186 cryst->mono_p = True;
1188 cryst->mono_p = False;
1193 cryst->colors = (XColor *) malloc(sizeof (*cryst->colors) * (cryst->ncolors + 1));
1194 cryst->cycle_p = has_writable_cells(mi->xgwa.screen, MI_VISUAL(mi));
1195 if (cryst->cycle_p) {
1196 if (MI_IS_FULLRANDOM(mi)) {
1198 cryst->cycle_p = False;
1200 cryst->cycle_p = True;
1202 cryst->cycle_p = cycle_p;
1205 if (!cryst->mono_p) {
1206 if (!(LRAND() % 10))
1207 make_random_colormap(MI_DISPLAY(mi), MI_VISUAL(mi), cryst->cmap, cryst->colors, &cryst->ncolors,
1208 True, True, &cryst->cycle_p, True);
1209 else if (!(LRAND() % 2))
1210 make_uniform_colormap(MI_DISPLAY(mi), MI_VISUAL(mi), cryst->cmap, cryst->colors, &cryst->ncolors,
1211 True, &cryst->cycle_p, True);
1213 make_smooth_colormap(MI_DISPLAY(mi), MI_VISUAL(mi), cryst->cmap, cryst->colors, &cryst->ncolors,
1214 True, &cryst->cycle_p, True);
1216 #if 0 /* #### wrong! -jwz */
1217 XInstallColormap(display, cryst->cmap);
1219 if (cryst->ncolors < 2) {
1221 cryst->no_colors = True;
1223 cryst->no_colors = False;
1224 if (cryst->ncolors <= 2)
1225 cryst->mono_p = True;
1228 cryst->cycle_p = False;
1231 for (i = 0; i < cryst->num_atom; i++) {
1234 atom0 = &cryst->atom[i];
1235 if (MI_IS_INSTALL(mi) && MI_NPIXELS(mi) > 2) {
1236 if (cryst->ncolors > 2)
1237 atom0->colour = NRAND(cryst->ncolors - 2) + 2;
1239 atom0->colour = 1; /* Just in case */
1240 XSetForeground(display, cryst->gc, cryst->colors[atom0->colour].pixel);
1242 if (MI_NPIXELS(mi) > 2)
1243 atom0->colour = MI_PIXEL(mi, NRAND(MI_NPIXELS(mi)));
1245 atom0->colour = 1; /*Xor'red so WHITE may not be appropriate */
1246 XSetForeground(display, cryst->gc, atom0->colour);
1248 atom0->x0 = NRAND(cryst->a);
1249 atom0->y0 = NRAND(cryst->b);
1250 atom0->velocity[0] = NRAND(7) - 3;
1251 atom0->velocity[1] = NRAND(7) - 3;
1252 atom0->velocity_a = (NRAND(7) - 3) * PI_RAD;
1253 atom0->angle = NRAND(90) * PI_RAD;
1254 atom0->at_type = NRAND(3);
1256 atom0->size_at = DEF_SIZ_ATOM;
1257 else if (size_atom > 0)
1258 atom0->size_at = size_atom;
1260 atom0->size_at = NRAND(-size_atom) + 1;
1262 if (atom0->at_type == 2)
1263 atom0->num_point = 3;
1265 atom0->num_point = 4;
1266 crystal_setupatom(atom0, cryst->gamma);
1267 crystal_drawatom(mi, atom0);
1269 XSetFunction(display, cryst->gc, GXcopy);
1273 reshape_crystal(ModeInfo * mi, int width, int height)
1275 release_crystal(mi);
1279 XSCREENSAVER_MODULE ("Crystal", crystal)