Dadas las coordenadas de un punto bidimensional p(x 0 , y 0 ). Encuentre los puntos a una distancia L de él, tal que la línea formada al unir estos puntos tenga una pendiente de M.
Ejemplos:
Input : p = (2, 1) L = sqrt(2) M = 1 Output :3, 2 1, 0 Explanation: The two points are sqrt(2) distance away from the source and have the required slope m = 1. Input : p = (1, 0) L = 5 M = 0 Output : 6, 0 -4, 0
Necesitamos encontrar dos puntos que estén a una distancia L del punto dado, en una línea con pendiente M.
La idea se ha presentado en la publicación a continuación.
Encuentra esquinas de rectángulo usando puntos medios
Según la pendiente de entrada, el problema se puede clasificar en 3 categorías.
- Si la pendiente es cero, solo necesitamos ajustar la coordenada x del punto de origen
- Si la pendiente es infinita, necesitamos ajustar la coordenada y
- Para otros valores de pendiente, podemos usar las siguientes ecuaciones para encontrar los puntos
Ahora usando la fórmula anterior podemos encontrar los puntos requeridos.
C++
// C++ program to find the points on a line of // slope M at distance L #include <bits/stdc++.h> using namespace std; // structure to represent a co-ordinate // point struct Point { float x, y; Point() { x = y = 0; } Point(float a, float b) { x = a, y = b; } }; // Function to print pair of points at // distance 'l' and having a slope 'm' // from the source void printPoints(Point source, float l, int m) { // m is the slope of line, and the // required Point lies distance l // away from the source Point Point a, b; // slope is 0 if (m == 0) { a.x = source.x + l; a.y = source.y; b.x = source.x - l; b.y = source.y; } // if slope is infinite else if (m == std::numeric_limits<float> ::max()) { a.x = source.x; a.y = source.y + l; b.x = source.x; b.y = source.y - l; } else { float dx = (l / sqrt(1 + (m * m))); float dy = m * dx; a.x = source.x + dx; a.y = source.y + dy; b.x = source.x - dx; b.y = source.y - dy; } // print the first Point cout << a.x << ", " << a.y << endl; // print the second Point cout << b.x << ", " << b.y << endl; } // driver function int main() { Point p(2, 1), q(1, 0); printPoints(p, sqrt(2), 1); cout << endl; printPoints(q, 5, 0); return 0; }
Java
// Java program to find the points on // a line of slope M at distance L class GFG{ // Class to represent a co-ordinate // point static class Point { float x, y; Point() { x = y = 0; } Point(float a, float b) { x = a; y = b; } }; // Function to print pair of points at // distance 'l' and having a slope 'm' // from the source static void printPoints(Point source, float l, int m) { // m is the slope of line, and the // required Point lies distance l // away from the source Point Point a = new Point(); Point b = new Point(); // Slope is 0 if (m == 0) { a.x = source.x + l; a.y = source.y; b.x = source.x - l; b.y = source.y; } // If slope is infinite else if (Double.isInfinite(m)) { a.x = source.x; a.y = source.y + l; b.x = source.x; b.y = source.y - l; } else { float dx = (float)(l / Math.sqrt(1 + (m * m))); float dy = m * dx; a.x = source.x + dx; a.y = source.y + dy; b.x = source.x - dx; b.y = source.y - dy; } // Print the first Point System.out.println(a.x + ", " + a.y); // Print the second Point System.out.println(b.x + ", " + b.y); } // Driver code public static void main(String[] args) { Point p = new Point(2, 1), q = new Point(1, 0); printPoints(p, (float)Math.sqrt(2), 1); System.out.println(); printPoints(q, 5, 0); } } // This code is contributed by Rajnis09
C#
// C# program to find the points on // a line of slope M at distance L using System; class GFG{ // Class to represent a co-ordinate // point public class Point { public float x, y; public Point() { x = y = 0; } public Point(float a, float b) { x = a; y = b; } }; // Function to print pair of points at // distance 'l' and having a slope 'm' // from the source static void printPoints(Point source, float l, int m) { // m is the slope of line, and the // required Point lies distance l // away from the source Point Point a = new Point(); Point b = new Point(); // Slope is 0 if (m == 0) { a.x = source.x + l; a.y = source.y; b.x = source.x - l; b.y = source.y; } // If slope is infinite else if (Double.IsInfinity(m)) { a.x = source.x; a.y = source.y + l; b.x = source.x; b.y = source.y - l; } else { float dx = (float)(l / Math.Sqrt( 1 + (m * m))); float dy = m * dx; a.x = source.x + dx; a.y = source.y + dy; b.x = source.x - dx; b.y = source.y - dy; } // Print the first Point Console.WriteLine(a.x + ", " + a.y); // Print the second Point Console.WriteLine(b.x + ", " + b.y); } // Driver code public static void Main(String[] args) { Point p = new Point(2, 1), q = new Point(1, 0); printPoints(p, (float)Math.Sqrt(2), 1); Console.WriteLine(); printPoints(q, 5, 0); } } // This code is contributed by Amit Katiyar
Python3
# Python program to find the points on a line of # slope M at distance L import math # structure to represent a co-ordinate # point class Point: def __init__(self, x, y): self.x = x self.y = y # Function to print pair of points at # distance 'l' and having a slope 'm' # from the source def printPoints(source, l, m): # m is the slope of line, and the # required Point lies distance l # away from the source Point a = Point(0, 0) b = Point(0, 0) # slope is 0 if m == 0: a.x = source.x + l a.y = source.y b.x = source.x - l b.y = source.y # if slope is infinite elif m != math.isfinite(m): a.x = source.x a.y = source.y + l b.x = source.x b.y = source.y - l else: dx = (l / math.sqrt(1 + (m * m))) dy = m * dx a.x = source.x + dx a.y = source.y + dy b.x = source.x - dx b.y = source.y - dy # print the first Point print(f"{a.x}, {a.y}") # print the second Point print(f"{b.x}, {b.y}") # driver function p = Point(2, 1) q = Point(1, 0) printPoints(p, math.sqrt(2), 1) print("\n") printPoints(q, 5, 0) # The code is contributed by Gautam goel(gautamgoel962)
Javascript
<script> // Javascript program to find the points on // a line of slope M at distance L // Class to represent a co-ordinate // point class Point { constructor(x, y) { this.x = x; this.y = y; } } // Function to print pair of points at // distance 'l' and having a slope 'm' // from the source function printPoints(source, l, m) { // m is the slope of line, and the // required Point lies distance l // away from the source Point let a = new Point(); let b = new Point(); // Slope is 0 if (m == 0) { a.x = source.x + l; a.y = source.y; b.x = source.x - l; b.y = source.y; } // If slope is infinite else if (!isFinite(m)) { a.x = source.x; a.y = source.y + l; b.x = source.x; b.y = source.y - l; } else { var dx = (l / Math.sqrt(1 + (m * m))); var dy = m * dx; a.x = source.x + dx; a.y = source.y + dy; b.x = source.x - dx; b.y = source.y - dy; } // Print the first Point document.write(a.x + ", " + a.y+"\n"); // Print the second Point document.write(b.x + ", " + b.y+"\n"); } // Driver code let p = new Point(2, 1); let q = new Point(1, 0); printPoints(p, Math.sqrt(2), 1); document.write("\n"); printPoints(q, 5, 0); // This code is contributed by shruti456rawal </script>
Producción:
3, 2 1, 0 6, 0 -4, 0
Complejidad de tiempo: O(1)
Espacio Auxiliar: O(1)
Ashutosh Kumar contribuye con este artículo 😀 Si te gusta GeeksforGeeks y te gustaría contribuir, también puedes escribir un artículo usando write.geeksforgeeks.org o enviar tu artículo por correo a review-team@geeksforgeeks.org. Vea su artículo que aparece en la página principal de GeeksforGeeks y ayude a otros Geeks.
Escriba comentarios si encuentra algo incorrecto o si desea compartir más información sobre el tema tratado anteriormente.
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA