-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMainWindow.xaml.vb
6793 lines (5812 loc) · 270 KB
/
MainWindow.xaml.vb
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
'کتابخانه ریاضی
Imports System.Math
' کتابخانه گرافیک
Imports System.Windows.Media
Imports System.Windows.Media.Imaging
Imports System.Windows.Media.Media3D
Imports System.Windows.Shapes
Imports System.Windows.Media.Animation
'کتابخانه متن
Imports System.Text
Imports System.Text.RegularExpressions
'کتابخانه فایل
Imports System.IO
Imports Microsoft.Win32
Imports System.ComponentModel
Imports System.Collections.ObjectModel
Imports System.Globalization
Imports System.Reflection
Imports System.Runtime.InteropServices
Imports System.Windows.Threading
Imports System.Xml.Serialization
Imports AKP_Math_Graph_Plotter.AKP.Input.Joystick
Imports System.Media
Class MainWindow
Implements INotifyPropertyChanged
Public Event PropertyChanged As PropertyChangedEventHandler _
Implements INotifyPropertyChanged.PropertyChanged
Private Sub NotifyPropertyChanged(Optional ByVal propertyName As String = Nothing)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
End Sub
'بارگذاری پنجره
Private Sub Window_Load(sender As Object, e As RoutedEventArgs)
UseLayoutRounding = True
CenterPoint = New Point(Cor2D_Behind.ActualWidth / 2, Cor2D_Behind.ActualHeight / 2)
DrawCoordinateSystem2D()
DrawCoordinateSystem3D()
MathObjList.Add(New MathObject(99))
AddFunction()
DeveloperMarquee()
'If IsTouchScreen() Then
' MarqueeMsg("صفحه لمسی شناسایی شد . هماهنگی کامل با صفحه لمسی و تبلت های ویندوزی بزودی ...", 4)
'End If
AKPTerminal.Owner = Window.GetWindow(Me)
AboutWindow.Owner = Window.GetWindow(Me)
ExamplesWindow.Owner = Window.GetWindow(Me)
'Joystick = XboxController.RetrieveController(0)
Joystick.StartPolling()
End Sub
'بستن پنجره
Private Sub Window_Closed(sender As Object, e As EventArgs)
Joystick.StopPolling()
Application.Current.Shutdown()
End Sub
'پنجره ها جانبی
Public AKPTerminal As New TerminalWindow
Private Sub OpenTerminal(sender As Object, e As RoutedEventArgs)
AKPTerminal.Show()
End Sub
Dim ExamplesWindow As New Examples
Private Sub OpenExamples(sender As Object, e As RoutedEventArgs)
ExamplesWindow.Show()
End Sub
Dim AboutWindow As New About
Private Sub OpenAbout(sender As Object, e As RoutedEventArgs)
AboutWindow.Show()
End Sub
Private Sub OpenHelp(sender As Object, e As RoutedEventArgs)
Dim D As New Dialog("برای راهنما به فایل PDF موجود در سی دی نرم افزار مراجعه کنید .", "Help")
D.ShowDialog()
End Sub
'تنظیمات رسم
Private Sub ShowDrawSetting(sender As Object, e As RoutedEventArgs)
DrawSettingPopup.IsOpen = Not DrawSettingPopup.IsOpen
End Sub
Shared Property UnlockBetaFeatures As Boolean = False
'اطلاعات برنامه نویس
Private Sub DeveloperMarquee()
Dev0.Inlines.Add(New Run("نرم افزار مدلساز و رسم نمودار ریاضی ") With {.Foreground = Brushes.Blue})
Dev0.Inlines.Add(New Run("برنامه نویس : ") With {.Foreground = Brushes.Red})
Dev0.Inlines.Add("آرمین کاظمی ")
Dev0.Inlines.Add(New Run("دبیرستان شهید مدنی 1 تبریز ") With {.Foreground = Brushes.DarkGreen})
Dev0.Inlines.Add(New Run("ناحیه 4 ") With {.Foreground = Brushes.Olive})
Dev0.UpdateLayout()
Dim DevAnim As New DoubleAnimation
DevAnim.From = DevCan.ActualWidth
DevAnim.To = -1 * Dev0.ActualWidth
DevAnim.Duration = TimeSpan.FromSeconds(ScrollTime)
DevAnim.RepeatBehavior = RepeatBehavior.Forever
Dev0.BeginAnimation(Canvas.LeftProperty, DevAnim)
End Sub
'پیام به کاربر
Private ScrollTime As Integer = 15
Private Sub MarqueeMsg(Msg As String, Optional ReadTime As Integer = 2)
Dev0.Inlines.Add(New Run(" " + Msg) With {.Foreground = Brushes.LimeGreen})
Dev0.UpdateLayout()
Dim DevAnim As New DoubleAnimation
DevAnim.From = DevCan.ActualWidth
DevAnim.To = -1 * Dev0.ActualWidth
ScrollTime += ReadTime
DevAnim.Duration = TimeSpan.FromSeconds(ScrollTime)
DevAnim.RepeatBehavior = RepeatBehavior.Forever
Dev0.BeginAnimation(Canvas.LeftProperty, DevAnim)
End Sub
'بررسی صفحه لمسی بودن دستگاه
Private Function IsTouchScreen() As Boolean
For Each TabletDevice As TabletDevice In Tablet.TabletDevices
If TabletDevice.Type = TabletDeviceType.Touch Then
Return True
End If
Next
Return False
End Function
#Region "ابزار های ریاضی"
'محاسبه گر ریاضی
Dim AKPEval As New MathEvaluator
'تولید کننده عدد تصادفی
Dim Rnd As New Random
#End Region
#Region "لیست اشیا"
Public Property MathObjList As New ObservableCollection(Of MathObject)
'اضافه کردن تابع
Public Sub AddFunction(Optional Exp As String = "", Optional Ty As Integer = 1, _
Optional DS As Double = Double.NegativeInfinity, Optional DE As Double = Double.PositiveInfinity, _
Optional DS2 As Double = Double.NegativeInfinity, Optional DE2 As Double = Double.PositiveInfinity, _
Optional DExcept As DoubleCollection = Nothing, _
Optional EqBrush As Brush = Nothing, Optional ThickN As Double = 2, Optional Opacity As Double = 1, _
Optional IsVisible As Boolean = True)
If DExcept Is Nothing Then DExcept = New DoubleCollection
If EqBrush Is Nothing Then EqBrush = New SolidColorBrush(PickColor())
MathObjList.Insert(Eq_ListBox.Items.Count - 1, New MathObject(0) With {.Inner = New Equation With {.Expression = Exp, .Type = Ty,
.DomainStart = DS, .DomainEnd = DE, .SecondDomainStart = DS2, .SecondDomainEnd = DE2, .DomainExcept = DExcept,
.Brush = EqBrush, .Thickness = ThickN, .Opacity = Opacity, .IsVisible = IsVisible,
.HasError = False}})
Eq_ListBox.Items.Refresh()
Eq_ListBox.SelectedIndex = Eq_ListBox.Items.Count - 2
End Sub
Public Sub AddFunction(F As Equation)
MathObjList.Insert(Eq_ListBox.Items.Count - 1, New MathObject(0) With {.Inner = F})
Eq_ListBox.Items.Refresh()
End Sub
Public Sub AddEmptyFunction(sender As Object, e As RoutedEventArgs)
AddFunction()
AutoRedraw()
End Sub
'اضافه کردن متغییر
Public Sub AddVariable(Optional Name As String = Nothing, Optional Value As Double = 0, _
Optional Min As Double = -10, Optional Max As Double = 10, _
Optional By As Double = 1, Optional Duration As Double = 0.5, _
Optional TickMode As Variable.TickModes = Variable.TickModes.AutoReverse, Optional Autoplay As Boolean = False)
If Name = Nothing Then Name = PickChar()
MathObjList.Insert(Eq_ListBox.Items.Count - 1, New MathObject(1) With {.Inner = New Variable With {.Name = Name, .Value = Value, .By = By _
, .Duration = Duration, .Min = Min, .Max = Max, .TickMode = TickMode, .Autoplay = Autoplay}})
Eq_ListBox.Items.Refresh()
Eq_ListBox.SelectedIndex = Eq_ListBox.Items.Count - 2
End Sub
Public Sub AddVariable(V As Variable)
MathObjList.Insert(Eq_ListBox.Items.Count - 1, New MathObject(1) With {.Inner = V})
Eq_ListBox.Items.Refresh()
End Sub
Public Sub AddEmptyVariable(sender As Object, e As RoutedEventArgs)
AddVariable()
AutoRedraw()
End Sub
'اضافه کردن نقطه
Public Sub AddPoint(Optional Name As String = Nothing, _
Optional X As String = "0", Optional Y As String = "0", Optional Z As String = "0", _
Optional PColor As Color = Nothing, Optional IsVisible As Boolean = True, Optional IsLight As Boolean = False)
If PColor = Nothing Then PColor = PickColor()
If Name = Nothing Then Name = PickChar("P")
MathObjList.Insert(Eq_ListBox.Items.Count - 1, New MathObject(2) With {.Inner = New APointViewer With {.Name = Name, .Point = New APoint(X, Y, Z), .Color = PColor, .IsVisible = IsVisible, .IsLight = IsLight}})
Eq_ListBox.Items.Refresh()
Eq_ListBox.SelectedIndex = Eq_ListBox.Items.Count - 2
End Sub
Public Sub AddPoint(P As APointViewer)
MathObjList.Insert(Eq_ListBox.Items.Count - 1, New MathObject(2) With {.Inner = P})
Eq_ListBox.Items.Refresh()
End Sub
Public Sub AddEmptyPoint(sender As Object, e As RoutedEventArgs)
AddPoint()
AutoRedraw()
End Sub
'اضافه کردن بردار
Public Sub AddVector(Optional Name As String = Nothing, _
Optional I As String = "0", Optional J As String = "0", Optional K As String = "0", _
Optional PointName As String = "", _
Optional PColor As Color = Nothing, Optional IsVisible As Boolean = True)
If PColor = Nothing Then PColor = PickColor()
If Name = Nothing Then Name = PickChar("V")
MathObjList.Insert(Eq_ListBox.Items.Count - 1, New MathObject(3) With {.Inner = New AVectorViewer With {.Name = Name, .Vector = New AVector(I, J, K), .PointName = PointName, .Color = PColor, .IsVisible = IsVisible}})
Eq_ListBox.Items.Refresh()
Eq_ListBox.SelectedIndex = Eq_ListBox.Items.Count - 2
End Sub
Public Sub AddVector(V As AVectorViewer)
MathObjList.Insert(Eq_ListBox.Items.Count - 1, New MathObject(3) With {.Inner = V})
Eq_ListBox.Items.Refresh()
End Sub
Public Sub AddEmptyVector(sender As Object, e As RoutedEventArgs)
AddVector()
AutoRedraw()
End Sub
'اضافه کردن شکل
Public Sub AddShape(Optional Name As String = "", Optional ShapeType As ShapeType = ShapeType.None, _
Optional PointName As String = "", Optional VectorName As String = "", _
Optional Length As String = "", Optional Width As String = "", Optional Height As String = "", _
Optional Radius As String = "", Optional Radius2 As String = "", Optional Radius3 As String = "",
Optional FaceCount As Integer = 3, Optional CubeFace As Integer = BoxFaces.All, _
Optional BaseCap As Boolean = True, Optional TopCap As Boolean = True, _
Optional ThetaDiv As Integer = 40, Optional PhiDiv As Integer = 20, _
Optional ShBrush As Brush = Nothing, Optional Visibility As Boolean = True)
If ShBrush Is Nothing Then ShBrush = New SolidColorBrush(PickColor())
MathObjList.Insert(Eq_ListBox.Items.Count - 1, New MathObject(4) With {.Inner = New AShape With {.Name = Name, .Type = ShapeType, .PointName = PointName, .VectorName = VectorName, .Length = Length, .Width = Width, .Height = Height, _
.Radius = Radius, .Radius2 = Radius2, .Radius3 = Radius3, .FaceCount = FaceCount, .CubeFace = CubeFace, .BaseCap = BaseCap, .TopCap = TopCap, .ThetaDiv = ThetaDiv, .PhiDiv = PhiDiv, .Brush = ShBrush, .IsVisible = Visibility}})
Eq_ListBox.Items.Refresh()
Eq_ListBox.SelectedIndex = Eq_ListBox.Items.Count - 2
End Sub
Public Sub AddShape(S As AShape)
MathObjList.Insert(Eq_ListBox.Items.Count - 1, New MathObject(4) With {.Inner = S})
Eq_ListBox.Items.Refresh()
End Sub
Public Sub AddEmptyCone()
AddShape(, ShapeType.Cone)
End Sub
Public Sub AddEmptyTCone()
AddShape(, ShapeType.TCone)
End Sub
Public Sub AddEmptyCylinder()
AddShape(, ShapeType.Cylinder)
End Sub
Public Sub AddEmptyPyramid()
AddShape(, ShapeType.Pyramid)
End Sub
Public Sub AddEmptyCube()
AddShape(, ShapeType.Cube)
End Sub
Public Sub AddEmptyBox()
AddShape(, ShapeType.Box)
End Sub
Public Sub AddEmptySphere()
AddShape(, ShapeType.Sphere)
End Sub
Public Sub AddEmptyEllipsoid()
AddShape(, ShapeType.Ellipsoid)
End Sub
'حذف
Public Sub Delete(MO As MathObject)
If MO.Type <> 1 And MO.Type <> 99 Then
For Each GO In MO.Inner.GraphObjectt
If Cor2D_Front.Children.Contains(TryCast(GO, UIElement)) Then
Cor2D_Front.Children.Remove(GO)
End If
If VisualModel.Children.Contains(TryCast(GO, Model3D)) Then
VisualModel.Children.Remove(GO)
End If
Next
End If
End Sub
'حذف همه
Public Sub DeleteAll()
Cor2D_Front.Children.Clear()
VisualModel.Children.Clear()
MathObjList.Clear()
MathObjList.Add(New MathObject(99))
End Sub
#End Region
#Region "ابزار های تابع"
'ساده سازی ضابطه
Public Function SimplifyFunction(ByRef Eq As Equation, ByRef Success As Boolean) As String
' Input : y=f(x)
' Undirect Tasks : Detect Function Type , Check For Error
' Direct Output : f(x)
Dim EqExp As String = Eq.Expression
Dim IsCompletlyTyped As Boolean = EqExp.Contains("=")
Dim IsParametric As Boolean = EqExp.Contains(",") 'معادله پارامتریک
Dim IsImplicit As Boolean = False 'تابع ضمنی
Dim Output As String = ""
Success = True
'ورودی کارتزین
Dim InX As Boolean = False
Dim InY As Boolean = False
Dim InZ As Boolean = False
'ورودی قطبی
Dim InT As Boolean = False
Dim InR As Boolean = False
Dim InP As Boolean = False
'خروجی کارتزین
Dim OutX As Boolean = False
Dim OutY As Boolean = False
Dim OutZ As Boolean = False
'خروجی قطبی
Dim OutT As Boolean = False
Dim OutR As Boolean = False
Dim OutP As Boolean = False
If Not (EqExp Is Nothing Or EqExp = "") Then
If IsParametric Then 'معادله پارامتریک است
Dim Pram() As String = EqExp.Split(",")
If Pram.Length = 2 Then
'Eq.Type = 7
Return EqExp
ElseIf Pram.Length = 3 Then
End If
End If
If IsCompletlyTyped Then ' ضابطه تابع بصورت کامل تایپ شده
Dim Func() As String = EqExp.Split("=")
Dim FuncFrist As String = Func(0).Trim(" ")
Dim FuncSecond As String = Func(1).Trim(" ")
If FuncFrist.Length = 1 And (Not IsNumeric(FuncFrist)) Then ' تابع واقعی
Select Case FuncFrist.ToLower
Case "x"
OutX = True
Case "y"
OutY = True
Case "z"
OutZ = True
Case "t"
OutT = True
Case "r"
OutR = True
Case "p"
OutP = True
End Select
Output = FuncSecond
ElseIf FuncSecond.Length = 1 And (Not IsNumeric(FuncSecond)) Then ' تابع واقعی با جابجایی
Select Case FuncSecond.ToLower
Case "x"
OutX = True
Case "y"
OutY = True
Case "z"
OutZ = True
Case "t"
OutT = True
Case "r"
OutR = True
Case "p"
OutP = True
End Select
Output = FuncFrist
Else ' تابع ضمنی
IsImplicit = True
Success = False
'Eq.HasError = True
'Eq.ErrorMsg = "خروجی تابع باید بر حسب y یا x (کارتزین) و یا r یا t (قطبی) باشد لطفا به بخش راهنما مراجعه کنید"
End If
Else 'ضابطه تابع بصورت ناقص تایپ شده
''TODO AUTO DETECT
Success = False
'Eq.HasError = True
'Eq.ErrorMsg = "خروجی تابع مشخص نشده است ! لطفا به بخش راهنما مراجعه کنید"
End If
Else 'ضابطه تابع تایپ نشده
Success = False
End If
EquationTools.FixNotWrittenMul(Output)
Return Output.ToLower
End Function
'تشخیص وابسته بودن به متغییر
Public Function HaveVarsInFormula(Eq As Equation) As Boolean
Dim EqMain As String = Eq.Expression.ToLower
For Each MOV As MathObject In MathObjList
If MOV.Type = 1 Then
Dim Var As Variable = MOV.Inner
If EqMain.Contains(Var.Name) Then
EqMain = EquationTools.Replace(EqMain, Var.Name, Var.Value)
Return True
End If
End If
Next
Return False
End Function
#End Region
#Region "رسم کلی"
'حالت رسم - 2 یا 3 یا 4 بعدی
Private m_DrawMode As String = "2D"
Public Property DrawMode As String
Get
Return m_DrawMode
End Get
Set(value As String)
If Not (value = m_DrawMode) Then
m_DrawMode = value
NotifyPropertyChanged("DrawMode")
ClearGraphObjects()
End If
End Set
End Property
Private Sub SwitchDM()
If DrawMode = "2D" Then
IsCor3DModified = True
DrawMode = "3D"
ElseIf DrawMode = "3D" Then
IsCor3DModified = True
DrawMode = "4D"
ElseIf DrawMode = "4D" Then
IsCor2DModified = True
DrawMode = "2D"
End If
AutoRedraw()
End Sub
Private Sub ReverseSwitchDM()
If DrawMode = "2D" Then
IsCor3DModified = True
DrawMode = "4D"
ElseIf DrawMode = "3D" Then
IsCor2DModified = True
DrawMode = "2D"
ElseIf DrawMode = "4D" Then
IsCor3DModified = True
DrawMode = "3D"
End If
AutoRedraw()
End Sub
'رسم دوباره نمودار ها
Public Sub AutoRedraw()
If DrawMode = "2D" Then
Unload3D()
Draw2D()
ElseIf DrawMode = "3D" Then
Draw3D()
ElseIf DrawMode = "4D" Then
Draw4D()
End If
End Sub
Public Sub AutoRedrawCM()
If DrawMode = "2D" Then IsCor2DModified = True
If DrawMode = "3D" Then IsCor3DModified = True
AutoRedraw()
End Sub
'تازی سازی نمودار بعد از تغییر سایز
Public Sub SizeChangedRedraw() Handles Cor2D_Behind.SizeChanged
If DrawMode = "2D" Then
AutoRedrawCM()
End If
End Sub
'پاک سازی نمودار های از پیش رسم شده
Public Sub ClearGraphObjects()
For Each MO As MathObject In MathObjList
If (MO.Type <> 1) And (MO.Type <> 99) Then
For Each GO In MO.Inner.GraphObject
If Cor2D_Front.Children.Contains(TryCast(GO, UIElement)) Then
Cor2D_Front.Children.Remove(GO)
End If
If VisualModel.Children.Contains(TryCast(GO, Model3D)) Then
VisualModel.Children.Remove(GO)
End If
Next
MO.Inner.GraphObject.Clear()
End If
Next
End Sub
'بازگرداندن تنظیمات به حالت اولیه
Public Sub RestoreDefaults()
DrawMode = "2D"
CorDetail = 3
CorBackground = Brushes.White
Cor3DType = Cor3DTypes.Central
ActivePlaneMode = PlaneMode.Normal
Labels3DMode = Labels3D.Space
ColoringMethod = 0
ColorX = True
ColorY = True
ColorZ = True
ColorW = True
Len = 1
LenScale = 100
Len3DX = 1
Len3DY = 1
Len3DZ = 1
LenVPX = 5
LenVPY = 5
LenVPZ = 5
LenVPScale = 1
ExtraX = 0
ExtraY = 0
MovedCamera2D = New Vector(0, 0)
IsCor2DModified = True
IsCor3DModified = True
Acc = 50
Acc3D = 5
CameraLight = True
SemiZoom = 5
Zoom = 0
ZoomMode3D = ZoomModes3D.Viewport
End Sub
'جزئیات دستگاه مختصات
Private m_CorDetail As Integer = 3
' 0 --> No Cor 1 --> Just Main Division 2 --> Main And Sub Division 3 --> With Labels
Public Property CorDetail As Integer
Get
Return m_CorDetail
End Get
Set(value As Integer)
m_CorDetail = value
NotifyPropertyChanged("CorDetail")
DrawCoordinateSystem2D()
DrawCoordinateSystem3D()
DrawCoordinateSystem4D()
End Set
End Property
'پس زمینه دستگاه مختصات
Private m_CorBackground As Brush = Brushes.White
Public Property CorBackground As Brush
Get
If m_CorBackground IsNot Nothing Then
Return m_CorBackground
Else
Return Brushes.White
End If
End Get
Set(value As Brush)
m_CorBackground = value
NotifyPropertyChanged("CorBackground")
End Set
End Property
#End Region
#Region "سیستم رنگ بندی"
Private m_ColoringMethod As ColorMethod = ColorMethod.Brush
Public Property ColoringMethod As ColorMethod
Get
Return m_ColoringMethod
End Get
Set(value As ColorMethod)
m_ColoringMethod = value
NotifyPropertyChanged("ColoringMethod")
End Set
End Property
Public Enum ColorMethod
Brush
ByCor
End Enum
#Region "ColorX,Y,Z,W Bools"
Private m_ColorX As Boolean = True
Public Property ColorX As Boolean
Get
Return m_ColorX
End Get
Set(value As Boolean)
m_ColorX = value
NotifyPropertyChanged("ColorX")
End Set
End Property
Private m_ColorY As Boolean = True
Public Property ColorY As Boolean
Get
Return m_ColorY
End Get
Set(value As Boolean)
m_ColorY = value
NotifyPropertyChanged("ColorY")
End Set
End Property
Private m_ColorZ As Boolean = True
Public Property ColorZ As Boolean
Get
Return m_ColorZ
End Get
Set(value As Boolean)
m_ColorZ = value
NotifyPropertyChanged("ColorZ")
End Set
End Property
Private m_ColorW As Boolean = True
Public Property ColorW As Boolean
Get
Return m_ColorW
End Get
Set(value As Boolean)
m_ColorW = value
NotifyPropertyChanged("ColorW")
End Set
End Property
#End Region
#End Region
#Region "رسم 2 بعدی"
'تنظیمات رسم
Private m_acc As Integer = 50
Public Property Acc As Integer
Get
Return m_acc
End Get
Set(value As Integer)
m_acc = value
NotifyPropertyChanged("Acc")
End Set
End Property
'تنظیمات دستگاه مختصات
Public Property Len As Double = 1
Public Property LenScale As Double = 100
Public Property ExtraX As Single = 0
Public Property ExtraY As Single = 0
Public Property IsCor2DModified As Boolean = True
'تنظیمات جابجایی
Public Property CenterPoint As Point
Public Property MovedCamera2D As New Vector(0, 0)
Private Sub Draw2D()
If DrawMode = "2D" Then
If IsCor2DModified Then
ExtraX = (-MovedCamera2D.X \ LenScale)
ExtraY = (-MovedCamera2D.Y \ LenScale)
DrawCoordinateSystem2D()
DebugMsg("REDRAW --> Cor2D", 1)
End If
For Each MO As MathObject In MathObjList
MO.Inner.HasError = False
MO.Inner.ErrorMsg = ""
Select Case MO.Type
Case 0 'Equation
If MO.Inner.NeedsRecalculation Or HaveVarsInFormula(MO.Inner) Or IsCor2DModified Then
For Each GO In MO.Inner.GraphObject
Cor2D_Front.Children.Remove(GO)
Next
MO.Inner.GraphObject.Clear()
If MO.Inner.IsVisible Then DrawGraph2D(MO.Inner)
End If
Case 2 'Point
For Each GO In MO.Inner.GraphObject
Cor2D_Front.Children.Remove(GO)
Next
MO.Inner.GraphObject.Clear()
If MO.Inner.IsVisible Then DrawPoint2D(MO.Inner)
Case 3 'Vector
For Each GO In MO.Inner.GraphObject
Cor2D_Front.Children.Remove(GO)
Next
MO.Inner.GraphObject.Clear()
If MO.Inner.IsVisible Then DrawVector2D(MO.Inner)
Case 4 'Shape
MO.Inner.HasError = True
MO.Inner.ErrorMsg = "لطفا حالت رسم را به 3 بعدی تغییر دهید"
End Select
Next
IsCor2DModified = False
End If
End Sub
'رسم دستگاه مختصات 2 بعدی
Public Sub DrawCoordinateSystem2D()
Cor2D_Behind.Children.Clear()
Dim AW As Double = Cor2D_Behind.ActualWidth
Dim AH As Double = Cor2D_Behind.ActualHeight
Dim DivY As Integer = (AH \ (LenScale * 2)) + 1
Dim DivX As Integer = (AW \ (LenScale * 2)) + 1
CenterPoint = New Point(AW / 2, AH / 2) + MovedCamera2D
'Pens
Dim PenMain As New Pen(New SolidColorBrush(Colors.Black), 2)
Dim PenS1 As New Pen(Brushes.DarkGray, 0.5)
Dim PenS2 As New Pen(Brushes.Gray, 0.3)
PenS2.DashStyle = New DashStyle({7, 3}, 5)
If CorDetail >= 1 Then
'محور های اصلی
DrawLine2D(PenMain, CenterPoint.X, 0, CenterPoint.X, AH)
DrawLine2D(PenMain, 0, CenterPoint.Y, AW, CenterPoint.Y)
End If
If CorDetail >= 2 Then
If CorDetail = 3 Then DrawText2D(CenterPoint.X - 10, CenterPoint.Y, "0", Colors.Black)
For i = -DivX + ExtraX To DivX + ExtraX
Dim Value As Double
Value = 0 + Len * i
If Value <> 0 Then
DrawLine2D(PenS1, (CenterPoint.X + LenScale * i), 0, (CenterPoint.X + LenScale * i), AH)
If CorDetail = 3 Then
Dim TS As Size = MeasureTextSize(Value.ToString)
DrawText2D(CenterPoint.X + LenScale * i - (TS.Width / 2), CenterPoint.Y, Value.ToString, Colors.Black)
End If
End If
Next
For i = -DivY + ExtraY To DivY + ExtraY
Dim Value As Double
Value = 0 - (Len * i)
If Value <> 0 Then
DrawLine2D(PenS1, 0, (CenterPoint.Y + LenScale * i), AW, (CenterPoint.Y + LenScale * i))
If CorDetail = 3 Then
Dim TS As Size = MeasureTextSize(Value.ToString)
DrawText2D(CenterPoint.X - (TS.Width + 4), CenterPoint.Y + LenScale * i - (TS.Width / 2), Value.ToString, Colors.Black)
End If
End If
Next
End If
End Sub
'رسم نمودار
Private Sub DrawGraph2D(Equation As Equation)
Try
EquationTools.DetectEqType(Equation, Me.DrawMode)
For Each PL As PointCollection In GetGraphPoints2D(Equation)
If Equation.Type <> EquationType.CartesianN Then
Dim Pol As New Polyline
Pol.Stroke = Equation.Brush
Pol.StrokeThickness = Equation.Thickness
Pol.Points = PL
Equation.GraphObject.Add(Pol)
Cor2D_Front.Children.Add(Pol)
Equation.NeedsRecalculation = False
Else
For Each Pt As Point In PL
Dim El As New Ellipse
Dim ElW As Integer = 4
El.Fill = Equation.Brush
El.Width = 2 * ElW
El.Height = 2 * ElW
Canvas.SetLeft(El, Pt.X - ElW)
Canvas.SetTop(El, Pt.Y - ElW)
Equation.GraphObject.Add(El)
Cor2D_Front.Children.Add(El)
Equation.NeedsRecalculation = False
Next
End If
Next
Catch Ex As Exception
Equation.HasError = True
Equation.ErrorMsg = "خطا در رسم" & vbCrLf & Ex.Message
End Try
End Sub
Private Function GetGraphPoints2D(ByRef Equation As Equation) As List(Of PointCollection)
'Cartesian
'Type = 1 y=f(x)
'Type = 2 x=f(y)
'Polar
'Type = 4 r=f(t)
'Type = 5 t=f(r)
'Parametric 2D
'Type = 7 x=f(u) & y=f(u)
Dim ValidTypes() As EquationType = {EquationType.CartesianY, EquationType.CartesianX, EquationType.CartesianN, EquationType.PolarR, EquationType.PolarT, EquationType.ParametricU}
If Not ValidTypes.Contains(Equation.Type) Then
Return New List(Of PointCollection)
End If
Dim AW As Double = Cor2D_Front.ActualWidth
Dim AH As Double = Cor2D_Front.ActualHeight
Dim DivY As Integer = (AH \ (LenScale * 2)) + 1
Dim DivX As Integer = (AW \ (LenScale * 2)) + 1
Dim LenY As Double = (AH / LenScale) * Len / 2
Dim LenX As Double = (AW / LenScale) * Len / 2
Dim SimplifySuccess As Boolean
Dim EqMain As String = SimplifyFunction(Equation, SimplifySuccess)
If SimplifySuccess = False Then Return New List(Of PointCollection)
Dim PointsList As New List(Of PointCollection)
Dim AccValue As Double = (1 / Acc) * (Len / 2)
Dim EqDC As Boolean = EqMain.Contains("int") _
Or EqMain.Contains("floor") _
Or EqMain.Contains("ceil") _
Or EqMain.Contains("tan")
Dim AllowedSpace As Double = 5
Dim LastPointWasOutside As Boolean = False
'جایگذاری متغییر ها
For Each MOV As MathObject In MathObjList
If MOV.Type = 1 Then
Dim Var As Variable = MOV.Inner
EqMain = EquationTools.Replace(EqMain, Var.Name, Var.Value)
End If
Next
DebugMsg("EqM --> " & EqMain, 3)
Select Case Equation.Type
Case EquationType.CartesianY
Dim PC As New PointCollection
' Draw Based On x - تابع دکارتی
Dim DS As Single
Dim DE As Single
If Equation.DomainStart = Double.NegativeInfinity Then
DS = -LenX + (-MovedCamera2D.X / LenScale) * Len
Else
DS = Equation.DomainStart
End If
If Equation.DomainEnd = Double.PositiveInfinity Then
DE = +LenX + (-MovedCamera2D.X / LenScale) * Len
Else
DE = Equation.DomainEnd
End If
Dim EqStr As String
Dim EvalResult As Double
Dim x, y As Single
EqStr = EquationTools.Replace(EqMain, "x", DS)
EvalResult = AKPEval.Eval(EqStr, New Boolean, True)
EvalResult = Val(EvalResult)
'اولین برد
Equation.RangeMax = -EvalResult
Equation.RangeMin = -EvalResult
Dim OldPoint As New Point(CenterPoint.X + DS * (AW / (LenX * 2)), CenterPoint.Y - EvalResult * (AH / (LenY * 2)))
For i = DS To DE Step AccValue
EqStr = EquationTools.Replace(EqMain, "x", MathTools.Round(i))
Dim EvalSuccess As Boolean
EvalResult = AKPEval.Eval(EqStr, EvalSuccess)
If EvalSuccess Then
EvalResult = Val(EvalResult)
x = CenterPoint.X + i * (AW / (LenX * 2))
y = CenterPoint.Y - EvalResult * (AH / (LenY * 2))
If EvalResult > Equation.RangeMax Then Equation.RangeMax = EvalResult
If EvalResult < Equation.RangeMin Then Equation.RangeMin = EvalResult
'صرف نظر از رسم نقاطی که بیرون صفحه قرار دارند
Dim IsInTheScreen As Boolean = InTheScreen(x, y)
'بررسی پیوستگی نمودار
Dim IsConnected As Boolean = True
Dim NewPoint As New Point(x, y)
If EqDC And PointsSpace(OldPoint, NewPoint) > AllowedSpace Then IsConnected = False
OldPoint = NewPoint
If IsInTheScreen Then
If LastPointWasOutside Then
IsConnected = False
LastPointWasOutside = False
End If
If IsConnected Then
PC.Add(NewPoint)
Else
PointsList.Add(PC)
PC = New PointCollection
End If
Else
LastPointWasOutside = True
End If
End If
Next
PointsList.Add(PC)
Case EquationType.CartesianX
Dim PC As New PointCollection
' Draw Based On y
Dim DS As Single
Dim DE As Single
If Equation.DomainStart = Double.NegativeInfinity Then
DS = -LenY + +(MovedCamera2D.Y / LenScale) * Len
Else
DS = Val(Equation.DomainStart)
End If
If Equation.DomainEnd = Double.PositiveInfinity Then
DE = +LenY + +(MovedCamera2D.Y / LenScale) * Len
Else
DE = Val(Equation.DomainEnd)
End If
Dim EqStr As String
Dim EvalResult As Double
Dim x, y As Single
EqStr = EquationTools.Replace(EqMain, "y", DS)
EvalResult = AKPEval.Eval(EqStr, New Boolean, True)
EvalResult = Val(EvalResult)
'اولین برد
Equation.RangeMax = EvalResult
Equation.RangeMin = EvalResult
Dim OldPoint As New Point(CenterPoint.X + DS * (AW / (LenX * 2)), CenterPoint.Y - EvalResult * (AH / (LenY * 2)))
For i = DS To DE Step AccValue
EqStr = EquationTools.Replace(EqMain, "y", MathTools.Round(i))
Dim EvalSuccess As Boolean
EvalResult = AKPEval.Eval(EqStr, EvalSuccess)
If EvalSuccess Then
EvalResult = Val(EvalResult)
y = CenterPoint.Y - i * (Cor2D_Front.Height / (LenY * 2))
x = CenterPoint.X + EvalResult * (Cor2D_Front.Width / (LenX * 2))
If EvalResult > Equation.RangeMax Then Equation.RangeMax = EvalResult
If EvalResult < Equation.RangeMin Then Equation.RangeMin = EvalResult
Dim IsInTheScreen As Boolean = InTheScreen(x, y)
Dim IsConnected As Boolean = True
Dim NewPoint As New Point(x, y)
If EqDC And PointsSpace(OldPoint, NewPoint) > AllowedSpace Then IsConnected = False
OldPoint = NewPoint
If IsInTheScreen Then
If LastPointWasOutside Then
IsConnected = False
LastPointWasOutside = False
End If
If IsConnected Then
PC.Add(NewPoint)
Else
PointsList.Add(PC)
PC = New PointCollection
End If
Else
LastPointWasOutside = True
End If
End If