1 /* drawing stuff for rubik.c M.J.Round 3rd Feb 90 */
7 extern struct GfxBase
* GfxBase
;
11 int tr
[120] = /* transformations for 90 degree side rotates */
50 extern struct points point
; /* holds posn.s of all points */
51 extern struct points start
;
52 extern struct one_square square
[56];
53 extern struct Window
*wnd1
;
54 extern struct Window
*wnd2
;
55 extern struct Window
*window
;
56 extern struct Screen
*scr1
;
57 extern struct Screen
*scr2
;
58 extern struct Screen
*screen
;
61 extern short xcent
,ycent
;
62 extern short angle(short, short, unsigned short);
63 extern unsigned short hypoten (short, short);
64 extern unsigned short scaling
;
66 extern void roll (short);
67 extern void yaw (short);
68 extern void pitch (short);
69 extern void fb (short, short);
70 extern void bt (short, short);
71 extern void lr (short, short);
73 void twist (short ident
, short amount
)
74 /* ident = 0 to 5 for front,back,(bottom),(top),left,right */
75 /* amount = -1, +1 or +2 (90 degree twists) */
82 for (i
= 0; i
<amount
; i
++)
84 for (j
= ident
* 20; j
< ident
* 20 + 20; j
+= 4)
86 c
= (square
[tr
[j
]]).colour
;
87 for (k
= j
; k
< (j
+ 3); k
++)
88 (square
[tr
[k
]]).colour
= (square
[tr
[k
+1]]).colour
;
89 (square
[tr
[j
+3]]).colour
= c
;
95 for (i
= 0; i
< (-amount
); i
++)
97 for (j
= ident
* 20; j
< ident
* 20 + 20; j
+= 4)
99 c
= (square
[tr
[j
+3]]).colour
;
100 for (k
= j
+ 3; k
> j
; k
--)
101 (square
[tr
[k
]]).colour
= (square
[tr
[k
-1]]).colour
;
102 (square
[tr
[j
]]).colour
= c
;
108 /* return true if a square is clockwise else false */
109 int clockwise(register int i
)
115 d
=(viewdist
+(square
[i
].corner
[0]->z
)) << scaling
;
116 x1
= xcent
+(viewdist
*square
[i
].corner
[0]->x
)/d
;
117 y1
= ycent
+(viewdist
*square
[i
].corner
[0]->y
)/d
;
119 d
=(viewdist
+(square
[i
].corner
[1]->z
)) << scaling
;
120 x2
= xcent
+(viewdist
*square
[i
].corner
[1]->x
)/d
;
121 y2
= ycent
+(viewdist
*square
[i
].corner
[1]->y
)/d
;
123 d
=(viewdist
+(square
[i
].corner
[2]->z
)) << scaling
;
124 x3
= xcent
+(viewdist
*square
[i
].corner
[2]->x
)/d
;
125 y3
= ycent
+(viewdist
*square
[i
].corner
[2]->y
)/d
;
133 ang1
= angle(x
, y
, (r
= hypoten (x
, y
)));
139 ang2
= angle(x
, y
, (r
= hypoten (x
, y
)));
153 void draw_square(register int i
)
157 SetAPen (window
->RPort
,square
[i
].colour
);
158 SetDrMd(window
->RPort
, JAM1
);
160 AreaMove (window
->RPort
,x1
,y1
);
161 AreaDraw (window
->RPort
,x2
,y2
);
162 AreaDraw (window
->RPort
,x3
,y3
);
163 d
=(viewdist
+(square
[i
].corner
[3]->z
)) << scaling
;
167 xcent
+(viewdist
*square
[i
].corner
[3]->x
)/d
,
168 ycent
+(viewdist
*square
[i
].corner
[3]->y
)/d
170 AreaEnd (window
->RPort
);
173 /* this routine renders the cube with roll, yaw and pitch alpha, beta, gamma
174 if twists is non-zero, <layer> is rotated delta degrees at a time, through
175 <twists> 90 degree twists.
192 point
= start
; /* this copies the 'zeroed' cube to the workspace */
196 theta
+= (twists
> 0) ? -delta
: delta
;
198 if ((theta
<= -90) || (theta
>= 90))
200 theta
= (twists
> 0) ? 1 : -1;
215 bt((short)(layer
-2),theta
);
218 lr((short)(layer
-4),theta
);
231 SetAPen(window
->RPort
,7);
232 RectFill(window
->RPort
,2,10,190,198);
239 else if ((i
% 3) == 0)
242 else if (clockwise(54))
247 if (clockwise(twistlist
[i
]))
248 draw_square(twistlist
[i
]);
249 else if ((i
% 3) == 0)
256 while ((j
< 21) && ((twistlist
[j
]) < i
))
259 if ((j
> 20) || (i
!= twistlist
[j
]))
263 else if ((i
% 3) == 0)
270 if (clockwise(55)) /* need this to get corners */
277 while ((j
< 21) && ((twistlist
[j
]) < i
))
280 if ((j
> 20) || (i
!= twistlist
[j
]))
284 else if ((i
% 3) == 0)
290 if (clockwise(twistlist
[i
]))
291 draw_square(twistlist
[i
]);
292 else if ((i
% 3) == 0)
296 ScreenToFront(screen
);
312 int whatsquare(int x
, int y
) /* returns square number, or -1 */
314 struct Window
*viswindow
;
325 for (i
= 0; i
< 4 && (colour
= ReadPixel(viswindow
->RPort
,x
,y
)) == 0; i
++)
339 } /* find a nearby square if they click on the lines */
341 if (colour
<= 0 || colour
== 7)
346 viswindow
->RPort
,2,10, /* source posn */
347 window
->RPort
,2,10, /* dest posn */
349 0xc0 /* direct copy */
352 SetAPen(window
->RPort
,7);
353 Flood (window
->RPort
,1,x
,y
);
359 if (ReadPixel(window
->RPort
,x
,y
) != 7)
367 int paints(int x
, int y
, int pen
) /* returns square number, or -1 */
369 struct Window
*viswindow
;
380 for (i
= 0; i
< 4 && (colour
= ReadPixel(viswindow
->RPort
,x
,y
)) == 0; i
++)
394 } /* find a nearby square if they click on the lines */
396 if (colour
<= 0 || colour
== 7)
399 SetAPen(viswindow
->RPort
,pen
);
400 Flood (viswindow
->RPort
,1,x
,y
);
402 return (whatsquare(x
,y
));
405 void findangles (short *alpha
, short *beta
, short *gamma
)
409 y
= ((point
.xyz
[0] [0] [5]).y
+ (point
.xyz
[5] [5] [5]).y
)/2;
410 z
= ((point
.xyz
[0] [0] [5]).z
+ (point
.xyz
[5] [5] [5]).z
)/2;
412 pitch ((short) -(*gamma
= -angle (z
,y
,hypoten (z
,y
))));
414 x
= ((point
.xyz
[0] [0] [5]).x
+ (point
.xyz
[5] [5] [5]).x
)/2;
415 z
= ((point
.xyz
[0] [0] [5]).z
+ (point
.xyz
[5] [5] [5]).z
)/2;
417 yaw ((short) -(*beta
= -angle (z
,x
,hypoten (z
,x
))));
419 x
= ((point
.xyz
[0] [5] [0]).x
+ (point
.xyz
[5] [5] [5]).x
)/2;
420 y
= ((point
.xyz
[0] [5] [0]).y
+ (point
.xyz
[5] [5] [5]).y
)/2;
422 *alpha
= -angle (y
,x
,hypoten (y
,x
));