[英]p5.js Draw a box when the conditions are not met using an if statement
我一直在用这个拉我的头发几个小时。当光标位于某个位置时,我必须使用 if 语句创建形状,我已经完成了。 如果两个条件都不满足,我还需要绘制一个形状,我只完成了一半。 是否有人能够就我完成任务需要考虑的事项提供一些指导? enter image description here
function draw()
{
// draw the image
image(img,0,0);
//Write your code below here ...
//91m distance dark circle
if(dist(mouseX, mouseY,1491, 585)<= 91)
{
fill(0,139,139);
ellipse(1491, 585,91 * 2, 91 * 2);
}
//Fish wholsalers
if(mouseX > 1590 && mouseX < 1691
&& mouseY > 614 && mouseY < 691)
{
fill(25, 25, 112)
rect(1590,614,104,77)
}
//neither position
if(dist(mouseX, mouseY,1491, 585)>= 91 && (mouseX > 1690 && mouseX < 1590 && mouseY < 614 && mouseY > 691) )
{
fill(124, 252, 0)
rect(1564, 183, 322, 173 )
}
我认为您必须执行以下操作:
//91m dist dark circle
if( yourCondition )
{
yourCode;
}
// Fish wholsalers
else if( yourOtherCondition )
{
yourOtherCode;
}
// neither position
else
{
yourOtherOtherCode;
}
最简单的解决方案是使用 2 个变量。
评估鼠标是否在in_range
:
var in_range = dist(mouseX, mouseY,1491, 585)<= 91;
评估鼠标是否在边界内:
var in_bounds = mouseX > 1590 && mouseX < 1691 && mouseY > 614 && mouseY < 691;
根据条件in_range
、 in_bounds
分别绘制矩形!in_range && !in_bounds
。
var in_range = dist(mouseX, mouseY,1491, 585)<= 91;
var in_bounds = mouseX > 1590 && mouseX < 1691 && mouseY > 614 && mouseY < 691;
if (in_range)
{
fill(0,139,139);
ellipse(1491, 585,91 * 2, 91 * 2);
}
if (in_bounds)
{
fill(25, 25, 112)
rect(1590,614,104,77)
}
if(!in_range && !in_bounds)
{
fill(124, 252, 0)
rect(1564, 183, 322, 173 )
}
如果不允许使用变量:
的相反<
是>=
和相反>
就是<=
A && B
的反义词分别是!(A && B)
!A || !B
!A || !B
。
x > A && x < B
的反义词是x >= A || x <= B
x >= A || x <= B
。
你的情况是:
if (!(dist(mouseX, mouseY,1491, 585) <= 91) &&
!(mouseX > 1590 && mouseX < 1691 && mouseY > 614 && mouseY < 691))
{
fill(124, 252, 0)
rect(1564, 183, 322, 173 )
}
或者
if (dist(mouseX, mouseY,1491, 585) > 91 &&
(mouseX <= 1590 || mouseX >= 1691 || mouseY <= 614 || mouseY >= 691))
{
fill(124, 252, 0)
rect(1564, 183, 322, 173 )
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.