-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhyperbolicPlay1.5_fourHexagons.bb
613 lines (473 loc) · 11.9 KB
/
hyperbolicPlay1.5_fourHexagons.bb
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
Graphics 700,700
R = 300
Type point
Field id
Field x#,y#
Field u#,v#
Field tu#, tv#
Field r,g,b
Field links.point[4]
Field numlinks
End Type
Type camera
Field id
Field x#, y#
Field u#, v#
Field du#, dv#
Field snap.point
End Type
init(R);, 7)
draw(R)
SetBuffer(BackBuffer())
While Not KeyHit(1)
If getInput(R)
Cls
draw(R)
Flip
EndIf
Wend
End
Function init(R)
n = 0
; num = 10
dis# = 1.15
; ang = 0
; startX = -10
; startY = 10
p.point = New point
p\x = startX
p\y = startY
p\id = 1
prevPoint.point = p
lastPoint.point = Null
For k = 0 To 3
newX# = offsetStep(0,0, dis,k*90, 1)
newY# = offsetStep(0,0, dis,k*90, 2)
; p.point = New point
; p\x = newX
; p\y = newY
; p\u = transform(p\x,p\y, 1)
; p\v = transform(p\x,p\y, 2)
; lastPoint = Last point
; p\id = lastPoint\id + 1
; p\r = 255
; p\g = 255
; p\b = 255
; p\numLinks = 1
; p\links[1] = prevPoint
; prevPoint = p
polygon(newX,newY, dis, 6, k*30)
Next
cam.camera = New camera
cam\id = 1
cam\snap = First point
End Function
Function getInput(R)
mx = MouseX()
my = MouseY()
mkey = 0
tx# = 0
ty# = 0
scrollSpeed# = 0.10
k# = (Exp(scrollSpeed)+1/Exp(scrollSpeed))/2 ;cosh(scrollSpeed)
If KeyDown(32) Or KeyDown(205)
tx = scrollSpeed
ElseIf KeyDown(30) Or KeyDown(203)
tx = -scrollSpeed
EndIf
If KeyDown(17) Or KeyDown(200)
ty = -scrollSpeed
ElseIf KeyDown(31) Or KeyDown(208)
ty = scrollSpeed
EndIf
cam.camera = First camera
If KeyHit(49)
;Stop
If cam\snap = First point
cam\snap = Last point
Else
cam\snap = Before cam\snap
EndIf
cam\x = cam\snap\x
cam\y = cam\snap\y
Return 1
ElseIf KeyHit(50)
If cam\snap = Last point
cam\snap = First point
Else
cam\snap = After cam\snap
EndIf
cam\x = cam\snap\x
cam\y = cam\snap\y
Return 1
EndIf
If tx <> 0 Or ty <> 0
; DebugLog "k = "+k
; DebugLog "cam\x = "+cam\x+", cam\y = "+cam\y
dx# = 0
dy# = 0
; If cam\y = 0
; ax# = 1
; Else
; ax# = cam\y*cam\y
; EndIf
ax# = 1 + cam\y*cam\y
bx# = 2*(1-k)*cam\x
; If cam\x = 0
; ay# = 1
; Else
; ay# = cam\x*cam\x
; EndIf
ay# = 1 + cam\x*cam\x
by# = 2*(1-k)*cam\y
c# = (1-k*k + 2*(1-k) * (cam\x*cam\x + cam\y*cam\y) )
; DebugLog "a = "+a+", b = "+b+", c = "+c
; dxpos# = 0
; dxneg# = 0
; dypos# = 0
; dyneg# = 0
If tx <> 0
dxpos# = (-bx + Sqr(bx*bx-4*ax*c))/(2*ax)
dxneg# = (-bx - Sqr(bx*bx-4*ax*c))/(2*ax)
; DebugLog "dxpos = "+dxpos+", dxneg = "+dxneg
Else
dxpos = 0
dxneg = 0
EndIf
If ty <> 0
dypos# = (-by + Sqr(by*by-4*ay*c))/(2*ay)
dyneg# = (-by - Sqr(by*by-4*ay*c))/(2*ay)
; DebugLog "dypos = "+dypos+", dyneg = "+dyneg
Else
dypos = 0
dyneg = 0
EndIf
; If a*dxpos*dxpos + b*dxpos + c = 0
; DebugLog "dxpos passes the test!"
; ElseIf a*dxneg*dxneg + b*dxneg + c = 0
; DebugLog "dxneg passes the test!"
; ElseIf a*dypos*dypos + b*dypos + c = 0
; DebugLog "dypos passes the test!"
; ElseIf a*dyneg*dyneg + b*dyneg + c = 0
; DebugLog "dyneg passes the test!"
; EndIf
; DebugLog "dxpos check: "+(ax*dxpos*dxpos + bx*dxpos + c)
; DebugLog "dxneg check: "+(ax*dxneg*dxneg + bx*dxneg + c)
; DebugLog "dypos check: "+(ay*dypos*dypos + by*dypos + c)
; DebugLog "dyneg check: "+(ay*dyneg*dyneg + by*dyneg + c)
If dxpos < 0
temp# = dxpos
dxpos = dxneg
dxneg = temp
EndIf
If dypos < 0
temp# = dypos
dypos = dyneg
dyneg = temp
EndIf
If tx > 0
cam\x = cam\x + dxpos
Else
cam\x = cam\x + dxneg
EndIf
If ty > 0
cam\y = cam\y + dypos
Else
cam\y = cam\y + dyneg
EndIf
; DebugLog "cam\x = "+cam\x+", cam\y = "+cam\y
; DebugLog ""
Return 1
EndIf
If MouseXSpeed() Or MouseYSpeed() Return 1
Return 0
End Function
Function draw(R)
gw = GraphicsWidth()
gh = GraphicsHeight()
Color 255,255,255
circ(gw/2,gh/2, R, 0)
;circ(GraphicsWidth()/2,GraphicsHeight()/2, 2, 1)
cam.camera = First camera
cam\u = transform(cam\x,cam\y, 1); + cam\dv
cam\v = transform(cam\x,cam\y, 2); + cam\du
;tempu# = translate(cam\u,cam\v, cam\du,cam\dv, 1)
;tempv# = translate(cam\u,cam\v, cam\du,cam\dv, 2)
;cam\u = tempu
;cam\v = tempv
;cam\x = invTransform(cam\u,cam\v, 1)
;cam\y = invTransform(cam\u,cam\v, 2)
For p.point = Each point
Color p\r,p\g,p\b
; pt# = Sqr(1 + p\x*p\x + p\y*p\y)
; camt# = Sqr(1 + cam\x*cam\x + cam\y*cam\y)
;
; B# = pt*camt - p\x*cam\x - p\y*cam\y
;
; ang# = ATan2(cam\y-p\y, cam\x-p\x)
; slope# = (1.*cam\y-p\y)/(cam\x-p\x)
; D# = Sqr(1 + (cam\x-p\x)*(cam\x-p\x) + (cam\y-p\y)*(cam\y-p\y))
; mult# = B/D
;
; pxt# = (p\x-cam\x)*mult; * Sgn(cam\x-p\x)
; pyt# = (p\y-cam\y)*mult;*slope ;* Sgn(cam\y-p\y)
;
; pxt# = cam\x-p\x
; pyt# = cam\y-p\y
;
; p\tu = transform(pxt,pyt, 1)
; p\tv = transform(pxt,pyt, 2)
If KeyHit(57) Stop
p\tu = translate(p\u,p\v, -cam\u,-cam\v, 1)
p\tv = translate(p\u,p\v, -cam\u,-cam\v, 2)
drawx = R * p\tu + gw/2
drawy = R * p\tv + gh/2
circ(drawx,drawy, 3, 1)
Color p\r/2,p\g/2,p\b/2
circ(R*p\u+gw/2,R*p\v+gh/2, 3, 1)
Next
Local vals#[3]
; Return
For p1.point = Each point
For idx = 1 To p1\numlinks
p2.point = p1\links[idx]
u1# = p1\tu
v1# = p1\tv
u2# = p2\tu
v2# = p2\tv
denom# = u1*v2-u2*v1
If KeyHit(57) And Abs(p1\x) < 0.1 And Abs(p1\y) = 0.1; And idx = 4
Stop
EndIf
If Abs(denom) < 0.001
Color 255,255,255
p1tx = R * p1\tu + gw/2
p1ty = R * p1\tv + gh/2
p2tx = R * p2\tu + gw/2
p2ty = R * p2\tv + gh/2
Line p1tx,p1ty, p2tx,p2ty
Else
f# = (u1*u1+v1*v1+1)/2.
g# = (u2*u2+v2*v2+1)/2.
If Abs(v2) > 0.0001
h# = (v2*f-v1*g)/denom
k# = (g-u2*h)/v2
ElseIf Abs(v1) > 0.0001
h# = (v2*f-v1*g)/denom
k# = (f-u1*h)/v1
Else
Stop
EndIf
rad# = Sqr(h*h+k*k-1)
angMin# = ATan2(v1-k,u1-h)
angMax# = ATan2(v2-k,u2-h)
angDiff# = (angMax-angMin)
If angDiff * 2 <> angDiff
While angDiff < -180 Or angDiff > 180
If angDiff < -180
angDiff = angDiff + 360
Else
angDiff = angDiff - 360
EndIf
Wend
numSteps = 20
angStep# = angDiff/numSteps
Color 255,255,255
For n = 0 To numSteps-1
ang# = angMin + n*angStep
x1 = R*(h+rad*Cos(ang)) + GraphicsWidth()/2
y1 = R*(k+rad*Sin(ang)) + GraphicsHeight()/2
x2 = R*(h+rad*Cos(ang+angStep)) + GraphicsWidth()/2
y2 = R*(k+rad*Sin(ang+angStep)) + GraphicsHeight()/2
Line x1,y1, x2,y2
Next
EndIf
EndIf
Next
Next
;circle around mouse
; omx = MouseX()-gw/2
; omy = MouseY()-gh/2
; Color 0,255,255
; Line MouseX()-2,MouseY(), MouseX()+2,MouseY()
; Line MouseX(),MouseY()-2, MouseX(),MouseY()+2
;DebugLog "omx = "+omx+", omy = "+omy
; If omx*omx + omy*omy < R*R
;DebugLog "Ding!"
; mcRad# = 1
; mx# = invTransform(1.*omx/R,1.*omy/R,1)
; my# = invTransform(1.*omx/R,1.*omy/R,2)
;DebugLog "mx = "+mx+", my = "+my
;k# = (Exp(mcRad)+1/Exp(mcRad))/2
; For theta = 0 To 360-5 Step 5
; hx2# = mx + rpos*Cos(theta)
; hy2# = my + rpos*Sin(theta)
;
; hx3# = mx + rneg*Cos(theta)
; hy3# = my + rneg*Sin(theta)
; hx2# = offsetStep(mx,my, mcRad,theta, 1)
; hy2# = offsetStep(mx,my, mcRad,theta, 2)
;
; hx3# = offsetStep(mx,my, mcRad,theta+5, 1)
; hy3# = offsetStep(mx,my, mcRad,theta+5, 2)
;
; ;Stop
;
; drawx1# = transform(hx2,hy2,1)*R + gw/2
; drawy1# = transform(hx2,hy2,2)*R + gh/2
;
; drawx2# = transform(hx3,hy3,1)*R + gw/2
; drawy2# = transform(hx3,hy3,2)*R + gh/2
;
;; Color 255,0,0
;; Plot drawx1*R + gw/2, drawy1*R + gh/2
;;
;; Color 0,0,255
;; Plot drawx2*R + gw/2, drawy2*R + gh/2
;
; Color 255,255,255
; circ(drawx1,drawy1, 1,1)
;; Plot drawx1,drawy1
;
; If theta < 180
; Color 0,0,255
; Else
; Color 255,0,0
; EndIf
; Line drawx1,drawy1, drawx2,drawy2
;
; If theta = 0 Or theta = 180
; Color 0,255,0
; Line omx+gw/2,omy+gh/2, drawx1,drawy1
; EndIf
;
;; Color 255,255,255
;; Plot drawx1,drawy1
; Next
; EndIf
Color 255,255,0
Line gw/2-2,gh/2, gw/2+2,gh/2
Line gw/2,gh/2-2, gw/2,gh/2+2
Color 128,128,0
cx = R*cam\u+gw/2
cy = R*cam\v+gh/2
Line cx-2,cy, cx+2,cy
Line cx,cy-2, cx,cy+2
End Function
Function offsetStep#(px#,py#, d#,theta#, which)
k# = (Exp(d)+1/Exp(d))/2
a# = 1 + px^2*Sin(theta)^2 + py^2*Cos(theta)^2 - 2*px*py*Cos(theta)*Sin(theta)
b# = 2*(1-k)*(px*Cos(theta) + py*Sin(theta))
c# = (1 - k*k + 2*(1-k)*(px*px + py*py) )
det# = b*b-4*a*c
rpos# = (-b + Sqr(det))/(2*a)
rneg# = (-b - Sqr(det))/(2*a)
If rpos < 0
temp# = rpos
rpos = rneg
rneg = temp
EndIf
If which = 1
Return px + rpos*Cos(theta)
ElseIf which = 2
Return py + rpos*Sin(theta)
EndIf
End Function
Function translate#(pu#,pv#, tu#,tv#, which) ;(pu,pv) = point to translate, (tu,tv) = point to move to origin
vdotx# = pu*tu + pv*tv ;tu*p\u + tv*p\v
mag2v# = tu*tu + tv*tv ;tu*tu + tv*tv
mag2x# = pu*pu + pv*pv ;p\u*p\u + p\v*p\v
If which = 1
Return ( (1.+2*vdotx+mag2x)*tu + (1.-mag2v)*pu ) / (1. + 2*vdotx + mag2v*mag2x)
ElseIf which = 2
Return ( (1.+2*vdotx+mag2x)*tv + (1.-mag2v)*pv ) / (1. + 2*vdotx + mag2v*mag2x)
EndIf
End Function
Function getYpoints(p1.point,p2.point, x, vals[3])
u1 = p1\x - GraphicsWidth()/2
v1 = p1\y - GraphicsHeight()/2
u2 = p2\x - GraphicsWidth()/2
v2 = p2\y - GraphicsHeight()/2
A = 1
B = (v1*(u1*u1+u2*u2)-u1*(v1*v1+v2*v2)+v1-u1)/(u1*v2-u2*v1)
C = 1 + x*x + x * (u2*(v1*v1+v2*v2)-v2*(u1*u1+u2*u2)+u2-v2)/(u1*v2-u2*v1)
det = B*B-4*A*C ;determinant
If det < 0
vals[0] = 0
Return
ElseIf det = 0
vals[0] = 1
vals[1] = -B/(2*A)
Return
ElseIf det > 0
vals[0] = 2
vals[1] = (-B - Sqr(det))/(2*A)
vals[2] = (-B + Sqr(det))/(2*A)
Return
EndIf
End Function
Function transform#(x#,y#, which)
;If x*x+y*y > 1
; Return -1
;EndIf
t# = Sqr(1+x*x+y*y)
If which = 1
Return x/(1.+t)
ElseIf which = 2
Return y/(1.+t)
EndIf
End Function
Function invTransform#(u#,v#, which)
denom# = 1.-u*u-v*v
If which = 1
Return 2.*u/denom
ElseIf which = 2
Return 2.*v/denom
EndIf
End Function
Function polygon(centerX#,centerY#, rad#, sides, angoffset#)
angstep# = 360./sides
prevPoint.point = Last point
idStart = prevPoint\id
prevPoint = Null
begPoint.point = Null
centerU# = transform(centerX,centerY, 1)
centerV# = transform(centerX,centerY, 2)
For i = 0 To sides-1
p.point = New point
tempx# = offsetStep(0,0, rad, i*angstep + angoffset, 1)
tempy# = offsetStep(0,0, rad, i*angstep + angoffset, 2)
; p\x = tempx ;translate(tempx,tempy, -centerX,-centerY, 1)
; p\y = tempy ;translate(tempx,tempy, -centerX,-centerY, 2)
tempu# = transform(tempx,tempy, 1)
tempv# = transform(tempx,tempy, 2)
p\u = translate(tempu,tempv, centerU,centerV, 1)
p\v = translate(tempu,tempv, centerU,centerV, 2)
p\x = invTransform(p\u,p\v, 1)
p\y = invTransform(p\u,p\v, 2)
p\r = 255
p\g = 255
p\b = 255
p\id = idStart + 1
idStart = idStart + 1
;If begPoint <> Null
; begPoint = p
;EndIf
If prevPoint <> Null
prevPoint\links[1] = p
prevPoint\numLinks = 1
EndIf
prevPoint = p
Next
prevPoint\numLinks = 1
For j = 1 To sides-1
p = Before p
Next
prevPoint\links[1] = p
End Function
Function D#(x1,y1, x2,y2)
Return Sqr((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1))
End Function
Function circ(x,y,r, fill=0)
Oval x-r,y-r,2*r+1,2*r+1, fill
End Function