2010-10-29 36 views
3

有人可以幫我嗎。我理解一條線的方程以及如何解決紙上的零截取問題,但是我無法將其轉換爲代碼。更具體地講,我需要計算在該行截取具備兩種不同功能任何給定的X和Y座標點......Line x和y n截取點/ C++

double CalcLineYIntercept(LINE *line, double yintercept) { } 
double CalcLineXIntercept(LINE *line, double xintercept) { } 

所以,CalcLineYIntercept將返回點的X座標線截取yintercept哪裏(不一定爲零)。我無法將代數方程轉換爲代碼(並且我知道C++是一種代數語言,但代碼本身不會簡單地隔離變量)。有沒有簡單的方法來實現這一點?

非常感謝您

+1

我們需要聲明'LINE'來回答這個問題。也許還要寫下你想要實現的公式。 – 2010-10-29 11:52:35

+0

LINE是簡單的兩點座標,雙倍,x1,y1,x2,y2。 y = mx + b是線的方程,y2 - y1/x2 - x1是斜率(m),b是偏移量,但我不確定如何在這裏使用b來表示等式,或者如何把它寫入代碼。我需要n截距點(任何給定座標的截取點,不只是零) – user491232 2010-10-29 11:59:42

+1

@ user491232:請編輯您的問題並添加您在評論中提供的信息。 – 2010-10-29 12:04:03

回答

0

減去yintercept從線路的Ÿ座標 - ,然後用數學你已經知道XŸ = 0比照適用於解決xintercept

+0

真棒。謝謝 – user491232 2010-10-29 12:11:01

3
double CalcLineYIntercept(LINE *line, double yintercept) 
{ 
    dx = line->x2 - line->x1; 
    dy = line->y2 - line->y1; 

    deltay = yintercept - line->y2; 
    if (dy != 0) { 
     //dy very close to 0 will be numerically unstable, account for that 
     intercept = line->x2 + (dx/dy) * deltay; 
    } 
    else { 
     //line is parrallel to x-axis, will never reach yintercept 
     intercept = NaN; 
    } 
} 

反轉x和y得到另一個函數。

+0

甜美。謝謝 – user491232 2010-10-29 12:16:19

+0

如果這回答了你的問題,也許你可以接受這個問題作爲答案? – 2010-11-01 10:39:22