一、从0°开始到90°,每次增加5,求斜率。
#include <iostream>
#include <cmath> // 引入数学库,用于tan和M_PI
float slope(double angleDegrees) {
// 将角度转换为弧度
double angleRadians = angleDegrees * (M_PI / 180.0);
// 计算斜率(正切值)
double slope = tan(angleRadians);
// 输出结果
//std::cout << "The slope for an angle of " << angleDegrees << " degrees is: " << slope << std::endl;
return slope;
}
int main() {
for(int i=0; i<180;i+=5){
float mySlop = slope(i);
std::cout << "angle:" << i << " ratio:" << mySlop << std:: endl;
}
}
二、已知三个点,先求角度,然后再根据角度求斜率
#include <iostream>
#include <cmath>
// 定义一个二维向量类
class Vector2D {
public:
double x, y;
Vector2D(double x, double y) : x(x), y(y) {}
// 向量减法
Vector2D operator-(const Vector2D& v) const {
return Vector2D(x - v.x, y - v.y);
}
// 向量点积
double dot(const Vector2D& v) const {
return x * v.x + y * v.y;
}
// 向量长度
double length() const {
return std::sqrt(x * x + y * y);
}
// 向量与X轴正方向的夹角(弧度)
double angleWithXAxis() const {
return std::atan2(y, x);
}
};
int main() {
// A点作为原点
Vector2D A(0, 0);
// B点的坐标
Vector2D B(3, 6);
// C点的坐标
Vector2D C(2, 1);
// 计算向量BA和向量AC
Vector2D BA = A - B; // 注意这里是BA而不是AB
Vector2D AC = C - A;
// 计算向量BA和AC与X轴的夹角
double angleBA = BA.angleWithXAxis();
double angleAC = AC.angleWithXAxis();
// 计算两向量与X轴夹角的差值
double angleDifference = angleAC - angleBA;
// 输出结果(弧度)
std::cout << "Angle difference between BA and AC in radians: " << angleDifference << std::endl;
// 如果需要输出角度值
double angleDifferenceDegrees = angleDifference * (180.0 / M_PI);
std::cout << "Angle difference between BA and AC in degrees: " << angleDifferenceDegrees << std::endl;
// 如果理解为角度的"斜率",即角度变化率
// 可以简单理解为 tan(angleDifference)
double angleSlope = std::tan(angleDifference);
std::cout << "Slope of the angle difference (tan value): " << angleSlope << std::endl;
return 0;
}
三、已知三个点,求斜率
#include <iostream>
#include <cmath>
// 定义一个二维向量类
class Vector2D {
public:
double x, y;
Vector2D(double x, double y) : x(x), y(y) {}
// 向量减法
Vector2D operator-(const Vector2D& v) const {
return Vector2D(x - v.x, y - v.y);
}
// 向量点积
double dot(const Vector2D& v) const {
return x * v.x + y * v.y;
}
// 向量长度
double length() const {
return std::sqrt(x * x + y * y);
}
// 向量归一化
Vector2D normalized() const {
double len = length();
return Vector2D(x / len, y / len);
}
// 向量旋转(顺时针)
Vector2D rotateClockwise90Degrees() const {
return Vector2D(y, -x);
}
};
int main() {
// A点作为原点
Vector2D A(0, 0);
// B点的坐标
Vector2D B(3, 6);
// C点的坐标
Vector2D C(2, 1);
// 将B点转换为相对于A点的向量
Vector2D AB = B - A;
// 将AB向量归一化
Vector2D ABUnit = AB.normalized();
// 计算AB向量垂直方向的单位向量(顺时针90度旋转)
Vector2D ABPerp = ABUnit.rotateClockwise90Degrees();
// 将C点转换为相对于A点的向量
Vector2D AC = C - A;
// 在新坐标系中,C点的位置
double newCx = AC.dot(ABUnit); // C点沿AB方向的投影
double newCy = AC.dot(ABPerp); // C点垂直于AB方向的距离
// AC的斜率(假设newCx != 0)
if (newCx != 0) {
double slope = newCy / newCx;
std::cout << "Slope of AC in the new coordinate system: " << slope << std::endl;
} else {
std::cout << "The line AC is vertical in the new coordinate system." << std::endl;
}
return 0;
}