summaryrefslogtreecommitdiffstats
path: root/hacks/crystal.c
blob: 9f33529c5ebdb52f96c5a621394cdd8ab43da2e6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
/* -*- Mode: C; tab-width: 4 -*- */
/* crystal --- polygons moving according to plane group rules */

#if 0
static const char sccsid[] = "@(#)crystal.c	4.12 98/09/10 xlockmore";
#endif

/*-
 * Copyright (c) 1997 by Jouk Jansen <joukj@crys.chem.uva.nl>
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose and without fee is hereby granted,
 * provided that the above copyright notice appear in all copies and that
 * both that copyright notice and this permission notice appear in
 * supporting documentation.
 *
 * This file is provided AS IS with no warranties of any kind.  The author
 * shall have no liability with respect to the infringement of copyrights,
 * trade secrets or any patents by this file or any part thereof.  In no
 * event will the author be liable for any lost revenue or profits or
 * other special, indirect and consequential damages.
 *
 * The author should like to be notified if changes have been made to the
 * routine.  Response will only be guaranteed when a VMS version of the 
 * program is available.
 *
 * A moving polygon-mode. The polygons obey 2D-planegroup symmetry.
 *
 * The groupings of the cells fall in 3 categories:
 *   oblique groups 1 and 2 where the angle gamma ranges from 60 to 120 degrees
 *   square groups 3 through 11 where the angle gamma is 90 degrees
 *   hexagonal groups 12 through 17 where the angle gamma is 120 degrees
 *
 * Revision History:
 * 03-Dec-98: Random inversion of y-axis included to simulate hexagonal groups
 *             with an angle of 60 degrees.
 * 10-Sep-98: new colour scheme
 * 24-Feb-98: added option centre which turns on/off forcing the centre of
 *              the screen to be used
 *            added option maxsize which forces the dimensions to be chasen
 *              in such ua way that the largest possible part of the screen is
 *              used 
 *            When only one unit cell is drawn, it is chosen at random
 * 18-Feb-98: added support for negative numbers with -nx and -ny meaning
 *            "random" choice with given maximum
 *            added +/-grid option. If -cell is specified this option
 *            determines if one or all unit cells are drawn.
 *            -batchcount is now a parameter for all the objects on the screen
 *            instead of the number of "unique" objects
 *            The maximum size of the objects now scales with the part
 *            of the screen used.
 *            fixed "size" problem. Now very small non-vissable objects
 *            are not allowed
 * 13-Feb-98: randomized the unit cell size
 *            runtime options -/+cell (turn on/off unit cell drawing)
 *             -nx num (number of translational symmetries in x-direction
 *             -ny num (idem y-direction but ignored for square and
 *               hexagonal space groups
 *               i.e. try xlock -mode crystal -nx 3 -ny 2
 *            Fullrandom overrules the -/+cell option.
 * 05-Feb-98: Revision + bug repairs
 *            shows unit cell
 *            use part of the screen for unit cell
 *            in hexagonal and square groups a&b axis forced to be equal
 *            cell angle for oblique groups randomly chosen between 60 and 120
 *   bugs solved: planegroups with cell angles <> 90.0 now work properly
 * 19-Sep-97: Added remaining hexagonal groups
 * 12-Jun-97: Created
 */

#ifdef STANDALONE
# define DEFAULTS		"*delay:		60000	\n" \
						 "*count:		 -500	\n" \
						 "*cycles:		  200	\n" \
						 "*size:		  -15	\n" \
						 "*ncolors:		  100	\n" \
						 "*fpsSolid:	   True	\n" \
						 "*ignoreRotation: True \n" \

# define release_crystal 0
# define reshape_crystal 0
# define crystal_handle_event 0
# include "xlockmore.h"		/* in xscreensaver distribution */
#else /* STANDALONE */
# include "xlock.h"			/* in xlockmore distribution */
# include "color.h"
#endif /* STANDALONE */

#define DEF_CELL "True"		/* Draw unit cell */
#define DEF_GRID "False"	/* Draw unit all cell if DEF_CELL is True */
#define DEF_NX "-3"		/* number of unit cells in x-direction */
#define DEF_NX1 1		/* number of unit cells in x-direction */
#define DEF_NY "-3"		/* number of unit cells in y-direction */
#define DEF_NY1 1		/* number of unit cells in y-direction */
#define DEF_CENTRE "False"
#define DEF_MAXSIZE "False"
#define DEF_CYCLE "True"

#undef NRAND
#define NRAND(n)           ( (n) ? (int) (LRAND() % (n)) : 0)

#define min(a,b) ((a) <= (b) ? (a) : (b))

static int  nx, ny;

static Bool unit_cell, grid_cell, centre, maxsize, cycle_p;

static XrmOptionDescRec opts[] =
{
	{"-nx", "crystal.nx", XrmoptionSepArg, 0},
	{"-ny", "crystal.ny", XrmoptionSepArg, 0},
	{"-centre", ".crystal.centre", XrmoptionNoArg, "on"},
	{"+centre", ".crystal.centre", XrmoptionNoArg, "off"},
	{"-maxsize", ".crystal.maxsize", XrmoptionNoArg, "on"},
	{"+maxsize", ".crystal.maxsize", XrmoptionNoArg, "off"},
	{"-cell", ".crystal.cell", XrmoptionNoArg, "on"},
	{"+cell", ".crystal.cell", XrmoptionNoArg, "off"},
	{"-grid", ".crystal.grid", XrmoptionNoArg, "on"},
	{"+grid", ".crystal.grid", XrmoptionNoArg, "off"},
	{"-shift", ".crystal.shift", XrmoptionNoArg, "on"},
	{"+shift", ".crystal.shift", XrmoptionNoArg, "off"}
};

static argtype vars[] =
{
	{&nx, "nx", "nx", DEF_NX, t_Int},
	{&ny, "ny", "ny", DEF_NY, t_Int},
	{&centre, "centre", "Centre", DEF_CENTRE, t_Bool},
	{&maxsize, "maxsize", "Maxsize", DEF_MAXSIZE, t_Bool},
	{&unit_cell, "cell", "Cell", DEF_CELL, t_Bool},
	{&grid_cell, "grid", "Grid", DEF_GRID, t_Bool},
	{&cycle_p, "shift", "Shift", DEF_CYCLE, t_Bool}
};
static OptionStruct desc[] =
{
	{"-nx num", "Number of unit cells in x-direction"},
	{"-ny num", "Number of unit cells in y-direction"},
	{"-/+centre", "turn on/off centering on screen"},
	{"-/+maxsize", "turn on/off use of maximum part of screen"},
	{"-/+cell", "turn on/off drawing of unit cell"},
   {"-/+grid", "turn on/off drawing of grid of unit cells (if -cell is on)"},
	{"-/+shift", "turn on/off colour cycling"}
};

