簡體   English   中英

p5.j​​s 不滿足條件時用if語句畫一個框

[英]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_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 )  
}

如果不允許使用變量:

的相反<>=和相反>就是<=
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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM