Dados dos números a y b donde b < a forman un rectángulo con puntos (0, b), (b, 0), (ab, b), (b, ab). Dado un punto (x, y), la tarea es verificar si este punto se encuentra dentro o sobre el rectángulo o no.
Ejemplos:
Input: a = 7, b = 2, x = 5, y = 2; Output: Given point does not lie on the rectangle Input: a = 7, b = 2, x = 4, y = 5; Output: Given point lies inside the rectangle
Enfoque: Una manera eficiente es formar la ecuación de 4 líneas de un rectángulo. Entonces, si un punto dado se encuentra en o sobre el rectángulo si y solo si, sustituyendo el punto dado en:
- La línea lateral derecha ( xyb = 0 ) debe dar el valor menor o igual a cero.
- La línea lateral izquierda ( x-y+a = 0 ) debe dar el valor mayor o igual a uno.
- La línea lateral superior ( x+y-2*a+b = 0 ) debe dar el valor menor o igual a cero.
- La línea lateral inferior ( x+yb = 0 ) debe dar el valor mayor o igual a uno.
C++
// C++ program to Check whether a given point // lies inside or on the rectangle or not #include <bits/stdc++.h> using namespace std; // function to Check whether a given point // lies inside or on the rectangle or not bool LiesInsieRectangle(int a, int b, int x, int y) { if (x - y - b <= 0 && x - y + b >= 0 && x + y - 2 * a + b <= 0 && x + y - b >= 0) return true; return false; } // Driver code int main() { int a = 7, b = 2, x = 4, y = 5; if (LiesInsieRectangle(a, b, x, y)) cout << "Given point lies inside the rectangle"; else cout << "Given point does not lie on the rectangle"; return 0; }
C
// C program to Check whether a given point // lies inside or on the rectangle or not #include <stdio.h> #include <stdbool.h> // function to Check whether a given point // lies inside or on the rectangle or not bool LiesInsieRectangle(int a, int b, int x, int y) { if (x - y - b <= 0 && x - y + b >= 0 && x + y - 2 * a + b <= 0 && x + y - b >= 0) return true; return false; } // Driver code int main() { int a = 7, b = 2, x = 4, y = 5; if (LiesInsieRectangle(a, b, x, y)) printf("Given point lies inside the rectangle"); else printf("Given point does not lie on the rectangle"); return 0; } // This code is contributed by kothavvsaakash.
Java
// Java program to Check whether // a given point lies inside or // on the rectangle or not class GFG { // function to Check whether // a given point lies inside // or on the rectangle or not static boolean LiesInsieRectangle(int a, int b, int x, int y) { if (x - y - b <= 0 && x - y + b >= 0 && x + y - 2 * a + b <= 0 && x + y - b >= 0) return true; return false; } // Driver code public static void main(String[] args) { int a = 7, b = 2, x = 4, y = 5; if (LiesInsieRectangle(a, b, x, y)) System.out.println("Given point lies " + "inside the rectangle"); else System.out.println("Given point does not " + "lie on the rectangle"); } } // This code is contributed // by ChitraNayal
Python3
# Python 3 program to Check whether # a given point lies inside or on # the rectangle or not # function to Check whether a given # point lies inside or on the # rectangle or not def LiesInsieRectangle(a, b, x, y) : if(x - y - b <= 0 and x - y + b >= 0 and x + y - 2 * a + b <= 0 and x + y - b >= 0) : return True return False # Driver code if __name__ == "__main__" : # multiple assignments a, b, x, y = 7, 2, 4, 5 if LiesInsieRectangle(a, b, x, y) : print("Given point lies inside" " the rectangle") else : print("Given point does not lie" " on the rectangle") # This code is contributed by ANKITRAI1
C#
// C# program to Check whether // a given point lies inside // or on the rectangle or not using System; class GFG { // function to Check whether // a given point lies inside // or on the rectangle or not static bool LiesInsieRectangle(int a, int b, int x, int y) { if (x - y - b <= 0 && x - y + b >= 0 && x + y - 2 * a + b <= 0 && x + y - b >= 0) return true; return false; } // Driver code public static void Main() { int a = 7, b = 2, x = 4, y = 5; if (LiesInsieRectangle(a, b, x, y)) Console.Write("Given point lies " + "inside the rectangle"); else Console.Write("Given point does not " + "lie on the rectangle"); } } // This code is contributed // by ChitraNayal
PHP
<?php // PHP program to Check whether // a given point lies inside // or on the rectangle or not // function to Check whether // a given point lies inside // or on the rectangle or not function LiesInsieRectangle($a, $b, $x, $y) { if ($x - $y - $b <= 0 && $x - $y + $b >= 0 && $x + $y - 2 * $a + $b <= 0 && $x + $y - $b >= 0) return true; return false; } // Driver code $a = 7; $b = 2; $x = 4; $y = 5; if (LiesInsieRectangle($a, $b, $x, $y)) echo "Given point lies ". "inside the rectangle"; else echo "Given point does not". " lie on the rectangle"; // This code is contributed by mits ?>
Javascript
<script> // Javascript program to Check whether a given point // lies inside or on the rectangle or not // function to Check whether a given point // lies inside or on the rectangle or not function LiesInsieRectangle(a, b, x, y) { if (x - y - b <= 0 && x - y + b >= 0 && x + y - 2 * a + b <= 0 && x + y - b >= 0) return true; return false; } // Driver code let a = 7, b = 2, x = 4, y = 5; if (LiesInsieRectangle(a, b, x, y)) document.write("Given point lies inside the rectangle"); else document.write("Given point does not lie on the rectangle"); // This code is contributed by Mayank Tyagi </script>
Producción:
Given point lies inside the rectangle
Tiempo Complejidad: O(1)
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por pawan_asipu y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA