直线方程求x坐标c语言,已知两点坐标,求直线方程、距离其中一点距离为L的某点...
總覺得代碼理應(yīng)是無所不能的,尤其是在復(fù)雜的計算方面。而最近一個項目,要求計算坐標(biāo)點(diǎn),這尼瑪遇到了要解方程組的情況,還是一元二次方程組。當(dāng)時整個人都不好了,上網(wǎng)到處搜尋,也無法找到那種可以把表達(dá)式列出來,就給你解出來的方法。不過還好,網(wǎng)友的一些代碼給了我不少的啟發(fā),于是摸出難得一用的紙筆,老老實(shí)實(shí)在草稿紙上演算,最終有了以下代碼:
private void pointXY() {
Point curPoint = new Point(20, 30);// 當(dāng)前坐標(biāo)
Point nextPoint = new Point(35, 42);// 下個點(diǎn)坐標(biāo)
double distance = Math.sqrt(Math.pow(curPoint.x - nextPoint.x, 2)
+ Math.pow(curPoint.y - nextPoint.y, 2));// 兩點(diǎn)的坐標(biāo)距離
double lenthUnit = distance / 5;// 單位長度
// 第一步:求得直線方程相關(guān)參數(shù)y=kx+b
double k = (curPoint.y - nextPoint.y) * 1.0
/ (curPoint.x - nextPoint.x);// 坐標(biāo)直線斜率k
double b = curPoint.y - k * curPoint.x;// 坐標(biāo)直線b
// 第二步:求得在直線y=kx+b上,距離當(dāng)前坐標(biāo)距離為L的某點(diǎn)
// 一元二次方程Ax^2+Bx+C=0中,
// 一元二次方程求根公式:
// 兩根x1,x2= [-B±√(B^2-4AC)]/2A
// ①(y-y0)^2+(x-x0)^2=L^2;
// ②y=kx+b;
// 式中x,y即為根據(jù)以上lenthUnit單位長度(這里就是距離L)對應(yīng)點(diǎn)的坐標(biāo)
// 由①②表達(dá)式得到:(k^2+1)x^2+2[(b-y0)k-x0]x+[(b-y0)^2+x0^2-L^2]=0
double A = Math.pow(k, 2) + 1;// A=k^2+1;
double B = 2 * ((b - curPoint.y) * k - curPoint.x);// B=2[(b-y0)k-x0];
int m = 1;
double L = m * lenthUnit;
// C=(b-y0)^2+x0^2-L^2
double C = Math.pow(b - curPoint.y, 2) + Math.pow(curPoint.x, 2)
- Math.pow(L, 2);
// 兩根x1,x2= [-B±√(B^2-4AC)]/2A
double x1 = (-B + Math.sqrt(Math.pow(B, 2) - 4 * A * C)) / (2 * A);
double x2 = (-B - Math.sqrt(Math.pow(B, 2) - 4 * A * C)) / (2 * A);
double x = 0;// 最后確定是在已知兩點(diǎn)之間的某點(diǎn)
if (x1 == x2) {
x = x1;
} else if (curPoint.x <= x1 && x1 <= nextPoint.x || nextPoint.x <= x1
&& x1 <= curPoint.x) {
x = x1;
} else if (curPoint.x <= x2 && x2 <= nextPoint.x || nextPoint.x <= x2
&& x2 <= curPoint.x) {
x = x2;
}
double y = k * x + b;
Point mPoint = new Point((int) x, (int) y);
}
總結(jié)
以上是生活随笔為你收集整理的直线方程求x坐标c语言,已知两点坐标,求直线方程、距离其中一点距离为L的某点...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 李雅普诺夫稳定性理论 matlab,李雅
- 下一篇: STM32——时钟系统