简体   繁体   English

p5.j​​s 不满足条件时用if语句画一个框

[英]p5.js Draw a box when the conditions are not met using an if statement

i have been pulling my hair out with this for few hours.I have to create shapes when the cursor is in a certain place using if statements, which i have completed.我一直在用这个拉我的头发几个小时。当光标位于某个位置时,我必须使用 if 语句创建形状,我已经完成了。 I also need to draw a shape if neither of the conditions are met, this i have only managed to half complete.如果两个条件都不满足,我还需要绘制一个形状,我只完成了一半。 IS anybody able to give some guidance on what i need to think about to complete the task?是否有人能够就我完成任务需要考虑的事项提供一些指导? 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 )  
}

I think you have to do something like:我认为您必须执行以下操作:

//91m dist dark circle
if( yourCondition )
{

    yourCode;

}

// Fish wholsalers
else if( yourOtherCondition )
{

    yourOtherCode;

}

// neither position
else
{

    yourOtherOtherCode;

}

The easiest solution is to use 2 variables.最简单的解决方案是使用 2 个变量。

Evaluate if the mouse is in_range :评估鼠标是否在in_range

var in_range = dist(mouseX, mouseY,1491, 585)<= 91;

Evaluate if the mouse is in bounds:评估鼠标是否在边界内:

var in_bounds = mouseX > 1590 && mouseX < 1691 && mouseY > 614 && mouseY < 691; 

Draw the rectangles dependent on the conditions in_range , in_bounds respectively !in_range && !in_bounds .根据条件in_rangein_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 )  
}

If you are not allowed to use variables:如果不允许使用变量:

The opposite of < is >= and the opposite of > is <= .的相反<>=和相反>就是<=
The opposite of A && B is !(A && B) respectively !A || !B A && B的反义词分别是!(A && B) !A || !B !A || !B . !A || !B
The opposite of x > A && x < B is x >= A || x <= B x > A && x < B的反义词是x >= A || x <= B x >= A || x <= B . x >= A || x <= B

In your case that is:你的情况是:

if (!(dist(mouseX, mouseY,1491, 585) <= 91) &&
    !(mouseX > 1590 && mouseX < 1691 && mouseY > 614 && mouseY < 691))
{
    fill(124, 252, 0)
    rect(1564, 183, 322, 173 )  
}

Or或者

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM