-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path打砖块.lua
More file actions
1679 lines (1414 loc) · 45 KB
/
Copy path打砖块.lua
File metadata and controls
1679 lines (1414 loc) · 45 KB
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
--打砖块
--更新 增加了砖块厚度
--有人说难度低,其实这只是为了演示Nature Math绘图和物理引擎的示例,或者把它当做休闲小游戏😘
--碰到砖块有15%的概率产生新小球,点击小球还可以查看小球的运动信息
--开学前最后一个项目
--好好学习,天天向上
Duo={}
Duo.game=function()
require "import"
--import "graph"
Duo.graph()
import "com.google.android.material.dialog.MaterialAlertDialogBuilder"
--graph.backgroundColor=0xFF383838
graph.lam=130
graph.o=Vector(w*0.05,h*0.8)
math.randomseed(os.time())
paintFill= Paint().setColor(0xFF4E4E4E).setStrokeWidth(5).setStyle(Paint.Style.STROKE)--STROKE
colors={
0xff179299,--深青色
0xff8839ef,--紫色
0xffdd7878,--橙红
0xff1e66f5,--深蓝
0xff6c6f85,--灰色
--0xffd20f39,--红色
0xffea76cb,--粉色
}
function 砖块() end
砖块={
__call=function(_,data)
return 砖块.new(data)
end,
__index = {
new=function(data)
local par=Particle({x=data.x})
par.r=data.r or .4
par.k=data.k or 1000
par.n=data.n or 5
par.isControl=data.isControl or false
par.paint=data.paint or paint
par.paintText=Paint().setColor(0xFFFFFFFF).setAntiAlias(true).setTextAlign(Paint.Align.LEFT).setTextSize(40).setStrokeCap(Paint.Cap.ROUND).setStyle(Paint.Style.FILL).setStrokeWidth(3)
return setmetatable(par,砖块)
end,
onDraw=function(砖块_,canvas)
graph.drawRect(canvas,砖块_.x+Vector(砖块_.r,砖块_.r),砖块_.x-Vector(砖块_.r,砖块_.r),砖块_.paint)
graph.drawText(canvas,""..砖块_.n,砖块_.x,砖块_.paintText)
end,
collision=function(砖块_,particle)
--local F=Vector()
local isCollision=false
local x,y=particle.x.x,particle.x.y
local x0,y0,r=砖块_.x.x,砖块_.x.y,砖块_.r
local y_down,y_up,x_left,x_right=y0-r,y0+r,x0-r,x0+r
if x>x_left and x<x_right and y>y_down and y<y_up then
isCollision=true
local dx=particle.x - 砖块_.x
if math.abs(dx.x)>math.abs(dx.y) then--在左右受力
if dx.x<0 then--在左
--F.x=-(砖块_.r+dx.x)*砖块_.k
particle.v.x=-particle.v.x
else--在右
--F.x=(砖块_.r-dx.x)*砖块_.k
particle.v.x=-particle.v.x
end
else--在上下
if dx.y<0 then--在下
--F.y=-(砖块_.r+dx.y)*砖块_.k
particle.v.y=-particle.v.y
else
--F.y=(砖块_.r-dx.y)*砖块_.k
particle.v.y=-particle.v.y
end
end
end
return isCollision
end,
}
}
setmetatable(砖块, 砖块)
砖块组={}
for x=.5,5.5 do
for y=3.5,9.5 do
砖块组[#砖块组+1]=砖块({
x=Vector(x,y),
paint=Paint().setColor(colors[math.random(1,#colors)]).setStrokeWidth(5).setStyle(Paint.Style.FILL),
n=math.random(1,5)
})
end
end
砖块组[#砖块组+1]=砖块({x=Vector(5,10+10),r=10,isControl=true})
砖块组[#砖块组+1]=砖块({x=Vector(6+10,5),r=10,isControl=true})
砖块组[#砖块组+1]=砖块({x=Vector(-10,5),r=10,isControl=true})
砖块组[#砖块组+1]=砖块({x=Vector(0,-.5),r=.5,isControl=true})
小球组={}
for i=1,1 do
小球组[#小球组+1]=Particle({x=Vector(),v=Vector(2,1.2)})
end
function main()
for n=1,#小球组 do
local A=小球组[n]
for i=1,#砖块组 do
local item=砖块组[i]
if item
if item:collision(A) and not(item.isControl) then
if item.n>1
item.n=item.n-1
else
table.remove(砖块组,i)
end
if math.random(1,100)<=15 then
print_("裂变!得到1个小球")
小球组[#小球组+1]=Particle({x=A.x,v=Vector(math.random(-20,20)/10,math.random(-20,20)/10)})
end
end
end
end
A:update()
end
end
function showDialog(标题,内容,Todo)
local dialog=MaterialAlertDialogBuilder(activity)
.setTitle(标题)
.setMessage(内容)
.setPositiveButton("确定",{onClick=function() Todo() end})
.show()
end
Env.dt=0.02
timer_=Ticker()
timer_.Period=Env.dt*1000
timer_.onTick=function()
Env.t=Env.t+Env.dt
main()
data=小球组
if #砖块组==4 then
print_("恭喜通关")
timer_.stop()
showDialog("恭喜通关","打完了所有砖块",function() activity.finish() end)
end
end
timer_.start()
function onDestroy()
timer_.stop()
end
function onDraw(canvas,graph)
for i=1,#砖块组 do
local item=砖块组[i]
item:onDraw(canvas)
end
--graph.drawPoint(canvas,A.x,)
end
var_a.setValueFrom(0)
var_a.setValueTo(6)
var_a.setValue(3)
var_a.addOnChangeListener({
onValueChange=function(view,value,bool)
local a=tonumber(value)
砖块组[#砖块组].x.x=a
end
})
end
Duo.graph=function()
--[[**
* @Name: 一个小游戏.lua
* @Date: 2024-02-15 01:15:18
* @Description: No special instructions
*
**]]
require "import"
import "android.os.*"
import "android.app.*"
import "android.view.*"
import "android.widget.*"
import "android.graphics.*"
import "android.animation.*"
import "com.bumptech.glide.Glide"
import "com.google.android.material.dialog.MaterialAlertDialogBuilder"
--导入nature库,手册没有(
--import "nature"
Duo.nature()
local Slider = luajava.bindClass "com.google.android.material.slider.Slider"
local MaterialCardView = luajava.bindClass "com.google.android.material.card.MaterialCardView"
--获取设备屏幕宽高
h=activity.getHeight()
w=activity.getWidth()
--初始化随机数种子
math.randomseed(os.time())
--重写print
print_=function(...)
txt.setText(txt.text.."\n"..(...))
end
graph={
tps={},
tps_0={},
dtps=Vector(),
o=Vector(w/2,h/2),
o_0=Vector(w/2,h/2),--
size=#Vector(w/2,h/2),--屏幕大小
tpl_0=0,
tpl=0,
lam=150,
lam_0=150,
tpn=0,
debugMode=false,
select={},--被选择的几何对象,内部保存对象的标签
}
--Animation
local animation = ValueAnimator.ofFloat({ 0, 5*math.pi }).setDuration(3000).setRepeatCount(-1).setRepeatMode(2).start()
--初始化一些笔
paint = Paint().setColor(0xee5E35B1).setStyle(Paint.Style.STROKE).setStrokeWidth(10).setAntiAlias(true).setStrokeCap(Paint.Cap.ROUND)
paintOut= Paint().setColor(0xFFF6F1ED).setStrokeWidth(10).setStyle(Paint.Style.STROKE).setShadowLayer(30,0,0,0xFFF5EEE2)
paintOut2= Paint().setColor(0xFFAE4839).setStrokeWidth(10).setStyle(Paint.Style.STROKE).setShadowLayer(30,0,0,0xFFAE4839)
paintWeb = Paint().setColor(0xFFF6F1ED).setStrokeWidth(10)
paintFill= Paint().setColor(0xFFF6F1ED).setStrokeWidth(5).setStyle(Paint.Style.FILL).setShadowLayer(30,0,0,0xFFF5EEE2)
paintControl= Paint().setColor(0xFF5E4B3C).setStrokeWidth(8).setStyle(Paint.Style.STROKE)
paintText = Paint().setColor(0xFFC2AE8E).setAntiAlias(true).setTextAlign(Paint.Align.LEFT).setTextSize(50).setStrokeCap(Paint.Cap.ROUND).setStyle(Paint.Style.FILL).setStrokeWidth(3)
paintTreasure_1=Paint().setColor(0xFFFCF4C9).setStrokeWidth(10).setStyle(Paint.Style.STROKE).setShadowLayer(30,0,0,0xFFF5EEE2)
paintTreasure_2=Paint().setColor(0xFFCBBC89).setStrokeWidth(10).setStyle(Paint.Style.FILL).setShadowLayer(30,0,0,0xFFF5EEE2)
paintTreasure_T = Paint().setColor(0xFFF6F1ED).setAntiAlias(true).setTextAlign(Paint.Align.LEFT).setTextSize(35).setStrokeCap(Paint.Cap.ROUND).setStyle(Paint.Style.FILL).setStrokeWidth(3)
paintTreasure_3=Paint().setColor(0xFF6AA03B).setStrokeWidth(10).setStyle(Paint.Style.STROKE).setShadowLayer(30,0,0,0xFFF5EEE2)
paint坐标轴=Paint().setColor(0xAA252525).setStyle(Paint.Style.STROKE).setStrokeWidth(4).setAntiAlias(true).setStrokeCap(Paint.Cap.ROUND)
paint_Red=Paint().setColor(0xFFE60721).setStyle(Paint.Style.STROKE).setStrokeWidth(5).setAntiAlias(true).setStrokeCap(Paint.Cap.ROUND)
paint_Blue=Paint().setColor(0xFF0066C7).setStyle(Paint.Style.STROKE).setStrokeWidth(10).setAntiAlias(true).setStrokeCap(Paint.Cap.ROUND)
paint_Green=Paint().setColor(0xFF007F24).setStyle(Paint.Style.STROKE).setStrokeWidth(8).setAntiAlias(true).setStrokeCap(Paint.Cap.ROUND)
paint_Black=Paint().setColor(0xFF252525).setStyle(Paint.Style.STROKE).setStrokeWidth(5).setAntiAlias(true).setStrokeCap(Paint.Cap.ROUND)
function showDialog(标题,内容,Todo)
local dialog=MaterialAlertDialogBuilder(activity)
.setTitle(标题)
.setMessage(内容)
.setPositiveButton("确定",{onClick=function() Todo() end})
.show()
end
--布局表
local layout =
{ FrameLayout,
layout_width = 'fill',
layout_height = 'fill',
{ SurfaceView;
layout_width = 'fill',
layout_height = 'fill',
id = "surface",
},
{ TextView,
text="Duo提供技术支持",
--textColor=0xFFF6F1ED,
textSize="12dp",
layout_gravity="bottom",
id="txt",
},
{ Button;
layout_gravity="bottom|right";
text="reset";
onClick=function()
graph.reset()
end,
},
{ Button;
layout_width="220px";
layout_height="130px";
layout_gravity="top|left";
id="btn1";
},
{ Button;
layout_width="220px";
layout_height="130px";
layout_gravity="top|right";
id="btn2";
text="";
},
{
Slider,--滑动条
layout_marginBottom="50dp",
layout_gravity="bottom",
id="var_a",
ValueFrom=-5,
Value=0,
ValueTo=5,
alpha=0.9,
},
{MaterialCardView,
layout_height="150dp",
layout_width="220dp",
radius="3dp",cardElevation=0,
layout_margin='5dp',strokeColor=0xFFB2B2B2,
strokeWidth=1.5,cardBackgroundColor=0xFFFFFFff,
layout_gravity="top|center",
id="select_card",
{ LinearLayout,
orientation='vertical',
layout_width='fill',
layout_height='fill',
background='#00FFFFFF',
{MaterialCardView,
layout_height="30dp",
layout_width="220dp",
radius="0dp",cardElevation=0,
layout_margin='0dp',
strokeWidth=0,cardBackgroundColor=0xFFD2E9FF,
layout_gravity="top|center",
{ TextView,
text="Duo提供技术支持",
--textColor=0xFFF6F1ED,
textSize="16dp",layout_margin='2dp',
layout_gravity="bottom",
id="select_card_title",
},
},
{MaterialCardView,
layout_height="1.5dp",
layout_width="fill",
radius="6dp",cardElevation=0,
layout_margin='0dp',
strokeWidth=0,cardBackgroundColor=0xFF999999,
layout_gravity="top|center",
},
{MaterialCardView,
layout_height="fill",
layout_width="fill",
radius="6dp",cardElevation=0,
layout_margin='0dp',
strokeWidth=0,cardBackgroundColor=0xFFFFFFFF,
layout_gravity="top|center",
{ TextView,
text="Duo提供技术支持",
--textColor=0xFFF6F1ED,
textSize="12dp",layout_margin='2dp',
layout_gravity="top",
id="select_card_txt",
},
},
},
}
}
activity.setContentView(loadlayout(layout))
select_card_dimiss=function(a)
if a then
select_card.alpha=0
else
select_card.alpha=1
end
end
select_card_dimiss(true)
graph.backgroundColor=0xFFffffff
graph.reset=function()
graph.lam=150
graph.o=Vector(w*0.05,h*0.8)
end
graph.toSP=function(v)
return Vector(v.x*graph.lam+graph.o.x,
v.y*(-graph.lam)+graph.o.y)
end
graph.drawPoint=function(canvas,v,p_)
local p_=p_ or paint
canvas.drawCircle(v.x*graph.lam+graph.o.x, v.y*(-graph.lam)+graph.o.y, 8, p_)
end
graph.drawParticle=function(canvas,particle,p_)
local p_=p_ or paint
canvas.drawCircle(particle.x.x*graph.lam+graph.o.x, particle.x.y*(-graph.lam)+graph.o.y, 8, p_)
end
canvas__=Canvas
graph.drawRect=function(canvas,p1,p2,p_)
local p_=p_ or paint
canvas.drawRect(p1.x*graph.lam+graph.o.x, p1.y*(-graph.lam)+graph.o.y,
p2.x*graph.lam+graph.o.x, p2.y*(-graph.lam)+graph.o.y,
p_)
end
graph.drawText=function(canvas,str,p,p_)
local p_=p_ or paint
canvas.drawText(str,p.x*graph.lam+graph.o.x, p.y*(-graph.lam)+graph.o.y,p_)
end
graph.drawSegment=function(canvas,p0,p1,p_)
local p_=p_ or paint
canvas.drawLine(
p0.x*graph.lam+o.x,
-p0.y*graph.lam+o.y,
(p1.x)*graph.lam+o.x,
-(p1.y)*graph.lam+o.y, p)
end
graph.drawLine=function(canvas,l,p_)
local p_=p_ or paint
local a=(graph.size/graph.lam)
canvas.drawLine(
(l.p.x-a*l.v.x)*graph.lam+o.x,
-(l.p.y-a*l.v.y)*graph.lam+o.y,
(l.p.x+a*l.v.x)*graph.lam+o.x,
-(l.p.y+a*l.v.y)*graph.lam+o.y, p_)
end
graph.drawConic0=function(canvas,c,p_)
local p_=p_ or paint
local p0=graph.toSP(c:indexPoint(0))
local p1
local dx=0.15*(1/graph.lam)
local path = Path()
path.moveTo(p0.x,p0.y)
if dx<=0.05 then dx=0.05 end
for theta=0,2*pi+dx,dx do
p1=graph.toSP(c:indexPoint(theta))
path.lineTo(p1.x,p1.y)
end
canvas.drawPath(path,p_)
end
graph.drawTriangle=function(canvas,t,p_)
local p_=p_ or paint
local path = Path()
local pa=graph.toSP(t.a)
path.moveTo(pa.x,pa.y)
local pb=graph.toSP(t.b)
path.lineTo(pb.x,pb.y)
local pc=graph.toSP(t.c)
path.lineTo(pc.x,pc.y)
path.close()
canvas.drawPath(path,p_)
end
graph.drawCurve=function(canvas,c,p_)
local p_=p_ or paint
local p0=graph.toSP(c:indexPoint(c.range[1]))
local p1
local dx=0.15*(1/graph.lam)
local path = Path()
path.moveTo(p0.x,p0.y)
if dx<=0.05 then dx=0.05 end
for t=c.range[1],c.range[2]+dx,dx do
p1=graph.toSP(c:indexPoint(t))
path.lineTo(p1.x,p1.y)
end
canvas.drawPath(path,p_)
end
data={}
onDraw=function() end
graph.onTouch=function() end
graph.onTouch_ACTION_DOWN=function() end
local holder = surface.getHolder()
holder.addCallback(SurfaceHolder.Callback {
surfaceChanged = function(holder, format, width, height)
end,
surfaceCreated = function(holder)
animation.addUpdateListener(ValueAnimator.AnimatorUpdateListener {
onAnimationUpdate = function(animate)
local k = animate.getAnimatedValue()
local canvas = holder.lockCanvas()
if canvas ~= nil then
--设置背景颜色
canvas.drawColor(graph.backgroundColor)
o=graph.o
Lambda=graph.lam
graph.drawPoint(canvas,Vector())
graph.drawPoint(canvas,Vector(1))
graph.drawPoint(canvas,Vector(0,1))
canvas.drawLine(-o.x+o.x,0+o.y,(w-o.x)+o.x,0+o.y, paint坐标轴)
canvas.drawLine(0+o.x,-o.y+o.y,0+o.x,(h-o.y)+o.y, paint坐标轴)
onDraw(canvas,graph)
--[[
for n=1,#data do
local item=data[n]
local class=getmetatable(item)
if class == Conic0 then
graph.drawConic0(canvas,item,paint_Black)
elseif class == Vector then
graph.drawPoint(canvas, item, paint_Blue)
elseif class == Line then
graph.drawLine(canvas, item, paint_Green)
elseif class == Curve then
graph.drawCurve(canvas, item, paint_Red)
elseif class == Triangle then
graph.drawTriangle(canvas, item, paint_Red)
end
end
]]
for a, item in pairs(data) do
local class=getmetatable(item)
if class == Conic0 then
graph.drawConic0(canvas,item,item.paint or paint_Black)
elseif class == Vector then
graph.drawPoint(canvas, item, item.paint or paint_Blue)
elseif class == Line then
graph.drawLine(canvas, item, item.paint or paint_Green)
elseif class == Curve then
graph.drawCurve(canvas, item, item.paint or paint_Red)
elseif class == Triangle then
graph.drawTriangle(canvas, item, item.paint or paint_Red)
elseif class == Particle then
graph.drawParticle(canvas, item, item.paint or paint_Blue)
end
end
if #graph.select==1 then
local item=data[(graph.select[1])]
local class=getmetatable(item)
if class == Particle then
select_card_dimiss(false)
local sp=graph.toSP(item.x)--该对象在屏幕上的位置
select_card.x=sp.x
select_card.y=sp.y
select_card_title.setText("质点: "..graph.select[1])
select_card_txt.setText("m: "..item.m.."\nx: ("..item.x.x..", "..item.x.y..")\nv: ("..item.v.x..", "..item.v.y..")\na: ("..item.a.x..", "..item.a.y..")\nq: "..item.q)
end
else
select_card_dimiss(true)
end
end
holder.unlockCanvasAndPost(canvas)
end
})
end,
surfaceDestroyed = function(holder)
animation.removeAllUpdateListeners()
animation.cancel()
end
})
Env.t=0
Env.dt=0.02
timer_=Ticker()
timer_.Period=Env.dt*1000
timer_.onTick=function()
Env.t=Env.t+Env.dt
main()
end
--timer_.start()
--退出程序后回收Ticker
function onDestroy()
timer_.stop()
end
--处理触摸事件
surface.onTouch =function(v, event)
graph.onTouch(v, event)
--获取有多少个手指碰到屏幕
local PointerCount = event.getPointerCount()
graph.tpn=PointerCount
--print_(event.getActionMasked())
--为每个手指记录触摸坐标
for i = 1, PointerCount do
if PointerCount==2
graph.tps[i]=Vector(event.getX(i-1),event.getY(i-1))
elseif PointerCount==1
local p=Vector(event.getX(i-1),event.getY(i-1))
graph.tps[1]=p
graph.tps[2]=false
end
end
if graph.debugMode print_(""..type(graph.tps[1])) end
--手指碰到屏幕
if(event.getActionMasked() == MotionEvent.ACTION_DOWN)
if graph.debugMode print_("手指碰到屏幕") end
graph.o_0=graph.o
graph.tps_0[1]=Vector(event.getX(1-1),event.getY(1-1))
--触摸点在坐标系中的坐标
local gtp=Vector((graph.tps_0[1].x-graph.o.x)/graph.lam,-(graph.tps_0[1].y-graph.o.y)/graph.lam)
--print_(dump(gtp))
for a, item in pairs(data) do
local class=getmetatable(item)
if class == Particle then
local dx=gtp-item.x
if (#dx)*graph.lam<50 then--检测被触摸
graph.select[1]=a--向选择对象表中添加此对象
--[[
select_card_dimiss(false)
local sp=graph.toSP(item.x)--该对象在屏幕上的位置
select_card.x=sp.x
select_card.y=sp.y
select_card_title.setText("质点: "..a)
select_card_txt.setText("m: "..item.m.."\nx: ("..item.x.x..", "..item.x.y..")\nv: ("..item.v.x..", "..item.v.y..")\na: ("..item.a.x..", "..item.a.y..")\nq: "..item.q)
--]]
end
end
end
graph.onTouch_ACTION_DOWN(gtp)
elseif(event.getActionMasked() == MotionEvent.ACTION_POINTER_DOWN && PointerCount <= 2)
--print_("次要手指碰到屏幕")
--graph.o_0=graph.o
graph.tps_0[2]=Vector(event.getX(2-1),event.getY(2-1))
graph.tpl_0 = #(graph.tps[1] - graph.tps[2])
graph.lam_0 = graph.lam
--]]
--手指移动
elseif(event.getActionMasked() == MotionEvent.ACTION_MOVE)
graph.select={}
select_card_dimiss(true)
--local dtp= 0.5*(graph.tps[1]+graph.tps[2]) - 0.5*(graph.tps_0[1]+graph.tps_0[2])
local dtp= graph.tps[1]-graph.tps_0[1]
graph.o = graph.o_0 + dtp*0.8--*Number.BooleanToNumber(not(graph.tps[2]))
if(PointerCount >= 2)
--print_("当有两根手指落下就计算缩放")
--[
graph.tpl = #(graph.tps[1] - graph.tps[2])
local dtpl = graph.tpl - graph.tpl_0
graph.lam=graph.lam_0*(dtpl/graph.tpl_0+1)
--]]
end
elseif(event.getActionMasked() == MotionEvent.ACTION_POINTER_UP)
--print_("次要手指离开")
graph.o_0=graph.o
graph.tps_0[1]=Vector(event.getX(1-1),event.getY(1-1))
elseif(event.getActionMasked() == MotionEvent.ACTION_UP)
-- print_("手指离开")
end
return true
end
end
Duo.nature=function()
--nature
info={
author="Duo",
version=5.02,
date="2023.6.3",
info=[[分享_自用数学库,将嵌入Duo Nature,可能存在错误,欢迎大家指出。将持续更新(因为学业原因,更新周期较长为一个月左右)。包括函数,统计,解析几何等,自习课写的程序长达50页,之后将加入线段,向量,三角形,圆锥曲线等几何类型(本来在纸上写好了,每次回来时间不太够,慢慢码吧)。提供互相作用,例如,求公共点,求垂直,平分线,平行线,角分线,等等等等()在Duo Nature环境下,可以直观展示各个几何类型及相互作用]],
history={
v1={"2023.3.05","基本初等函数,常数,统计,逻辑与判断"},
v2={"2023.4.03","点,直线,更普遍的加减乘除"},
v3={"2023.5.1","点,向量,直线,全部替换为二维和三维通用,新增平面,操作控制台输出,向量运算,空间直线的位置关系初步"},
v4={"2023.5.25","新增物理环境"},
v5={"2023.6","重写"},
},
}
Env={
e=math.exp(1),
pi=math.pi,
huge=math.huge,
inf=math.huge,
nan=0/0,
phi=(math.sqrt(5)-1)/2,
t=0,
dt=0.01,
dx=0.001,
d=10^(-7),
g=-10,
}
e=Env.e
pi=Env.pi
--指,对数
function log(a,b) return math.log10(b)/math.log10(a) end--对数
function lg(a) return math.log10(a) end--常用对数
function ln(a) return math.log(a) end--自然对数
function exp(a) return math.exp(a) end--自然指数函数
--三角
function sin(a) return math.sin(a) end--一般
function cos(a) return math.cos(a) end
function tan(a) return math.tan(a) end
function sinh(a) return math.sinh(a) end--双曲
function cosh(a) return math.cosh(a) end
function tanh(a) return math.tanh(a) end
function arcsin(a) return math.asin(a) end--反
function arccos(a) return math.acos(a) end
function arctan(a) return math.atan(a) end
--其它
function abs(a) return math.abs(a) end
function floor(a) return math.floor(a) end--向下取整
function ceil(a) return math.ceil(a) end--向上取整
function deg(a) return math.deg(a) end--角度转换
function rad(a) return math.rad(a) end
function max(...) return math.max(...) end--值
function min(...) return math.min(...) end
function mod(a,b) return a%b end
function atan2(a,b) return math.atan2(a,b) end--坐标转换
function random(a,b) return math.random(a,b) end--随机
function sqrt(a) return math.sqrt(a) end
function sinc(x)--信号分析
if x==0 then return 1
else return sin(x)/x
end
end
function sgn(x)--析离符号
if x>0 return 1
elseif x==0 return 0
else return -1
end
end
function key(x)--开关函数
if x>0 return 1 else return 0 end
end
function key0(x)--自然单位阶跃函数
return (sgn(x)+1)/2
end
function key1(x)--经典单位阶跃函数
if x>=0 return 1 else return 0 end
end
function root(x)--根函数
if x==0 return 1 else return 0 end
end
--三角函数互转
function sin2cos(s,x) return sqrt(1-(sin(s))^2)*sgn(x) end
function cos2sin(c,y) return sqrt(1-(cos(c))^2)*sgn(y) end
function sin2tan(s,x) return (sin(s)/sqrt(1-(sin(s))^2))*sgn(x) end
function cos2tan(c,y) return (sqrt(1-(cos(c))^2)/cos(c))*sgn(y) end
function tan2sin(t,y) return sqrt(1/(1+(1/(tan(t))^2)))*sgn(y) end
function tan2cos(t,x) return sqrt(1/(1+((tan(t))^2)))*sgn(x) end
--三角函数转角
function sin2w(s,x) return arcsin(s)+(1/2)*(1-sgn(x))*(math.pi-2*arcsin(s)) end
function cos2w(c,y) return arccos(c)*sgn(y) end
function tan2w(t,y) return arctan(t)+(math.pi/2)*(1-sgn(y)) end
--阶乘
function factorial(n)
if n == 0 or n == 1 then
return 1
else
return n * factorial(n - 1)
end
end
--#Number 数值类
Number={
BooleanToNumber=function(b)
if b then return 1 else return 0 end
end,
}
--#Equation 方程
function Equation() end
Equation={
solve2x2LinearSystem=function(a1, b1, c1, a2, b2, c2)
--[[a1 x + b1 y = c1, a2 x + b2 y = c2 ]]
local D = a1 * b2 - a2 * b1-- 计算行列式D
if D == 0 then-- 检查行列式是否为零
return nil,nil, "error: 行列式为零,方程组无解或有无穷多解"
else-- 计算x和y的值
local x = (c1 * b2 - c2 * b1) / D
local y = (a1 * c2 - a2 * c1) / D
return x, y
end
end,
solveSinForMainRoot=function(a,w,p,c)--计算三角方程的主解 a sin(w t+p)+c=0
local u=math.asin(-c/a)
return {(u-p)/w,(math.pi-u-p)/w}
end,--print(Equation.solveSinForMainRoot(2,3,4,-1)[2])
solveCosSinForMainRoot=function(u,v,c)--计算三角方程的主解 u cos(t)+v sin(t)+c=0
local a=math.sqrt(u^2+v^2)*sgn(v)
local p=math.atan(u/v)
return Equation.solveSinForMainRoot(a,1,p,c)
end,--print(Equation.solveCosSinForMainRoot(2,3,1)[2])
}
--#Is 判断类
function Is() end
Is={
Oddnumber=function(x)--判断奇数
if is.integer(x) and not(is.evennumber(x)) then
return true
else return false
end
end,
Integer=function(x)--判断整数
if x==floor(x) then
return true
else return false
end
end,
Evennumber=function(x)--判断偶数
if is.integer(x) and x%2==0 then
return true
else return false
end
end,
Zero=function(data)--判断一个: 整数等于零 or 浮点数接近零
return math.abs(data)<=Env.d
end,
Equality=function(a,b)--判断一个: 整数相等 or 浮点数接近
return Number.isZero(a-b)
end,
Vector=function(data)--判断是否为向量
return getmetatable(data) == Vector
end,
Number=function(data)--判断是否为数字
return type(data)=='number'
end,
Table=function(data)--判断是否为数组
return type(data)=='table'
end,
}
--#Vector 向量
function Vector() end
Vector={
__call=function(_,x,y,z)
return Vector.new(x,y,z)
end,
__add=function(m,n)
return Vector.add(m,n)
end,
__sub=function(m,n)
return Vector.sub(m,n)
end,
__mul=function(m,n)
if Is.Vector(m) and Is.Vector(n) then
return Vector.dot(m,n)
elseif (Is.Number(m) and Is.Vector(n)) then
return Vector.scale(n,m)
elseif (Is.Number(n) and Is.Vector(m)) then
return Vector.scale(m,n)
end
end,
__div=function(m,n)
return Vector.div(m,n)
end,
__len=function(m)
return Vector.len(m)
end,
__unm = function(m)
return Vector.scale(-1,m)
end,
__index={
new=function(a,b,c)
return setmetatable({x=a or 0.0,y=b or 0.0,z=c or 0.0 },Vector)
end,
new_ang=function(theta,phi,l)
local theta=theta or 0
local phi=phi or 0
local l=l or 1
local x=cos(theta)*cos(phi)*l
local y=sin(theta)*cos(phi)*l
local z=sin(phi)*l
return setmetatable({x=x,y=y,z=z },Vector)
end,
new_ang2=function(theta,l)
local l=l or 1
local x=cos(theta)*l
local y=sin(theta)*l