wolen
wolen

Reputation: 715

Point is close to the diagonal

I would like to calculate if the point is near a diagonal of rectangle. The rectangle is representing as two points (min and max). Now I have third point and I would like to check if is near the diagonal.

if (minBound < pointVector2 && pointVector2 < maxBound) {
    CheckIfIsNearTheDiagonal(50, true);
}

minBound and maxBound are the border points (Vector2)

I'd like to check how far the point pointVector2 is from (specific) diagonal. The distance compare with the argument maxDistance and return if is in the range around the diagonal.

bool CheckIfIsNearTheDiagonal(float maxDistance, bool isLeftDownToRightUp ){
   // Somehow count distance
   return distance < maxDistance
}

Is there any simple way or I must calculate general form of equation of a line and a distance between the point and the line?

Upvotes: 2

Views: 632

Answers (1)

Francesco Baruchelli
Francesco Baruchelli

Reputation: 7468

This gives you the distance from Point p0 and the line passing in p1 and p2:

    public double Distance(Point p1, Point p2, Point p0)
    {
        double m = (p2.Y - p1.Y) / (p2.X - p1.X);
        double q = (p1.Y * p2.X - p2.Y * p1.X) / (p2.X - p1.X);
        return Math.Abs((p0.Y - m * p0.X - q) / Math.Sqrt(1 + m * m));
    }

Upvotes: 2

Related Questions