Jay
Jay

Reputation: 13

How do I define an inclusive range of values?

Right now, I'm trying to program it so that if my curser position == a rectangle's position, I can drag it around. I want the coordinates to have a ±20 pixel leniency, so if I'm off by ±20 pixels I can still drag the rectangle around. Here is (essentially) what I had so far:

if m_pos[0] == Rect36[0] + (0 <= x <= 16):
    same_x = True
if m_pos[1] == Rect36[1] + (0 <= y <= 16):
    same_y = True

m_pos[0] and Rect36[0] would be the respective x-coordinate, and m_pos[1] and Rect36[1] would be the respective y-coordinate. What could I put in place of the intervals to make this work? Thanks in advance!!

Upvotes: 0

Views: 189

Answers (1)

John Gordon
John Gordon

Reputation: 33310

Use the absolute value of the difference.

if abs(m_pos[0] - Rect36[0]) <= 20:
    same_x = True

Upvotes: 1

Related Questions