ENTRYPOINT ModeSpecOpt crystal_opts =
{sizeof opts / sizeof opts[0], opts, sizeof vars / sizeof vars[0], vars, desc};

#ifdef USE_MODULES
ModStruct   crystal_description =
{"crystal", "init_crystal", "draw_crystal", NULL,
 "refresh_crystal", "init_crystal", "free_crystal", &crystal_opts,
 60000, -40, 200, -15, 64, 1.0, "",
 "Shows polygons in 2D plane groups", 0, NULL};

#endif

#define DEF_NUM_ATOM 10

#define DEF_SIZ_ATOM 10

#define PI_RAD (M_PI / 180.0)

static Bool centro[17] =
{
	False,
	True,
	False,
	False,
	False,
	True,
	True,
	True,
	True,
	True,
	True,
	True,
	False,
	False,
	False,
	True,
	True
};

static Bool primitive[17] =
{
	True,
	True,
	True,
	True,
	False,
	True,
	True,
	True,
	False,
	True,
	True,
	True,
	True,
	True,
	True,
	True,
	True
};

static short numops[34] =
{
	1, 0,
	1, 0,
	9, 7,
	2, 0,
	9, 7,
	9, 7,
	4, 2,
	5, 3,
	9, 7,
	8, 6,
	10, 6,
	8, 4,
	16, 13,
	19, 13,
	16, 10,
	19, 13,
	19, 13
};

static short operation[114] =
{
	1, 0, 0, 1, 0, 0,
	-1, 0, 0, 1, 0, 1,
	-1, 0, 0, 1, 1, 0,
	1, 0, 0, 1, 0, 0,
	-1, 0, 0, 1, 1, 1,
	1, 0, 0, 1, 1, 1,
	0, -1, 1, 0, 0, 0,
	1, 0, 0, 1, 0, 0,
	-1, 0, 0, 1, 0, 0,
	0, 1, 1, 0, 0, 0,
	-1, 0, -1, 1, 0, 0,
	1, -1, 0, -1, 0, 0,
	0, 1, 1, 0, 0, 0,
	0, -1, 1, -1, 0, 0,
	-1, 1, -1, 0, 0, 0,
	1, 0, 0, 1, 0, 0,
	0, -1, -1, 0, 0, 0,
	-1, 1, 0, 1, 0, 0,
	1, 0, 1, -1, 0, 0
};

typedef struct {
	unsigned long colour;
	int         x0, y0, velocity[2];
	float       angle, velocity_a;
	int         num_point, at_type, size_at;
	XPoint      xy[5];
} crystalatom;

typedef struct {
	Bool        painted;
	int         win_width, win_height, num_atom;
	int         planegroup, a, b, offset_w, offset_h, nx, ny;
	float       gamma;
	crystalatom *atom;
	GC          gc;
	Bool        unit_cell, grid_cell;
	Colormap    cmap;
	XColor     *colors;
	int         ncolors;
	Bool        cycle_p, mono_p, no_colors;
	unsigned long blackpixel, whitepixel, fg, bg;
	int         direction, invert;
	unsigned long grid_pixel;
	int         inx, iny;
} crystalstruct;

static crystalstruct *crystals = NULL;

static void
trans_coor(XPoint * xyp, XPoint * new_xyp, int num_points,
	   float gamma)
{
	int         i;

	for (i = 0; i <= num_points; i++) {
		new_xyp[i].x = xyp[i].x +
			(int) (xyp[i].y * sin((gamma - 90.0) * PI_RAD));
		new_xyp[i].y = (int) (xyp[i].y / cos((gamma - 90.0) * PI_RAD));
	}
}

static void
trans_coor_back(XPoint * xyp, XPoint * new_xyp,
		int num_points, float gamma, int offset_w, int offset_h ,
		int winheight , int invert )
{
	int         i;

	for (i = 0; i <= num_points; i++) {
		new_xyp[i].y = (int) (xyp[i].y * cos((gamma - 90) * PI_RAD)) +
			offset_h;
		new_xyp[i].x = xyp[i].x - (int) (xyp[i].y * sin((gamma - 90.0)
						       * PI_RAD)) + offset_w;
	   if ( invert ) new_xyp[i].y = winheight - new_xyp[i].y;
	}
}

