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" \
78 "*ignoreRotation: True \n" \
80 # define crystal_handle_event 0
81 # include "xlockmore.h" /* in xscreensaver distribution */
82 #else /* STANDALONE */
83 # include "xlock.h" /* in xlockmore distribution */
85 #endif /* STANDALONE */
87 #define DEF_CELL "True" /* Draw unit cell */
88 #define DEF_GRID "False" /* Draw unit all cell if DEF_CELL is True */
89 #define DEF_NX "-3" /* number of unit cells in x-direction */
90 #define DEF_NX1 1 /* number of unit cells in x-direction */
91 #define DEF_NY "-3" /* number of unit cells in y-direction */
92 #define DEF_NY1 1 /* number of unit cells in y-direction */
93 #define DEF_CENTRE "False"
94 #define DEF_MAXSIZE "False"
95 #define DEF_CYCLE "True"
98 #define NRAND(n) ( (n) ? (int) (LRAND() % (n)) : 0)
100 #define min(a,b) ((a) <= (b) ? (a) : (b))
104 static Bool unit_cell, grid_cell, centre, maxsize, cycle_p;
106 static XrmOptionDescRec opts[] =
108 {"-nx", "crystal.nx", XrmoptionSepArg, 0},
109 {"-ny", "crystal.ny", XrmoptionSepArg, 0},
110 {"-centre", ".crystal.centre", XrmoptionNoArg, "on"},
111 {"+centre", ".crystal.centre", XrmoptionNoArg, "off"},
112 {"-maxsize", ".crystal.maxsize", XrmoptionNoArg, "on"},
113 {"+maxsize", ".crystal.maxsize", XrmoptionNoArg, "off"},
114 {"-cell", ".crystal.cell", XrmoptionNoArg, "on"},
115 {"+cell", ".crystal.cell", XrmoptionNoArg, "off"},
116 {"-grid", ".crystal.grid", XrmoptionNoArg, "on"},
117 {"+grid", ".crystal.grid", XrmoptionNoArg, "off"},
118 {"-shift", ".crystal.shift", XrmoptionNoArg, "on"},
119 {"+shift", ".crystal.shift", XrmoptionNoArg, "off"}
122 static argtype vars[] =
124 {&nx, "nx", "nx", DEF_NX, t_Int},
125 {&ny, "ny", "ny", DEF_NY, t_Int},
126 {¢re, "centre", "Centre", DEF_CENTRE, t_Bool},
127 {&maxsize, "maxsize", "Maxsize", DEF_MAXSIZE, t_Bool},
128 {&unit_cell, "cell", "Cell", DEF_CELL, t_Bool},
129 {&grid_cell, "grid", "Grid", DEF_GRID, t_Bool},
130 {&cycle_p, "shift", "Shift", DEF_CYCLE, t_Bool}
132 static OptionStruct desc[] =
134 {"-nx num", "Number of unit cells in x-direction"},
135 {"-ny num", "Number of unit cells in y-direction"},
136 {"-/+centre", "turn on/off centering on screen"},
137 {"-/+maxsize", "turn on/off use of maximum part of screen"},
138 {"-/+cell", "turn on/off drawing of unit cell"},
139 {"-/+grid", "turn on/off drawing of grid of unit cells (if -cell is on)"},
140 {"-/+shift", "turn on/off colour cycling"}
143 ENTRYPOINT ModeSpecOpt crystal_opts =
144 {sizeof opts / sizeof opts[0], opts, sizeof vars / sizeof vars[0], vars, desc};
147 ModStruct crystal_description =
148 {"crystal", "init_crystal", "draw_crystal", "release_crystal",
149 "refresh_crystal", "init_crystal", NULL, &crystal_opts,
150 60000, -40, 200, -15, 64, 1.0, "",
151 "Shows polygons in 2D plane groups", 0, NULL};
155 #define DEF_NUM_ATOM 10
157 #define DEF_SIZ_ATOM 10
159 #define PI_RAD (M_PI / 180.0)
161 static Bool centro[17] =
182 static Bool primitive[17] =
203 static short numops[34] =
224 static short operation[114] =
248 unsigned long colour;
249 int x0, y0, velocity[2];
250 float angle, velocity_a;
251 int num_point, at_type, size_at;
257 int win_width, win_height, num_atom;
258 int planegroup, a, b, offset_w, offset_h, nx, ny;
262 Bool unit_cell, grid_cell;
266 Bool cycle_p, mono_p, no_colors;
267 unsigned long blackpixel, whitepixel, fg, bg;
268 int direction, invert;
271 static crystalstruct *crystals = NULL;
274 trans_coor(XPoint * xyp, XPoint * new_xyp, int num_points,
279 for (i = 0; i <= num_points; i++) {
280 new_xyp[i].x = xyp[i].x +
281 (int) (xyp[i].y * sin((gamma - 90.0) * PI_RAD));
282 new_xyp[i].y = (int) (xyp[i].y / cos((gamma - 90.0) * PI_RAD));
287 trans_coor_back(XPoint * xyp, XPoint * new_xyp,
288 int num_points, float gamma, int offset_w, int offset_h ,
289 int winheight , int invert )
293 for (i = 0; i <= num_points; i++) {
294 new_xyp[i].y = (int) (xyp[i].y * cos((gamma - 90) * PI_RAD)) +
296 new_xyp[i].x = xyp[i].x - (int) (xyp[i].y * sin((gamma - 90.0)
297 * PI_RAD)) + offset_w;
298 if ( invert ) new_xyp[i].y = winheight - new_xyp[i].y;
303 crystal_setupatom(crystalatom * atom0, float gamma)
308 y0 = (int) (atom0->y0 * cos((gamma - 90) * PI_RAD));
309 x0 = atom0->x0 - (int) (atom0->y0 * sin((gamma - 90.0) * PI_RAD));
310 switch (atom0->at_type) {
311 case 0: /* rectangles */
312 xy[0].x = x0 + (int) (2 * atom0->size_at *
314 (int) (atom0->size_at * sin(atom0->angle));
315 xy[0].y = y0 + (int) (atom0->size_at *
317 (int) (2 * atom0->size_at * sin(atom0->angle));
318 xy[1].x = x0 + (int) (2 * atom0->size_at *
320 (int) (atom0->size_at * sin(atom0->angle));
321 xy[1].y = y0 - (int) (atom0->size_at *
323 (int) (2 * atom0->size_at * sin(atom0->angle));
324 xy[2].x = x0 - (int) (2 * atom0->size_at *
326 (int) (atom0->size_at * sin(atom0->angle));
327 xy[2].y = y0 - (int) (atom0->size_at *
329 (int) (2 * atom0->size_at * sin(atom0->angle));
330 xy[3].x = x0 - (int) (2 * atom0->size_at *
332 (int) (atom0->size_at * sin(atom0->angle));
333 xy[3].y = y0 + (int) (atom0->size_at *
335 (int) (2 * atom0->size_at *
339 trans_coor(xy, atom0->xy, 4, gamma);
341 case 1: /* squares */
342 xy[0].x = x0 + (int) (1.5 * atom0->size_at *
344 (int) (1.5 * atom0->size_at *
346 xy[0].y = y0 + (int) (1.5 * atom0->size_at *
348 (int) (1.5 * atom0->size_at *
350 xy[1].x = x0 + (int) (1.5 * atom0->size_at *
352 (int) (1.5 * atom0->size_at *
354 xy[1].y = y0 - (int) (1.5 * atom0->size_at *
356 (int) (1.5 * atom0->size_at *
358 xy[2].x = x0 - (int) (1.5 * atom0->size_at *
360 (int) (1.5 * atom0->size_at *
362 xy[2].y = y0 - (int) (1.5 * atom0->size_at *
364 (int) (1.5 * atom0->size_at *
366 xy[3].x = x0 - (int) (1.5 * atom0->size_at *
368 (int) (1.5 * atom0->size_at *
370 xy[3].y = y0 + (int) (1.5 * atom0->size_at *
372 (int) (1.5 * atom0->size_at *
376 trans_coor(xy, atom0->xy, 4, gamma);
378 case 2: /* triangles */
379 xy[0].x = x0 + (int) (1.5 * atom0->size_at *
381 xy[0].y = y0 + (int) (1.5 * atom0->size_at *
383 xy[1].x = x0 + (int) (1.5 * atom0->size_at *
385 (int) (1.5 * atom0->size_at *
387 xy[1].y = y0 - (int) (1.5 * atom0->size_at *
389 (int) (1.5 * atom0->size_at *
391 xy[2].x = x0 - (int) (1.5 * atom0->size_at *
393 (int) (1.5 * atom0->size_at *
395 xy[2].y = y0 - (int) (1.5 * atom0->size_at *
397 (int) (1.5 * atom0->size_at *
401 trans_coor(xy, atom0->xy, 3, gamma);
407 crystal_drawatom(ModeInfo * mi, crystalatom * atom0)
409 crystalstruct *cryst;
410 Display *display = MI_DISPLAY(mi);
411 Window window = MI_WINDOW(mi);
414 cryst = &crystals[MI_SCREEN(mi)];
415 for (j = numops[2 * cryst->planegroup + 1];
416 j < numops[2 * cryst->planegroup]; j++) {
417 XPoint xy[5], new_xy[5];
421 xtrans = operation[j * 6] * atom0->x0 + operation[j * 6 + 1] *
422 atom0->y0 + (int) (operation[j * 6 + 4] * cryst->a /
424 ytrans = operation[j * 6 + 2] * atom0->x0 + operation[j * 6 +
425 3] * atom0->y0 + (int) (operation[j * 6 + 5] *
428 if (xtrans < -cryst->a)
429 xtrans = 2 * cryst->a;
432 } else if (xtrans >= cryst->a)
438 else if (ytrans >= cryst->b)
442 for (k = 0; k < atom0->num_point; k++) {
443 xy[k].x = operation[j * 6] * atom0->xy[k].x +
444 operation[j * 6 + 1] *
445 atom0->xy[k].y + (int) (operation[j * 6 + 4] *
448 xy[k].y = operation[j * 6 + 2] * atom0->xy[k].x +
449 operation[j * 6 + 3] *
450 atom0->xy[k].y + (int) (operation[j * 6 + 5] *
454 xy[atom0->num_point].x = xy[0].x;
455 xy[atom0->num_point].y = xy[0].y;
456 for (l = 0; l < cryst->nx; l++) {
457 for (m = 0; m < cryst->ny; m++) {
459 for (k = 0; k <= atom0->num_point; k++) {
460 xy_1[k].x = xy[k].x + l * cryst->a;
461 xy_1[k].y = xy[k].y + m * cryst->b;
463 trans_coor_back(xy_1, new_xy, atom0->num_point,
464 cryst->gamma, cryst->offset_w,
468 XFillPolygon(display, window, cryst->gc, new_xy,
469 atom0->num_point, Convex, CoordModeOrigin);
472 if (centro[cryst->planegroup] == True) {
473 for (k = 0; k <= atom0->num_point; k++) {
474 xy[k].x = cryst->a - xy[k].x;
475 xy[k].y = cryst->b - xy[k].y;
477 for (l = 0; l < cryst->nx; l++) {
478 for (m = 0; m < cryst->ny; m++) {
480 for (k = 0; k <= atom0->num_point; k++) {
481 xy_1[k].x = xy[k].x + l * cryst->a;
482 xy_1[k].y = xy[k].y + m * cryst->b;
484 trans_coor_back(xy_1, new_xy, atom0->num_point,
490 XFillPolygon(display, window, cryst->gc,
492 atom0->num_point, Convex,
497 if (primitive[cryst->planegroup] == False) {
498 if (xy[atom0->num_point].x >= (int) (cryst->a / 2.0))
499 xtrans = (int) (-cryst->a / 2.0);
501 xtrans = (int) (cryst->a / 2.0);
502 if (xy[atom0->num_point].y >= (int) (cryst->b / 2.0))
503 ytrans = (int) (-cryst->b / 2.0);
505 ytrans = (int) (cryst->b / 2.0);
506 for (k = 0; k <= atom0->num_point; k++) {
507 xy[k].x = xy[k].x + xtrans;
508 xy[k].y = xy[k].y + ytrans;
510 for (l = 0; l < cryst->nx; l++) {
511 for (m = 0; m < cryst->ny; m++) {
513 for (k = 0; k <= atom0->num_point; k++) {
514 xy_1[k].x = xy[k].x + l * cryst->a;
515 xy_1[k].y = xy[k].y + m * cryst->b;
517 trans_coor_back(xy_1, new_xy, atom0->num_point,
523 XFillPolygon(display, window, cryst->gc,
525 atom0->num_point, Convex,
529 if (centro[cryst->planegroup] == True) {
532 for (k = 0; k <= atom0->num_point; k++) {
533 xy1[k].x = cryst->a - xy[k].x;
534 xy1[k].y = cryst->b - xy[k].y;
536 for (l = 0; l < cryst->nx; l++) {
537 for (m = 0; m < cryst->ny; m++) {
539 for (k = 0; k <= atom0->num_point; k++) {
540 xy_1[k].x = xy1[k].x + l * cryst->a;
541 xy_1[k].y = xy1[k].y + m * cryst->b;
543 trans_coor_back(xy_1, new_xy, atom0->num_point,
549 XFillPolygon(display, window,
551 new_xy, atom0->num_point,
552 Convex, CoordModeOrigin);
560 ENTRYPOINT void init_crystal(ModeInfo * mi);
561 ENTRYPOINT void release_crystal(ModeInfo * mi);
565 draw_crystal(ModeInfo * mi)
567 Display *display = MI_DISPLAY(mi);
568 crystalstruct *cryst = &crystals[MI_SCREEN(mi)];
571 #ifdef HAVE_COCOA /* Don't second-guess Quartz's double-buffering */
572 XClearWindow(MI_DISPLAY(mi), MI_WINDOW(mi));
575 if (cryst->no_colors) {
580 cryst->painted = True;
581 MI_IS_DRAWN(mi) = True;
582 XSetFunction(display, cryst->gc, GXxor);
585 if (cryst->cycle_p) {
586 rotate_colors(mi->xgwa.screen, cryst->cmap,
587 cryst->colors, cryst->ncolors,
589 if (!(LRAND() % 1000))
590 cryst->direction = -cryst->direction;
592 for (i = 0; i < cryst->num_atom; i++) {
595 atom0 = &cryst->atom[i];
597 if (MI_IS_INSTALL(mi) && MI_NPIXELS(mi) > 2) {
598 XSetForeground(display, cryst->gc, cryst->colors[atom0->colour].pixel);
600 XSetForeground(display, cryst->gc, atom0->colour);
602 crystal_drawatom(mi, atom0);
603 atom0->velocity[0] += NRAND(3) - 1;
604 atom0->velocity[0] = MAX(-20, MIN(20, atom0->velocity[0]));
605 atom0->velocity[1] += NRAND(3) - 1;
606 atom0->velocity[1] = MAX(-20, MIN(20, atom0->velocity[1]));
607 atom0->x0 += atom0->velocity[0];
608 /*if (cryst->gamma == 90.0) { */
610 atom0->x0 += cryst->a;
611 else if (atom0->x0 >= cryst->a)
612 atom0->x0 -= cryst->a;
613 atom0->y0 += atom0->velocity[1];
615 atom0->y0 += cryst->b;
616 else if (atom0->y0 >= cryst->b)
617 atom0->y0 -= cryst->b;
619 atom0->velocity_a += ((float) NRAND(1001) - 500.0) / 2000.0;
620 atom0->angle += atom0->velocity_a;
621 crystal_setupatom(atom0, cryst->gamma);
622 crystal_drawatom(mi, atom0);
624 XSetFunction(display, cryst->gc, GXcopy);
628 refresh_crystal(ModeInfo * mi)
630 Display *display = MI_DISPLAY(mi);
631 Window window = MI_WINDOW(mi);
632 crystalstruct *cryst = &crystals[MI_SCREEN(mi)];
638 XSetFunction(display, cryst->gc, GXxor);
640 if (cryst->unit_cell) {
641 int y_coor1 , y_coor2;
643 if (MI_NPIXELS(mi) > 2)
644 XSetForeground(display, cryst->gc, MI_PIXEL(mi, NRAND(MI_NPIXELS(mi))));
646 XSetForeground(display, cryst->gc, MI_WHITE_PIXEL(mi));
647 if (cryst->grid_cell) {
651 y_coor1 = y_coor2 = cryst->win_height - cryst->offset_h;
653 y_coor1 = y_coor2 = cryst->offset_h;
654 XDrawLine(display, window, cryst->gc, cryst->offset_w,
655 y_coor1, cryst->offset_w + cryst->nx * cryst->a,
659 y_coor1 = cryst->win_height - cryst->offset_h;
660 y_coor2 = cryst->win_height - (int) (cryst->ny *
662 cos((cryst->gamma - 90) * PI_RAD)) -
667 y_coor1 = cryst->offset_h;
668 y_coor2 = (int) (cryst->ny * cryst->b *
669 cos((cryst->gamma - 90) * PI_RAD)) +
672 XDrawLine(display, window, cryst->gc, cryst->offset_w,
673 y_coor1, (int) (cryst->offset_w - cryst->ny * cryst->b *
674 sin((cryst->gamma - 90) * PI_RAD)),
677 for (iny = 1; iny <= cryst->ny; iny++) {
680 y_coor1 = cryst->win_height -
681 (int) (iny * cryst->b * cos((cryst->gamma - 90) *
682 PI_RAD)) - cryst->offset_h;
683 y_coor2 = cryst->win_height -
684 (int) (iny * cryst->b * cos((cryst->gamma - 90) *
690 y_coor1 = (int) (iny * cryst->b * cos((cryst->gamma - 90) *
691 PI_RAD)) + cryst->offset_h;
692 y_coor2 = (int) (iny * cryst->b * cos((cryst->gamma - 90) * PI_RAD)) +
695 XDrawLine(display, window, cryst->gc,
696 (int) (cryst->offset_w +
697 inx * cryst->a - (int) (iny * cryst->b *
698 sin((cryst->gamma - 90) * PI_RAD))),
700 (int) (cryst->offset_w - iny * cryst->b *
701 sin((cryst->gamma - 90) * PI_RAD)),
705 for (inx = 1; inx <= cryst->nx; inx++) {
708 y_coor1 =cryst->win_height -
709 (int) (iny * cryst->b *
710 cos((cryst->gamma - 90) *
711 PI_RAD)) - cryst->offset_h;
712 y_coor2 =cryst->win_height - cryst->offset_h;
716 y_coor1 =(int) (iny * cryst->b *
717 cos((cryst->gamma - 90) *
718 PI_RAD)) + cryst->offset_h;
719 y_coor2 =cryst->offset_h;
721 XDrawLine(display, window, cryst->gc,
722 (int) (cryst->offset_w +
723 inx * cryst->a - (int) (iny * cryst->b *
724 sin((cryst->gamma - 90) * PI_RAD))),
726 cryst->offset_w + inx * cryst->a,
732 inx = NRAND(cryst->nx);
733 iny = NRAND(cryst->ny);
736 y_coor1 =cryst->win_height -
737 (int) (iny * cryst->b *
738 cos((cryst->gamma - 90) *
741 y_coor2 =cryst->win_height -
742 (int) ( ( iny + 1 ) * cryst->b *
743 cos((cryst->gamma - 90) *
749 y_coor1 =(int) (iny * cryst->b *
750 cos((cryst->gamma - 90) *
753 y_coor2 =(int) (( iny + 1 ) * cryst->b *
754 cos((cryst->gamma - 90) *
758 XDrawLine(display, window, cryst->gc,
759 cryst->offset_w + inx * cryst->a - (int) (iny * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
761 cryst->offset_w + (inx + 1) * cryst->a - (int) (iny * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
763 XDrawLine(display, window, cryst->gc,
764 cryst->offset_w + inx * cryst->a - (int) (iny * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
766 cryst->offset_w + inx * cryst->a - (int) ((iny + 1) * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
768 XDrawLine(display, window, cryst->gc,
769 cryst->offset_w + (inx + 1) * cryst->a - (int) (iny * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
771 cryst->offset_w + (inx + 1) * cryst->a - (int) ((iny + 1) * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
773 XDrawLine(display, window, cryst->gc,
774 cryst->offset_w + inx * cryst->a - (int) ((iny + 1) * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
776 cryst->offset_w + (inx + 1) * cryst->a - (int) ((iny + 1) * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
780 for (i = 0; i < cryst->num_atom; i++) {
783 atom0 = &cryst->atom[i];
784 if (MI_IS_INSTALL(mi) && MI_NPIXELS(mi) > 2) {
785 XSetForeground(display, cryst->gc, cryst->colors[atom0->colour].pixel);
787 XSetForeground(display, cryst->gc, atom0->colour);
789 crystal_drawatom(mi, atom0);
791 XSetFunction(display, cryst->gc, GXcopy);
795 release_crystal(ModeInfo * mi)
797 Display *display = MI_DISPLAY(mi);
799 if (crystals != NULL) {
802 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
803 crystalstruct *cryst = &crystals[screen];
805 if (MI_IS_INSTALL(mi) && MI_NPIXELS(mi) > 2) {
806 MI_WHITE_PIXEL(mi) = cryst->whitepixel;
807 MI_BLACK_PIXEL(mi) = cryst->blackpixel;
809 MI_FG_PIXEL(mi) = cryst->fg;
810 MI_BG_PIXEL(mi) = cryst->bg;
812 if (cryst->colors && cryst->ncolors && !cryst->no_colors)
813 free_colors(mi->xgwa.screen, cryst->cmap, cryst->colors,
816 (void) free((void *) cryst->colors);
817 #if 0 /* #### wrong! -jwz */
818 XFreeColormap(display, cryst->cmap);
821 if (cryst->gc != NULL)
822 XFreeGC(display, cryst->gc);
823 if (cryst->atom != NULL)
824 (void) free((void *) cryst->atom);
826 (void) free((void *) crystals);
832 init_crystal(ModeInfo * mi)
834 Display *display = MI_DISPLAY(mi);
835 Window window = MI_WINDOW(mi);
836 crystalstruct *cryst;
837 int i, max_atoms, size_atom, neqv;
843 if (crystals == NULL) {
844 if ((crystals = (crystalstruct *) calloc(MI_NUM_SCREENS(mi),
845 sizeof (crystalstruct))) == NULL)
848 cryst = &crystals[MI_SCREEN(mi)];
851 if (MI_IS_INSTALL(mi) && MI_NPIXELS(mi) > 2) {
855 extern char *background;
856 extern char *foreground;
858 cryst->fg = MI_FG_PIXEL(mi);
859 cryst->bg = MI_BG_PIXEL(mi);
861 cryst->blackpixel = MI_BLACK_PIXEL(mi);
862 cryst->whitepixel = MI_WHITE_PIXEL(mi);
863 #if 0 /* #### wrong! -jwz */
864 cryst->cmap = XCreateColormap(display, window,
865 MI_VISUAL(mi), AllocNone);
866 XSetWindowColormap(display, window, cryst->cmap);
868 cryst->cmap = mi->xgwa.colormap;
870 (void) XParseColor(display, cryst->cmap, "black", &color);
871 (void) XAllocColor(display, cryst->cmap, &color);
872 MI_BLACK_PIXEL(mi) = color.pixel;
873 (void) XParseColor(display, cryst->cmap, "white", &color);
874 (void) XAllocColor(display, cryst->cmap, &color);
875 MI_WHITE_PIXEL(mi) = color.pixel;
877 (void) XParseColor(display, cryst->cmap, background, &color);
878 (void) XAllocColor(display, cryst->cmap, &color);
879 MI_BG_PIXEL(mi) = color.pixel;
880 (void) XParseColor(display, cryst->cmap, foreground, &color);
881 (void) XAllocColor(display, cryst->cmap, &color);
882 MI_FG_PIXEL(mi) = color.pixel;
887 if ((cryst->gc = XCreateGC(display, MI_WINDOW(mi),
888 (unsigned long) 0, (XGCValues *) NULL)) == None)
893 cryst->painted = False;
894 XSetFunction(display, cryst->gc, GXxor);
897 /*Set up crystal data */
898 cryst->direction = (LRAND() & 1) ? 1 : -1;
899 if (MI_IS_FULLRANDOM(mi)) {
901 cryst->unit_cell = True;
903 cryst->unit_cell = False;
905 cryst->unit_cell = unit_cell;
906 if (cryst->unit_cell) {
907 if (MI_IS_FULLRANDOM(mi)) {
909 cryst->grid_cell = True;
911 cryst->grid_cell = False;
913 cryst->grid_cell = grid_cell;
915 cryst->win_width = MI_WIDTH(mi);
916 cryst->win_height = MI_HEIGHT(mi);
917 cell_min = min(cryst->win_width / 2 + 1, MIN_CELL);
918 cell_min = min(cell_min, cryst->win_height / 2 + 1);
919 cryst->planegroup = NRAND(17);
920 cryst->invert = NRAND(2);
921 if (MI_IS_VERBOSE(mi))
922 (void) fprintf(stdout, "Selected plane group no %d\n",
923 cryst->planegroup + 1);
924 if (cryst->planegroup > 11)
925 cryst->gamma = 120.0;
926 else if (cryst->planegroup < 2)
927 cryst->gamma = 60.0 + NRAND(60);
930 neqv = numops[2 * cryst->planegroup] - numops[2 * cryst->planegroup + 1];
931 if (centro[cryst->planegroup] == True)
933 if (primitive[cryst->planegroup] == False)
940 cryst->nx = NRAND(-nx) + 1;
943 if (cryst->planegroup > 8)
944 cryst->ny = cryst->nx;
948 cryst->ny = NRAND(-ny) + 1;
951 neqv = neqv * cryst->nx * cryst->ny;
953 cryst->num_atom = MI_COUNT(mi);
954 max_atoms = MI_COUNT(mi);
955 if (cryst->num_atom == 0) {
956 cryst->num_atom = DEF_NUM_ATOM;
957 max_atoms = DEF_NUM_ATOM;
958 } else if (cryst->num_atom < 0) {
959 max_atoms = -cryst->num_atom;
960 cryst->num_atom = NRAND(-cryst->num_atom) + 1;
963 cryst->num_atom = cryst->num_atom / neqv + 1;
965 if (cryst->atom == NULL)
966 cryst->atom = (crystalatom *) calloc(max_atoms, sizeof (
970 if (cryst->planegroup < 13) {
974 if (cryst->planegroup < 10) {
975 cryst->b = cryst->win_height;
976 cryst->a = cryst->win_width;
978 cryst->b = min(cryst->win_height, cryst->win_width);
982 cryst->gamma = 120.0;
983 cryst->a = (int) (cryst->win_width * 2.0 / 3.0);
985 cryst->offset_h = (int) (cryst->b * 0.25 *
986 cos((cryst->gamma - 90) * PI_RAD));
987 cryst->offset_w = (int) (cryst->b * 0.5);
991 cryst->offset_w = -1;
992 while (max_repeat-- &&
993 (cryst->offset_w < 4 || (int) (cryst->offset_w - cryst->b *
994 sin((cryst->gamma - 90) * PI_RAD)) < 4)
996 cryst->b = NRAND((int) (cryst->win_height / (cos((cryst->gamma - 90) *
997 PI_RAD))) - cell_min) + cell_min;
998 if (cryst->planegroup > 8)
1001 cryst->a = NRAND(cryst->win_width - cell_min) + cell_min;
1002 cryst->offset_w = (int) ((cryst->win_width - (cryst->a - cryst->b *
1003 sin((cryst->gamma - 90) *
1006 cryst->offset_h = (int) ((cryst->win_height - cryst->b * cos((
1007 cryst->gamma - 90) * PI_RAD)) / 2.0);
1009 if (cryst->offset_h > 0)
1010 cryst->offset_h = NRAND(2 * cryst->offset_h);
1011 cryst->offset_w = (int) (cryst->win_width - cryst->a -
1013 fabs(sin((cryst->gamma - 90) * PI_RAD)));
1014 if (cryst->gamma > 90.0) {
1015 if (cryst->offset_w > 0)
1016 cryst->offset_w = NRAND(cryst->offset_w) +
1017 (int) (cryst->b * sin((cryst->gamma - 90) * PI_RAD));
1019 cryst->offset_w = (int) (cryst->b * sin((cryst->gamma - 90) *
1021 } else if (cryst->offset_w > 0)
1022 cryst->offset_w = NRAND(cryst->offset_w);
1024 cryst->offset_w = 0;
1028 size_atom = min((int) ((float) (cryst->a) / 40.) + 1,
1029 (int) ((float) (cryst->b) / 40.) + 1);
1030 if (MI_SIZE(mi) < size_atom) {
1031 if (MI_SIZE(mi) < -size_atom)
1032 size_atom = -size_atom;
1034 size_atom = MI_SIZE(mi);
1036 cryst->a = cryst->a / cryst->nx;
1037 cryst->b = cryst->b / cryst->ny;
1038 if (cryst->unit_cell) {
1039 int y_coor1 , y_coor2;
1041 if (MI_NPIXELS(mi) > 2)
1042 XSetForeground(display, cryst->gc, MI_PIXEL(mi, NRAND(MI_NPIXELS(mi))));
1044 XSetForeground(display, cryst->gc, MI_WHITE_PIXEL(mi));
1045 if (cryst->grid_cell) {
1048 if ( cryst->invert )
1049 y_coor1 = y_coor2 = cryst->win_height - cryst->offset_h;
1051 y_coor1 = y_coor2 = cryst->offset_h;
1052 XDrawLine(display, window, cryst->gc, cryst->offset_w,
1053 y_coor1, cryst->offset_w + cryst->nx * cryst->a,
1055 if ( cryst->invert )
1057 y_coor1 = cryst->win_height - cryst->offset_h;
1058 y_coor2 = cryst->win_height - (int) (cryst->ny *
1060 cos((cryst->gamma - 90) * PI_RAD)) -
1065 y_coor1 = cryst->offset_h;
1066 y_coor2 = (int) (cryst->ny * cryst->b *
1067 cos((cryst->gamma - 90) * PI_RAD)) +
1070 XDrawLine(display, window, cryst->gc, cryst->offset_w,
1071 y_coor1, (int) (cryst->offset_w - cryst->ny * cryst->b *
1072 sin((cryst->gamma - 90) * PI_RAD)),
1075 for (iny = 1; iny <= cryst->ny; iny++) {
1076 if ( cryst->invert )
1078 y_coor1 = cryst->win_height -
1079 (int) (iny * cryst->b * cos((cryst->gamma - 90) *
1080 PI_RAD)) - cryst->offset_h;
1081 y_coor2 = cryst->win_height -
1082 (int) (iny * cryst->b * cos((cryst->gamma - 90) *
1088 y_coor1 = (int) (iny * cryst->b * cos((cryst->gamma - 90) *
1089 PI_RAD)) + cryst->offset_h;
1090 y_coor2 = (int) (iny * cryst->b * cos((cryst->gamma - 90) * PI_RAD)) +
1093 XDrawLine(display, window, cryst->gc,
1094 (int) (cryst->offset_w +
1095 inx * cryst->a - (int) (iny * cryst->b *
1096 sin((cryst->gamma - 90) * PI_RAD))),
1098 (int) (cryst->offset_w - iny * cryst->b *
1099 sin((cryst->gamma - 90) * PI_RAD)),
1103 for (inx = 1; inx <= cryst->nx; inx++) {
1104 if ( cryst->invert )
1106 y_coor1 =cryst->win_height -
1107 (int) (iny * cryst->b *
1108 cos((cryst->gamma - 90) *
1109 PI_RAD)) - cryst->offset_h;
1110 y_coor2 =cryst->win_height - cryst->offset_h;
1114 y_coor1 =(int) (iny * cryst->b *
1115 cos((cryst->gamma - 90) *
1116 PI_RAD)) + cryst->offset_h;
1117 y_coor2 =cryst->offset_h;
1119 XDrawLine(display, window, cryst->gc,
1120 (int) (cryst->offset_w +
1121 inx * cryst->a - (int) (iny * cryst->b *
1122 sin((cryst->gamma - 90) * PI_RAD))),
1124 cryst->offset_w + inx * cryst->a,
1130 inx = NRAND(cryst->nx);
1131 iny = NRAND(cryst->ny);
1132 if ( cryst->invert )
1134 y_coor1 =cryst->win_height -
1135 (int) (iny * cryst->b *
1136 cos((cryst->gamma - 90) *
1139 y_coor2 =cryst->win_height -
1140 (int) ( ( iny + 1 ) * cryst->b *
1141 cos((cryst->gamma - 90) *
1147 y_coor1 =(int) (iny * cryst->b *
1148 cos((cryst->gamma - 90) *
1151 y_coor2 =(int) (( iny + 1 ) * cryst->b *
1152 cos((cryst->gamma - 90) *
1156 XDrawLine(display, window, cryst->gc,
1157 cryst->offset_w + inx * cryst->a - (int) (iny * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
1159 cryst->offset_w + (inx + 1) * cryst->a - (int) (iny * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
1161 XDrawLine(display, window, cryst->gc,
1162 cryst->offset_w + inx * cryst->a - (int) (iny * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
1164 cryst->offset_w + inx * cryst->a - (int) ((iny + 1) * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
1166 XDrawLine(display, window, cryst->gc,
1167 cryst->offset_w + (inx + 1) * cryst->a - (int) (iny * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
1169 cryst->offset_w + (inx + 1) * cryst->a - (int) ((iny + 1) * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
1171 XDrawLine(display, window, cryst->gc,
1172 cryst->offset_w + inx * cryst->a - (int) ((iny + 1) * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
1174 cryst->offset_w + (inx + 1) * cryst->a - (int) ((iny + 1) * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
1178 if (MI_IS_INSTALL(mi) && MI_NPIXELS(mi) > 2) {
1179 /* Set up colour map */
1180 if (cryst->colors && cryst->ncolors && !cryst->no_colors)
1181 free_colors(mi->xgwa.screen, cryst->cmap,
1182 cryst->colors, cryst->ncolors);
1184 (void) free((void *) cryst->colors);
1186 cryst->ncolors = MI_NCOLORS(mi);
1187 if (cryst->ncolors < 2)
1189 if (cryst->ncolors <= 2)
1190 cryst->mono_p = True;
1192 cryst->mono_p = False;
1197 cryst->colors = (XColor *) malloc(sizeof (*cryst->colors) * (cryst->ncolors + 1));
1198 cryst->cycle_p = has_writable_cells(mi->xgwa.screen, MI_VISUAL(mi));
1199 if (cryst->cycle_p) {
1200 if (MI_IS_FULLRANDOM(mi)) {
1202 cryst->cycle_p = False;
1204 cryst->cycle_p = True;
1206 cryst->cycle_p = cycle_p;
1209 if (!cryst->mono_p) {
1210 if (!(LRAND() % 10))
1211 make_random_colormap(mi->xgwa.screen, MI_VISUAL(mi),
1212 cryst->cmap, cryst->colors,
1214 True, True, &cryst->cycle_p, True);
1215 else if (!(LRAND() % 2))
1216 make_uniform_colormap(mi->xgwa.screen, MI_VISUAL(mi),
1217 cryst->cmap, cryst->colors,
1218 &cryst->ncolors, True,
1219 &cryst->cycle_p, True);
1221 make_smooth_colormap(mi->xgwa.screen, MI_VISUAL(mi),
1222 cryst->cmap, cryst->colors,
1224 True, &cryst->cycle_p, True);
1226 #if 0 /* #### wrong! -jwz */
1227 XInstallColormap(display, cryst->cmap);
1229 if (cryst->ncolors < 2) {
1231 cryst->no_colors = True;
1233 cryst->no_colors = False;
1234 if (cryst->ncolors <= 2)
1235 cryst->mono_p = True;
1238 cryst->cycle_p = False;
1241 for (i = 0; i < cryst->num_atom; i++) {
1244 atom0 = &cryst->atom[i];
1245 if (MI_IS_INSTALL(mi) && MI_NPIXELS(mi) > 2) {
1246 if (cryst->ncolors > 2)
1247 atom0->colour = NRAND(cryst->ncolors - 2) + 2;
1249 atom0->colour = 1; /* Just in case */
1250 XSetForeground(display, cryst->gc, cryst->colors[atom0->colour].pixel);
1252 if (MI_NPIXELS(mi) > 2)
1253 atom0->colour = MI_PIXEL(mi, NRAND(MI_NPIXELS(mi)));
1255 atom0->colour = 1; /*Xor'red so WHITE may not be appropriate */
1256 XSetForeground(display, cryst->gc, atom0->colour);
1258 atom0->x0 = NRAND(cryst->a);
1259 atom0->y0 = NRAND(cryst->b);
1260 atom0->velocity[0] = NRAND(7) - 3;
1261 atom0->velocity[1] = NRAND(7) - 3;
1262 atom0->velocity_a = (NRAND(7) - 3) * PI_RAD;
1263 atom0->angle = NRAND(90) * PI_RAD;
1264 atom0->at_type = NRAND(3);
1266 atom0->size_at = DEF_SIZ_ATOM;
1267 else if (size_atom > 0)
1268 atom0->size_at = size_atom;
1270 atom0->size_at = NRAND(-size_atom) + 1;
1272 if (atom0->at_type == 2)
1273 atom0->num_point = 3;
1275 atom0->num_point = 4;
1276 crystal_setupatom(atom0, cryst->gamma);
1277 crystal_drawatom(mi, atom0);
1279 XSetFunction(display, cryst->gc, GXcopy);
1283 reshape_crystal(ModeInfo * mi, int width, int height)
1285 release_crystal(mi);
1289 XSCREENSAVER_MODULE ("Crystal", crystal)