给定四个整数a, b, c和d ,代表直线的系数,方程为 (ax + by + c = 0),任务是找到通过给定点的两条直线的方程并与给定的直线成角度α。
例子:
Input: a = 2, b = 3, c = -7, x1 = 4, y1 = 9, α = 30
Output: y = -0.49x +10
y = -15.51x + 71
Input: a = 3, b = -2, c = 4, x1 = 3, y1 = 4, α = 55
Output: y = 43.73x -127
y = -0.39x +5
方法:
- 设P (x1, y1)为给定点,线LMN (图 1)为与 x 轴正方向成角度θ的给定线。
- 令PMR和PNS是两条所需的线,它们与给定的线成角度(α) 。
- 让这些线分别在R和S处与 x 轴相交。
- 假设直线PMR和PNS分别与 x 轴的正方向成角度(θ1)和(θ2) 。
- 然后用直线的斜点形式,两条直线的方程为:
… (1)
… (2)
and are the slopes of lines PMR and PNS respectively.
- 现在考虑三角形LMR :
Using the property: An exterior angle of a triangle is equal to the sum of the two opposite interior angles
… (3)
- 现在考虑三角形LNS :
… (4)
- 现在我们计算 (tanθ) 的值:
By formula,
slope of given line
- 现在将方程 (3) 和 (4) 中的 (tan(θ1)) 和 (tan(θ2)) 的值替换为方程 (1) 和 (2) 以获得两条线的最终方程:
Line PMR :
Line PNS :
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find slope of given line
double line_slope(double a, double b)
{
if (a != 0)
return -b / a;
// Special case when slope of
// line is infinity or is
// perpendicular to x-axis
else
return (-2);
}
// Function to find equations of lines
// passing through the given point
// and making an angle with given line
void line_equation(double a, double b,
double c, double x1,
double y1, double alfa)
{
// Set the precision
cout << fixed << setprecision(2);
// Store slope of given line
double given_slope = line_slope(a, b);
// Convert degrees to radians
double x = alfa * 3.14159 / 180;
// Special case when slope of
// given line is infinity:
// In this case slope of one line
// will be equal to alfa
// and the other line will be
// equal to (180-alfa)
if (given_slope == -2) {
// In this case slope of
// required lines can't be
// infinity
double slope_1 = tan(x);
double slope_2 = tan(3.14159 - x);
// g and f are the variables
// of required equations
int g = x1, f = x1;
g *= (-slope_1);
g += y1;
// Print first line equation
if (g > 0)
cout << "y = " << slope_1
<< "x +" << g << endl;
if (g <= 0)
cout << "y = " << slope_1
<< "x " << g << endl;
f *= (-slope_2);
f += y1;
// Print second line equation
if (f > 0) {
cout << "y = " << slope_2
<< "x +" << f << endl;
}
if (f <= 0)
cout << "y = " << slope_2
<< "x " << f << endl;
return;
}
// Special case when slope of
// required line becomes infinity
if (1 - tan(x) * given_slope == 0) {
cout << "x = " << x1 << endl;
}
if (1 + tan(x) * given_slope == 0) {
cout << "x = " << x1 << endl;
}
// General case
double slope_1 = (given_slope + tan(x))
/ (1 - tan(x) * given_slope);
double slope_2 = (given_slope - tan(x))
/ (1 + tan(x) * given_slope);
// g and f are the variables
// of required equations
int g = x1, f = x1;
g *= (-slope_1);
g += y1;
// Print first line equation
if (g > 0 && 1 - tan(x) * given_slope != 0)
cout << "y = " << slope_1
<< "x +" << g << endl;
if (g <= 0 && 1 - tan(x) * given_slope != 0)
cout << "y = " << slope_1
<< "x " << g << endl;
f *= (-slope_2);
f += y1;
// Print second line equation
if (f > 0 && 1 + tan(x) * given_slope != 0) {
cout << "y = " << slope_2
<< "x +" << f << endl;
}
if (f <= 0 && 1 + tan(x) * given_slope != 0)
cout << "y = " << slope_2
<< "x " << f << endl;
}
// Driver Code
int main()
{
// Given Input
double a = 2, b = 3, c = -7;
double x1 = 4, y1 = 9;
double alfa = 30;
// Function Call
line_equation(a, b, c, x1, y1, alfa);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG{
// Function to find slope of given line
static double line_slope(double a, double b)
{
if (a != 0)
return -b / a;
// Special case when slope of
// line is infinity or is
// perpendicular to x-axis
else
return (-2);
}
// Function to find equations of lines
// passing through the given point
// and making an angle with given line
static void line_equation(double a, double b,
double c, double x1,
double y1, double alfa)
{
// Store slope of given line
double given_slope = line_slope(a, b);
// Convert degrees to radians
double x = alfa * 3.14159 / 180;
// Special case when slope of
// given line is infinity:
// In this case slope of one line
// will be equal to alfa
// and the other line will be
// equal to (180-alfa)
if (given_slope == -2)
{
// In this case slope of
// required lines can't be
// infinity
double slope_1 = Math.tan(x);
double slope_2 = Math.tan(3.14159 - x);
// g and f are the variables
// of required equations
int g = (int)x1, f = (int)x1;
g *= (-slope_1);
g += y1;
// Print first line equation
if (g > 0)
System.out.println("y = " +
(Math.round(slope_1 * 100.0) / 100.0) +
"x +" + (Math.round(g * 100.0) / 100.0));
if (g <= 0)
System.out.println("y = " +
(Math.round(slope_1 * 100.0) / 100.0) +
"x " + (Math.round(g * 100.0) / 100.0));
f *= (-slope_2);
f += y1;
// Print second line equation
if (f > 0)
{
System.out.println("y = " +
(Math.round(slope_2 * 100.0) / 100.0) +
"x +" + (Math.round(f * 100.0) / 100.0));
}
if (f <= 0)
System.out.println("y = " +
(Math.round(slope_1 * 100.0) / 100.0) +
"x " + (Math.round(g * 100.0) / 100.0));
return;
}
// Special case when slope of
// required line becomes infinity
if (1 - Math.tan(x) * given_slope == 0)
{
System.out.println("x = " +
(Math.round(x1 * 100.0) / 100.0));
}
if (1 + Math.tan(x) * given_slope == 0)
{
System.out.println("x = " +
(Math.round(x1 * 100.0) / 100.0));
}
// General case
double slope_1 = (given_slope + Math.tan(x)) /
(1 - Math.tan(x) * given_slope);
double slope_2 = (given_slope - Math.tan(x)) /
(1 + Math.tan(x) * given_slope);
// g and f are the variables
// of required equations
int g = (int)x1, f = (int)x1;
g *= (-slope_1);
g += y1;
// Print first line equation
if (g > 0 && 1 - Math.tan(x) * given_slope != 0)
System.out.println("y = " +
(Math.round(slope_1 * 100.0) / 100.0) +
"x +" + (Math.round(g * 100.0) / 100.0));
if (g <= 0 && 1 - Math.tan(x) * given_slope != 0)
System.out.println("y = " +
(Math.round(slope_1 * 100.0) / 100.0) +
"x " + (Math.round(g * 100.0) / 100.0));
f *= (-slope_2);
f += y1;
// Print second line equation
if (f > 0 && 1 + Math.tan(x) * given_slope != 0)
{
System.out.println("y = " +
(Math.round(slope_2 * 100.0) / 100.0) +
"x +" + (Math.round(f * 100.0) / 100.0));
}
if (f <= 0 && 1 + Math.tan(x) * given_slope != 0)
System.out.println("y = " +
(Math.round(slope_2 * 100.0) / 100.0) +
"x +" + (Math.round(f * 100.0) / 100.0));
}
// Driver Code
public static void main (String[] args)
{
// Given Input
double a = 2, b = 3, c = -7;
double x1 = 4, y1 = 9;
double alfa = 30;
// Function Call
line_equation(a, b, c, x1, y1, alfa);
}
}
// This code is contributed by Dharanendra L V.
Python3
# Python3 program for the above approach
import math
# Function to find slope of given line
def line_slope(a, b):
if (a != 0):
return -b / a
# Special case when slope of
# line is infinity or is
# perpendicular to x-axis
else:
return (-2)
# Function to find equations of lines
# passing through the given point
# and making an angle with given line
def line_equation(a, b, c, x1, y1, alfa):
# Store slope of given line
given_slope = line_slope(a, b)
# Convert degrees to radians
x = alfa * 3.14159 / 180
# Special case when slope of
# given line is infinity:
# In this case slope of one line
# will be equal to alfa
# and the other line will be
# equal to (180-alfa)
if (given_slope == -2):
# In this case slope of
# required lines can't be
# infinity
slope_1 = math.tan(x)
slope_2 = math.tan(3.14159 - x)
# g and f are the variables
# of required equations
g = x1, f = x1
g *= (-slope_1)
g += y1
# Print first line equation
if (g > 0):
print("y = ", round(slope_1, 2),
"x +" , round(g));
if (g <= 0):
print("y = ", round(slope_1, 2),
"x ", round(g))
f *= (-slope_2)
f += y1
# Print second line equation
if (f > 0):
print("y = ", round(slope_2, 2),
"x +", round(f))
if (f <= 0):
print("y = " , round(slope_2, 2),
"x " , round(f))
return
# Special case when slope of
# required line becomes infinity
if (1 - math.tan(x) * given_slope == 0):
print("x =", x1)
if (1 + math.tan(x) * given_slope == 0):
print("x =", x1)
# General case
slope_1 = ((given_slope + math.tan(x)) /
(1 - math.tan(x) * given_slope))
slope_2 = ((given_slope - math.tan(x)) /
(1 + math.tan(x) * given_slope))
# g and f are the variables
# of required equations
g = x1
f = x1
g *= (-slope_1)
g += y1
# Print first line equation
if (g > 0 and 1 - math.tan(x) * given_slope != 0):
print("y = ", round(slope_1, 2),
"x +", round(g))
if (g <= 0 and 1 - math.tan(x) * given_slope != 0):
print("y = ", round(slope_1, 2),
"x ", round(g))
f *= (-slope_2)
f += y1
# Print second line equation
if (f > 0 and 1 + math.tan(x) * given_slope != 0):
print("y = ", round(slope_2, 2),
"x +", round(f))
if (f <= 0 and 1 + math.tan(x) * given_slope != 0):
print("y = ", round(slope_2, 2),
"x " , round(f))
# Driver Code
if __name__ == "__main__":
# Given Input
a = 2
b = 3
c = -7
x1 = 4
y1 = 9
alfa = 30
# Function Call
line_equation(a, b, c, x1, y1, alfa)
# This code is contributed by ukasp
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
public class GFG{
// Function to find slope of given line
static double line_slope(double a, double b)
{
if (a != 0)
return -b / a;
// Special case when slope of
// line is infinity or is
// perpendicular to x-axis
else
return (-2);
}
// Function to find equations of lines
// passing through the given point
// and making an angle with given line
static void line_equation(double a, double b,
double c, double x1,
double y1, double alfa)
{
// Store slope of given line
double given_slope = line_slope(a, b);
// Convert degrees to radians
double x = alfa * 3.14159 / 180;
double slope_1,slope_2;
double g,f;
// Special case when slope of
// given line is infinity:
// In this case slope of one line
// will be equal to alfa
// and the other line will be
// equal to (180-alfa)
if (given_slope == -2)
{
// In this case slope of
// required lines can't be
// infinity
slope_1 = Math.Tan(x);
slope_2 = Math.Tan(3.14159 - x);
// g and f are the variables
// of required equations
g = (int)x1;
f = (int)x1;
g *= (-slope_1);
g += y1;
// Print first line equation
if (g > 0)
Console.WriteLine("y = " +
(Math.Round(slope_1 * 100.0) / 100.0) +
"x +" + (Math.Round((int)g * 100.0) / 100.0));
if (g <= 0)
Console.WriteLine("y = " +
(Math.Round(slope_1 * 100.0) / 100.0) +
"x " + (Math.Round((int)g * 100.0) / 100.0));
f *= (-slope_2);
f += y1;
// Print second line equation
if (f > 0)
{
Console.WriteLine("y = " +
(Math.Round(slope_2 * 100.0) / 100.0) +
"x +" + (Math.Round((int)f * 100.0) / 100.0));
}
if (f <= 0)
Console.WriteLine("y = " +
(Math.Round(slope_1 * 100.0) / 100.0) +
"x " + (Math.Round((int)g * 100.0) / 100.0));
return;
}
// Special case when slope of
// required line becomes infinity
if (1 - Math.Tan(x) * given_slope == 0)
{
Console.WriteLine("x = " +
(Math.Round(x1 * 100.0) / 100.0));
}
if (1 + Math.Tan(x) * given_slope == 0)
{
Console.WriteLine("x = " +
(Math.Round(x1 * 100.0) / 100.0));
}
// General case
slope_1 = (given_slope + Math.Tan(x)) /
(1 - Math.Tan(x) * given_slope);
slope_2 = (given_slope - Math.Tan(x)) /
(1 + Math.Tan(x) * given_slope);
// g and f are the variables
// of required equations
g = (int)x1;
f = (int)x1;
g *= (-slope_1);
g += y1;
// Print first line equation
if (g > 0 && 1 - Math.Tan(x) * given_slope != 0)
Console.WriteLine("y = " +
(Math.Round(slope_1 * 100.0) / 100.0) +
"x +" + (Math.Round((int)g * 100.0) / 100.0));
if (g <= 0 && 1 - Math.Tan(x) * given_slope != 0)
Console.WriteLine("y = " +
(Math.Round(slope_1 * 100.0) / 100.0) +
"x " + (Math.Round((int)g * 100.0) / 100.0));
f *= (-slope_2);
f += y1;
// Print second line equation
if (f > 0 && 1 + Math.Tan(x) * given_slope != 0)
{
Console.WriteLine("y = " +
(Math.Round(slope_2 * 100.0) / 100.0) +
"x +" + (Math.Round((int)f * 100.0) / 100.0));
}
if (f <= 0 && 1 + Math.Tan(x) * given_slope != 0)
Console.WriteLine("y = " +
(Math.Round(slope_2 * 100.0) / 100.0) +
"x +" + (Math.Round((int)f * 100.0) / 100.0));
}
// Driver Code
public static void Main(String[] args)
{
// Given Input
double a = 2, b = 3, c = -7;
double x1 = 4, y1 = 9;
double alfa = 30;
// Function Call
line_equation(a, b, c, x1, y1, alfa);
}
}
// This code contributed by shikhasingrajput
输出:
y = -0.49x +10
y = -15.51x +71
时间复杂度: O(1)
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。