用 C++ 求曲線上給定點的法線
假設我們有一條曲線,如 y = x(A - x);我們需要找到該曲線上給定點 (x,y) 處的法線。此處,A 是一個整數,x 和 y 也是整數。
要解決此問題,我們需要檢查給定點是否在曲線上;如果在,則求該曲線的導數,即為:
$$\frac{\text{d}y}{\text{d}x}=A-2x$$
然後將 x 和 y 代入 dy/dx,再使用以下公式求法線:$$Y-y=-\lgroup\frac{\text{d}x}{\text{d}y}\rgroup*\lgroup X-x \rgroup$$
示例
#include<iostream> using namespace std; void getNormal(int A, int x, int y) { int differentiation = A - x * 2; if (y == (2 * x - x * x)) { if (differentiation < 0) cout << 0 - differentiation << "y = " << "x" << (0 - x) + (y * differentiation); else if (differentiation > 0) cout << differentiation << "y = " << "-x+" << x + differentiation * y; else cout << "x = " << x; } else cout << "Not possible"; } int main() { int A = 5, x = 2, y = 0; cout << "Equation of normal is: "; getNormal(A, x, y); }
輸出
Equation of normal is: 1y = -x+2
廣告