Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!

Idiom #178 Check if point is inside rectangle

Set boolean b to true if if the point with coordinates (x,y) is inside the rectangle with coordinates (x1,y1,x2,y2) , or to false otherwise.
Describe if the edges are considered to be inside the rectangle.

isInsideRect(X1, Y1, X2, Y2, PX, PY) when X1 =< PX andalso PX =< X2 andalso Y1 =< PY andalso PY =< Y2 -> true;
isInsideRect(_, _, _, _, _, _) -> false.
int isInsideRect(double x1, double y1, double x2, double y2, double px, double py){
	return px >= x1 && px <= x2 && py >= y1 && py <= y2; 
}
using System.Windows;
b = new Rect(new Point(x1, y1), new Point(x2, y2)).Contains(x, y);
bool b = x1<x && x<x2 && y1<y && y<y2;
import "dart:math";
var r = Rectangle.fromPoints(Point(x1,y1),Point(x2,y2));
var b = r.containsPoint(Point(x,y));
logical :: b
b = (x1 < x) .and. (x < x2) .and. (y1 < y) .and. (y < y2)
import "image"
p := image.Pt(x, y)
r := image.Rect(x1, y1, x2, y2)
b := p.In(r)
b = x >= x1 && x <= x2 && y >= y1 && y <= y2
const pointInRect = ({x1, y1, x2, y2}, {x, y}) => (
  (x > x1 && x < x2) && (y > y1 && y < y2)
)
boolean b = x <= x2 && x >= x1 && y <= y2 && y >= y1;
import java.awt.geom.Rectangle2D;
double w = x2 - x1, h = y2 - y1;
Rectangle2D r = new Rectangle2D.Double(x1, y1, w, h);
boolean b = r.contains(x, y);
import java.awt.Rectangle;
int w = x2 - x1, h = y2 - y1;
Rectangle r = new Rectangle(x1, y1, w, h);
boolean b = r.contains(x, y);
(setf b (and (< x1 x x2)
             (< y1 y y2)))
uses Types;
b := PtInRect(Rect(x1,y1,x2,y2), Point(x,y));
use feature 'signatures';
sub insideRect($x1,$y1,$x2,$y2, $px,$py) {
    return
            $x1 <= $px and $px <= $x2
        and $y1 <= $py and $py <= $y1
}
b = (x1 < x < x2) and (y1 < y < y2)
class Rectangle:
    def __init__(self, x, y, w, h):
        self.x, self.y = x, y
        self.w, self.h = w, h
    def contains(self, x, y):
        a, b = self.x, self.y
        w, h = self.w, self.h
        return a <= x <= (a + w) and \
               b <= y <= (b + h)
w, h = x2 - x1, y2 - y1
r = Rectangle(x1, y1, w, h)
b = r.contains(x, y)
Point = Struct.new(:x, :y)

Rect  = Struct.new(:x1, :y1, :x2, :y2) do
  def contains?(point)
    point.x.between?(x1,x2) && point.y.between?(y1,y2)
  end
end

b = Rect.new(0,0,2,5).contains?(Point.new(0,0))
struct Rect {
    x1: i32,
    x2: i32,
    y1: i32,
    y2: i32,
}

impl Rect {
    fn contains(&self, x: i32, y: i32) -> bool {
        return self.x1 < x && x < self.x2 && self.y1 < y && y < self.y2;
    }
}

New implementation...
< >
Bart