static void
crystal_setupatom(crystalatom * atom0, float gamma)
{
	XPoint      xy[5];
	int         x0, y0;

	y0 = (int) (atom0->y0 * cos((gamma - 90) * PI_RAD));
	x0 = atom0->x0 - (int) (atom0->y0 * sin((gamma - 90.0) * PI_RAD));
	switch (atom0->at_type) {
		case 0:	/* rectangles */
			xy[0].x = x0 + (int) (2 * atom0->size_at *
					      cos(atom0->angle)) +
				(int) (atom0->size_at * sin(atom0->angle));
			xy[0].y = y0 + (int) (atom0->size_at *
					      cos(atom0->angle)) -
				(int) (2 * atom0->size_at * sin(atom0->angle));
			xy[1].x = x0 + (int) (2 * atom0->size_at *
					      cos(atom0->angle)) -
				(int) (atom0->size_at * sin(atom0->angle));
			xy[1].y = y0 - (int) (atom0->size_at *
					      cos(atom0->angle)) -
				(int) (2 * atom0->size_at * sin(atom0->angle));
			xy[2].x = x0 - (int) (2 * atom0->size_at *
					      cos(atom0->angle)) -
				(int) (atom0->size_at * sin(atom0->angle));
			xy[2].y = y0 - (int) (atom0->size_at *
					      cos(atom0->angle)) +
				(int) (2 * atom0->size_at * sin(atom0->angle));
			xy[3].x = x0 - (int) (2 * atom0->size_at *
					      cos(atom0->angle)) +
				(int) (atom0->size_at * sin(atom0->angle));
			xy[3].y = y0 + (int) (atom0->size_at *
					      cos(atom0->angle)) +
				(int) (2 * atom0->size_at *
				       sin(atom0->angle));
			xy[4].x = xy[0].x;
			xy[4].y = xy[0].y;
			trans_coor(xy, atom0->xy, 4, gamma);
			return;
		case 1:	/* squares */
			xy[0].x = x0 + (int) (1.5 * atom0->size_at *
					      cos(atom0->angle)) +
				(int) (1.5 * atom0->size_at *
				       sin(atom0->angle));
			xy[0].y = y0 + (int) (1.5 * atom0->size_at *
					      cos(atom0->angle)) -
				(int) (1.5 * atom0->size_at *
				       sin(atom0->angle));
			xy[1].x = x0 + (int) (1.5 * atom0->size_at *
					      cos(atom0->angle)) -
				(int) (1.5 * atom0->size_at *
				       sin(atom0->angle));
			xy[1].y = y0 - (int) (1.5 * atom0->size_at *
					      cos(atom0->angle)) -
				(int) (1.5 * atom0->size_at *
				       sin(atom0->angle));
			xy[2].x = x0 - (int) (1.5 * atom0->size_at *
					      cos(atom0->angle)) -
				(int) (1.5 * atom0->size_at *
				       sin(atom0->angle));
			xy[2].y = y0 - (int) (1.5 * atom0->size_at *
					      cos(atom0->angle)) +
				(int) (1.5 * atom0->size_at *
				       sin(atom0->angle));
			xy[3].x = x0 - (int) (1.5 * atom0->size_at *
					      cos(atom0->angle)) +
				(int) (1.5 * atom0->size_at *
				       sin(atom0->angle));
			xy[3].y = y0 + (int) (1.5 * atom0->size_at *
					      cos(atom0->angle)) +
				(int) (1.5 * atom0->size_at *
				       sin(atom0->angle));
			xy[4].x = xy[0].x;
			xy[4].y = xy[0].y;
			trans_coor(xy, atom0->xy, 4, gamma);
			return;
		case 2:	/* triangles */
			xy[0].x = x0 + (int) (1.5 * atom0->size_at *
					      sin(atom0->angle));
			xy[0].y = y0 + (int) (1.5 * atom0->size_at *
					      cos(atom0->angle));
			xy[1].x = x0 + (int) (1.5 * atom0->size_at *
					      cos(atom0->angle)) -
				(int) (1.5 * atom0->size_at *
				       sin(atom0->angle));
			xy[1].y = y0 - (int) (1.5 * atom0->size_at *
					      cos(atom0->angle)) -
				(int) (1.5 * atom0->size_at *
				       sin(atom0->angle));
			xy[2].x = x0 - (int) (1.5 * atom0->size_at *
					      cos(atom0->angle)) -
				(int) (1.5 * atom0->size_at *
				       sin(atom0->angle));
			xy[2].y = y0 - (int) (1.5 * atom0->size_at *
					      cos(atom0->angle)) +
				(int) (1.5 * atom0->size_at *
				       sin(atom0->angle));
			xy[3].x = xy[0].x;
			xy[3].y = xy[0].y;
			trans_coor(xy, atom0->xy, 3, gamma);
			return;
	}
}

static void
crystal_drawatom(ModeInfo * mi, crystalatom * atom0)
{
	crystalstruct *cryst;
	Display    *display = MI_DISPLAY(mi);
	Window      window = MI_WINDOW(mi);
	int         j, k, l, m;

	cryst = &crystals[MI_SCREEN(mi)];
	for (j = numops[2 * cryst->planegroup + 1];
	     j < numops[2 * cryst->planegroup]; j++) {
		XPoint      xy[5], new_xy[5];
		XPoint      xy_1[5];
		int         xtrans, ytrans;

		xtrans = operation[j * 6] * atom0->x0 + operation[j * 6 + 1] *
			atom0->y0 + (int) (operation[j * 6 + 4] * cryst->a /
					   2.0);
		ytrans = operation[j * 6 + 2] * atom0->x0 + operation[j * 6 +
			       3] * atom0->y0 + (int) (operation[j * 6 + 5] *
						       cryst->b / 2.0);
		if (xtrans < 0) {
			if (xtrans < -cryst->a)
				xtrans = 2 * cryst->a;
			else
				xtrans = cryst->a;
		} else if (xtrans >= cryst->a)
			xtrans = -cryst->a;
		else
			xtrans = 0;
		if (ytrans < 0)
			ytrans = cryst->b;
		else if (ytrans >= cryst->b)
			ytrans = -cryst->b;
		else
			ytrans = 0;
		for (k = 0; k < atom0->num_point; k++) {
			xy[k].x = operation[j * 6] * atom0->xy[k].x +
				operation[j * 6 + 1] *
				atom0->xy[k].y + (int) (operation[j * 6 + 4] *
							cryst->a / 2.0) +
				xtrans;
			xy[k].y = operation[j * 6 + 2] * atom0->xy[k].x +
				operation[j * 6 + 3] *
				atom0->xy[k].y + (int) (operation[j * 6 + 5] *
							cryst->b / 2.0) +
				ytrans;
		}
		xy[atom0->num_point].x = xy[0].x;
		xy[atom0->num_point].y = xy[0].y;
		for (l = 0; l < cryst->nx; l++) {
			for (m = 0; m < cryst->ny; m++) {

				for (k = 0; k <= atom0->num_point; k++) {
					xy_1[k].x = xy[k].x + l * cryst->a;
					xy_1[k].y = xy[k].y + m * cryst->b;
				}
				trans_coor_back(xy_1, new_xy, atom0->num_point,
						cryst->gamma, cryst->offset_w,
						cryst->offset_h ,
						cryst->win_height,
						cryst->invert);
				XFillPolygon(display, window, cryst->gc, new_xy,
				  atom0->num_point, Convex, CoordModeOrigin);
			}
		}
		if (centro[cryst->planegroup] == True) {
			for (k = 0; k <= atom0->num_point; k++) {
				xy[k].x = cryst->a - xy[k].x;
				xy[k].y = cryst->b - xy[k].y;
			}
			for (l = 0; l < cryst->nx; l++) {
				for (m = 0; m < cryst->ny; m++) {

					for (k = 0; k <= atom0->num_point; k++) {
						xy_1[k].x = xy[k].x + l * cryst->a;
						xy_1[k].y = xy[k].y + m * cryst->b;
					}
					trans_coor_back(xy_1, new_xy, atom0->num_point,
							cryst->gamma,
							cryst->offset_w,
							cryst->offset_h ,
							cryst->win_height ,
							cryst->invert);
					XFillPolygon(display, window, cryst->gc,
						     new_xy,
						     atom0->num_point, Convex,
						     CoordModeOrigin);
				}
			}
		}
		if (primitive[cryst->planegroup] == False) {
			if (xy[atom0->num_point].x >= (int) (cryst->a / 2.0))
				xtrans = (int) (-cryst->a / 2.0);
			else
				xtrans = (int) (cryst->a / 2.0);
			if (xy[atom0->num_point].y >= (int) (cryst->b / 2.0))
				ytrans = (int) (-cryst->b / 2.0);
			else
				ytrans = (int) (cryst->b / 2.0);
			for (k = 0; k <= atom0->num_point; k++) {
				xy[k].x = xy[k].x + xtrans;
				xy[k].y = xy[k].y + ytrans;
			}
			for (l = 0; l < cryst->nx; l++) {
				for (m = 0; m < cryst->ny; m++) {

					for (k = 0; k <= atom0->num_point; k++) {
						xy_1[k].x = xy[k].x + l * cryst->a;
						xy_1[k].y = xy[k].y + m * cryst->b;
					}
					trans_coor_back(xy_1, new_xy, atom0->num_point,
							cryst->gamma,
							cryst->offset_w,
							cryst->offset_h ,
							cryst->win_height,
							cryst->invert);
					XFillPolygon(display, window, cryst->gc,
						     new_xy,
						     atom0->num_point, Convex,
						     CoordModeOrigin);
				}
			}
			if (centro[cryst->planegroup] == True) {
				XPoint      xy1[5];

				for (k = 0; k <= atom0->num_point; k++) {
					xy1[k].x = cryst->a - xy[k].x;
					xy1[k].y = cryst->b - xy[k].y;
				}
				for (l = 0; l < cryst->nx; l++) {
					for (m = 0; m < cryst->ny; m++) {

						for (k = 0; k <= atom0->num_point; k++) {
							xy_1[k].x = xy1[k].x + l * cryst->a;
							xy_1[k].y = xy1[k].y + m * cryst->b;
						}
						trans_coor_back(xy_1, new_xy, atom0->num_point,
								cryst->gamma,
								cryst->offset_w,
								cryst->offset_h ,
								cryst->win_height,
								cryst->invert);
						XFillPolygon(display, window,
							     cryst->gc,
						    new_xy, atom0->num_point,
						    Convex, CoordModeOrigin);
					}
				}
			}
		}
	}
}

