-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBCD_Arithmetic.py
180 lines (159 loc) · 4.89 KB
/
BCD_Arithmetic.py
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
from manim import *
from pyparsing import col
class Add(Scene):
def construct(self):
text = Text("BCD Arithmetic")
text.set_color(BLUE)
self.play(Write(text))
self.wait()
self.play(Unwrite(text))
self.wait()
###############################
text = (
MathTex("(", "1", "8", "4", ")_{10}", "+", "(", "5", "7", "6", ")_{10}")
.arrange(buff=0.2)
.shift(LEFT * 4)
.shift(UP * 3)
)
# Upper Numbera
self.play(Write(text))
self.wait()
textbcd1 = (
MathTex("0001", "1000", " 0100")
.arrange(buff=0.23)
.shift(RIGHT * 2)
.shift(UP * 3)
)
self.play(Write(textbcd1))
fr1 = SurroundingRectangle(text[1], buff=0.1)
fr8 = SurroundingRectangle(text[2], buff=0.1)
fr4 = SurroundingRectangle(text[3], buff=0.1)
fr21 = SurroundingRectangle(textbcd1[0], buff=0.1)
fr28 = SurroundingRectangle(textbcd1[1], buff=0.1)
fr24 = SurroundingRectangle(textbcd1[2], buff=0.1)
self.play(Create(fr1), Create(fr8), Create(fr4))
self.wait()
self.play(
ReplacementTransform(fr1, fr21),
ReplacementTransform(fr8, fr28),
ReplacementTransform(fr4, fr24),
)
self.wait()
##################################
# Lower Numbers
textbcd2 = (
MathTex("+0101", "0111", " 0110")
.arrange(buff=0.23)
.shift(RIGHT * 1.8)
.shift(UP * 2)
)
self.play(Write(textbcd2))
self.wait()
fr5 = SurroundingRectangle(text[7], buff=0.1)
fr7 = SurroundingRectangle(text[8], buff=0.1)
fr6 = SurroundingRectangle(text[9], buff=0.1)
fr25 = SurroundingRectangle(textbcd2[0], buff=0.1)
fr27 = SurroundingRectangle(textbcd2[1], buff=0.1)
fr26 = SurroundingRectangle(textbcd2[2], buff=0.1)
self.play(Create(fr5), Create(fr7), Create(fr6))
self.wait()
self.play(
ReplacementTransform(fr5, fr25),
ReplacementTransform(fr7, fr27),
ReplacementTransform(fr6, fr26),
)
self.wait()
self.play(
Uncreate(fr21),
Uncreate(fr28),
Uncreate(fr24),
Uncreate(fr25),
Uncreate(fr27),
Uncreate(fr26),
)
d1 = Dot([0, 2, 0])
d2 = Dot([4, 2, 0])
self.play(
Create(
Line(d1, d2)
# .stretch(4, 1)
.shift(DOWN * 0.5)
)
)
###############################
# calculate the addition
textbcd3 = (
MathTex("0111", "10000", "1010")
.arrange(buff=0.2)
.shift(RIGHT * 2)
.shift(UP * 1)
)
self.play(Write(textbcd3))
self.wait()
result1 = SurroundingRectangle(textbcd3, buff=0.1, color=RED)
self.play(Create(result1))
txt1 = Text("1010 and 10000 are >= then 1010").shift(DOWN * 2)
txt2 = Text("Thus we have to add 0110 to them").shift(DOWN * 2.8)
self.play(Write(txt1))
self.play(Write(txt2))
self.play(Uncreate(result1))
###############################
textbcd4 = (
MathTex("+0000", "0110", "0110")
.arrange(buff=0.22)
.shift(RIGHT * 1.8)
.shift(DOWN * 0.2)
)
self.play(Write(textbcd4))
self.wait()
result1 = SurroundingRectangle(textbcd4, buff=0.1, color=BLUE)
self.play(Create(result1))
self.play(Unwrite(txt1), Unwrite(txt2), Uncreate(result1))
d1 = Dot([0, 0, 0])
d2 = Dot([4, 0, 0])
self.play(
Create(
Line(d1, d2)
# .stretch(4, 1)
.shift(DOWN * 0.5)
)
)
##############################
# write result
textbcd5 = (
MathTex("0111", "0110", "0000")
.arrange(buff=0.2)
.shift(RIGHT * 2)
.shift(DOWN * 1.3)
)
self.play(Write(textbcd5))
self.wait()
result1 = SurroundingRectangle(textbcd5, buff=0.1, color=GREEN)
self.play(Create(result1))
self.wait()
##############################
# write final result
text = (
MathTex(
"(",
"1",
"8",
"4",
")_{10}",
"+",
"(",
"5",
"7",
"6",
")_{10}",
"=",
"760",
)
.arrange(buff=0.1)
.shift(LEFT * 1.2)
.shift(DOWN * 2.3)
)
# Upper Numbera
self.play(Write(text))
self.wait()
self.wait()