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.

317 lines
11 KiB

  1. function StandardAbacus(stage,rods,topnumber,factor,bottomnumber,base,colours,startvalue){
  2. if (startvalue === undefined) startvalue=rods;
  3. //top bar: 8/5*blockh
  4. //middle bar: blockh
  5. //bottom bar: 8/5*blockh
  6. //margin between blocks: blockw/3
  7. //margin between blocks on line: blockh/20
  8. //block 25,33
  9. //top margin: screen height/10
  10. //blockheight given screen: 9*screen height/10
  11. this.blockScaleYFromX = 25/33;
  12. this.topBottomBarScale = 8/5;
  13. this.leftRightBarScale = 8/5*this.blockScaleYFromX;
  14. this.middleBarScale = 1;
  15. this.horizontalMargin = 1/3;
  16. this.verticalMargin = 1/20;
  17. this.extraBeads = 2;
  18. this.rods = rods;
  19. this.blockHeight = 0;
  20. this.blockWidth = 0;
  21. this.abacusHeight = 0;
  22. this.abacusWidth = 0;
  23. this.upperBlockHeight = 0;
  24. this.lowerBlockHeight = 0;
  25. this.stage = stage;
  26. this.topcolumns = [];
  27. this.bottomcolumns = [];
  28. this.rodtext = [];
  29. this.answertext = null;
  30. this.trix = null;
  31. this.startx = null;
  32. this.blockGivenWidth = function(width){
  33. var blockwidth = width/((2*this.leftRightBarScale)+rods+this.horizontalMargin*rods);
  34. return blockwidth;
  35. }
  36. this.blockGivenHeight = function(height){
  37. var blockheight = height/((2*this.topBottomBarScale)+(topnumber+this.extraBeads)+this.verticalMargin*(topnumber+this.extraBeads)+this.middleBarScale+(bottomnumber+this.extraBeads)+this.verticalMargin*(bottomnumber+this.extraBeads));
  38. return blockheight;
  39. }
  40. this.heightGivenBlockWidth = function(blockwidth){
  41. var blockheight = this.blockScaleYFromX*blockwidth;
  42. var h = blockheight*((2*this.topBottomBarScale)+(topnumber+this.extraBeads)+this.verticalMargin*(topnumber+this.extraBeads)+this.middleBarScale+(bottomnumber+this.extraBeads)+this.verticalMargin*(bottomnumber+this.extraBeads));
  43. return h;
  44. }
  45. this.widthGivenBlockWidth = function(blockwidth){
  46. var w = blockwidth*((2*this.leftRightBarScale)+rods+this.horizontalMargin*rods);
  47. return w;
  48. }
  49. this.initValues = function(){
  50. var height = (9/10-1/20)*stage.canvas.height;
  51. var width = 9/10*stage.canvas.width;
  52. var w = this.blockGivenWidth(width);
  53. var h = this.blockScaleYFromX*w;
  54. if (this.heightGivenBlockWidth(w)>height){
  55. h = this.blockGivenHeight(height);
  56. w = h/this.blockScaleYFromX;
  57. console.log("Using height");
  58. }
  59. this.blockHeight = h;
  60. this.blockWidth = w;
  61. this.abacusHeight = this.heightGivenBlockWidth(this.blockWidth);
  62. this.abacusWidth = this.widthGivenBlockWidth(this.blockWidth);
  63. this.upperBlockHeight = this.blockHeight*((topnumber+this.extraBeads)+this.verticalMargin*(topnumber+this.extraBeads));
  64. this.lowerBlockHeight = this.blockHeight*((bottomnumber+this.extraBeads)+this.verticalMargin*(bottomnumber+this.extraBeads));
  65. console.log(this.blockHeight);
  66. console.log(this.blockWidth);
  67. console.log(this.abacusHeight);
  68. console.log(this.abacusWidth);
  69. this.startx = ((stage.canvas.width-this.abacusWidth)/2)+(this.leftRightBarScale*this.blockWidth)+((this.horizontalMargin*this.blockWidth)/2)+(this.blockWidth/2);
  70. }
  71. this.initRectangle = function(){
  72. var rect = new createjs.Shape();
  73. rect.graphics.beginStroke("#000000");
  74. rect.graphics.setStrokeStyle(this.leftRightBarScale*this.blockWidth);
  75. 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);
  76. rect.x = (stage.canvas.width-this.abacusWidth)/2;
  77. rect.y = stage.canvas.height/10;
  78. stage.addChild(rect);
  79. }
  80. this.initDivider = function(){
  81. var rect = new createjs.Shape();
  82. rect.graphics.beginFill("#000000").drawRect(0,0,this.abacusWidth-2*(this.leftRightBarScale*this.blockWidth),this.middleBarScale*this.blockHeight);
  83. rect.x = (stage.canvas.width-this.abacusWidth)/2+(this.leftRightBarScale*this.blockWidth);
  84. rect.y = stage.canvas.height/10+(this.leftRightBarScale*this.blockWidth)+this.upperBlockHeight;
  85. stage.addChild(rect);
  86. console.log(rect);
  87. }
  88. this.initTriangle = function(){
  89. this.tri = new createjs.Shape();
  90. var width = this.blockWidth/4*3;
  91. var y = stage.canvas.height/10+2*(this.leftRightBarScale*this.blockWidth)/3;
  92. var startx = ((stage.canvas.width-this.abacusWidth)/2)+(this.leftRightBarScale*this.blockWidth)+((this.horizontalMargin*this.blockWidth)/2)+(this.blockWidth/2);
  93. var incr = ((this.horizontalMargin*this.blockWidth))+this.blockWidth;
  94. var x = startx+(incr*(rods-1));
  95. 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();
  96. this.tri.x = x-(this.blockWidth/2);
  97. this.tri.y = y;
  98. stage.addChild(this.tri);
  99. this.trix = this.tri.x-this.startx;
  100. var c = this.tri;
  101. var t = this;
  102. this.tri.on("mousedown", function (evt) {
  103. this.offset = {x: c.x - evt.stageX, y: c.y - evt.stageY};
  104. });
  105. this.tri.on("pressmove", function (evt) {
  106. if (evt.stageX + this.offset.x < startx-(t.blockWidth/2)){
  107. c.x = startx-(t.blockWidth/2);
  108. } else if (evt.stageX + this.offset.x > x+(t.blockWidth/2)){
  109. c.x = x+(t.blockWidth/2);
  110. } else {
  111. c.x = evt.stageX + this.offset.x;
  112. }
  113. t.trix = c.x-t.startx;
  114. });
  115. }
  116. this.initColumns = function(){
  117. var startx = ((stage.canvas.width-this.abacusWidth)/2)+(this.leftRightBarScale*this.blockWidth)+((this.horizontalMargin*this.blockWidth)/2)+(this.blockWidth/2);
  118. var incr = ((this.horizontalMargin*this.blockWidth))+this.blockWidth;
  119. //console.log("======");
  120. //console.log(startx);
  121. //console.log(incr);
  122. var starty = stage.canvas.height/10+(this.leftRightBarScale*this.blockWidth);
  123. var endy = starty+this.upperBlockHeight;
  124. var grey = ["#A0A0A0","#6B6B6B"];
  125. var black = ["#000000","#000000"];
  126. var colinuse;
  127. console.log(colours);
  128. var fill = colours.fill;
  129. var stroke = colours.stroke;
  130. var xocolinuse;
  131. var val;
  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).mul(factor);
  144. } else {
  145. val = new window.Fraction(Math.pow(base,startvalue-item-1)).mul(factor);
  146. }
  147. var c = new StandardAbacusColumn(startx,starty,endy,topnumber,0,topnumber+this.extraBeads,xocolinuse,colinuse,this,val,true);
  148. c.init();
  149. this.topcolumns.push(c);
  150. startx+=incr;
  151. }
  152. starty = endy+this.middleBarScale*this.blockHeight;
  153. endy = starty+this.lowerBlockHeight;
  154. startx = ((stage.canvas.width-this.abacusWidth)/2)+(this.leftRightBarScale*this.blockWidth)+((this.horizontalMargin*this.blockWidth)/2)+(this.blockWidth/2);
  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. if (startvalue-item-1<0){
  164. var div = new window.Fraction(1);
  165. val = new window.Fraction(Math.pow(base,Math.abs(startvalue-item-1)));
  166. val = div.div(val);
  167. } else {
  168. val = new window.Fraction(Math.pow(base,startvalue-item-1));
  169. }
  170. var c = new StandardAbacusColumn(startx,starty,endy,0,bottomnumber,bottomnumber+this.extraBeads,xocolinuse,colinuse,this,val,false);
  171. c.init();
  172. this.bottomcolumns.push(c);
  173. startx+=incr;
  174. }
  175. }
  176. this.initTextItems = function(){
  177. var startx = ((stage.canvas.width-this.abacusWidth)/2)+(this.leftRightBarScale*this.blockWidth)+((this.horizontalMargin*this.blockWidth)/2)+(this.blockWidth/2);
  178. var incr = ((this.horizontalMargin*this.blockWidth))+this.blockWidth;
  179. for (var i = 0; i<rods; i++){
  180. var text = new createjs.Text("",(stage.canvas.width/70).toString()+"px Arial", "#FFF");
  181. text.set({
  182. textAlign: 'center'
  183. });
  184. text.x = startx;
  185. text.y = stage.canvas.height/10+this.abacusHeight-(this.leftRightBarScale*this.blockWidth)/2;
  186. stage.addChild(text);
  187. this.rodtext.push(text);
  188. startx += incr;
  189. }
  190. var text = new createjs.Text("",(this.blockWidth).toString()+"px Arial", "#000");
  191. text.set({
  192. textAlign: 'center'
  193. });
  194. text.x = stage.canvas.width/2;
  195. text.y = stage.canvas.height/40;
  196. stage.addChild(text);
  197. this.answertext = text;
  198. }
  199. this.updateTextItems = function(){
  200. if (this.bottomcolumns.length==rods){
  201. var sumarr = [];
  202. var total = new window.Fraction(0,1);
  203. for (var i = 0; i<rods; i++){
  204. this.topcolumns[i].updateAges();
  205. this.bottomcolumns[i].updateAges();
  206. var topuse = this.topcolumns[i].howManyInUse();
  207. var bottomuse = this.bottomcolumns[i].howManyInUse();
  208. var sum = factor*topuse.length;
  209. sum += bottomuse.length;
  210. if (sum!=0){
  211. this.rodtext[i].text=sum.toString();
  212. } else {
  213. this.rodtext[i].text="";
  214. }
  215. var tempsum = new window.Fraction(0,1);
  216. var tmp = this.topcolumns[i].value;
  217. tmp = tmp.mul(topuse.length);
  218. tempsum = tempsum.add(tmp);
  219. tmp = this.bottomcolumns[i].value;
  220. tmp = tmp.mul(bottomuse.length);
  221. tempsum = tempsum.add(tmp);
  222. var zero = new window.Fraction(0);
  223. if (!tempsum.equals(zero)){
  224. sumarr.push(tempsum.toFraction(true));
  225. total = total.add(tempsum);
  226. }
  227. }
  228. var zero = new window.Fraction(0);
  229. //console.log(sumarr);
  230. if (sumarr.length==1){
  231. var str = total.toFraction(true);
  232. this.answertext.text = str;
  233. } else if (!total.equals(zero)){
  234. var str = "";
  235. for (var i = 0; i<sumarr.length-1; i++){
  236. str += sumarr[i]+" + ";
  237. }
  238. str += sumarr[sumarr.length-1]+" = "+total.toFraction(true);
  239. this.answertext.text = str;
  240. } else {
  241. this.answertext.text = "";
  242. }
  243. if((this.answertext.text).length >= 40) {
  244. this.answertext.font = parseInt(((this.blockWidth / (this.answertext.text).length)*40)) + "px Arial";
  245. } else {
  246. this.answertext.font = (this.blockWidth).toString()+"px Arial";
  247. }
  248. }
  249. }
  250. this.restore = function(arr){
  251. var toparr = arr[0];
  252. var botarr = arr[1];
  253. for (var i = 0; i<arr[0].length; i++){
  254. this.topcolumns[i].restore(arr[0][i]);
  255. }
  256. for (var i = 0; i<arr[1].length; i++){
  257. this.bottomcolumns[i].restore(arr[1][i]);
  258. }
  259. for (var i = 0; i<arr[0].length; i++){
  260. this.topcolumns[i].restoreAge(arr[0][i]);
  261. }
  262. for (var i = 0; i<arr[1].length; i++){
  263. this.bottomcolumns[i].restoreAge(arr[1][i]);
  264. }
  265. }
  266. this.save = function(){
  267. var arr = [[],[]];
  268. for (var i = 0; i<this.topcolumns.length; i++){
  269. arr[0].push(this.topcolumns[i].save());
  270. }
  271. for (var i = 0; i<this.bottomcolumns.length; i++){
  272. arr[1].push(this.bottomcolumns[i].save());
  273. }
  274. return arr;
  275. }
  276. this.saveTri = function(){
  277. console.log(this.trix);
  278. return this.trix/this.abacusWidth;
  279. }
  280. this.restoreTri = function(x){
  281. console.log(x);
  282. this.tri.x = this.startx+(x*this.abacusWidth);
  283. this.trix = this.tri.x-this.startx;
  284. console.log(this.tri.x);
  285. }
  286. this.init = function(){
  287. this.initValues();
  288. this.initRectangle();
  289. this.initDivider();
  290. this.initColumns();
  291. this.initTextItems();
  292. this.initTriangle();
  293. }
  294. }