not really known
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

391 lines
12 KiB

  1. function OneColumnAbacus(stage,rods,number,base,colours,startvalue,schety,fractions,caacupe,rodf){
  2. if (startvalue === undefined) startvalue=rods;
  3. if (schety === undefined) schety=false;
  4. if (fractions === undefined) fractions=false;
  5. if (caacupe === undefined) caacupe=false;
  6. if (rodf === undefined) rodf=false;
  7. //top bar: 8/5*blockh
  8. //middle bar: blockh
  9. //bottom bar: 8/5*blockh
  10. //margin between blocks: blockw/3
  11. //margin between blocks on line: blockh/20
  12. //block 25,33
  13. //top margin: screen height/10
  14. //blockheight given screen: 9*screen height/10
  15. this.blockScaleYFromX = 25/33;
  16. this.topBottomBarScale = 8/5;
  17. this.leftRightBarScale = 8/5*this.blockScaleYFromX;
  18. this.middleBarScale = 1;
  19. this.horizontalMargin = 1/3;
  20. this.verticalMargin = 1/20;
  21. this.extraBeads = 2;
  22. this.rods = rods;
  23. this.blockHeight = 0;
  24. this.blockWidth = 0;
  25. this.abacusHeight = 0;
  26. this.abacusWidth = 0;
  27. this.upperBlockHeight = 0;
  28. this.lowerBlockHeight = 0;
  29. this.colHeight = 0;
  30. this.stage = stage;
  31. this.columns = [];
  32. this.rodtext = [];
  33. this.answertext = null;
  34. this.trix = null;
  35. this.startx = null;
  36. this.schetyColumns = [1000000000,100000000,10000000,1000000,100000,10000,1000,100,10,1,0.25,0.1,0.01,0.001,0.0001];
  37. this.fractionColumns = [100000,10000,1000,100,10,1,[1,2],[1,3],[1,4],[1,5],[1,6],[1,8],[1,9],[1,10],[1,12]];
  38. this.rodsColumns = [[1,1],[1,2],[1,3],[1,4],[1,5],[1,6],[1,7],[1,8],[1,9],[1,10]];
  39. this.rodsColours = ["#FFFFFF","#FC0D1B","#88FD31","#FC28FC","#FFFD38","#1FCC23","#000000","#AA6717","#23CCFC","#FD8824"];
  40. this.blockGivenWidth = function(width){
  41. var blockwidth = width/((2*this.leftRightBarScale)+rods+this.horizontalMargin*rods);
  42. return blockwidth;
  43. }
  44. this.blockGivenHeight = function(height){
  45. var blockheight = height/((2*this.topBottomBarScale)+(number+this.extraBeads)+this.verticalMargin*(number+this.extraBeads));
  46. return blockheight;
  47. }
  48. this.heightGivenBlockWidth = function(blockwidth){
  49. var blockheight = this.blockScaleYFromX*blockwidth;
  50. var h = blockheight*((2*this.topBottomBarScale)+(number+this.extraBeads)+this.verticalMargin*(number+this.extraBeads));
  51. return h;
  52. }
  53. this.widthGivenBlockWidth = function(blockwidth){
  54. var w = blockwidth*((2*this.leftRightBarScale)+rods+this.horizontalMargin*rods);
  55. return w;
  56. }
  57. this.initValues = function(){
  58. var height = (9/10-1/20)*stage.canvas.height;
  59. var width = 9/10*stage.canvas.width;
  60. var w = this.blockGivenWidth(width);
  61. var h = this.blockScaleYFromX*w;
  62. if (this.heightGivenBlockWidth(w)>height){
  63. h = this.blockGivenHeight(height);
  64. w = h/this.blockScaleYFromX;
  65. console.log("Using height");
  66. }
  67. this.blockHeight = h;
  68. this.blockWidth = w;
  69. this.abacusHeight = this.heightGivenBlockWidth(this.blockWidth);
  70. this.abacusWidth = this.widthGivenBlockWidth(this.blockWidth);
  71. this.colHeight = this.blockHeight*((number+this.extraBeads)+this.verticalMargin*(number+this.extraBeads));
  72. console.log(this.blockHeight);
  73. console.log(this.blockWidth);
  74. console.log(this.abacusHeight);
  75. console.log(this.abacusWidth);
  76. this.startx = ((stage.canvas.width-this.abacusWidth)/2)+(this.leftRightBarScale*this.blockWidth)+((this.horizontalMargin*this.blockWidth)/2)+(this.blockWidth/2);
  77. }
  78. this.initRectangle = function(){
  79. var rect = new createjs.Shape();
  80. rect.graphics.beginStroke("#000000");
  81. rect.graphics.setStrokeStyle(this.leftRightBarScale*this.blockWidth);
  82. rect.graphics.beginFill("#C0C0C0").drawRoundRect((this.leftRightBarScale*this.blockWidth)/2,(this.leftRightBarScale*this.blockWidth)/2,this.abacusWidth-(this.leftRightBarScale*this.blockWidth),this.abacusHeight-(this.leftRightBarScale*this.blockWidth),2/3*this.blockWidth);
  83. rect.x = (stage.canvas.width-this.abacusWidth)/2;
  84. rect.y = stage.canvas.height/10;
  85. stage.addChild(rect);
  86. }
  87. this.initTriangle = function(){
  88. this.tri = new createjs.Shape();
  89. var width = this.blockWidth/4*3;
  90. var y = stage.canvas.height/10+2*(this.leftRightBarScale*this.blockWidth)/3;
  91. var startx = ((stage.canvas.width-this.abacusWidth)/2)+(this.leftRightBarScale*this.blockWidth)+((this.horizontalMargin*this.blockWidth)/2)+(this.blockWidth/2);
  92. var incr = ((this.horizontalMargin*this.blockWidth))+this.blockWidth;
  93. var x = startx+(incr*(rods-1));
  94. this.tri.graphics.moveTo(0-width/2,0).beginFill("#FC0D1B").lineTo(0+width/2,0).lineTo(0,0+(this.leftRightBarScale*this.blockWidth)/3).lineTo(0-width/2,0).closePath();
  95. this.tri.x = x-(this.blockWidth/2);
  96. this.tri.y = y;
  97. this.trix = this.tri.x-this.startx;
  98. stage.addChild(this.tri);
  99. var c = this.tri;
  100. var t = this;
  101. this.tri.on("mousedown", function (evt) {
  102. this.offset = {x: c.x - evt.stageX, y: c.y - evt.stageY};
  103. });
  104. this.tri.on("pressmove", function (evt) {
  105. if (evt.stageX + this.offset.x < startx-(t.blockWidth/2)){
  106. c.x = startx-(t.blockWidth/2);
  107. } else if (evt.stageX + this.offset.x > x+(t.blockWidth/2)){
  108. c.x = x+(t.blockWidth/2);
  109. } else {
  110. c.x = evt.stageX + this.offset.x;
  111. }
  112. t.trix = c.x-t.startx;
  113. });
  114. }
  115. this.initColumns = function(){
  116. var startx = ((stage.canvas.width-this.abacusWidth)/2)+(this.leftRightBarScale*this.blockWidth)+((this.horizontalMargin*this.blockWidth)/2)+(this.blockWidth/2);
  117. var incr = ((this.horizontalMargin*this.blockWidth))+this.blockWidth;
  118. //console.log("======");
  119. //console.log(startx);
  120. //console.log(incr);
  121. var starty = stage.canvas.height/10+(this.leftRightBarScale*this.blockWidth);
  122. var endy = starty+this.colHeight;
  123. var grey = ["#A0A0A0","#6B6B6B"];
  124. var black = ["#000000","#000000"];
  125. var colinuse;
  126. console.log(colours);
  127. var fill = colours.fill;
  128. var stroke = colours.stroke;
  129. var xocolinuse;
  130. var val;
  131. if (schety==false&&fractions==false&&caacupe==false&&rodf==false){
  132. for (var item = 0; item<rods; item++){
  133. if (item%2==0){
  134. colinuse = black;
  135. xocolinuse = fill;
  136. } else {
  137. colinuse = grey;
  138. xocolinuse = stroke;
  139. }
  140. if (startvalue-item-1<0){
  141. var div = new window.Fraction(1);
  142. val = new window.Fraction(Math.pow(base,Math.abs(startvalue-item-1)));
  143. val = div.div(val);
  144. } else {
  145. val = new window.Fraction(Math.pow(base,startvalue-item-1));
  146. }
  147. var c = new StandardAbacusColumn(startx,starty,endy,0,number,number+this.extraBeads,xocolinuse,colinuse,this,val,false);
  148. c.init();
  149. this.columns.push(c);
  150. startx+=incr;
  151. }
  152. } else if (schety==true){
  153. var heightcol = number+this.extraBeads;
  154. var theight;
  155. for (var item = 0; item<rods; item++){
  156. if (item%2==0){
  157. colinuse = black;
  158. xocolinuse = fill;
  159. } else {
  160. colinuse = grey;
  161. xocolinuse = stroke;
  162. }
  163. val = new window.Fraction(this.schetyColumns[item]);
  164. console.log(val);
  165. if(item==10){
  166. theight=4;
  167. } else {
  168. theight=number;
  169. }
  170. var c = new StandardAbacusColumn(startx,starty,endy,0,theight,heightcol,xocolinuse,colinuse,this,val,false,false,true);
  171. c.init();
  172. this.columns.push(c);
  173. startx+=incr;
  174. }
  175. } else if (caacupe==true||fractions==true){
  176. var heightcol = number+this.extraBeads;
  177. var theight;
  178. for (var item = 0; item<rods; item++){
  179. if (item%2==0){
  180. colinuse = black;
  181. xocolinuse = fill;
  182. } else {
  183. colinuse = grey;
  184. xocolinuse = stroke;
  185. }
  186. if (item>5){
  187. xocolinuse="#000000";
  188. }
  189. val = new window.Fraction(this.fractionColumns[item]);
  190. console.log(val);
  191. if(item>5){
  192. theight=this.fractionColumns[item][1];
  193. } else {
  194. theight=10;
  195. }
  196. if (caacupe==true){
  197. var c = new PosNegColumn(startx,starty,endy,theight,heightcol,xocolinuse,colinuse,this,val,false,false);
  198. } else {
  199. var c = new StandardAbacusColumn(startx,starty,endy,0,theight,heightcol,xocolinuse,colinuse,this,val,false,false);
  200. }
  201. c.init();
  202. this.columns.push(c);
  203. startx+=incr;
  204. }
  205. } else {
  206. console.log("rods");
  207. var heightcol = number+this.extraBeads;
  208. var theight;
  209. for (var item = 0; item<rods; item++){
  210. if (item%2==0){
  211. colinuse = black;
  212. } else {
  213. colinuse = grey;
  214. }
  215. xocolinuse = this.rodsColours[item];
  216. val = new window.Fraction(this.rodsColumns[item]);
  217. console.log(val);
  218. theight = this.rodsColumns[item][1];
  219. var c = new PosNegColumn(startx,starty,endy,theight,heightcol,xocolinuse,colinuse,this,val,false,true);
  220. c.init();
  221. this.columns.push(c);
  222. console.log(c);
  223. startx+=incr;
  224. }
  225. }
  226. }
  227. this.restore = function(arr){
  228. for (var i = 0; i<arr.length; i++){
  229. this.columns[i].restore(arr[i]);
  230. }
  231. for (var i = 0; i<arr.length; i++){
  232. this.columns[i].restoreAge(arr[i]);
  233. }
  234. }
  235. this.save = function(){
  236. var arr = [];
  237. for (var i = 0; i<this.columns.length; i++){
  238. arr.push(this.columns[i].save());
  239. }
  240. return arr;
  241. }
  242. this.saveTri = function(){
  243. console.log(this.trix);
  244. return this.trix/this.abacusWidth;
  245. }
  246. this.restoreTri = function(x){
  247. console.log(x);
  248. this.tri.x = this.startx+(x*this.abacusWidth);
  249. this.trix = this.tri.x-this.startx;
  250. console.log(this.tri.x);
  251. }
  252. this.initTextItems = function(){
  253. var startx = ((stage.canvas.width-this.abacusWidth)/2)+(this.leftRightBarScale*this.blockWidth)+((this.horizontalMargin*this.blockWidth)/2)+(this.blockWidth/2);
  254. var incr = ((this.horizontalMargin*this.blockWidth))+this.blockWidth;
  255. for (var i = 0; i<rods; i++){
  256. var text = new createjs.Text("",(stage.canvas.width/70).toString()+"px Arial", "#FFF");
  257. text.set({
  258. textAlign: 'center'
  259. });
  260. text.x = startx;
  261. text.y = stage.canvas.height/10+this.abacusHeight-(this.leftRightBarScale*this.blockWidth)/2;
  262. stage.addChild(text);
  263. this.rodtext.push(text);
  264. startx += incr;
  265. }
  266. var text = new createjs.Text("",(this.blockWidth).toString()+"px Arial", "#000");
  267. text.set({
  268. textAlign: 'center'
  269. });
  270. text.x = stage.canvas.width/2;
  271. text.y = stage.canvas.height/40;
  272. stage.addChild(text);
  273. this.answertext = text;
  274. }
  275. this.updatePosNegTextItems = function(){
  276. if (this.columns.length==rods){
  277. var sumarr = [];
  278. var total = new window.Fraction(0,1);
  279. for (var i = 0; i<rods; i++){
  280. this.columns[i].updateAges();
  281. var huse = this.columns[i].howManyInUse();
  282. var usep = huse[0];
  283. var usen = huse[1];
  284. var sum = usep.length-usen.length;
  285. if (sum!=0){
  286. this.rodtext[i].text=sum.toString();
  287. } else {
  288. this.rodtext[i].text="";
  289. }
  290. var tempsum = this.columns[i].value.mul(usep.length);
  291. var ntempsum = this.columns[i].value.mul(usen.length);
  292. var tsum = tempsum.sub(ntempsum);
  293. var zero = new window.Fraction(0);
  294. if (!tsum.equals(zero)){
  295. sumarr.push(tsum.toFraction(true));
  296. total=total.add(tsum);
  297. }
  298. }
  299. var zero = new window.Fraction(0);
  300. if (sumarr.length==1){
  301. var str = total.toFraction(true);
  302. this.answertext.text = str;
  303. } else if (!total.equals(zero)){
  304. var str = "";
  305. for (var i = 0; i<sumarr.length-1; i++){
  306. str += sumarr[i]+" + ";
  307. }
  308. str += sumarr[sumarr.length-1]+" = "+total.toFraction(true);
  309. this.answertext.text = str;
  310. } else {
  311. this.answertext.text = "";
  312. }
  313. if((this.answertext.text).length >= 40) {
  314. this.answertext.font = parseInt(((this.blockWidth / (this.answertext.text).length)*40)) + "px Arial";
  315. } else {
  316. this.answertext.font = (this.blockWidth).toString()+"px Arial";
  317. }
  318. }
  319. }
  320. this.updateTextItems = function(){
  321. if (caacupe==true||rodf==true){
  322. this.updatePosNegTextItems();
  323. } else if (this.columns.length==rods){
  324. var sumarr = [];
  325. var total = new window.Fraction(0,1);
  326. for (var i = 0; i<rods; i++){
  327. this.columns[i].updateAges();
  328. var use = this.columns[i].howManyInUse();
  329. var sum = use.length;
  330. if (sum!=0){
  331. this.rodtext[i].text=sum.toString();
  332. } else {
  333. this.rodtext[i].text="";
  334. }
  335. var tempsum = this.columns[i].value.mul(use.length);
  336. var zero = new window.Fraction(0);
  337. if (!tempsum.equals(zero)){
  338. sumarr.push(tempsum.toFraction(true));
  339. total=total.add(tempsum);
  340. }
  341. }
  342. var zero = new window.Fraction(0);
  343. if (sumarr.length==1){
  344. var str = total.toFraction(true);
  345. this.answertext.text = str;
  346. } else if (!total.equals(zero)){
  347. var str = "";
  348. for (var i = 0; i<sumarr.length-1; i++){
  349. str += sumarr[i]+" + ";
  350. }
  351. str += sumarr[sumarr.length-1]+" = "+total.toFraction(true);
  352. this.answertext.text = str;
  353. } else {
  354. this.answertext.text = "";
  355. }
  356. if((this.answertext.text).length >= 40) {
  357. this.answertext.font = parseInt(((this.blockWidth / (this.answertext.text).length)*40)) + "px Arial";
  358. } else {
  359. this.answertext.font = (this.blockWidth).toString()+"px Arial";
  360. }
  361. }
  362. }
  363. this.init = function(){
  364. this.initValues();
  365. this.initRectangle();
  366. this.initColumns();
  367. this.initTextItems();
  368. this.initTriangle();
  369. }
  370. }