1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* crystal --- polygons moving according to plane group rules */
4 #if !defined( lint ) && !defined( SABER )
5 static const char sccsid[] = "@(#)crystal.c 4.12 98/09/10 xlockmore";
10 * Copyright (c) 1997 by Jouk Jansen <joukj@crys.chem.uva.nl>
12 * Permission to use, copy, modify, and distribute this software and its
13 * documentation for any purpose and without fee is hereby granted,
14 * provided that the above copyright notice appear in all copies and that
15 * both that copyright notice and this permission notice appear in
16 * supporting documentation.
18 * This file is provided AS IS with no warranties of any kind. The author
19 * shall have no liability with respect to the infringement of copyrights,
20 * trade secrets or any patents by this file or any part thereof. In no
21 * event will the author be liable for any lost revenue or profits or
22 * other special, indirect and consequential damages.
24 * The author should like to be notified if changes have been made to the
25 * routine. Response will only be guaranteed when a VMS version of the
26 * program is available.
28 * A moving polygon-mode. The polygons obey 2D-planegroup symmetry.
30 * The groupings of the cells fall in 3 categories:
31 * oblique groups 1 and 2 where the angle gamma ranges from 60 to 120 degrees
32 * square groups 3 through 11 where the angle gamma is 90 degrees
33 * hexagonal groups 12 through 17 where the angle gamma is 120 degrees
36 * 03-Dec-98: Random inversion of y-axis included to simulate hexagonal groups
37 * with an angle of 60 degrees.
38 * 10-Sep-98: new colour scheme
39 * 24-Feb-98: added option centre which turns on/off forcing the centre of
40 * the screen to be used
41 * added option maxsize which forces the dimensions to be chasen
42 * in such ua way that the largest possible part of the screen is
44 * When only one unit cell is drawn, it is chosen at random
45 * 18-Feb-98: added support for negative numbers with -nx and -ny meaning
46 * "random" choice with given maximum
47 * added +/-grid option. If -cell is specified this option
48 * determines if one or all unit cells are drawn.
49 * -batchcount is now a parameter for all the objects on the screen
50 * instead of the number of "unique" objects
51 * The maximum size of the objects now scales with the part
53 * fixed "size" problem. Now very small non-vissable objects
55 * 13-Feb-98: randomized the unit cell size
56 * runtime options -/+cell (turn on/off unit cell drawing)
57 * -nx num (number of translational symmetries in x-direction
58 * -ny num (idem y-direction but ignored for square and
59 * hexagonal space groups
60 * i.e. try xlock -mode crystal -nx 3 -ny 2
61 * Fullrandom overrules the -/+cell option.
62 * 05-Feb-98: Revision + bug repairs
64 * use part of the screen for unit cell
65 * in hexagonal and square groups a&b axis forced to be equal
66 * cell angle for oblique groups randomly chosen between 60 and 120
67 * bugs solved: planegroups with cell angles <> 90.0 now work properly
68 * 19-Sep-97: Added remaining hexagonal groups
73 # define PROGCLASS "Crystal"
74 # define HACK_INIT init_crystal
75 # define HACK_DRAW draw_crystal
76 # define crystal_opts xlockmore_opts
77 # define DEFAULTS "*delay: 60000 \n" \
82 "*fullrandom: True \n" \
84 # include "xlockmore.h" /* in xscreensaver distribution */
85 #else /* STANDALONE */
86 # include "xlock.h" /* in xlockmore distribution */
88 #endif /* STANDALONE */
90 #define DEF_CELL "True" /* Draw unit cell */
91 #define DEF_GRID "False" /* Draw unit all cell if DEF_CELL is True */
92 #define DEF_NX "-3" /* number of unit cells in x-direction */
93 #define DEF_NX1 1 /* number of unit cells in x-direction */
94 #define DEF_NY "-3" /* number of unit cells in y-direction */
95 #define DEF_NY1 1 /* number of unit cells in y-direction */
96 #define DEF_CENTRE "False"
97 #define DEF_MAXSIZE "False"
98 #define DEF_CYCLE "True"
100 #define min(a,b) ((a) <= (b) ? (a) : (b))
103 void release_crystal(ModeInfo * mi);
108 static Bool unit_cell, grid_cell, centre, maxsize, cycle_p;
110 static XrmOptionDescRec opts[] =
112 {"-nx", "crystal.nx", XrmoptionSepArg, (caddr_t) NULL},
113 {"-ny", "crystal.ny", XrmoptionSepArg, (caddr_t) NULL},
114 {"-centre", ".crystal.centre", XrmoptionNoArg, (caddr_t) "on"},
115 {"+centre", ".crystal.centre", XrmoptionNoArg, (caddr_t) "off"},
116 {"-maxsize", ".crystal.maxsize", XrmoptionNoArg, (caddr_t) "on"},
117 {"+maxsize", ".crystal.maxsize", XrmoptionNoArg, (caddr_t) "off"},
118 {"-cell", ".crystal.cell", XrmoptionNoArg, (caddr_t) "on"},
119 {"+cell", ".crystal.cell", XrmoptionNoArg, (caddr_t) "off"},
120 {"-grid", ".crystal.grid", XrmoptionNoArg, (caddr_t) "on"},
121 {"+grid", ".crystal.grid", XrmoptionNoArg, (caddr_t) "off"},
122 {"-shift", ".crystal.shift", XrmoptionNoArg, (caddr_t) "on"},
123 {"+shift", ".crystal.shift", XrmoptionNoArg, (caddr_t) "off"}
126 static argtype vars[] =
128 {(caddr_t *) & nx, "nx", "nx", DEF_NX, t_Int},
129 {(caddr_t *) & ny, "ny", "ny", DEF_NY, t_Int},
130 {(caddr_t *) & centre, "centre", "Centre", DEF_CENTRE, t_Bool},
131 {(caddr_t *) & maxsize, "maxsize", "Maxsize", DEF_MAXSIZE, t_Bool},
132 {(caddr_t *) & unit_cell, "cell", "Cell", DEF_CELL, t_Bool},
133 {(caddr_t *) & grid_cell, "grid", "Grid", DEF_GRID, t_Bool},
134 {(caddr_t *) & cycle_p, "shift", "Shift", DEF_CYCLE, t_Bool}
136 static OptionStruct desc[] =
138 {"-nx num", "Number of unit cells in x-direction"},
139 {"-ny num", "Number of unit cells in y-direction"},
140 {"-/+centre", "turn on/off centering on screen"},
141 {"-/+maxsize", "turn on/off use of maximum part of screen"},
142 {"-/+cell", "turn on/off drawing of unit cell"},
143 {"-/+grid", "turn on/off drawing of grid of unit cells (if -cell is on)"},
144 {"-/+shift", "turn on/off colour cycling"}
147 ModeSpecOpt crystal_opts =
148 {sizeof opts / sizeof opts[0], opts, sizeof vars / sizeof vars[0], vars, desc};
151 ModStruct crystal_description =
152 {"crystal", "init_crystal", "draw_crystal", "release_crystal",
153 "refresh_crystal", "init_crystal", NULL, &crystal_opts,
154 60000, -40, 200, -15, 64, 1.0, "",
155 "Shows polygons in 2D plane groups", 0, NULL};
159 #define DEF_NUM_ATOM 10
161 #define DEF_SIZ_ATOM 10
163 #define PI_RAD (M_PI / 180.0)
165 static Bool centro[17] =
186 static Bool primitive[17] =
207 static short numops[34] =
228 static short operation[114] =
252 unsigned long colour;
253 int x0, y0, velocity[2];
254 float angle, velocity_a;
255 int num_point, at_type, size_at;
261 int win_width, win_height, num_atom;
262 int planegroup, a, b, offset_w, offset_h, nx, ny;
266 Bool unit_cell, grid_cell;
270 Bool cycle_p, mono_p, no_colors;
271 unsigned long blackpixel, whitepixel, fg, bg;
272 int direction, invert;
275 static crystalstruct *crystals = NULL;
278 trans_coor(XPoint * xyp, XPoint * new_xyp, int num_points,
283 for (i = 0; i <= num_points; i++) {
284 new_xyp[i].x = xyp[i].x +
285 (int) (xyp[i].y * sin((gamma - 90.0) * PI_RAD));
286 new_xyp[i].y = (int) (xyp[i].y / cos((gamma - 90.0) * PI_RAD));
291 trans_coor_back(XPoint * xyp, XPoint * new_xyp,
292 int num_points, float gamma, int offset_w, int offset_h ,
293 int winheight , int invert )
297 for (i = 0; i <= num_points; i++) {
298 new_xyp[i].y = (int) (xyp[i].y * cos((gamma - 90) * PI_RAD)) +
300 new_xyp[i].x = xyp[i].x - (int) (xyp[i].y * sin((gamma - 90.0)
301 * PI_RAD)) + offset_w;
302 if ( invert ) new_xyp[i].y = winheight - new_xyp[i].y;
307 crystal_setupatom(crystalatom * atom0, float gamma)
312 y0 = (int) (atom0->y0 * cos((gamma - 90) * PI_RAD));
313 x0 = atom0->x0 - (int) (atom0->y0 * sin((gamma - 90.0) * PI_RAD));
314 switch (atom0->at_type) {
315 case 0: /* rectangles */
316 xy[0].x = x0 + (int) (2 * atom0->size_at *
318 (int) (atom0->size_at * sin(atom0->angle));
319 xy[0].y = y0 + (int) (atom0->size_at *
321 (int) (2 * atom0->size_at * sin(atom0->angle));
322 xy[1].x = x0 + (int) (2 * atom0->size_at *
324 (int) (atom0->size_at * sin(atom0->angle));
325 xy[1].y = y0 - (int) (atom0->size_at *
327 (int) (2 * atom0->size_at * sin(atom0->angle));
328 xy[2].x = x0 - (int) (2 * atom0->size_at *
330 (int) (atom0->size_at * sin(atom0->angle));
331 xy[2].y = y0 - (int) (atom0->size_at *
333 (int) (2 * atom0->size_at * sin(atom0->angle));
334 xy[3].x = x0 - (int) (2 * atom0->size_at *
336 (int) (atom0->size_at * sin(atom0->angle));
337 xy[3].y = y0 + (int) (atom0->size_at *
339 (int) (2 * atom0->size_at *
343 trans_coor(xy, atom0->xy, 4, gamma);
345 case 1: /* squares */
346 xy[0].x = x0 + (int) (1.5 * atom0->size_at *
348 (int) (1.5 * atom0->size_at *
350 xy[0].y = y0 + (int) (1.5 * atom0->size_at *
352 (int) (1.5 * atom0->size_at *
354 xy[1].x = x0 + (int) (1.5 * atom0->size_at *
356 (int) (1.5 * atom0->size_at *
358 xy[1].y = y0 - (int) (1.5 * atom0->size_at *
360 (int) (1.5 * atom0->size_at *
362 xy[2].x = x0 - (int) (1.5 * atom0->size_at *
364 (int) (1.5 * atom0->size_at *
366 xy[2].y = y0 - (int) (1.5 * atom0->size_at *
368 (int) (1.5 * atom0->size_at *
370 xy[3].x = x0 - (int) (1.5 * atom0->size_at *
372 (int) (1.5 * atom0->size_at *
374 xy[3].y = y0 + (int) (1.5 * atom0->size_at *
376 (int) (1.5 * atom0->size_at *
380 trans_coor(xy, atom0->xy, 4, gamma);
382 case 2: /* triangles */
383 xy[0].x = x0 + (int) (1.5 * atom0->size_at *
385 xy[0].y = y0 + (int) (1.5 * atom0->size_at *
387 xy[1].x = x0 + (int) (1.5 * atom0->size_at *
389 (int) (1.5 * atom0->size_at *
391 xy[1].y = y0 - (int) (1.5 * atom0->size_at *
393 (int) (1.5 * atom0->size_at *
395 xy[2].x = x0 - (int) (1.5 * atom0->size_at *
397 (int) (1.5 * atom0->size_at *
399 xy[2].y = y0 - (int) (1.5 * atom0->size_at *
401 (int) (1.5 * atom0->size_at *
405 trans_coor(xy, atom0->xy, 3, gamma);
411 crystal_drawatom(ModeInfo * mi, crystalatom * atom0)
413 crystalstruct *cryst;
414 Display *display = MI_DISPLAY(mi);
415 Window window = MI_WINDOW(mi);
418 cryst = &crystals[MI_SCREEN(mi)];
419 for (j = numops[2 * cryst->planegroup + 1];
420 j < numops[2 * cryst->planegroup]; j++) {
421 XPoint xy[5], new_xy[5];
425 xtrans = operation[j * 6] * atom0->x0 + operation[j * 6 + 1] *
426 atom0->y0 + (int) (operation[j * 6 + 4] * cryst->a /
428 ytrans = operation[j * 6 + 2] * atom0->x0 + operation[j * 6 +
429 3] * atom0->y0 + (int) (operation[j * 6 + 5] *
432 if (xtrans < -cryst->a)
433 xtrans = 2 * cryst->a;
436 } else if (xtrans >= cryst->a)
442 else if (ytrans >= cryst->b)
446 for (k = 0; k < atom0->num_point; k++) {
447 xy[k].x = operation[j * 6] * atom0->xy[k].x +
448 operation[j * 6 + 1] *
449 atom0->xy[k].y + (int) (operation[j * 6 + 4] *
452 xy[k].y = operation[j * 6 + 2] * atom0->xy[k].x +
453 operation[j * 6 + 3] *
454 atom0->xy[k].y + (int) (operation[j * 6 + 5] *
458 xy[atom0->num_point].x = xy[0].x;
459 xy[atom0->num_point].y = xy[0].y;
460 for (l = 0; l < cryst->nx; l++) {
461 for (m = 0; m < cryst->ny; m++) {
463 for (k = 0; k <= atom0->num_point; k++) {
464 xy_1[k].x = xy[k].x + l * cryst->a;
465 xy_1[k].y = xy[k].y + m * cryst->b;
467 trans_coor_back(xy_1, new_xy, atom0->num_point,
468 cryst->gamma, cryst->offset_w,
472 XFillPolygon(display, window, cryst->gc, new_xy,
473 atom0->num_point, Convex, CoordModeOrigin);
476 if (centro[cryst->planegroup] == True) {
477 for (k = 0; k <= atom0->num_point; k++) {
478 xy[k].x = cryst->a - xy[k].x;
479 xy[k].y = cryst->b - xy[k].y;
481 for (l = 0; l < cryst->nx; l++) {
482 for (m = 0; m < cryst->ny; m++) {
484 for (k = 0; k <= atom0->num_point; k++) {
485 xy_1[k].x = xy[k].x + l * cryst->a;
486 xy_1[k].y = xy[k].y + m * cryst->b;
488 trans_coor_back(xy_1, new_xy, atom0->num_point,
494 XFillPolygon(display, window, cryst->gc,
496 atom0->num_point, Convex,
501 if (primitive[cryst->planegroup] == False) {
502 if (xy[atom0->num_point].x >= (int) (cryst->a / 2.0))
503 xtrans = (int) (-cryst->a / 2.0);
505 xtrans = (int) (cryst->a / 2.0);
506 if (xy[atom0->num_point].y >= (int) (cryst->b / 2.0))
507 ytrans = (int) (-cryst->b / 2.0);
509 ytrans = (int) (cryst->b / 2.0);
510 for (k = 0; k <= atom0->num_point; k++) {
511 xy[k].x = xy[k].x + xtrans;
512 xy[k].y = xy[k].y + ytrans;
514 for (l = 0; l < cryst->nx; l++) {
515 for (m = 0; m < cryst->ny; m++) {
517 for (k = 0; k <= atom0->num_point; k++) {
518 xy_1[k].x = xy[k].x + l * cryst->a;
519 xy_1[k].y = xy[k].y + m * cryst->b;
521 trans_coor_back(xy_1, new_xy, atom0->num_point,
527 XFillPolygon(display, window, cryst->gc,
529 atom0->num_point, Convex,
533 if (centro[cryst->planegroup] == True) {
536 for (k = 0; k <= atom0->num_point; k++) {
537 xy1[k].x = cryst->a - xy[k].x;
538 xy1[k].y = cryst->b - xy[k].y;
540 for (l = 0; l < cryst->nx; l++) {
541 for (m = 0; m < cryst->ny; m++) {
543 for (k = 0; k <= atom0->num_point; k++) {
544 xy_1[k].x = xy1[k].x + l * cryst->a;
545 xy_1[k].y = xy1[k].y + m * cryst->b;
547 trans_coor_back(xy_1, new_xy, atom0->num_point,
553 XFillPolygon(display, window,
555 new_xy, atom0->num_point,
556 Convex, CoordModeOrigin);
565 draw_crystal(ModeInfo * mi)
567 Display *display = MI_DISPLAY(mi);
568 crystalstruct *cryst = &crystals[MI_SCREEN(mi)];
571 if (cryst->no_colors) {
576 cryst->painted = True;
577 MI_IS_DRAWN(mi) = True;
578 XSetFunction(display, cryst->gc, GXxor);
581 if (cryst->cycle_p) {
582 rotate_colors(display, cryst->cmap, cryst->colors, cryst->ncolors,
584 if (!(LRAND() % 1000))
585 cryst->direction = -cryst->direction;
587 for (i = 0; i < cryst->num_atom; i++) {
590 atom0 = &cryst->atom[i];
591 if (MI_IS_INSTALL(mi) && MI_NPIXELS(mi) > 2) {
592 XSetForeground(display, cryst->gc, cryst->colors[atom0->colour].pixel);
594 XSetForeground(display, cryst->gc, atom0->colour);
596 crystal_drawatom(mi, atom0);
597 atom0->velocity[0] += NRAND(3) - 1;
598 atom0->velocity[0] = MAX(-20, MIN(20, atom0->velocity[0]));
599 atom0->velocity[1] += NRAND(3) - 1;
600 atom0->velocity[1] = MAX(-20, MIN(20, atom0->velocity[1]));
601 atom0->x0 += atom0->velocity[0];
602 /*if (cryst->gamma == 90.0) { */
604 atom0->x0 += cryst->a;
605 else if (atom0->x0 >= cryst->a)
606 atom0->x0 -= cryst->a;
607 atom0->y0 += atom0->velocity[1];
609 atom0->y0 += cryst->b;
610 else if (atom0->y0 >= cryst->b)
611 atom0->y0 -= cryst->b;
613 atom0->velocity_a += ((float) NRAND(1001) - 500.0) / 2000.0;
614 atom0->angle += atom0->velocity_a;
615 crystal_setupatom(atom0, cryst->gamma);
616 crystal_drawatom(mi, atom0);
618 XSetFunction(display, cryst->gc, GXcopy);
622 refresh_crystal(ModeInfo * mi)
624 Display *display = MI_DISPLAY(mi);
625 Window window = MI_WINDOW(mi);
626 crystalstruct *cryst = &crystals[MI_SCREEN(mi)];
632 XSetFunction(display, cryst->gc, GXxor);
634 if (cryst->unit_cell) {
635 int y_coor1 , y_coor2;
637 if (MI_NPIXELS(mi) > 2)
638 XSetForeground(display, cryst->gc, MI_PIXEL(mi, NRAND(MI_NPIXELS(mi))));
640 XSetForeground(display, cryst->gc, MI_WHITE_PIXEL(mi));
641 if (cryst->grid_cell) {
645 y_coor1 = y_coor2 = cryst->win_height - cryst->offset_h;
647 y_coor1 = y_coor2 = cryst->offset_h;
648 XDrawLine(display, window, cryst->gc, cryst->offset_w,
649 y_coor1, cryst->offset_w + cryst->nx * cryst->a,
653 y_coor1 = cryst->win_height - cryst->offset_h;
654 y_coor2 = cryst->win_height - (int) (cryst->ny *
656 cos((cryst->gamma - 90) * PI_RAD)) -
661 y_coor1 = cryst->offset_h;
662 y_coor2 = (int) (cryst->ny * cryst->b *
663 cos((cryst->gamma - 90) * PI_RAD)) +
666 XDrawLine(display, window, cryst->gc, cryst->offset_w,
667 y_coor1, (int) (cryst->offset_w - cryst->ny * cryst->b *
668 sin((cryst->gamma - 90) * PI_RAD)),
671 for (iny = 1; iny <= cryst->ny; iny++) {
674 y_coor1 = cryst->win_height -
675 (int) (iny * cryst->b * cos((cryst->gamma - 90) *
676 PI_RAD)) - cryst->offset_h;
677 y_coor2 = cryst->win_height -
678 (int) (iny * cryst->b * cos((cryst->gamma - 90) *
684 y_coor1 = (int) (iny * cryst->b * cos((cryst->gamma - 90) *
685 PI_RAD)) + cryst->offset_h;
686 y_coor2 = (int) (iny * cryst->b * cos((cryst->gamma - 90) * PI_RAD)) +
689 XDrawLine(display, window, cryst->gc,
690 (int) (cryst->offset_w +
691 inx * cryst->a - (int) (iny * cryst->b *
692 sin((cryst->gamma - 90) * PI_RAD))),
694 (int) (cryst->offset_w - iny * cryst->b *
695 sin((cryst->gamma - 90) * PI_RAD)),
699 for (inx = 1; inx <= cryst->nx; inx++) {
702 y_coor1 =cryst->win_height -
703 (int) (iny * cryst->b *
704 cos((cryst->gamma - 90) *
705 PI_RAD)) - cryst->offset_h;
706 y_coor2 =cryst->win_height - cryst->offset_h;
710 y_coor1 =(int) (iny * cryst->b *
711 cos((cryst->gamma - 90) *
712 PI_RAD)) + cryst->offset_h;
713 y_coor2 =cryst->offset_h;
715 XDrawLine(display, window, cryst->gc,
716 (int) (cryst->offset_w +
717 inx * cryst->a - (int) (iny * cryst->b *
718 sin((cryst->gamma - 90) * PI_RAD))),
720 cryst->offset_w + inx * cryst->a,
726 inx = NRAND(cryst->nx);
727 iny = NRAND(cryst->ny);
730 y_coor1 =cryst->win_height -
731 (int) (iny * cryst->b *
732 cos((cryst->gamma - 90) *
735 y_coor2 =cryst->win_height -
736 (int) ( ( iny + 1 ) * cryst->b *
737 cos((cryst->gamma - 90) *
743 y_coor1 =(int) (iny * cryst->b *
744 cos((cryst->gamma - 90) *
747 y_coor2 =(int) (( iny + 1 ) * cryst->b *
748 cos((cryst->gamma - 90) *
752 XDrawLine(display, window, cryst->gc,
753 cryst->offset_w + inx * cryst->a - (int) (iny * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
755 cryst->offset_w + (inx + 1) * cryst->a - (int) (iny * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
757 XDrawLine(display, window, cryst->gc,
758 cryst->offset_w + inx * cryst->a - (int) (iny * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
760 cryst->offset_w + inx * cryst->a - (int) ((iny + 1) * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
762 XDrawLine(display, window, cryst->gc,
763 cryst->offset_w + (inx + 1) * cryst->a - (int) (iny * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
765 cryst->offset_w + (inx + 1) * cryst->a - (int) ((iny + 1) * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
767 XDrawLine(display, window, cryst->gc,
768 cryst->offset_w + inx * cryst->a - (int) ((iny + 1) * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
770 cryst->offset_w + (inx + 1) * cryst->a - (int) ((iny + 1) * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
774 for (i = 0; i < cryst->num_atom; i++) {
777 atom0 = &cryst->atom[i];
778 if (MI_IS_INSTALL(mi) && MI_NPIXELS(mi) > 2) {
779 XSetForeground(display, cryst->gc, cryst->colors[atom0->colour].pixel);
781 XSetForeground(display, cryst->gc, atom0->colour);
783 crystal_drawatom(mi, atom0);
785 XSetFunction(display, cryst->gc, GXcopy);
789 release_crystal(ModeInfo * mi)
791 Display *display = MI_DISPLAY(mi);
793 if (crystals != NULL) {
796 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
797 crystalstruct *cryst = &crystals[screen];
799 if (MI_IS_INSTALL(mi) && MI_NPIXELS(mi) > 2) {
800 MI_WHITE_PIXEL(mi) = cryst->whitepixel;
801 MI_BLACK_PIXEL(mi) = cryst->blackpixel;
803 MI_FG_PIXEL(mi) = cryst->fg;
804 MI_BG_PIXEL(mi) = cryst->bg;
806 if (cryst->colors && cryst->ncolors && !cryst->no_colors)
807 free_colors(display, cryst->cmap, cryst->colors, cryst->ncolors);
809 (void) free((void *) cryst->colors);
810 XFreeColormap(display, cryst->cmap);
812 if (cryst->gc != NULL)
813 XFreeGC(display, cryst->gc);
814 if (cryst->atom != NULL)
815 (void) free((void *) cryst->atom);
817 (void) free((void *) crystals);
823 init_crystal(ModeInfo * mi)
825 Display *display = MI_DISPLAY(mi);
826 Window window = MI_WINDOW(mi);
827 crystalstruct *cryst;
828 int i, max_atoms, size_atom, neqv;
834 if (crystals == NULL) {
835 if ((crystals = (crystalstruct *) calloc(MI_NUM_SCREENS(mi),
836 sizeof (crystalstruct))) == NULL)
839 cryst = &crystals[MI_SCREEN(mi)];
842 if (MI_IS_INSTALL(mi) && MI_NPIXELS(mi) > 2) {
846 extern char *background;
847 extern char *foreground;
849 cryst->fg = MI_FG_PIXEL(mi);
850 cryst->bg = MI_BG_PIXEL(mi);
852 cryst->blackpixel = MI_BLACK_PIXEL(mi);
853 cryst->whitepixel = MI_WHITE_PIXEL(mi);
854 cryst->cmap = XCreateColormap(display, window,
855 MI_VISUAL(mi), AllocNone);
856 XSetWindowColormap(display, window, cryst->cmap);
857 (void) XParseColor(display, cryst->cmap, "black", &color);
858 (void) XAllocColor(display, cryst->cmap, &color);
859 MI_BLACK_PIXEL(mi) = color.pixel;
860 (void) XParseColor(display, cryst->cmap, "white", &color);
861 (void) XAllocColor(display, cryst->cmap, &color);
862 MI_WHITE_PIXEL(mi) = color.pixel;
864 (void) XParseColor(display, cryst->cmap, background, &color);
865 (void) XAllocColor(display, cryst->cmap, &color);
866 MI_BG_PIXEL(mi) = color.pixel;
867 (void) XParseColor(display, cryst->cmap, foreground, &color);
868 (void) XAllocColor(display, cryst->cmap, &color);
869 MI_FG_PIXEL(mi) = color.pixel;
874 if ((cryst->gc = XCreateGC(display, MI_WINDOW(mi),
875 (unsigned long) 0, (XGCValues *) NULL)) == None)
880 cryst->painted = False;
881 XSetFunction(display, cryst->gc, GXxor);
884 /*Set up crystal data */
885 cryst->direction = (LRAND() & 1) ? 1 : -1;
886 if (MI_IS_FULLRANDOM(mi)) {
888 cryst->unit_cell = True;
890 cryst->unit_cell = False;
892 cryst->unit_cell = unit_cell;
893 if (cryst->unit_cell) {
894 if (MI_IS_FULLRANDOM(mi)) {
896 cryst->grid_cell = True;
898 cryst->grid_cell = False;
900 cryst->grid_cell = grid_cell;
902 cryst->win_width = MI_WIDTH(mi);
903 cryst->win_height = MI_HEIGHT(mi);
904 cell_min = min(cryst->win_width / 2 + 1, MIN_CELL);
905 cell_min = min(cell_min, cryst->win_height / 2 + 1);
906 cryst->planegroup = NRAND(17);
907 cryst->invert = NRAND(2);
908 if (MI_IS_VERBOSE(mi))
909 (void) fprintf(stdout, "Selected plane group no %d\n",
910 cryst->planegroup + 1);
911 if (cryst->planegroup > 11)
912 cryst->gamma = 120.0;
913 else if (cryst->planegroup < 2)
914 cryst->gamma = 60.0 + NRAND(60);
917 neqv = numops[2 * cryst->planegroup] - numops[2 * cryst->planegroup + 1];
918 if (centro[cryst->planegroup] == True)
920 if (primitive[cryst->planegroup] == False)
927 cryst->nx = NRAND(-nx) + 1;
930 if (cryst->planegroup > 8)
931 cryst->ny = cryst->nx;
935 cryst->ny = NRAND(-ny) + 1;
938 neqv = neqv * cryst->nx * cryst->ny;
940 cryst->num_atom = MI_COUNT(mi);
941 max_atoms = MI_COUNT(mi);
942 if (cryst->num_atom == 0) {
943 cryst->num_atom = DEF_NUM_ATOM;
944 max_atoms = DEF_NUM_ATOM;
945 } else if (cryst->num_atom < 0) {
946 max_atoms = -cryst->num_atom;
947 cryst->num_atom = NRAND(-cryst->num_atom) + 1;
950 cryst->num_atom = cryst->num_atom / neqv + 1;
952 if (cryst->atom == NULL)
953 cryst->atom = (crystalatom *) calloc(max_atoms, sizeof (
957 if (cryst->planegroup < 13) {
961 if (cryst->planegroup < 10) {
962 cryst->b = cryst->win_height;
963 cryst->a = cryst->win_width;
965 cryst->b = min(cryst->win_height, cryst->win_width);
969 cryst->gamma = 120.0;
970 cryst->a = (int) (cryst->win_width * 2.0 / 3.0);
972 cryst->offset_h = (int) (cryst->b * 0.25 *
973 cos((cryst->gamma - 90) * PI_RAD));
974 cryst->offset_w = (int) (cryst->b * 0.5);
977 cryst->offset_w = -1;
978 while (cryst->offset_w < 4 || (int) (cryst->offset_w - cryst->b *
979 sin((cryst->gamma - 90) * PI_RAD)) < 4) {
980 cryst->b = NRAND((int) (cryst->win_height / (cos((cryst->gamma - 90) *
981 PI_RAD))) - cell_min) + cell_min;
982 if (cryst->planegroup > 8)
985 cryst->a = NRAND(cryst->win_width - cell_min) + cell_min;
986 cryst->offset_w = (int) ((cryst->win_width - (cryst->a - cryst->b *
987 sin((cryst->gamma - 90) *
990 cryst->offset_h = (int) ((cryst->win_height - cryst->b * cos((
991 cryst->gamma - 90) * PI_RAD)) / 2.0);
993 if (cryst->offset_h > 0)
994 cryst->offset_h = NRAND(2 * cryst->offset_h);
995 cryst->offset_w = (int) (cryst->win_width - cryst->a -
997 fabs(sin((cryst->gamma - 90) * PI_RAD)));
998 if (cryst->gamma > 90.0) {
999 if (cryst->offset_w > 0)
1000 cryst->offset_w = NRAND(cryst->offset_w) +
1001 (int) (cryst->b * sin((cryst->gamma - 90) * PI_RAD));
1003 cryst->offset_w = (int) (cryst->b * sin((cryst->gamma - 90) *
1005 } else if (cryst->offset_w > 0)
1006 cryst->offset_w = NRAND(cryst->offset_w);
1008 cryst->offset_w = 0;
1012 size_atom = min((int) ((float) (cryst->a) / 40.) + 1,
1013 (int) ((float) (cryst->b) / 40.) + 1);
1014 if (MI_SIZE(mi) < size_atom) {
1015 if (MI_SIZE(mi) < -size_atom)
1016 size_atom = -size_atom;
1018 size_atom = MI_SIZE(mi);
1020 cryst->a = cryst->a / cryst->nx;
1021 cryst->b = cryst->b / cryst->ny;
1022 if (cryst->unit_cell) {
1023 int y_coor1 , y_coor2;
1025 if (MI_NPIXELS(mi) > 2)
1026 XSetForeground(display, cryst->gc, MI_PIXEL(mi, NRAND(MI_NPIXELS(mi))));
1028 XSetForeground(display, cryst->gc, MI_WHITE_PIXEL(mi));
1029 if (cryst->grid_cell) {
1032 if ( cryst->invert )
1033 y_coor1 = y_coor2 = cryst->win_height - cryst->offset_h;
1035 y_coor1 = y_coor2 = cryst->offset_h;
1036 XDrawLine(display, window, cryst->gc, cryst->offset_w,
1037 y_coor1, cryst->offset_w + cryst->nx * cryst->a,
1039 if ( cryst->invert )
1041 y_coor1 = cryst->win_height - cryst->offset_h;
1042 y_coor2 = cryst->win_height - (int) (cryst->ny *
1044 cos((cryst->gamma - 90) * PI_RAD)) -
1049 y_coor1 = cryst->offset_h;
1050 y_coor2 = (int) (cryst->ny * cryst->b *
1051 cos((cryst->gamma - 90) * PI_RAD)) +
1054 XDrawLine(display, window, cryst->gc, cryst->offset_w,
1055 y_coor1, (int) (cryst->offset_w - cryst->ny * cryst->b *
1056 sin((cryst->gamma - 90) * PI_RAD)),
1059 for (iny = 1; iny <= cryst->ny; iny++) {
1060 if ( cryst->invert )
1062 y_coor1 = cryst->win_height -
1063 (int) (iny * cryst->b * cos((cryst->gamma - 90) *
1064 PI_RAD)) - cryst->offset_h;
1065 y_coor2 = cryst->win_height -
1066 (int) (iny * cryst->b * cos((cryst->gamma - 90) *
1072 y_coor1 = (int) (iny * cryst->b * cos((cryst->gamma - 90) *
1073 PI_RAD)) + cryst->offset_h;
1074 y_coor2 = (int) (iny * cryst->b * cos((cryst->gamma - 90) * PI_RAD)) +
1077 XDrawLine(display, window, cryst->gc,
1078 (int) (cryst->offset_w +
1079 inx * cryst->a - (int) (iny * cryst->b *
1080 sin((cryst->gamma - 90) * PI_RAD))),
1082 (int) (cryst->offset_w - iny * cryst->b *
1083 sin((cryst->gamma - 90) * PI_RAD)),
1087 for (inx = 1; inx <= cryst->nx; inx++) {
1088 if ( cryst->invert )
1090 y_coor1 =cryst->win_height -
1091 (int) (iny * cryst->b *
1092 cos((cryst->gamma - 90) *
1093 PI_RAD)) - cryst->offset_h;
1094 y_coor2 =cryst->win_height - cryst->offset_h;
1098 y_coor1 =(int) (iny * cryst->b *
1099 cos((cryst->gamma - 90) *
1100 PI_RAD)) + cryst->offset_h;
1101 y_coor2 =cryst->offset_h;
1103 XDrawLine(display, window, cryst->gc,
1104 (int) (cryst->offset_w +
1105 inx * cryst->a - (int) (iny * cryst->b *
1106 sin((cryst->gamma - 90) * PI_RAD))),
1108 cryst->offset_w + inx * cryst->a,
1114 inx = NRAND(cryst->nx);
1115 iny = NRAND(cryst->ny);
1116 if ( cryst->invert )
1118 y_coor1 =cryst->win_height -
1119 (int) (iny * cryst->b *
1120 cos((cryst->gamma - 90) *
1123 y_coor2 =cryst->win_height -
1124 (int) ( ( iny + 1 ) * cryst->b *
1125 cos((cryst->gamma - 90) *
1131 y_coor1 =(int) (iny * cryst->b *
1132 cos((cryst->gamma - 90) *
1135 y_coor2 =(int) (( iny + 1 ) * cryst->b *
1136 cos((cryst->gamma - 90) *
1140 XDrawLine(display, window, cryst->gc,
1141 cryst->offset_w + inx * cryst->a - (int) (iny * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
1143 cryst->offset_w + (inx + 1) * cryst->a - (int) (iny * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
1145 XDrawLine(display, window, cryst->gc,
1146 cryst->offset_w + inx * cryst->a - (int) (iny * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
1148 cryst->offset_w + inx * cryst->a - (int) ((iny + 1) * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
1150 XDrawLine(display, window, cryst->gc,
1151 cryst->offset_w + (inx + 1) * cryst->a - (int) (iny * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
1153 cryst->offset_w + (inx + 1) * cryst->a - (int) ((iny + 1) * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
1155 XDrawLine(display, window, cryst->gc,
1156 cryst->offset_w + inx * cryst->a - (int) ((iny + 1) * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
1158 cryst->offset_w + (inx + 1) * cryst->a - (int) ((iny + 1) * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
1162 if (MI_IS_INSTALL(mi) && MI_NPIXELS(mi) > 2) {
1163 /* Set up colour map */
1164 if (cryst->colors && cryst->ncolors && !cryst->no_colors)
1165 free_colors(display, cryst->cmap, cryst->colors, cryst->ncolors);
1167 (void) free((void *) cryst->colors);
1169 cryst->ncolors = MI_NCOLORS(mi);
1170 if (cryst->ncolors < 2)
1172 if (cryst->ncolors <= 2)
1173 cryst->mono_p = True;
1175 cryst->mono_p = False;
1180 cryst->colors = (XColor *) malloc(sizeof (*cryst->colors) * (cryst->ncolors + 1));
1181 cryst->cycle_p = has_writable_cells(mi->xgwa.screen, MI_VISUAL(mi));
1182 if (cryst->cycle_p) {
1183 if (MI_IS_FULLRANDOM(mi)) {
1185 cryst->cycle_p = False;
1187 cryst->cycle_p = True;
1189 cryst->cycle_p = cycle_p;
1192 if (!cryst->mono_p) {
1193 if (!(LRAND() % 10))
1194 make_random_colormap(MI_DISPLAY(mi), MI_VISUAL(mi), cryst->cmap, cryst->colors, &cryst->ncolors,
1195 True, True, &cryst->cycle_p, True);
1196 else if (!(LRAND() % 2))
1197 make_uniform_colormap(MI_DISPLAY(mi), MI_VISUAL(mi), cryst->cmap, cryst->colors, &cryst->ncolors,
1198 True, &cryst->cycle_p, True);
1200 make_smooth_colormap(MI_DISPLAY(mi), MI_VISUAL(mi), cryst->cmap, cryst->colors, &cryst->ncolors,
1201 True, &cryst->cycle_p, True);
1203 XInstallColormap(display, cryst->cmap);
1204 if (cryst->ncolors < 2) {
1206 cryst->no_colors = True;
1208 cryst->no_colors = False;
1209 if (cryst->ncolors <= 2)
1210 cryst->mono_p = True;
1213 cryst->cycle_p = False;
1216 for (i = 0; i < cryst->num_atom; i++) {
1219 atom0 = &cryst->atom[i];
1220 if (MI_IS_INSTALL(mi) && MI_NPIXELS(mi) > 2) {
1221 if (cryst->ncolors > 2)
1222 atom0->colour = NRAND(cryst->ncolors - 2) + 2;
1224 atom0->colour = 1; /* Just in case */
1225 XSetForeground(display, cryst->gc, cryst->colors[atom0->colour].pixel);
1227 if (MI_NPIXELS(mi) > 2)
1228 atom0->colour = MI_PIXEL(mi, NRAND(MI_NPIXELS(mi)));
1230 atom0->colour = 1; /*Xor'red so WHITE may not be appropriate */
1231 XSetForeground(display, cryst->gc, atom0->colour);
1233 atom0->x0 = NRAND(cryst->a);
1234 atom0->y0 = NRAND(cryst->b);
1235 atom0->velocity[0] = NRAND(7) - 3;
1236 atom0->velocity[1] = NRAND(7) - 3;
1237 atom0->velocity_a = (NRAND(7) - 3) * PI_RAD;
1238 atom0->angle = NRAND(90) * PI_RAD;
1239 atom0->at_type = NRAND(3);
1241 atom0->size_at = DEF_SIZ_ATOM;
1242 else if (size_atom > 0)
1243 atom0->size_at = size_atom;
1245 atom0->size_at = NRAND(-size_atom) + 1;
1247 if (atom0->at_type == 2)
1248 atom0->num_point = 3;
1250 atom0->num_point = 4;
1251 crystal_setupatom(atom0, cryst->gamma);
1252 crystal_drawatom(mi, atom0);
1254 XSync(display, False);
1255 XSetFunction(display, cryst->gc, GXcopy);