ENTRYPOINT void init_crystal(ModeInfo * mi);


ENTRYPOINT void
draw_crystal(ModeInfo * mi)
{
	Display    *display = MI_DISPLAY(mi);
	Window      window = MI_WINDOW(mi);
	crystalstruct *cryst = &crystals[MI_SCREEN(mi)];
	int         i;

#ifdef HAVE_JWXYZ	/* Don't second-guess Quartz's double-buffering */
    XClearWindow(MI_DISPLAY(mi), MI_WINDOW(mi));
#endif

	if (cryst->no_colors) {
		init_crystal(mi);
		return;
	}

	/* Moved from init_crystal because you can't draw after MI_CLEARWINDOW in
	   XScreenSaver. - Dave Odell <dmo2118@gmail.com>
	 */
	if (cryst->unit_cell
#ifndef HAVE_JWXYZ
		&& !cryst->painted
#endif
		) {
	   int y_coor1 , y_coor2;
	   
		XSetForeground(display, cryst->gc, cryst->grid_pixel);
		if (cryst->grid_cell) {
			int         inx, iny;

		   if ( cryst->invert )
		     y_coor1 = y_coor2 = cryst->win_height - cryst->offset_h;
		   else
		     y_coor1 = y_coor2 = cryst->offset_h;
			XDrawLine(display, window, cryst->gc, cryst->offset_w,
				  y_coor1, cryst->offset_w + cryst->nx * cryst->a,
				  y_coor2);
		   if ( cryst->invert )
		     {
			y_coor1 = cryst->win_height - cryst->offset_h;
			y_coor2 = cryst->win_height - (int) (cryst->ny *
							     cryst->b *
					 cos((cryst->gamma - 90) * PI_RAD)) -
			  cryst->offset_h;
		     }
		   else
		     {
			y_coor1 = cryst->offset_h;
			y_coor2 = (int) (cryst->ny * cryst->b *
					 cos((cryst->gamma - 90) * PI_RAD)) +
			  cryst->offset_h;
		     }
			XDrawLine(display, window, cryst->gc, cryst->offset_w,
				  y_coor1, (int) (cryst->offset_w - cryst->ny * cryst->b *
					  sin((cryst->gamma - 90) * PI_RAD)),
				  y_coor2);
			inx = cryst->nx;
			for (iny = 1; iny <= cryst->ny; iny++) {
		   if ( cryst->invert )
		     {
			y_coor1 = cryst->win_height -
			  (int) (iny * cryst->b * cos((cryst->gamma - 90) *
						  PI_RAD)) - cryst->offset_h;
			y_coor2 = cryst->win_height -
			  (int) (iny * cryst->b * cos((cryst->gamma - 90) *
						      PI_RAD)) -
			  cryst->offset_h;
		     }
		   else
		     {
			y_coor1 = (int) (iny * cryst->b * cos((cryst->gamma - 90) *
						  PI_RAD)) + cryst->offset_h;
			y_coor2 = (int) (iny * cryst->b * cos((cryst->gamma - 90) * PI_RAD)) +
					  cryst->offset_h;
		     }
				XDrawLine(display, window, cryst->gc,
					  (int) (cryst->offset_w +
				     inx * cryst->a - (int) (iny * cryst->b *
					 sin((cryst->gamma - 90) * PI_RAD))),
					  y_coor1,
				    (int) (cryst->offset_w - iny * cryst->b *
					   sin((cryst->gamma - 90) * PI_RAD)),
					  y_coor2);
			}
			iny = cryst->ny;
			for (inx = 1; inx <= cryst->nx; inx++) {
			   if ( cryst->invert )
			     {
				y_coor1 =cryst->win_height -
				  (int) (iny * cryst->b *
						cos((cryst->gamma - 90) *
						    PI_RAD)) - cryst->offset_h;
				y_coor2 =cryst->win_height - cryst->offset_h;
			     }
			   else
			     {
				y_coor1 =(int) (iny * cryst->b *
						cos((cryst->gamma - 90) *
						    PI_RAD)) + cryst->offset_h;
				y_coor2 =cryst->offset_h;
			     }
				XDrawLine(display, window, cryst->gc,
					  (int) (cryst->offset_w +
				     inx * cryst->a - (int) (iny * cryst->b *
					 sin((cryst->gamma - 90) * PI_RAD))),
					  y_coor1,
					  cryst->offset_w + inx * cryst->a,
					  y_coor2);
			}
		} else {
		   if ( cryst->invert )
		     {
			y_coor1 =cryst->win_height -
			  (int) (cryst->iny * cryst->b *
						  cos((cryst->gamma - 90) *
						      PI_RAD)) -
			  cryst->offset_h;
			y_coor2 =cryst->win_height -
			  (int) ( ( cryst->iny + 1 ) * cryst->b *
						  cos((cryst->gamma - 90) *
						      PI_RAD)) -
			  cryst->offset_h;
		     }
		   else
		     {
			y_coor1 =(int) (cryst->iny * cryst->b *
						  cos((cryst->gamma - 90) *
						      PI_RAD)) +
			  cryst->offset_h;
			y_coor2 =(int) (( cryst->iny + 1 ) * cryst->b *
						  cos((cryst->gamma - 90) *
						      PI_RAD)) +
			  cryst->offset_h;
		     }
			XDrawLine(display, window, cryst->gc,
				  cryst->offset_w + cryst->inx * cryst->a - (int) (cryst->iny * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
				  y_coor1,
				  cryst->offset_w + (cryst->inx + 1) * cryst->a - (int) (cryst->iny * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
				  y_coor1);
			XDrawLine(display, window, cryst->gc,
				  cryst->offset_w + cryst->inx * cryst->a - (int) (cryst->iny * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
				  y_coor1,
				  cryst->offset_w + cryst->inx * cryst->a - (int) ((cryst->iny + 1) * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
				  y_coor2);
			XDrawLine(display, window, cryst->gc,
				  cryst->offset_w + (cryst->inx + 1) * cryst->a - (int) (cryst->iny * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
				  y_coor1,
				  cryst->offset_w + (cryst->inx + 1) * cryst->a - (int) ((cryst->iny + 1) * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
				  y_coor2);
			XDrawLine(display, window, cryst->gc,
				  cryst->offset_w + cryst->inx * cryst->a - (int) ((cryst->iny + 1) * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
				  y_coor2,
				  cryst->offset_w + (cryst->inx + 1) * cryst->a - (int) ((cryst->iny + 1) * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
				  y_coor2);
		}
	}

	XSetFunction(display, cryst->gc, GXxor);

/* Rotate colours */
	if (cryst->cycle_p) {
		rotate_colors(mi->xgwa.screen, cryst->cmap,
                      cryst->colors, cryst->ncolors,
			      cryst->direction);
		if (!(LRAND() % 1000))
			cryst->direction = -cryst->direction;
	}
	for (i = 0; i < cryst->num_atom; i++) {
		crystalatom *atom0;

		atom0 = &cryst->atom[i];

		if (MI_IS_INSTALL(mi) && MI_NPIXELS(mi) > 2) {
			XSetForeground(display, cryst->gc, cryst->colors[atom0->colour].pixel);
		} else {
			XSetForeground(display, cryst->gc, atom0->colour);
		}
#ifndef HAVE_JWXYZ
		if(cryst->painted)
			crystal_drawatom(mi, atom0);
#endif
		atom0->velocity[0] += NRAND(3) - 1;
		atom0->velocity[0] = MAX(-20, MIN(20, atom0->velocity[0]));
		atom0->velocity[1] += NRAND(3) - 1;
		atom0->velocity[1] = MAX(-20, MIN(20, atom0->velocity[1]));
		atom0->x0 += atom0->velocity[0];
		/*if (cryst->gamma == 90.0) { */
		if (atom0->x0 < 0)
			atom0->x0 += cryst->a;
		else if (atom0->x0 >= cryst->a)
			atom0->x0 -= cryst->a;
		atom0->y0 += atom0->velocity[1];
		if (atom0->y0 < 0)
			atom0->y0 += cryst->b;
		else if (atom0->y0 >= cryst->b)
			atom0->y0 -= cryst->b;
		/*} */
		atom0->velocity_a += ((float) NRAND(1001) - 500.0) / 2000.0;
		atom0->angle += atom0->velocity_a;
		crystal_setupatom(atom0, cryst->gamma);
		crystal_drawatom(mi, atom0);
	}
	XSetFunction(display, cryst->gc, GXcopy);
	cryst->painted = True;
	MI_IS_DRAWN(mi) = True;
}

#ifndef STANDALONE
ENTRYPOINT void
refresh_crystal(ModeInfo * mi)
{
	Display    *display = MI_DISPLAY(mi);
	Window      window = MI_WINDOW(mi);
	crystalstruct *cryst = &crystals[MI_SCREEN(mi)];
	int         i;

	if (!cryst->painted)
		return;
	MI_CLEARWINDOW(mi);
	XSetFunction(display, cryst->gc, GXxor);

	if (cryst->unit_cell) {
	   int y_coor1 , y_coor2;
	   
		if (MI_NPIXELS(mi) > 2)
			XSetForeground(display, cryst->gc, MI_PIXEL(mi, NRAND(MI_NPIXELS(mi))));
		else
			XSetForeground(display, cryst->gc, MI_WHITE_PIXEL(mi));
		if (cryst->grid_cell) {
			int         inx, iny;

		   if ( cryst->invert )
		     y_coor1 = y_coor2 = cryst->win_height - cryst->offset_h;
		   else
		     y_coor1 = y_coor2 = cryst->offset_h;
			XDrawLine(display, window, cryst->gc, cryst->offset_w,
				  y_coor1, cryst->offset_w + cryst->nx * cryst->a,
				  y_coor2);
		   if ( cryst->invert )
		     {
			y_coor1 = cryst->win_height - cryst->offset_h;
			y_coor2 = cryst->win_height - (int) (cryst->ny *
							     cryst->b *
					 cos((cryst->gamma - 90) * PI_RAD)) -
			  cryst->offset_h;
		     }
		   else
		     {
			y_coor1 = cryst->offset_h;
			y_coor2 = (int) (cryst->ny * cryst->b *
					 cos((cryst->gamma - 90) * PI_RAD)) +
			  cryst->offset_h;
		     }
			XDrawLine(display, window, cryst->gc, cryst->offset_w,
				  y_coor1, (int) (cryst->offset_w - cryst->ny * cryst->b *
					  sin((cryst->gamma - 90) * PI_RAD)),
				  y_coor2);
			inx = cryst->nx;
			for (iny = 1; iny <= cryst->ny; iny++) {
		   if ( cryst->invert )
		     {
			y_coor1 = cryst->win_height -
			  (int) (iny * cryst->b * cos((cryst->gamma - 90) *
						  PI_RAD)) - cryst->offset_h;
			y_coor2 = cryst->win_height -
			  (int) (iny * cryst->b * cos((cryst->gamma - 90) *
						      PI_RAD)) -
			  cryst->offset_h;
		     }
		   else
		     {
			y_coor1 = (int) (iny * cryst->b * cos((cryst->gamma - 90) *
						  PI_RAD)) + cryst->offset_h;
			y_coor2 = (int) (iny * cryst->b * cos((cryst->gamma - 90) * PI_RAD)) +
					  cryst->offset_h;
		     }
				XDrawLine(display, window, cryst->gc,
					  (int) (cryst->offset_w +
				     inx * cryst->a - (int) (iny * cryst->b *
					 sin((cryst->gamma - 90) * PI_RAD))),
					  y_coor1,
				    (int) (cryst->offset_w - iny * cryst->b *
					   sin((cryst->gamma - 90) * PI_RAD)),
					  y_coor2);
			}
			iny = cryst->ny;
			for (inx = 1; inx <= cryst->nx; inx++) {
			   if ( cryst->invert )
			     {
				y_coor1 =cryst->win_height -
				  (int) (iny * cryst->b *
						cos((cryst->gamma - 90) *
						    PI_RAD)) - cryst->offset_h;
				y_coor2 =cryst->win_height - cryst->offset_h;
			     }
			   else
			     {
				y_coor1 =(int) (iny * cryst->b *
						cos((cryst->gamma - 90) *
						    PI_RAD)) + cryst->offset_h;
				y_coor2 =cryst->offset_h;
			     }
				XDrawLine(display, window, cryst->gc,
					  (int) (cryst->offset_w +
				     inx * cryst->a - (int) (iny * cryst->b *
					 sin((cryst->gamma - 90) * PI_RAD))),
					  y_coor1,
					  cryst->offset_w + inx * cryst->a,
					  y_coor2);
			}
		} else {
			int         inx, iny;

			inx = NRAND(cryst->nx);
			iny = NRAND(cryst->ny);
		   if ( cryst->invert )
		     {
			y_coor1 =cryst->win_height -
			  (int) (iny * cryst->b *
						  cos((cryst->gamma - 90) *
						      PI_RAD)) -
			  cryst->offset_h;
			y_coor2 =cryst->win_height -
			  (int) ( ( iny + 1 ) * cryst->b *
						  cos((cryst->gamma - 90) *
						      PI_RAD)) -
			  cryst->offset_h;
		     }
		   else
		     {
			y_coor1 =(int) (iny * cryst->b *
						  cos((cryst->gamma - 90) *
						      PI_RAD)) +
			  cryst->offset_h;
			y_coor2 =(int) (( iny + 1 ) * cryst->b *
						  cos((cryst->gamma - 90) *
						      PI_RAD)) +
			  cryst->offset_h;
		     }
			XDrawLine(display, window, cryst->gc,
				  cryst->offset_w + inx * cryst->a - (int) (iny * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
				  y_coor1,
				  cryst->offset_w + (inx + 1) * cryst->a - (int) (iny * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
				  y_coor1);
			XDrawLine(display, window, cryst->gc,
				  cryst->offset_w + inx * cryst->a - (int) (iny * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
				  y_coor1,
				  cryst->offset_w + inx * cryst->a - (int) ((iny + 1) * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
				  y_coor2);
			XDrawLine(display, window, cryst->gc,
				  cryst->offset_w + (inx + 1) * cryst->a - (int) (iny * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
				  y_coor1,
				  cryst->offset_w + (inx + 1) * cryst->a - (int) ((iny + 1) * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
				  y_coor2);
			XDrawLine(display, window, cryst->gc,
				  cryst->offset_w + inx * cryst->a - (int) ((iny + 1) * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
				  y_coor2,
				  cryst->offset_w + (inx + 1) * cryst->a - (int) ((iny + 1) * cryst->b * sin((cryst->gamma - 90) * PI_RAD)),
				  y_coor2);
		}
	}
	for (i = 0; i < cryst->num_atom; i++) {
		crystalatom *atom0;

		atom0 = &cryst->atom[i];
		if (MI_IS_INSTALL(mi) && MI_NPIXELS(mi) > 2) {
			XSetForeground(display, cryst->gc, cryst->colors[atom0->colour].pixel);
		} else {
			XSetForeground(display, cryst->gc, atom0->colour);
		}
		crystal_drawatom(mi, atom0);
	}
	XSetFunction(display, cryst->gc, GXcopy);
}
#endif

ENTRYPOINT void
free_crystal(ModeInfo * mi)
{
	Display    *display = MI_DISPLAY(mi);
	crystalstruct *cryst = &crystals[MI_SCREEN(mi)];

	if (MI_IS_INSTALL(mi) && MI_NPIXELS(mi) > 2) {
		MI_WHITE_PIXEL(mi) = cryst->whitepixel;
		MI_BLACK_PIXEL(mi) = cryst->blackpixel;
#ifndef STANDALONE
		MI_FG_PIXEL(mi) = cryst->fg;
		MI_BG_PIXEL(mi) = cryst->bg;
#endif
		if (cryst->colors && cryst->ncolors && !cryst->no_colors)
			free_colors(mi->xgwa.screen, cryst->cmap, cryst->colors,
                        cryst->ncolors);
		if (cryst->colors)
			(void) free((void *) cryst->colors);
#if 0 /* #### wrong! -jwz */
		XFreeColormap(display, cryst->cmap);
#endif
	}
	if (cryst->gc != NULL)
		XFreeGC(display, cryst->gc);
	if (cryst->atom != NULL)
		(void) free((void *) cryst->atom);
}

ENTRYPOINT void
init_crystal(ModeInfo * mi)
{
	Display    *display = MI_DISPLAY(mi);
	crystalstruct *cryst;
	int         i, max_atoms, size_atom, neqv;
	int         cell_min;

#define MIN_CELL 200

/* initialize */
	MI_INIT (mi, crystals);
	cryst = &crystals[MI_SCREEN(mi)];

	if (!cryst->gc) {
		if (MI_IS_INSTALL(mi) && MI_NPIXELS(mi) > 2) {
			XColor      color;

#ifndef STANDALONE
			extern char *background;
			extern char *foreground;

			cryst->fg = MI_FG_PIXEL(mi);
			cryst->bg = MI_BG_PIXEL(mi);
#endif
			cryst->blackpixel = MI_BLACK_PIXEL(mi);
			cryst->whitepixel = MI_WHITE_PIXEL(mi);
#if 0 /* #### wrong! -jwz */
			cryst->cmap = XCreateColormap(display, window,
						   MI_VISUAL(mi), AllocNone);
			XSetWindowColormap(display, window, cryst->cmap);
#else
            cryst->cmap = mi->xgwa.colormap;
#endif
			(void) XParseColor(display, cryst->cmap, "black", &color);
			(void) XAllocColor(display, cryst->cmap, &color);
			MI_BLACK_PIXEL(mi) = color.pixel;
			(void) XParseColor(display, cryst->cmap, "white", &color);
			(void) XAllocColor(display, cryst->cmap, &color);
			MI_WHITE_PIXEL(mi) = color.pixel;
#ifndef STANDALONE
			(void) XParseColor(display, cryst->cmap, background, &color);
			(void) XAllocColor(display, cryst->cmap, &color);
			MI_BG_PIXEL(mi) = color.pixel;
			(void) XParseColor(display, cryst->cmap, foreground, &color);
			(void) XAllocColor(display, cryst->cmap, &color);
			MI_FG_PIXEL(mi) = color.pixel;
#endif
			cryst->colors = 0;
			cryst->ncolors = 0;
		}
		if ((cryst->gc = XCreateGC(display, MI_WINDOW(mi),
			     (unsigned long) 0, (XGCValues *) NULL)) == None)
			return;
	}
/* Clear Display */
	MI_CLEARWINDOW(mi);
	cryst->painted = False;
	XSetFunction(display, cryst->gc, GXxor);


/*Set up crystal data */
	cryst->direction = (LRAND() & 1) ? 1 : -1;
	if (MI_IS_FULLRANDOM(mi)) {
		if (LRAND() & 1)
			cryst->unit_cell = True;
		else
			cryst->unit_cell = False;
	} else
		cryst->unit_cell = unit_cell;
	if (cryst->unit_cell) {
		if (MI_IS_FULLRANDOM(mi)) {
			if (LRAND() & 1)
				cryst->grid_cell = True;
			else
				cryst->grid_cell = False;
		} else
			cryst->grid_cell = grid_cell;
	}
	cryst->win_width = MI_WIDTH(mi);
	cryst->win_height = MI_HEIGHT(mi);
	cell_min = min(cryst->win_width / 2 + 1, MIN_CELL);
	cell_min = min(cell_min, cryst->win_height / 2 + 1);
	cryst->planegroup = NRAND(17);
        cryst->invert = NRAND(2);
	if (MI_IS_VERBOSE(mi))
		(void) fprintf(stdout, "Selected plane group no %d\n",
			       cryst->planegroup + 1);
	if (cryst->planegroup > 11)
		cryst->gamma = 120.0;
	else if (cryst->planegroup < 2)
		cryst->gamma = 60.0 + NRAND(60);
	else
		cryst->gamma = 90.0;
	neqv = numops[2 * cryst->planegroup] - numops[2 * cryst->planegroup + 1];
	if (centro[cryst->planegroup] == True)
		neqv = 2 * neqv;
	if (primitive[cryst->planegroup] == False)
		neqv = 2 * neqv;


	if (nx > 0)
		cryst->nx = nx;
	else if (nx < 0)
		cryst->nx = NRAND(-nx) + 1;
	else
		cryst->nx = DEF_NX1;
	if (cryst->planegroup > 8)
		cryst->ny = cryst->nx;
	else if (ny > 0)
		cryst->ny = ny;
	else if (ny < 0)
		cryst->ny = NRAND(-ny) + 1;
	else
		cryst->ny = DEF_NY1;
	neqv = neqv * cryst->nx * cryst->ny;

	cryst->num_atom = MI_COUNT(mi);
	max_atoms = MI_COUNT(mi);
	if (cryst->num_atom == 0) {
		cryst->num_atom = DEF_NUM_ATOM;
		max_atoms = DEF_NUM_ATOM;
	} else if (cryst->num_atom < 0) {
		max_atoms = -cryst->num_atom;
		cryst->num_atom = NRAND(-cryst->num_atom) + 1;
	}
	if (neqv > 1)
		cryst->num_atom = cryst->num_atom / neqv + 1;

	if (cryst->atom == NULL)
		cryst->atom = (crystalatom *) calloc(max_atoms, sizeof (
							       crystalatom));

	if (maxsize) {
		if (cryst->planegroup < 13) {
			cryst->gamma = 90.0;
			cryst->offset_w = 0;
			cryst->offset_h = 0;
			if (cryst->planegroup < 10) {
				cryst->b = cryst->win_height;
				cryst->a = cryst->win_width;
			} else {
				cryst->b = min(cryst->win_height, cryst->win_width);
				cryst->a = cryst->b;
			}
		} else {
			cryst->gamma = 120.0;
			cryst->a = (int) (cryst->win_width * 2.0 / 3.0);
			cryst->b = cryst->a;
			cryst->offset_h = (int) (cryst->b * 0.25 *
					  cos((cryst->gamma - 90) * PI_RAD));
			cryst->offset_w = (int) (cryst->b * 0.5);
		}
	} else {
		int max_repeat = 10;
		cryst->offset_w = -1;
		while (max_repeat-- && 
		  (cryst->offset_w < 4 || (int) (cryst->offset_w - cryst->b *
				    sin((cryst->gamma - 90) * PI_RAD)) < 4)
			   ) {
			cryst->b = NRAND((int) (cryst->win_height / (cos((cryst->gamma - 90) *
					    PI_RAD))) - cell_min) + cell_min;
			if (cryst->planegroup > 8)
				cryst->a = cryst->b;
			else
				cryst->a = NRAND(cryst->win_width - cell_min) + cell_min;
			cryst->offset_w = (int) ((cryst->win_width - (cryst->a - cryst->b *
						    sin((cryst->gamma - 90) *
							PI_RAD))) / 2.0);
		}
		cryst->offset_h = (int) ((cryst->win_height - cryst->b * cos((
					cryst->gamma - 90) * PI_RAD)) / 2.0);
		if (!centre) {
			if (cryst->offset_h > 0)
				cryst->offset_h = NRAND(2 * cryst->offset_h);
			cryst->offset_w = (int) (cryst->win_width - cryst->a -
						 cryst->b *
				    fabs(sin((cryst->gamma - 90) * PI_RAD)));
			if (cryst->gamma > 90.0) {
				if (cryst->offset_w > 0)
					cryst->offset_w = NRAND(cryst->offset_w) +
						(int) (cryst->b * sin((cryst->gamma - 90) * PI_RAD));
				else
					cryst->offset_w = (int) (cryst->b * sin((cryst->gamma - 90) *
								    PI_RAD));
			} else if (cryst->offset_w > 0)
				cryst->offset_w = NRAND(cryst->offset_w);
			else
				cryst->offset_w = 0;
		}
	}

	size_atom = min((int) ((float) (cryst->a) / 40.) + 1,
			(int) ((float) (cryst->b) / 40.) + 1);
	if (MI_SIZE(mi) < size_atom) {
		if (MI_SIZE(mi) < -size_atom)
			size_atom = -size_atom;
		else
			size_atom = MI_SIZE(mi);
	}
	cryst->a = cryst->a / cryst->nx;
	cryst->b = cryst->b / cryst->ny;
	if (MI_IS_INSTALL(mi) && MI_NPIXELS(mi) > 2) {
/* Set up colour map */
		if (cryst->colors && cryst->ncolors && !cryst->no_colors)
			free_colors(mi->xgwa.screen, cryst->cmap,
                        cryst->colors, cryst->ncolors);
		if (cryst->colors)
			(void) free((void *) cryst->colors);
		cryst->colors = 0;
		cryst->ncolors = MI_NCOLORS(mi);
		if (cryst->ncolors < 2)
			cryst->ncolors = 2;
		if (cryst->ncolors <= 2)
			cryst->mono_p = True;
		else
			cryst->mono_p = False;

		if (cryst->mono_p)
			cryst->colors = 0;
		else
			cryst->colors = (XColor *) malloc(sizeof (*cryst->colors) * (cryst->ncolors + 1));
		cryst->cycle_p = has_writable_cells(mi->xgwa.screen, MI_VISUAL(mi));
		if (cryst->cycle_p) {
			if (MI_IS_FULLRANDOM(mi)) {
				if (!NRAND(8))
					cryst->cycle_p = False;
				else
					cryst->cycle_p = True;
			} else {
				cryst->cycle_p = cycle_p;
			}
		}
		if (!cryst->mono_p) {
			if (!(LRAND() % 10))
				make_random_colormap(mi->xgwa.screen, MI_VISUAL(mi),
                                     cryst->cmap, cryst->colors,
                                     &cryst->ncolors,
                                     True, True, &cryst->cycle_p, True);
			else if (!(LRAND() % 2))
				make_uniform_colormap(mi->xgwa.screen, MI_VISUAL(mi),
                                      cryst->cmap, cryst->colors,
                                      &cryst->ncolors, True,
                                      &cryst->cycle_p, True);
			else
				make_smooth_colormap(mi->xgwa.screen, MI_VISUAL(mi),
                                     cryst->cmap, cryst->colors,
                                     &cryst->ncolors,
                                     True, &cryst->cycle_p, True);
		}
#if 0 /* #### wrong! -jwz */
		XInstallColormap(display, cryst->cmap);
#endif
		if (cryst->ncolors < 2) {
			cryst->ncolors = 2;
			cryst->no_colors = True;
		} else
			cryst->no_colors = False;
		if (cryst->ncolors <= 2)
			cryst->mono_p = True;

		if (cryst->mono_p)
			cryst->cycle_p = False;

	}
	for (i = 0; i < cryst->num_atom; i++) {
		crystalatom *atom0;

		atom0 = &cryst->atom[i];
		if (MI_IS_INSTALL(mi) && MI_NPIXELS(mi) > 2) {
			if (cryst->ncolors > 2)
				atom0->colour = NRAND(cryst->ncolors - 2) + 2;
			else
				atom0->colour = 1;	/* Just in case */
		} else {
			if (MI_NPIXELS(mi) > 2)
				atom0->colour = MI_PIXEL(mi, NRAND(MI_NPIXELS(mi)));
			else
				atom0->colour = 1;	/*Xor'red so WHITE may not be appropriate */
		}
		atom0->x0 = NRAND(cryst->a);
		atom0->y0 = NRAND(cryst->b);
		atom0->velocity[0] = NRAND(7) - 3;
		atom0->velocity[1] = NRAND(7) - 3;
		atom0->velocity_a = (NRAND(7) - 3) * PI_RAD;
		atom0->angle = NRAND(90) * PI_RAD;
		atom0->at_type = NRAND(3);
		if (size_atom == 0)
			atom0->size_at = DEF_SIZ_ATOM;
		else if (size_atom > 0)
			atom0->size_at = size_atom;
		else
			atom0->size_at = NRAND(-size_atom) + 1;
		atom0->size_at++;
		if (atom0->at_type == 2)
			atom0->num_point = 3;
		else
			atom0->num_point = 4;
		crystal_setupatom(atom0, cryst->gamma);
	}
	XSetFunction(display, cryst->gc, GXcopy);

	if (MI_NPIXELS(mi) > 2)
		cryst->grid_pixel = MI_PIXEL(mi, NRAND(MI_NPIXELS(mi)));
	else
		cryst->grid_pixel = MI_WHITE_PIXEL(mi);

	cryst->inx = NRAND(cryst->nx);
	cryst->iny = NRAND(cryst->ny);

}

XSCREENSAVER_MODULE ("Crystal", crystal)