-
Notifications
You must be signed in to change notification settings - Fork 0
/
MathematicalConstants.m
130 lines (68 loc) · 4.04 KB
/
MathematicalConstants.m
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
%{
MathematicalConstants class:
MathematicalConstants initializes buttons for mathematical constants such as π, e, √2, φ, and i.
It appends the value of these constants to the input expression when the respective
button is clicked, facilitating easy inclusion of these constants in calculations.
Hardcoding used for:
- The text and name for each mathematical constant button
- Assigning the callback function of each mathematical constant button
- The positioning of each mathematical constant button
%}
classdef MathematicalConstants
properties
ParentContainer % Parent container for the constants buttons
InputExpression % Reference to the input expression edit field
DropdownButton % Dropdown button to access mathematical constants
ConstantsPanel % Panel to hold the mathematical constants buttons
end
methods
function obj = MathematicalConstants(parent, inputExpr)
%{
Constructor for MathematicalConstants class. Initializes constants buttons.
%}
obj.ParentContainer = parent;
obj.InputExpression = inputExpr;
% Create a panel to hold the mathematical constants buttons
obj.ConstantsPanel = uipanel(parent, 'Position', [60, 235, 75, 75], 'Visible', 'off', 'BackgroundColor', [0.75 0.75 0.75]);
% Create a dropdown button with styling
obj.DropdownButton = uibutton(parent, 'Text', '▼ π', ...
'Position', [10, 235, 50, 30], 'ButtonPushedFcn', @(btn,event) obj.toggleConstantsPanel(), ...
'BackgroundColor', [0.8 0.8 0.8]);
% Create buttons for mathematical constants and add them to the panel
obj.createButtons();
end
function createButtons(obj)
%{
Creates buttons for π and e, with their respective values.
%}
% Define mathematical constants and their positions
constants = {'π', 'e', '√2', 'φ', 'i'};
values = {'pi', 'exp(1)', 'sqrt(2)', '(1 + sqrt(5))/2', '1i'};
positions = [5, 40, 30, 30; 40, 40, 30, 30; 5, 5, 30, 30; 40, 5, 30, 30; 5, -30, 30, 30; 40, -30, 30, 30; 5, -65, 30, 30; 40, -65, 30, 30];
% Iterate through constants to create buttons
for i = 1:length(constants)
const = constants{i};
val = values{i};
pos = positions(i, :);
% Button creation with callback to append constant
uibutton(obj.ConstantsPanel, 'Text', const, 'Position', pos, ...
'ButtonPushedFcn', @(btn,event) obj.appendToExpression(val));
end
end
function toggleConstantsPanel(obj)
% Toggle the visibility of the constants panel
if strcmp(obj.ConstantsPanel.Visible, 'off')
obj.ConstantsPanel.Visible = 'on';
else
obj.ConstantsPanel.Visible = 'off';
end
end
function appendToExpression(obj, val)
%{
Appends the value of the selected constant to the input expression.
%}
currentExpr = obj.InputExpression.Value;
obj.InputExpression.Value = [currentExpr, val];
end
end
end