Tweaking this code for rectangles.

I was wondering if this bit of code could be tweaked to apply for rectangles and not only circles? I’m trying to check the distance of the touch to the rectangular button.

i:dist(vec2()<20

What do you mean? Do you want to check if a point is within a rectangle or do you want to find the distance between a point and a rectangle

@Coder. I want to see if the touched point is within the rectangle.

Here’s a way to check if a is in b’s width/height

if math.abs(b.x - a.x)  < bWidth and math.abs(b.y - a.y) < bHeight then

(Pseudo-code)

@YoloSwag Here’s some code to check if a touch point is within a rectangle.


function setup()
    col=color(255)
end

function draw()
    background(40, 40, 50)
    fill(col)
    rect(200,300,200,100)
    fill(0,255,0)
    text("touch here",300,350)
end

function touched(t)
    col=(255)
    if t.state==BEGAN or t.state==MOVING then
        if t.x>200 and t.x<400 and t.y>300 and t.y<400 then
            col=color(255,0,0)
        end      
    end
end