//var n=1;
//initial settings
var min1=1;
var max1=5;
var min2=12;
var max2=50;
var timerID=-1;
var op=0;
var opSign=["+", "-", "X"];
if (lang==0){
var opStr=["the sum of the two numbers is","the difference of the two numbers (which must be positive) is","the product of the two numbers is"];
var minmaxStr=[" as small as possible.", " as big as possible."];
}else{
var opStr=[" 两个数之和为", " 两个数之差为"," 两个数字的乘积是"];
var minmaxStr=[" 尽可能小.", " 尽可能大."];
}
var maxmin=0;
var opType, answer=-99999;
var GONews;
function setHTML(id,text){
var e=document.getElementById(id);
if (e==null) return;
e.innerHTML=text;
}
class Movable {
static DOWN=1;
static UP=2;
static mouse=2;
static x1=0;
static y1=0;
static ctx;
static list=new Array(); // list of MyCirce
static list1=new Array();// list of Container
static list2=new Array();
static selected=null;
constructor(x,y){
this.x0=x;
this.y0=y;
}
inside(x,y){
return true;
}
draw (ctx1){
;
}
setLoc(x,y) {
this.x0=x;
this.y0=y;
}
move (e){
var dx=e.offsetX-Movable.x1;
var dy=e.offsetY-Movable.y1;
this.x0+=dx;
this.y0+=dy;
Movable.x1=e.offsetX;
Movable.y1=e.offsetY;
this.draw(Movable.ctx);
}
static clearContainer() {
var cont=Movable.list1;
for (var i=0;i
=0 && x<=this.w && y>=0 &&y<=this.h)
return true;
return false;
}
draw (ctx){
ctx.fillStyle="blue";
ctx.fillRect(this.x0, this.y0, this.w, this.h);
;
}
}
*/
class MyCircle extends Movable {
constructor (xx,y,r,id){
super (xx,y);
this.r=r;
//this.h=h;
this.id=id;
}
setlocation (x,y){
this.x0=x;
this.y0=y;
}
inside(x,y){
x-=this.x0;
y-=this.y0;
var r=this.r;
if((x*x+y*y)<(r*r))
return true;
return false;
}
/*move (e){
super.move(e);
var newx0=this.x0-300;
var newy0=this.y0-200;
var angleP=Math.atan2(newy0,newx0);
this.x0=300+75*Math.cos(angleP);
this.y0=200+75*Math.sin(angleP);
}*/
set(id) {
this.id=id;
}
draw (ctx){
ctx.strokeStyle="brown";
ctx.beginPath();
//ctx.lineTo(this.x0+20,this.y0+20);
var newx0=this.x0-300;
var newy0=this.y0-200;
var angleP=Math.atan2(newy0,newx0);
var d=Math.sqrt(newx0*newx0+newy0*newy0);
var newx=300+(d+this.r)*Math.cos(angleP);
var newy=200+(d+this.r)*Math.sin(angleP);
var newx1=300+(d-this.r)*Math.cos(angleP);
var newy1=200+(d-this.r)*Math.sin(angleP);
//this.x0=300+75*Math.cos(angleP);
//this.y0=200+75*Math.sin(angleP);
//ctx.moveTo(newx1,newy1);
//ctx.lineTo(newx,newy);
//ctx.arc(this.x0, this.y0, this.r,0,2*Math.PI);
ctx.fillStyle="red";
ctx.font="30px Arial";
ctx.textAlign = "center";
ctx.fillText(""+this.id,this.x0, this.y0+7);
if (angleP<0)angleP+=Math.PI*2;
angleP=angleP*180/Math.PI;
angleP=360-angleP;
angleP=angleP-90;
if (angleP<0)angleP=angleP+360;
//if (angleP<0) angleP=-angleP;
//ctx.fillText(""+(angleP).toFixed(0),this.x0, this.y0+7);
//ctx.moveTo(0, 0);
//ctx.lineTo(this.x0, this.y0)
ctx.stroke();
}
}
class MyContainer {
constructor (x,y,r,type, color){
//constructor (x,y,w, h,color){
this.x0=x;
this.y0=y;
this.r=r;
this.type=type;
this.color=color;
}
inside(x,y){
x-=this.x0;
y-=this.y0;
var r=this.r;
if((x*x+y*y)<(r*r))
return true;
return false;
}
draw (ctx){
ctx.strokeStyle=this.color;
ctx.beginPath();
ctx.arc(this.x0, this.y0, this.r,0,2*Math.PI);
ctx.stroke();
}
isOK(n){
if (this.inside(n.x0,n.y0)==false)
return 0;
var re=0;
switch (this.type) {
case 1:
if (n.id%2==1)
re= 1;
break;
case 2:
if (n.id%2==0)
re= 1;
break;
case 3:
if (n.id%3==0)
re= 1;
break;
}
}
}
function doMouseDown(event) {
//alert("mouse is down");
//var m=document.getElementById("mouseInfo");
//m.innerHTML="mouse is down";
Movable.mouseDown(event);
Movable.isInside(event);
}
function doMouseUp(event) {
//alert("mouse is up");
//var m=document.getElementById("mouseInfo");
//m.innerHTML="mouse is up";
Movable.mouseUp();
//Movable.positionOK();
CheckAnswer();
}
function doMouseMove(event) {
//alert("mouse is up");
//var m=document.getElementById("mouseInfo");
//m.innerHTML="mouse is moving: ("+event.offsetX+", "+event.offsetY+")";
if (Movable.isMouseDown() && Movable.moveSelected(event)) {
redraw(Movable.ctx);
}
}
function doMouseWheel(event) {
//alert("mouse is up");
//var m=document.getElementById("mouseInfo");
//m.innerHTML="mouse is wheeling: ("+event.offsetX+", "+event.offsetY+")";
}
function initCanvas(canvasName) {
var canvas = document.getElementById(canvasName);
canvas.addEventListener('mousedown',doMouseDown,false);
canvas.addEventListener('mouseup',doMouseUp,false);
canvas.addEventListener('mousemove',doMouseMove,false);
canvas.addEventListener('mousewheel',doMouseWheel,false);
//document.addEventListener('keydown',doKeyDown);
//document.addEventListener('keyup', doKeyUp);
return canvas;
}
function redraw(ctx) {
var n=getRndInteger(min1,max1);
ctx.fillStyle="white";
ctx.fillRect(0,0,canvas.width,canvas.height);
//ctx.arc(0,0,50,0,2*Math.PI);
Movable.redraw();
var mm;
if(lang==0){
if (maxmin==1) {
setHTML("c1","Maximize");
mm="Largest:";
} else {
setHTML("c1",mm="Minimize");
mm="Smallest:";
}
setHTML("c2","Place the four digits into the circles so that ");
var str= opStr[op]+ minmaxStr[maxmin];
}else{
if (maxmin==1) {
setHTML("c1","最大化");
mm="最大";
}else {
setHTML("c1","最小化");
mm="最小";
}
setHTML("c2","要将四位数字放入圆圈中, ");
var str= "所以这样" + opStr[op]+ minmaxStr[maxmin];
}
setHTML("c3",str);
/*
switch(opType) {
case '-':
setHTML("c3","so that the" + opStr[op]+ minmaxStr[maxmin]);
break;
case '+':
setHTML("c1","Small difference");
setHTML("c2","To place the digits 2,4,6,7 into the circles, ");
setHTML("c3","so that the difference between two two-digit numbers is as small as possible.");
break;
case 'X':
setHTML("c1","Small difference");
setHTML("c2","To place the digits 2,4,6,7 into the circles, ");
setHTML("c3","so that the difference between two two-digit numbers is as small as possible.");
break;
}
*/
if (GONews==1){
Movable.ctx.fillText ("Good job!!! Try next quesiton?",500,50);
}
ctx.font="50px Arial";
ctx.textAlign = "center";
ctx.beginPath();
ctx.fillStyle="red";
ctx.fillText(opType,330, 320);
ctx.font="30px Arial";
ctx.fillText(mm,330, 400);
ctx.moveTo (300,350);
ctx.lineTo (550,350);
ctx.stroke();
}
function getRndInteger(min, max) {
return Math.floor(Math.random() * (max - min + 1) ) + min;
}
function value(a,b) {
switch (opType) {
case '+':
return a+b;
case '-':
if (a0) {
clearInterval(timerID);
timerID=-1;
}*/
var num=new Array();
var x=0;
while (num.length!=4) {
n=getRndInteger(1,9);
if (num.indexOf(n)<0)
num.push(n);
}
op=getRndInteger(0,2);
opType=opSign[op];
maxmin=getRndInteger(0,1);
//============================
//Movable.clearContainer(); done in getNumbers()
/*
num = [2, 4, 3, 8];
maxmin=0;
op=2; opType=opSign[op];
*/
Movable.setLocations(50,50,50,0);
Movable.list[0].set(num[0]);
Movable.list[1].set(num[1]);
Movable.list[2].set(num[2]);
Movable.list[3].set(num[3]);
answer=calcAnswer(num, maxmin);
//====
redraw(ctx);
}
//======= initializing ========
var radius=100;
var canvas=initCanvas("myCanvas");
var ctx = canvas.getContext("2d");
Movable.ctx=ctx;
var min1 = 1;
var max1 = 1;
var min2 = 12;
var max2 = 24;
var n=0;
var nPosition;
var randomNum = 0;
var ss=getURLpara();
// ?p=min1,max1,min2,max2
// alert(paraStr+" s[0]="+s[0]+" s["+(s.length-1)+"]="+s[s.length-1]);
if (ss.length==2) {
//N=parseInt(s[0]);
min1=parseInt(ss[0]);
max1=parseInt(ss[1]);
//min2=parseInt(ss[2]);
//max2=parseInt(ss[3]);
}
// alert("min1="+min1+" "+max1+" "+min2+" "+max2);
//var n=getRndInteger(min1,max1);
//var randomNum=getRndInteger(min2,max2);
/*
var NN1=new MyContainer(400,200,30,1,"blue");
var NN2=new MyContainer(470,200,30,2,"red");
var NN3=new MyContainer(400,300,30,3,"green");
Movable.list1.push(NN1);
Movable.list1.push(NN2);
Movable.list1.push(NN3);
*/
var NN1=new MyContainer(400,200,30,1,"blue");
var NN2=new MyContainer(470,200,30,2,"red");
var NN3=new MyContainer(400,300,30,3,"green");
var NN4=new MyContainer(470,300,30,4,"purple");
Movable.list1.push(NN1);
Movable.list1.push(NN2);
Movable.list1.push(NN3);
Movable.list1.push(NN4);
Movable.add(new MyCircle(1*50,50,20,2));
Movable.add(new MyCircle(2*50,50,20,4));
Movable.add(new MyCircle(3*50,50,20,6));
Movable.add(new MyCircle(4*50,50,20,7));
getRandomPara();
showOrHide('foot',lang);
showOrHide('title1',lang);
showOrHide('title2',lang);
//redraw(ctx);
) processed by MyLexV4 -->