对象的最小和最大函数
min and max functions with objects
亲爱的大家,这是我第一次 post 来这里,我最近才开始尝试编码,所以请多多关照。
我目前正在上一门课程,在这门课程中,他们给了我们任务。我当前的任务有问题。
我将 post 下面的整个代码与我目前的尝试。我觉得我已经以各种合乎逻辑的方式尝试过这个,但它总是以失败告终。
如果有任何帮助,我们将不胜感激。
/*
Officer: 1585754
CaseNum: 303-2-49385492-1585754
Case 303 - The Case of the Crooked Attorney
Stage 3 - The Gates Bank
I’ve made an appointment for you at the Gates Bank to retrieve your safe deposit box from the vault.
Actually you will break into Torvalds’ one.
Crack the safe by doing the following:
Whilst the mouse is moving:
- Make CrypticSafeCombination_0 equal to the value of mouseY
- Use the 'min' function to prevent CrypticSafeCombination_0 from going above 11
Whilst the mouse is moving:
- Decrement CrypticSafeCombination_1 by 2
- Use the 'max' function to prevent CrypticSafeCombination_1 from falling below 4
Whilst the mouse is being dragged:
- Make CrypticSafeCombination_2 equal to the value of mouseX
- Use the 'max' function to prevent CrypticSafeCombination_2 from falling below 2
Whilst the mouse is moving:
- Decrement CrypticSafeCombination_3 by 1
- Use the 'max' function to prevent CrypticSafeCombination_3 from falling below 3
Whilst the mouse is moving:
- Make CrypticSafeCombination_4 equal to the value of mouseX
- Use the 'max' function to prevent CrypticSafeCombination_4 from falling below 13
This time you'll need to create the relevant event handlers yourself.
There are many possible ways of investigating this case, but you
should use ONLY the following commands:
- The assignment operator aka. the equals sign !
- mouseX, mouseY
- Incrementing +=
- Decrementing -=
- min, max
- constrain
*/
//declare the variables
var CrypticSafeCombination_0;
var CrypticSafeCombination_1;
var CrypticSafeCombination_2;
var CrypticSafeCombination_3;
var CrypticSafeCombination_4;
function preload()
{
//IMAGES WILL BE LOADED HERE
}
function setup()
{
createCanvas(512,512);
//initialise the variables
CrypticSafeCombination_0 = 0;
CrypticSafeCombination_1 = 0;
CrypticSafeCombination_2 = 0;
CrypticSafeCombination_3 = 0;
CrypticSafeCombination_4 = 0;
}
///////////////////EVENT HANDLERS///////////////////
//Create event handlers here to open the safe ...
**This is my current attempt**
function mouseMoved()
{
CrypticSafeCombination_0 = mouseY;
min = (mouseY, 11);
CrypticSafeCombination_1 -= 2;
max = (2, 4);
CrypticSafeCombination_3 -=1;
max = (1, 3);
CrypticSafeCombination_4 = mouseX;
max = (mouseX, 13);
}
function mouseDragged()
{
CrypticSafeCombination_2 = mouseX;
max = (mouseX, 2);
}
///////////////DO NOT CHANGE CODE BELOW THIS POINT///////////////////
function draw()
{
//Draw the safe door
background(70);
noStroke();
fill(29,110,6);
rect(26,26,width-52,width-52);
//Draw the combination dials
push();
translate(120,170);
drawDial(140,CrypticSafeCombination_0, 16);
pop();
push();
translate(120,380);
drawDial(140,CrypticSafeCombination_1, 23);
pop();
push();
translate(280,170);
drawDial(140,CrypticSafeCombination_2, 19);
pop();
push();
translate(280,380);
drawDial(140,CrypticSafeCombination_3, 15);
pop();
//Draw the lever
push();
translate(width - 125,256);
drawLever(CrypticSafeCombination_4);
pop();
}
function drawDial(diameter,num,maxNum)
{
//the combination lock
var r = diameter * 0.5;
var p = r * 0.6;
stroke(0);
fill(255,255,200);
ellipse(0,0,diameter,diameter);
fill(100);
noStroke();
ellipse(0,0,diameter*0.66,diameter*0.66);
fill(150,0,0);
triangle(
-p * 0.4,-r-p,
p * 0.4,-r-p,
0,-r-p/5
);
noStroke();
push();
var inc = 360/maxNum;
rotate(radians(-num * inc));
for(var i = 0; i < maxNum; i++)
{
push();
rotate(radians(i * inc));
stroke(0);
line(0,-r*0.66,0,-(r-10));
noStroke();
fill(0);
text(i,0,-(r-10));
pop();
}
pop();
}
function drawLever(rot)
{
push();
rotate(radians(-rot))
stroke(0);
fill(100);
rect(-10,0,20,100);
ellipse(0,0,50,50);
ellipse(0,100,35,35);
pop();
}
您误解了如何使用 function
s. min()
and max()
是接收 2 个参数和 return 1 个参数的函数。
例如x = min(a, b)
将参数 a
和 b
传递给函数 min()
。 min()
找到 a
和 b
和 return 中的最小值。 return 值分配给 x
.
例如y = max(y, c)
找到 y
和 c
中的最大值并将其分配给 y
.
将其应用于您的代码:
function mouseMoved()
{
// Make CrypticSafeCombination_0 equal to the value of mouseY
CrypticSafeCombination_0 = mouseY;
// Use the 'min' function to prevent CrypticSafeCombination_0 from going above 11
CrypticSafeCombination_0 = min(CrypticSafeCombination_0, 11);
// Decrement CrypticSafeCombination_1 by 2
CrypticSafeCombination_1 -= 2;
// Use the 'max' function to prevent CrypticSafeCombination_1 from falling below 4
CrypticSafeCombination_1 = max(CrypticSafeCombination_1, 4);
// Decrement CrypticSafeCombination_3 by 1
CrypticSafeCombination_3 -=1;
//Use the 'max' function to prevent CrypticSafeCombination_3 from falling below 3
CrypticSafeCombination_3 = max(CrypticSafeCombination_3, 3);
//Make CrypticSafeCombination_4 equal to the value of mouseX
CrypticSafeCombination_4 = mouseX;
// Use the 'max' function to prevent CrypticSafeCombination_4 from falling below 13
CrypticSafeCombination_4 = max(CrypticSafeCombination_4, 13);
}
function mouseDragged()
{
// Make CrypticSafeCombination_2 equal to the value of mouseX
CrypticSafeCombination_2 = mouseX;
// Use the 'max' function to prevent CrypticSafeCombination_2 from falling below 2
CrypticSafeCombination_2 = max(CrypticSafeCombination_2, 2);
}
或
function mouseMoved()
{
// Make CrypticSafeCombination_0 equal to the value of mouseY
// Use the 'min' function to prevent CrypticSafeCombination_0 from going above 11
CrypticSafeCombination_0 = min(mouseY, 11);
// Decrement CrypticSafeCombination_1 by 2
// Use the 'max' function to prevent CrypticSafeCombination_1 from falling below 4
CrypticSafeCombination_1 = max(CrypticSafeCombination_1-2, 4);
// Decrement CrypticSafeCombination_3 by 1
//Use the 'max' function to prevent CrypticSafeCombination_3 from falling below 3
CrypticSafeCombination_3 = max(CrypticSafeCombination_3-1, 3);
// Make CrypticSafeCombination_4 equal to the value of mouseX
// Use the 'max' function to prevent CrypticSafeCombination_4 from falling below 13
CrypticSafeCombination_4 = max(mouseX, 13);
}
function mouseDragged()
{
// Make CrypticSafeCombination_2 equal to the value of mouseX
// Use the 'max' function to prevent CrypticSafeCombination_2 from falling below 2
CrypticSafeCombination_2 = max(mouseX, 2);
}
亲爱的大家,这是我第一次 post 来这里,我最近才开始尝试编码,所以请多多关照。
我目前正在上一门课程,在这门课程中,他们给了我们任务。我当前的任务有问题。
我将 post 下面的整个代码与我目前的尝试。我觉得我已经以各种合乎逻辑的方式尝试过这个,但它总是以失败告终。
如果有任何帮助,我们将不胜感激。
/*
Officer: 1585754
CaseNum: 303-2-49385492-1585754
Case 303 - The Case of the Crooked Attorney
Stage 3 - The Gates Bank
I’ve made an appointment for you at the Gates Bank to retrieve your safe deposit box from the vault.
Actually you will break into Torvalds’ one.
Crack the safe by doing the following:
Whilst the mouse is moving:
- Make CrypticSafeCombination_0 equal to the value of mouseY
- Use the 'min' function to prevent CrypticSafeCombination_0 from going above 11
Whilst the mouse is moving:
- Decrement CrypticSafeCombination_1 by 2
- Use the 'max' function to prevent CrypticSafeCombination_1 from falling below 4
Whilst the mouse is being dragged:
- Make CrypticSafeCombination_2 equal to the value of mouseX
- Use the 'max' function to prevent CrypticSafeCombination_2 from falling below 2
Whilst the mouse is moving:
- Decrement CrypticSafeCombination_3 by 1
- Use the 'max' function to prevent CrypticSafeCombination_3 from falling below 3
Whilst the mouse is moving:
- Make CrypticSafeCombination_4 equal to the value of mouseX
- Use the 'max' function to prevent CrypticSafeCombination_4 from falling below 13
This time you'll need to create the relevant event handlers yourself.
There are many possible ways of investigating this case, but you
should use ONLY the following commands:
- The assignment operator aka. the equals sign !
- mouseX, mouseY
- Incrementing +=
- Decrementing -=
- min, max
- constrain
*/
//declare the variables
var CrypticSafeCombination_0;
var CrypticSafeCombination_1;
var CrypticSafeCombination_2;
var CrypticSafeCombination_3;
var CrypticSafeCombination_4;
function preload()
{
//IMAGES WILL BE LOADED HERE
}
function setup()
{
createCanvas(512,512);
//initialise the variables
CrypticSafeCombination_0 = 0;
CrypticSafeCombination_1 = 0;
CrypticSafeCombination_2 = 0;
CrypticSafeCombination_3 = 0;
CrypticSafeCombination_4 = 0;
}
///////////////////EVENT HANDLERS///////////////////
//Create event handlers here to open the safe ...
**This is my current attempt**
function mouseMoved()
{
CrypticSafeCombination_0 = mouseY;
min = (mouseY, 11);
CrypticSafeCombination_1 -= 2;
max = (2, 4);
CrypticSafeCombination_3 -=1;
max = (1, 3);
CrypticSafeCombination_4 = mouseX;
max = (mouseX, 13);
}
function mouseDragged()
{
CrypticSafeCombination_2 = mouseX;
max = (mouseX, 2);
}
///////////////DO NOT CHANGE CODE BELOW THIS POINT///////////////////
function draw()
{
//Draw the safe door
background(70);
noStroke();
fill(29,110,6);
rect(26,26,width-52,width-52);
//Draw the combination dials
push();
translate(120,170);
drawDial(140,CrypticSafeCombination_0, 16);
pop();
push();
translate(120,380);
drawDial(140,CrypticSafeCombination_1, 23);
pop();
push();
translate(280,170);
drawDial(140,CrypticSafeCombination_2, 19);
pop();
push();
translate(280,380);
drawDial(140,CrypticSafeCombination_3, 15);
pop();
//Draw the lever
push();
translate(width - 125,256);
drawLever(CrypticSafeCombination_4);
pop();
}
function drawDial(diameter,num,maxNum)
{
//the combination lock
var r = diameter * 0.5;
var p = r * 0.6;
stroke(0);
fill(255,255,200);
ellipse(0,0,diameter,diameter);
fill(100);
noStroke();
ellipse(0,0,diameter*0.66,diameter*0.66);
fill(150,0,0);
triangle(
-p * 0.4,-r-p,
p * 0.4,-r-p,
0,-r-p/5
);
noStroke();
push();
var inc = 360/maxNum;
rotate(radians(-num * inc));
for(var i = 0; i < maxNum; i++)
{
push();
rotate(radians(i * inc));
stroke(0);
line(0,-r*0.66,0,-(r-10));
noStroke();
fill(0);
text(i,0,-(r-10));
pop();
}
pop();
}
function drawLever(rot)
{
push();
rotate(radians(-rot))
stroke(0);
fill(100);
rect(-10,0,20,100);
ellipse(0,0,50,50);
ellipse(0,100,35,35);
pop();
}
您误解了如何使用 function
s. min()
and max()
是接收 2 个参数和 return 1 个参数的函数。
例如x = min(a, b)
将参数 a
和 b
传递给函数 min()
。 min()
找到 a
和 b
和 return 中的最小值。 return 值分配给 x
.
例如y = max(y, c)
找到 y
和 c
中的最大值并将其分配给 y
.
将其应用于您的代码:
function mouseMoved()
{
// Make CrypticSafeCombination_0 equal to the value of mouseY
CrypticSafeCombination_0 = mouseY;
// Use the 'min' function to prevent CrypticSafeCombination_0 from going above 11
CrypticSafeCombination_0 = min(CrypticSafeCombination_0, 11);
// Decrement CrypticSafeCombination_1 by 2
CrypticSafeCombination_1 -= 2;
// Use the 'max' function to prevent CrypticSafeCombination_1 from falling below 4
CrypticSafeCombination_1 = max(CrypticSafeCombination_1, 4);
// Decrement CrypticSafeCombination_3 by 1
CrypticSafeCombination_3 -=1;
//Use the 'max' function to prevent CrypticSafeCombination_3 from falling below 3
CrypticSafeCombination_3 = max(CrypticSafeCombination_3, 3);
//Make CrypticSafeCombination_4 equal to the value of mouseX
CrypticSafeCombination_4 = mouseX;
// Use the 'max' function to prevent CrypticSafeCombination_4 from falling below 13
CrypticSafeCombination_4 = max(CrypticSafeCombination_4, 13);
}
function mouseDragged()
{
// Make CrypticSafeCombination_2 equal to the value of mouseX
CrypticSafeCombination_2 = mouseX;
// Use the 'max' function to prevent CrypticSafeCombination_2 from falling below 2
CrypticSafeCombination_2 = max(CrypticSafeCombination_2, 2);
}
或
function mouseMoved()
{
// Make CrypticSafeCombination_0 equal to the value of mouseY
// Use the 'min' function to prevent CrypticSafeCombination_0 from going above 11
CrypticSafeCombination_0 = min(mouseY, 11);
// Decrement CrypticSafeCombination_1 by 2
// Use the 'max' function to prevent CrypticSafeCombination_1 from falling below 4
CrypticSafeCombination_1 = max(CrypticSafeCombination_1-2, 4);
// Decrement CrypticSafeCombination_3 by 1
//Use the 'max' function to prevent CrypticSafeCombination_3 from falling below 3
CrypticSafeCombination_3 = max(CrypticSafeCombination_3-1, 3);
// Make CrypticSafeCombination_4 equal to the value of mouseX
// Use the 'max' function to prevent CrypticSafeCombination_4 from falling below 13
CrypticSafeCombination_4 = max(mouseX, 13);
}
function mouseDragged()
{
// Make CrypticSafeCombination_2 equal to the value of mouseX
// Use the 'max' function to prevent CrypticSafeCombination_2 from falling below 2
CrypticSafeCombination_2 = max(mouseX, 2);
}